2626 * -u/--update: update the index instead of adding to it.
2727 */
2828
29- enum print_options {
30- SKIP = 1 ,
31- VERBOSE = 2 ,
32- UPDATE = 4 ,
29+ enum index_mode {
30+ INDEX_NONE ,
31+ INDEX_ADD ,
3332};
3433
35- struct print_payload {
36- enum print_options options ;
34+ struct index_options {
35+ int dry_run ;
36+ int verbose ;
3737 git_repository * repo ;
38+ enum index_mode mode ;
39+ int add_update ;
3840};
3941
4042/* Forward declarations for helpers */
41- static void parse_opts (int * options , int * count , int argc , char * argv []);
42- void init_array (git_strarray * array , int argc , char * * argv );
43+ static void parse_opts (const char * * repo_path , struct index_options * options , struct args_info * args );
4344int print_matched_cb (const char * path , const char * matched_pathspec , void * payload );
4445
45- int lg2_add (git_repository * repo , int argc , char * * argv )
46+ int lg2_add (git_repository * repo , int argc , char * * argv )
4647{
4748 git_index_matched_path_cb matched_cb = NULL ;
4849 git_index * index ;
4950 git_strarray array = {0 };
50- int options = 0 , count = 0 ;
51- struct print_payload payload = { 0 } ;
51+ struct index_options options ;
52+ struct args_info args = ARGS_INFO_INIT ;
5253
53- parse_opts (& options , & count , argc , argv );
54- init_array (& array , argc - count , argv + count );
54+ parse_opts (NULL , & options , & args );
55+ strarray_from_args (& array , & args );
5556
5657 check_lg2 (git_repository_index (& index , repo ), "Could not open repository index" , NULL );
5758
5859 /* Setup a callback if the requested options need it */
59- if (( options & VERBOSE ) || ( options & SKIP ) ) {
60+ if (options . verbose || options . dry_run ) {
6061 matched_cb = & print_matched_cb ;
6162 }
6263
63- /* Perform the requested action with the index and files */
64- payload .options = options ;
65- payload .repo = repo ;
64+ options .repo = repo ;
6665
67- if (options & UPDATE ) {
68- git_index_update_all (index , & array , matched_cb , & payload );
66+ /* Perform the requested action with the index and files */
67+ if (options .add_update ) {
68+ git_index_update_all (index , & array , matched_cb , & options );
6969 } else {
70- git_index_add_all (index , & array , 0 , matched_cb , & payload );
70+ git_index_add_all (index , & array , 0 , matched_cb , & options );
7171 }
7272
7373 /* Cleanup memory */
@@ -84,15 +84,14 @@ int lg2_add(git_repository *repo, int argc, char** argv)
8484 */
8585int print_matched_cb (const char * path , const char * matched_pathspec , void * payload )
8686{
87- struct print_payload p = * (struct print_payload * )(payload );
87+ struct index_options options = * (struct index_options * )(payload );
8888 int ret ;
8989 unsigned status ;
9090 (void )matched_pathspec ;
9191
9292 /* Get the file status */
93- if (git_status_file (& status , p .repo , path )) {
93+ if (git_status_file (& status , options .repo , path ) < 0 )
9494 return -1 ;
95- }
9695
9796 if ((status & GIT_STATUS_WT_MODIFIED ) || (status & GIT_STATUS_WT_NEW )) {
9897 printf ("add '%s'\n" , path );
@@ -101,9 +100,8 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
101100 ret = 1 ;
102101 }
103102
104- if (( p . options & SKIP )) {
103+ if (options . dry_run )
105104 ret = 1 ;
106- }
107105
108106 return ret ;
109107}
@@ -132,33 +130,39 @@ void print_usage(void)
132130 exit (1 );
133131}
134132
135- static void parse_opts (int * options , int * count , int argc , char * argv [] )
133+ static void parse_opts (const char * * repo_path , struct index_options * options , struct args_info * args )
136134{
137- int i ;
135+ if (args -> argc <= 1 )
136+ print_usage ();
138137
139- for (i = 1 ; i < argc ; ++ i ) {
140- if (argv [i ][0 ] != '-' )
141- break ;
142- else if (!strcmp (argv [i ], "--verbose" ) || !strcmp (argv [i ], "-v" ))
143- * options |= VERBOSE ;
144- else if (!strcmp (argv [i ], "--dry-run" ) || !strcmp (argv [i ], "-n" ))
145- * options |= SKIP ;
146- else if (!strcmp (argv [i ], "--update" ) || !strcmp (argv [i ], "-u" ))
147- * options |= UPDATE ;
148- else if (!strcmp (argv [i ], "-h" )) {
138+ for (args -> pos = 1 ; args -> pos < args -> argc ; ++ args -> pos ) {
139+ const char * curr = args -> argv [args -> pos ];
140+
141+ if (curr [0 ] != '-' ) {
142+ if (!strcmp ("add" , curr )) {
143+ options -> mode = INDEX_ADD ;
144+ continue ;
145+ } else if (options -> mode == INDEX_NONE ) {
146+ fprintf (stderr , "missing command: %s" , curr );
147+ print_usage ();
148+ break ;
149+ } else {
150+ /* We might be looking at a filename */
151+ break ;
152+ }
153+ } else if (match_bool_arg (& options -> verbose , args , "--verbose" ) ||
154+ match_bool_arg (& options -> dry_run , args , "--dry-run" ) ||
155+ match_str_arg (repo_path , args , "--git-dir" ) ||
156+ (options -> mode == INDEX_ADD && match_bool_arg (& options -> add_update , args , "--update" ))) {
157+ continue ;
158+ } else if (match_bool_arg (NULL , args , "--help" )) {
149159 print_usage ();
150160 break ;
151- } else if (!strcmp (argv [i ], "--" )) {
152- i ++ ;
161+ } else if (match_arg_separator (args )) {
153162 break ;
154163 } else {
155- fprintf (stderr , "Unsupported option %s.\n" , argv [ i ] );
164+ fprintf (stderr , "Unsupported option %s.\n" , curr );
156165 print_usage ();
157166 }
158167 }
159-
160- if (argc <= i )
161- print_usage ();
162-
163- * count = i ;
164168}
0 commit comments