Skip to content

Commit b9d0246

Browse files
committed
Reorganize rev-parse example
1 parent 784b3ab commit b9d0246

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

examples/rev-parse.c

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1-
#include <stdio.h>
2-
#include <git2.h>
3-
#include <stdlib.h>
4-
#include <string.h>
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
57

6-
static void check(int error, const char *message, const char *arg)
8+
#include "common.h"
9+
10+
11+
/* Forward declarations for helpers */
12+
struct parse_state {
13+
git_repository *repo;
14+
const char *repodir;
15+
const char *spec;
16+
int not;
17+
};
18+
static void parse_opts(struct parse_state *ps, int argc, char *argv[]);
19+
static int parse_revision(struct parse_state *ps);
20+
21+
22+
int main(int argc, char *argv[])
723
{
8-
if (!error)
9-
return;
10-
if (arg)
11-
fprintf(stderr, "%s %s (%d)\n", message, arg, error);
12-
else
13-
fprintf(stderr, "%s(%d)\n", message, error);
14-
exit(1);
24+
struct parse_state ps = {0};
25+
26+
git_threads_init();
27+
parse_opts(&ps, argc, argv);
28+
29+
check_lg2(parse_revision(&ps), "Parsing", NULL);
30+
31+
git_repository_free(ps.repo);
32+
git_threads_shutdown();
33+
34+
return 0;
1535
}
1636

1737
static void usage(const char *message, const char *arg)
@@ -24,25 +44,37 @@ static void usage(const char *message, const char *arg)
2444
exit(1);
2545
}
2646

27-
struct parse_state {
28-
git_repository *repo;
29-
const char *repodir;
30-
int not;
31-
};
47+
static void parse_opts(struct parse_state *ps, int argc, char *argv[])
48+
{
49+
struct args_info args = ARGS_INFO_INIT;
3250

33-
static int parse_revision(struct parse_state *ps, const char *revstr)
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+
65+
static int parse_revision(struct parse_state *ps)
3466
{
3567
git_revspec rs;
3668
char str[GIT_OID_HEXSZ + 1];
3769

3870
if (!ps->repo) {
3971
if (!ps->repodir)
4072
ps->repodir = ".";
41-
check(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL),
73+
check_lg2(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL),
4274
"Could not open repository from", ps->repodir);
4375
}
4476

45-
check(git_revparse(&rs, ps->repo, revstr), "Could not parse", revstr);
77+
check_lg2(git_revparse(&rs, ps->repo, ps->spec), "Could not parse", ps->spec);
4678

4779
if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) {
4880
git_oid_tostr(str, sizeof(str), git_object_id(rs.from));
@@ -56,9 +88,9 @@ static int parse_revision(struct parse_state *ps, const char *revstr)
5688

5789
if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
5890
git_oid base;
59-
check(git_merge_base(&base, ps->repo,
60-
git_object_id(rs.from), git_object_id(rs.to)),
61-
"Could not find merge base", revstr);
91+
check_lg2(git_merge_base(&base, ps->repo,
92+
git_object_id(rs.from), git_object_id(rs.to)),
93+
"Could not find merge base", ps->spec);
6294

6395
git_oid_tostr(str, sizeof(str), &base);
6496
printf("%s\n", str);
@@ -69,38 +101,9 @@ static int parse_revision(struct parse_state *ps, const char *revstr)
69101
git_object_free(rs.from);
70102
}
71103
else {
72-
check(0, "Invalid results from git_revparse", revstr);
104+
fatal("Invalid results from git_revparse", ps->spec);
73105
}
74106

75107
return 0;
76108
}
77109

78-
int main(int argc, char *argv[])
79-
{
80-
int i;
81-
char *a;
82-
struct parse_state ps;
83-
84-
git_threads_init();
85-
86-
memset(&ps, 0, sizeof(ps));
87-
88-
for (i = 1; i < argc; ++i) {
89-
a = argv[i];
90-
91-
if (a[0] != '-') {
92-
if (parse_revision(&ps, a) != 0)
93-
break;
94-
} else if (!strcmp(a, "--not"))
95-
ps.not = !ps.not;
96-
else if (!strncmp(a, "--git-dir=", strlen("--git-dir=")))
97-
ps.repodir = a + strlen("--git-dir=");
98-
else
99-
usage("Cannot handle argument", a);
100-
}
101-
102-
git_repository_free(ps.repo);
103-
git_threads_shutdown();
104-
105-
return 0;
106-
}

0 commit comments

Comments
 (0)