]>
Commit | Line | Data |
---|---|---|
b9d02460 | 1 | /* |
6cb831bd | 2 | * libgit2 "rev-parse" example - shows how to parse revspecs |
b9d02460 | 3 | * |
6cb831bd BS |
4 | * Written by the libgit2 contributors |
5 | * | |
6 | * To the extent possible under law, the author(s) have dedicated all copyright | |
7 | * and related and neighboring rights to this software to the public domain | |
8 | * worldwide. This software is distributed without any warranty. | |
9 | * | |
10 | * You should have received a copy of the CC0 Public Domain Dedication along | |
11 | * with this software. If not, see | |
12 | * <http://creativecommons.org/publicdomain/zero/1.0/>. | |
b9d02460 | 13 | */ |
8ba0ff69 | 14 | |
b9d02460 BS |
15 | #include "common.h" |
16 | ||
85c6730c | 17 | /** Forward declarations for helpers. */ |
b9d02460 | 18 | struct parse_state { |
b9d02460 BS |
19 | const char *repodir; |
20 | const char *spec; | |
21 | int not; | |
22 | }; | |
23 | static void parse_opts(struct parse_state *ps, int argc, char *argv[]); | |
0c9c969a | 24 | static int parse_revision(git_repository *repo, struct parse_state *ps); |
b9d02460 | 25 | |
0c9c969a | 26 | int lg2_rev_parse(git_repository *repo, int argc, char *argv[]) |
8ba0ff69 | 27 | { |
b9d02460 BS |
28 | struct parse_state ps = {0}; |
29 | ||
b9d02460 BS |
30 | parse_opts(&ps, argc, argv); |
31 | ||
0c9c969a | 32 | check_lg2(parse_revision(repo, &ps), "Parsing", NULL); |
b9d02460 BS |
33 | |
34 | return 0; | |
8ba0ff69 RB |
35 | } |
36 | ||
37 | static void usage(const char *message, const char *arg) | |
38 | { | |
39 | if (message && arg) | |
40 | fprintf(stderr, "%s: %s\n", message, arg); | |
41 | else if (message) | |
42 | fprintf(stderr, "%s\n", message); | |
43 | fprintf(stderr, "usage: rev-parse [ --option ] <args>...\n"); | |
44 | exit(1); | |
45 | } | |
46 | ||
b9d02460 BS |
47 | static void parse_opts(struct parse_state *ps, int argc, char *argv[]) |
48 | { | |
49 | struct args_info args = ARGS_INFO_INIT; | |
8ba0ff69 | 50 | |
b9d02460 BS |
51 | for (args.pos=1; args.pos < argc; ++args.pos) { |
52 | const char *a = argv[args.pos]; | |
53 | ||
54 | if (a[0] != '-') { | |
55 | if (ps->spec) | |
56 | usage("Too many specs", a); | |
57 | ps->spec = a; | |
58 | } else if (!strcmp(a, "--not")) | |
59 | ps->not = !ps->not; | |
60 | else if (!match_str_arg(&ps->repodir, &args, "--git-dir")) | |
61 | usage("Cannot handle argument", a); | |
62 | } | |
63 | } | |
64 | ||
0c9c969a | 65 | static int parse_revision(git_repository *repo, struct parse_state *ps) |
8ba0ff69 RB |
66 | { |
67 | git_revspec rs; | |
68 | char str[GIT_OID_HEXSZ + 1]; | |
69 | ||
0c9c969a | 70 | check_lg2(git_revparse(&rs, repo, ps->spec), "Could not parse", ps->spec); |
8ba0ff69 RB |
71 | |
72 | if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) { | |
73 | git_oid_tostr(str, sizeof(str), git_object_id(rs.from)); | |
74 | printf("%s\n", str); | |
75 | git_object_free(rs.from); | |
76 | } | |
77 | else if ((rs.flags & GIT_REVPARSE_RANGE) != 0) { | |
78 | git_oid_tostr(str, sizeof(str), git_object_id(rs.to)); | |
79 | printf("%s\n", str); | |
80 | git_object_free(rs.to); | |
81 | ||
82 | if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) { | |
83 | git_oid base; | |
0c9c969a | 84 | check_lg2(git_merge_base(&base, repo, |
b9d02460 BS |
85 | git_object_id(rs.from), git_object_id(rs.to)), |
86 | "Could not find merge base", ps->spec); | |
8ba0ff69 RB |
87 | |
88 | git_oid_tostr(str, sizeof(str), &base); | |
89 | printf("%s\n", str); | |
90 | } | |
91 | ||
92 | git_oid_tostr(str, sizeof(str), git_object_id(rs.from)); | |
93 | printf("^%s\n", str); | |
94 | git_object_free(rs.from); | |
95 | } | |
96 | else { | |
b9d02460 | 97 | fatal("Invalid results from git_revparse", ps->spec); |
8ba0ff69 RB |
98 | } |
99 | ||
100 | return 0; | |
101 | } | |
102 |