1
- #include <git2.h>
2
- #include <stdio.h>
3
- #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
+ */
7
+
8
+ #include "common.h"
4
9
#include <assert.h>
5
10
6
11
enum print_options {
@@ -14,19 +19,49 @@ struct print_payload {
14
19
git_repository * repo ;
15
20
};
16
21
17
- void init_array (git_strarray * array , int argc , char * * argv )
22
+ /* Forward declarations for helpers */
23
+ static void parse_opts (int * options , int * count , int argc , char * argv []);
24
+ void init_array (git_strarray * array , int argc , char * * argv );
25
+ int print_matched_cb (const char * path , const char * matched_pathspec , void * payload );
26
+
27
+ int main (int argc , char * * argv )
18
28
{
19
- unsigned int i ;
29
+ git_index_matched_path_cb matched_cb = NULL ;
30
+ git_repository * repo = NULL ;
31
+ git_index * index ;
32
+ git_strarray array = {0 };
33
+ int options = 0 , count = 0 ;
34
+ struct print_payload payload = {0 };
20
35
21
- array -> count = argc ;
22
- array -> strings = malloc (sizeof (char * ) * array -> count );
23
- assert (array -> strings != NULL );
36
+ git_threads_init ();
24
37
25
- for (i = 0 ; i < array -> count ; i ++ ) {
26
- array -> strings [i ]= argv [i ];
38
+ parse_opts (& options , & count , argc , argv );
39
+
40
+ init_array (& array , argc - count , argv + count );
41
+
42
+ check_lg2 (git_repository_open (& repo , "." ), "No git repository" , NULL );
43
+ check_lg2 (git_repository_index (& index , repo ), "Could not open repository index" , NULL );
44
+
45
+ if (options & VERBOSE || options & SKIP ) {
46
+ matched_cb = & print_matched_cb ;
27
47
}
28
48
29
- return ;
49
+ payload .options = options ;
50
+ payload .repo = repo ;
51
+
52
+ if (options & UPDATE ) {
53
+ git_index_update_all (index , & array , matched_cb , & payload );
54
+ } else {
55
+ git_index_add_all (index , & array , 0 , matched_cb , & payload );
56
+ }
57
+
58
+ git_index_write (index );
59
+ git_index_free (index );
60
+ git_repository_free (repo );
61
+
62
+ git_threads_shutdown ();
63
+
64
+ return 0 ;
30
65
}
31
66
32
67
int print_matched_cb (const char * path , const char * matched_pathspec , void * payload )
@@ -55,36 +90,46 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
55
90
return ret ;
56
91
}
57
92
93
+ void init_array (git_strarray * array , int argc , char * * argv )
94
+ {
95
+ unsigned int i ;
96
+
97
+ array -> count = argc ;
98
+ array -> strings = malloc (sizeof (char * ) * array -> count );
99
+ assert (array -> strings != NULL );
100
+
101
+ for (i = 0 ; i < array -> count ; i ++ ) {
102
+ array -> strings [i ]= argv [i ];
103
+ }
104
+
105
+ return ;
106
+ }
107
+
58
108
void print_usage (void )
59
109
{
60
110
fprintf (stderr , "usage: add [options] [--] file-spec [file-spec] [...]\n\n" );
61
111
fprintf (stderr , "\t-n, --dry-run dry run\n" );
62
112
fprintf (stderr , "\t-v, --verbose be verbose\n" );
63
113
fprintf (stderr , "\t-u, --update update tracked files\n" );
114
+ exit (1 );
64
115
}
65
116
66
-
67
- int main (int argc , char * * argv )
117
+ static void parse_opts (int * options , int * count , int argc , char * argv [])
68
118
{
69
- git_index_matched_path_cb matched_cb = NULL ;
70
- git_repository * repo = NULL ;
71
- git_index * index ;
72
- git_strarray array = {0 };
73
- int i , options = 0 ;
74
- struct print_payload payload = {0 };
119
+ int i ;
75
120
76
121
for (i = 1 ; i < argc ; ++ i ) {
77
122
if (argv [i ][0 ] != '-' ) {
78
123
break ;
79
124
}
80
125
else if (!strcmp (argv [i ], "--verbose" ) || !strcmp (argv [i ], "-v" )) {
81
- options |= VERBOSE ;
126
+ * options |= VERBOSE ;
82
127
}
83
128
else if (!strcmp (argv [i ], "--dry-run" ) || !strcmp (argv [i ], "-n" )) {
84
- options |= SKIP ;
129
+ * options |= SKIP ;
85
130
}
86
131
else if (!strcmp (argv [i ], "--update" ) || !strcmp (argv [i ], "-u" )) {
87
- options |= UPDATE ;
132
+ * options |= UPDATE ;
88
133
}
89
134
else if (!strcmp (argv [i ], "-h" )) {
90
135
print_usage ();
@@ -97,47 +142,11 @@ int main (int argc, char** argv)
97
142
else {
98
143
fprintf (stderr , "Unsupported option %s.\n" , argv [i ]);
99
144
print_usage ();
100
- return 1 ;
101
145
}
102
146
}
103
147
104
- if (argc <=i ) {
148
+ if (argc <=i )
105
149
print_usage ();
106
- return 1 ;
107
- }
108
150
109
- git_threads_init ();
110
-
111
- init_array (& array , argc - i , argv + i );
112
-
113
- if (git_repository_open (& repo , "." ) < 0 ) {
114
- fprintf (stderr , "No git repository\n" );
115
- return 1 ;
116
- }
117
-
118
- if (git_repository_index (& index , repo ) < 0 ) {
119
- fprintf (stderr , "Could not open repository index\n" );
120
- return 1 ;
121
- }
122
-
123
- if (options & VERBOSE || options & SKIP ) {
124
- matched_cb = & print_matched_cb ;
125
- }
126
-
127
- payload .options = options ;
128
- payload .repo = repo ;
129
-
130
- if (options & UPDATE ) {
131
- git_index_update_all (index , & array , matched_cb , & payload );
132
- } else {
133
- git_index_add_all (index , & array , 0 , matched_cb , & payload );
134
- }
135
-
136
- git_index_write (index );
137
- git_index_free (index );
138
- git_repository_free (repo );
139
-
140
- git_threads_shutdown ();
141
-
142
- return 0 ;
151
+ * count = i ;
143
152
}
0 commit comments