99#include "remote.h"
1010#include "transport.h"
1111#include "run-command.h"
12+ #include "parse-options.h"
1213
13- static const char fetch_usage [] = "git-fetch [-a | --append] [--upload-pack <upload-pack>] [-f | --force] [--no-tags] [-t | --tags] [-k | --keep] [-u | --update-head-ok] [--depth <depth>] [-v | --verbose] [<repository> <refspec>...]" ;
14+ static const char * const builtin_fetch_usage [] = {
15+ "git-fetch [options] [<repository> <refspec>...]" ,
16+ NULL
17+ };
1418
15- static int append , force , tags , no_tags , update_head_ok , verbose , quiet ;
19+ enum {
20+ TAGS_UNSET = 0 ,
21+ TAGS_DEFAULT = 1 ,
22+ TAGS_SET = 2
23+ };
24+
25+ static int append , force , keep , update_head_ok , verbose , quiet ;
26+ static int tags = TAGS_DEFAULT ;
1627static const char * depth ;
28+ static const char * upload_pack ;
1729static struct strbuf default_rla = STRBUF_INIT ;
1830static struct transport * transport ;
1931
32+ static struct option builtin_fetch_options [] = {
33+ OPT__QUIET (& quiet ),
34+ OPT__VERBOSE (& verbose ),
35+ OPT_BOOLEAN ('a' , "append" , & append ,
36+ "append to .git/FETCH_HEAD instead of overwriting" ),
37+ OPT_STRING (0 , "upload-pack" , & upload_pack , "PATH" ,
38+ "path to upload pack on remote end" ),
39+ OPT_BOOLEAN ('f' , "force" , & force ,
40+ "force overwrite of local branch" ),
41+ OPT_SET_INT ('t' , "tags" , & tags ,
42+ "fetch all tags and associated objects" , TAGS_SET ),
43+ OPT_BOOLEAN ('k' , "keep" , & keep , "keep downloaded pack" ),
44+ OPT_BOOLEAN ('u' , "update-head-ok" , & update_head_ok ,
45+ "allow updating of HEAD ref" ),
46+ OPT_STRING (0 , "depth" , & depth , "DEPTH" ,
47+ "deepen history of shallow clone" ),
48+ OPT_END ()
49+ };
50+
2051static void unlock_pack (void )
2152{
2253 if (transport )
@@ -81,7 +112,7 @@ static struct ref *get_ref_map(struct transport *transport,
81112
82113 const struct ref * remote_refs = transport_get_remote_refs (transport );
83114
84- if (ref_count || tags ) {
115+ if (ref_count || tags == TAGS_SET ) {
85116 for (i = 0 ; i < ref_count ; i ++ ) {
86117 get_fetch_map (remote_refs , & refs [i ], & tail , 0 );
87118 if (refs [i ].dst && refs [i ].dst [0 ])
@@ -90,7 +121,7 @@ static struct ref *get_ref_map(struct transport *transport,
90121 /* Merge everything on the command line, but not --tags */
91122 for (rm = ref_map ; rm ; rm = rm -> next )
92123 rm -> merge = 1 ;
93- if (tags ) {
124+ if (tags == TAGS_SET ) {
94125 struct refspec refspec ;
95126 refspec .src = "refs/tags/" ;
96127 refspec .dst = "refs/tags/" ;
@@ -482,10 +513,10 @@ static int do_fetch(struct transport *transport,
482513 struct ref * ref_map , * fetch_map ;
483514 struct ref * rm ;
484515 int autotags = (transport -> remote -> fetch_tags == 1 );
485- if (transport -> remote -> fetch_tags == 2 && ! no_tags )
486- tags = 1 ;
516+ if (transport -> remote -> fetch_tags == 2 && tags != TAGS_UNSET )
517+ tags = TAGS_SET ;
487518 if (transport -> remote -> fetch_tags == -1 )
488- no_tags = 1 ;
519+ tags = TAGS_UNSET ;
489520
490521 if (!transport -> get_refs_list || !transport -> fetch )
491522 die ("Don't know how to fetch from %s" , transport -> url );
@@ -515,7 +546,7 @@ static int do_fetch(struct transport *transport,
515546
516547 /* if neither --no-tags nor --tags was specified, do automated tag
517548 * following ... */
518- if (!( tags || no_tags ) && autotags ) {
549+ if (tags == TAGS_DEFAULT && autotags ) {
519550 ref_map = find_non_local_tags (transport , fetch_map );
520551 if (ref_map ) {
521552 transport_set_option (transport , TRANS_OPT_DEPTH , "0" );
@@ -546,80 +577,19 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
546577 int i ;
547578 static const char * * refs = NULL ;
548579 int ref_nr = 0 ;
549- const char * upload_pack = NULL ;
550- int keep = 0 ;
551580
552581 /* Record the command line for the reflog */
553582 strbuf_addstr (& default_rla , "fetch" );
554583 for (i = 1 ; i < argc ; i ++ )
555584 strbuf_addf (& default_rla , " %s" , argv [i ]);
556585
557- for (i = 1 ; i < argc ; i ++ ) {
558- const char * arg = argv [i ];
559-
560- if (arg [0 ] != '-' )
561- break ;
562- if (!strcmp (arg , "--append" ) || !strcmp (arg , "-a" )) {
563- append = 1 ;
564- continue ;
565- }
566- if (!prefixcmp (arg , "--upload-pack=" )) {
567- upload_pack = arg + 14 ;
568- continue ;
569- }
570- if (!strcmp (arg , "--upload-pack" )) {
571- i ++ ;
572- if (i == argc )
573- usage (fetch_usage );
574- upload_pack = argv [i ];
575- continue ;
576- }
577- if (!strcmp (arg , "--force" ) || !strcmp (arg , "-f" )) {
578- force = 1 ;
579- continue ;
580- }
581- if (!strcmp (arg , "--no-tags" )) {
582- no_tags = 1 ;
583- continue ;
584- }
585- if (!strcmp (arg , "--tags" ) || !strcmp (arg , "-t" )) {
586- tags = 1 ;
587- continue ;
588- }
589- if (!strcmp (arg , "--keep" ) || !strcmp (arg , "-k" )) {
590- keep = 1 ;
591- continue ;
592- }
593- if (!strcmp (arg , "--update-head-ok" ) || !strcmp (arg , "-u" )) {
594- update_head_ok = 1 ;
595- continue ;
596- }
597- if (!prefixcmp (arg , "--depth=" )) {
598- depth = arg + 8 ;
599- continue ;
600- }
601- if (!strcmp (arg , "--depth" )) {
602- i ++ ;
603- if (i == argc )
604- usage (fetch_usage );
605- depth = argv [i ];
606- continue ;
607- }
608- if (!strcmp (arg , "--quiet" ) || !strcmp (arg , "-q" )) {
609- quiet = 1 ;
610- continue ;
611- }
612- if (!strcmp (arg , "--verbose" ) || !strcmp (arg , "-v" )) {
613- verbose ++ ;
614- continue ;
615- }
616- usage (fetch_usage );
617- }
586+ argc = parse_options (argc , argv ,
587+ builtin_fetch_options , builtin_fetch_usage , 0 );
618588
619- if (i == argc )
589+ if (argc == 0 )
620590 remote = remote_get (NULL );
621591 else
622- remote = remote_get (argv [i ++ ]);
592+ remote = remote_get (argv [0 ]);
623593
624594 transport = transport_get (remote , remote -> url [0 ]);
625595 if (verbose >= 2 )
@@ -636,10 +606,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
636606 if (!transport -> url )
637607 die ("Where do you want to fetch from today?" );
638608
639- if (i < argc ) {
609+ if (argc > 1 ) {
640610 int j = 0 ;
641- refs = xcalloc (argc - i + 1 , sizeof (const char * ));
642- while (i < argc ) {
611+ refs = xcalloc (argc + 1 , sizeof (const char * ));
612+ for (i = 1 ; i < argc ; i ++ ) {
643613 if (!strcmp (argv [i ], "tag" )) {
644614 char * ref ;
645615 i ++ ;
@@ -651,7 +621,6 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
651621 refs [j ++ ] = ref ;
652622 } else
653623 refs [j ++ ] = argv [i ];
654- i ++ ;
655624 }
656625 refs [j ] = NULL ;
657626 ref_nr = j ;
0 commit comments