forked from AndreyG/libgit2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrev-parse.cpp
More file actions
71 lines (56 loc) · 1.5 KB
/
Copy pathrev-parse.cpp
File metadata and controls
71 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <boost/optional.hpp>
#include "git2cpp/threads_initializer.h"
#include "git2cpp/repo.h"
using namespace git;
static void usage(const char *message, const char *arg)
{
if (message && arg)
fprintf(stderr, "%s: %s\n", message, arg);
else if (message)
fprintf(stderr, "%s\n", message);
fprintf(stderr, "usage: rev-parse [ --option ] <args>...\n");
exit(1);
}
struct parse_state {
boost::optional<Repository> repo;
std::string repodir = ".";
};
void parse_revision(parse_state & ps, const char *revstr)
{
if (!ps.repo)
ps.repo = boost::in_place(ps.repodir);
Revspec rs = ps.repo->revparse(revstr);
if (auto commit = rs.single())
{
std::cout << id_to_str(commit->id()) << std::endl;
}
else
{
auto const & range = *rs.range();
std::cout << id_to_str(range.to.id()) << std::endl;
if ((rs.flags() & GIT_REVPARSE_MERGE_BASE) != 0) {
git_oid base = ps.repo->merge_base(range);
std::cout << id_to_str(&base) << std::endl;
}
std::cout << "^" << id_to_str(range.from.id()) << std::endl;
}
}
int main(int argc, char *argv[])
{
parse_state ps;
ThreadsInitializer threads_initalizer;
for (int i = 1; i < argc; ++i) {
const char * a = argv[i];
if (a[0] != '-')
parse_revision(ps, a);
else if (!strncmp(a, "--git-dir=", strlen("--git-dir=")))
ps.repodir = a + strlen("--git-dir=");
else
usage("Cannot handle argument", a);
}
return 0;
}