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" 
49#include  <assert.h> 
510
611enum  print_options  {
@@ -14,19 +19,49 @@ struct print_payload {
1419	git_repository  * repo ;
1520};
1621
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 )
1828{
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 };
2035
21- 	array -> count  =  argc ;
22- 	array -> strings  =  malloc (sizeof (char * ) *  array -> count );
23- 	assert (array -> strings != NULL );
36+ 	git_threads_init ();
2437
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 ;
2747	}
2848
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 ;
3065}
3166
3267int  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
5590	return  ret ;
5691}
5792
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+ 
58108void  print_usage (void )
59109{
60110	fprintf (stderr , "usage: add [options] [--] file-spec [file-spec] [...]\n\n" );
61111	fprintf (stderr , "\t-n, --dry-run    dry run\n" );
62112	fprintf (stderr , "\t-v, --verbose    be verbose\n" );
63113	fprintf (stderr , "\t-u, --update     update tracked files\n" );
114+ 	exit (1 );
64115}
65116
66- 
67- int  main  (int  argc , char * *  argv )
117+ static  void  parse_opts (int  * options , int  * count , int  argc , char  * argv [])
68118{
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 ;
75120
76121	for  (i  =  1 ; i  <  argc ; ++ i ) {
77122		if  (argv [i ][0 ] !=  '-' ) {
78123			break ;
79124		}
80125		else  if (!strcmp (argv [i ], "--verbose" ) ||  !strcmp (argv [i ], "-v" )) {
81- 			options  |= VERBOSE ;
126+ 			* options  |= VERBOSE ;
82127		}
83128		else  if (!strcmp (argv [i ], "--dry-run" ) ||  !strcmp (argv [i ], "-n" )) {
84- 			options  |= SKIP ;
129+ 			* options  |= SKIP ;
85130		}
86131		else  if (!strcmp (argv [i ], "--update" ) ||  !strcmp (argv [i ], "-u" )) {
87- 			options  |= UPDATE ;
132+ 			* options  |= UPDATE ;
88133		}
89134		else  if (!strcmp (argv [i ], "-h" )) {
90135			print_usage ();
@@ -97,47 +142,11 @@ int main (int argc, char** argv)
97142		else  {
98143			fprintf (stderr , "Unsupported option %s.\n" , argv [i ]);
99144			print_usage ();
100- 			return  1 ;
101145		}
102146	}
103147
104- 	if  (argc <=i ) { 
148+ 	if  (argc <=i )
105149		print_usage ();
106- 		return  1 ;
107- 	}
108150
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 ;
143152}
0 commit comments