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
+ */
5
7
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 [])
7
23
{
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 ;
15
35
}
16
36
17
37
static void usage (const char * message , const char * arg )
@@ -24,25 +44,37 @@ static void usage(const char *message, const char *arg)
24
44
exit (1 );
25
45
}
26
46
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 ;
32
50
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 )
34
66
{
35
67
git_revspec rs ;
36
68
char str [GIT_OID_HEXSZ + 1 ];
37
69
38
70
if (!ps -> repo ) {
39
71
if (!ps -> repodir )
40
72
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 ),
42
74
"Could not open repository from" , ps -> repodir );
43
75
}
44
76
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 );
46
78
47
79
if ((rs .flags & GIT_REVPARSE_SINGLE ) != 0 ) {
48
80
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)
56
88
57
89
if ((rs .flags & GIT_REVPARSE_MERGE_BASE ) != 0 ) {
58
90
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 );
62
94
63
95
git_oid_tostr (str , sizeof (str ), & base );
64
96
printf ("%s\n" , str );
@@ -69,38 +101,9 @@ static int parse_revision(struct parse_state *ps, const char *revstr)
69
101
git_object_free (rs .from );
70
102
}
71
103
else {
72
- check ( 0 , "Invalid results from git_revparse" , revstr );
104
+ fatal ( "Invalid results from git_revparse" , ps -> spec );
73
105
}
74
106
75
107
return 0 ;
76
108
}
77
109
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