22#include <stdio.h>
33#include <string.h>
44
5+ #include "../common.h"
56#include "common.h"
67
7- // This part is not strictly libgit2-dependent, but you can use this
8- // as a starting point for a git-like tool
8+ /* This part is not strictly libgit2-dependent, but you can use this
9+ * as a starting point for a git-like tool */
910
1011struct {
1112 char * name ;
@@ -18,37 +19,30 @@ struct {
1819 { NULL , NULL }
1920};
2021
21- static int run_command (git_cb fn , int argc , char * * argv )
22+ static int run_command (git_cb fn , git_repository * repo , struct args_info args )
2223{
2324 int error ;
24- git_repository * repo ;
25-
26- // Before running the actual command, create an instance of the local
27- // repository and pass it to the function.
28-
29- error = git_repository_open (& repo , ".git" );
30- if (error < 0 )
31- repo = NULL ;
3225
33- // Run the command. If something goes wrong, print the error message to stderr
34- error = fn (repo , argc , argv );
26+ /* Run the command. If something goes wrong, print the error message to stderr */
27+ error = fn (repo , args . argc - args . pos , & args . argv [ args . pos ] );
3528 if (error < 0 ) {
3629 if (giterr_last () == NULL )
3730 fprintf (stderr , "Error without message" );
3831 else
3932 fprintf (stderr , "Bad news:\n %s\n" , giterr_last ()-> message );
4033 }
4134
42- if (repo )
43- git_repository_free (repo );
44-
4535 return !!error ;
4636}
4737
4838int main (int argc , char * * argv )
4939{
5040 int i ;
5141 int return_code = 1 ;
42+ int error ;
43+ git_repository * repo ;
44+ struct args_info args = ARGS_INFO_INIT ;
45+ const char * git_dir = NULL ;
5246
5347 if (argc < 2 ) {
5448 fprintf (stderr , "usage: %s <cmd> [repo]\n" , argv [0 ]);
@@ -57,16 +51,39 @@ int main(int argc, char **argv)
5751
5852 git_libgit2_init ();
5953
54+ for (args .pos = 1 ; args .pos < args .argc ; ++ args .pos ) {
55+ char * a = args .argv [args .pos ];
56+
57+ if (a [0 ] != '-' ) {
58+ /* non-arg */
59+ break ;
60+ } else if (optional_str_arg (& git_dir , & args , "--git-dir" , ".git" )) {
61+ continue ;
62+ } else if (!strcmp (a , "--" )) {
63+ /* arg separator */
64+ break ;
65+ }
66+ }
67+
68+ /* Before running the actual command, create an instance of the local
69+ * repository and pass it to the function. */
70+
71+ error = git_repository_open (& repo , git_dir );
72+ if (error < 0 )
73+ repo = NULL ;
74+
6075 for (i = 0 ; commands [i ].name != NULL ; ++ i ) {
61- if (!strcmp (argv [1 ], commands [i ].name )) {
62- return_code = run_command (commands [i ].fn , -- argc , ++ argv );
76+ if (!strcmp (args . argv [args . pos ], commands [i ].name )) {
77+ return_code = run_command (commands [i ].fn , repo , args );
6378 goto shutdown ;
6479 }
6580 }
6681
6782 fprintf (stderr , "Command not found: %s\n" , argv [1 ]);
6883
6984shutdown :
85+ git_repository_free (repo );
86+
7087 git_libgit2_shutdown ();
7188
7289 return return_code ;
0 commit comments