1515#include "common.h"
1616#include <assert.h>
1717
18+ /**
19+ * The following example demonstrates how to add files with libgit2.
20+ *
21+ * It will use the repository in the current working directory, and act
22+ * on files passed as its parameters.
23+ *
24+ * Recognized options are:
25+ * -v/--verbose: show the file's status after acting on it.
26+ * -n/--dry-run: do not actually change the index.
27+ * -u/--update: update the index instead of adding to it.
28+ */
29+
1830enum print_options {
1931 SKIP = 1 ,
2032 VERBOSE = 2 ,
@@ -40,49 +52,57 @@ int lg2_add(git_repository *repo, int argc, char** argv)
4052 struct print_payload payload = {0 };
4153
4254 parse_opts (& options , & count , argc , argv );
43-
4455 init_array (& array , argc - count , argv + count );
4556
4657 check_lg2 (git_repository_index (& index , repo ), "Could not open repository index" , NULL );
4758
48- if (options & VERBOSE || options & SKIP ) {
59+ /* Setup a callback if the requested options need it */
60+ if ((options & VERBOSE ) || (options & SKIP )) {
4961 matched_cb = & print_matched_cb ;
5062 }
5163
64+ /* Perform the requested action with the index and files */
5265 payload .options = options ;
5366 payload .repo = repo ;
5467
55- if (options & UPDATE ) {
68+ if (options & UPDATE ) {
5669 git_index_update_all (index , & array , matched_cb , & payload );
5770 } else {
5871 git_index_add_all (index , & array , 0 , matched_cb , & payload );
5972 }
6073
74+ /* Cleanup memory */
6175 git_index_write (index );
6276 git_index_free (index );
6377
6478 return 0 ;
6579}
6680
81+ /*
82+ * This callback is called for each file under consideration by
83+ * git_index_(update|add)_all above.
84+ * It makes uses of the callback's ability to abort the action.
85+ */
6786int print_matched_cb (const char * path , const char * matched_pathspec , void * payload )
6887{
6988 struct print_payload p = * (struct print_payload * )(payload );
7089 int ret ;
7190 unsigned status ;
7291 (void )matched_pathspec ;
7392
93+ /* Get the file status */
7494 if (git_status_file (& status , p .repo , path )) {
7595 return -1 ;
7696 }
7797
78- if (status & GIT_STATUS_WT_MODIFIED || status & GIT_STATUS_WT_NEW ) {
98+ if (( status & GIT_STATUS_WT_MODIFIED ) || ( status & GIT_STATUS_WT_NEW ) ) {
7999 printf ("add '%s'\n" , path );
80100 ret = 0 ;
81101 } else {
82102 ret = 1 ;
83103 }
84104
85- if ( p .options & SKIP ) {
105+ if (( p .options & SKIP ) ) {
86106 ret = 1 ;
87107 }
88108
@@ -94,11 +114,11 @@ void init_array(git_strarray *array, int argc, char **argv)
94114 unsigned int i ;
95115
96116 array -> count = argc ;
97- array -> strings = malloc ( sizeof (char * ) * array -> count );
98- assert (array -> strings != NULL );
117+ array -> strings = calloc ( array -> count , sizeof (char * ) );
118+ assert (array -> strings != NULL );
99119
100- for ( i = 0 ; i < array -> count ; i ++ ) {
101- array -> strings [i ]= argv [i ];
120+ for ( i = 0 ; i < array -> count ; i ++ ) {
121+ array -> strings [i ] = argv [i ];
102122 }
103123
104124 return ;
@@ -118,33 +138,27 @@ static void parse_opts(int *options, int *count, int argc, char *argv[])
118138 int i ;
119139
120140 for (i = 1 ; i < argc ; ++ i ) {
121- if (argv [i ][0 ] != '-' ) {
141+ if (argv [i ][0 ] != '-' )
122142 break ;
123- }
124- else if (!strcmp (argv [i ], "--verbose" ) || !strcmp (argv [i ], "-v" )) {
143+ else if (!strcmp (argv [i ], "--verbose" ) || !strcmp (argv [i ], "-v" ))
125144 * options |= VERBOSE ;
126- }
127- else if (!strcmp (argv [i ], "--dry-run" ) || !strcmp (argv [i ], "-n" )) {
145+ else if (!strcmp (argv [i ], "--dry-run" ) || !strcmp (argv [i ], "-n" ))
128146 * options |= SKIP ;
129- }
130- else if (!strcmp (argv [i ], "--update" ) || !strcmp (argv [i ], "-u" )) {
147+ else if (!strcmp (argv [i ], "--update" ) || !strcmp (argv [i ], "-u" ))
131148 * options |= UPDATE ;
132- }
133- else if (!strcmp (argv [i ], "-h" )) {
149+ else if (!strcmp (argv [i ], "-h" )) {
134150 print_usage ();
135151 break ;
136- }
137- else if (!strcmp (argv [i ], "--" )) {
152+ } else if (!strcmp (argv [i ], "--" )) {
138153 i ++ ;
139154 break ;
140- }
141- else {
155+ } else {
142156 fprintf (stderr , "Unsupported option %s.\n" , argv [i ]);
143157 print_usage ();
144158 }
145159 }
146160
147- if (argc <= i )
161+ if (argc <= i )
148162 print_usage ();
149163
150164 * count = i ;
0 commit comments