Rewrite builtin-fetch option parsing to use parse_options().
This gets a little tricky because of the way --tags and --no-tags are handled, and the "tag <name>" syntax needs a little hand-holding too. Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Kristian Høgsberg committed
Dec 4, 2007 at 02:25 UTC
83201998730ac5f0b95ac832f3b727da26ab0129
1 file changed
+46
-77
builtin-fetch.c
+46
-77
@@ -9,14 +9,45 @@
9
#include "remote.h"
10
#include "transport.h"
11
#include "run-command.h"
12
+#include "parse-options.h"
13
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
+};
18
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;
27
static const char *depth;
28
+static const char *upload_pack;
29
static struct strbuf default_rla = STRBUF_INIT;
30
static struct transport *transport;
31
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
+
51
static void unlock_pack(void)
52
{
53
if (transport)
@@ -81,7 +112,7 @@ static struct ref *get_ref_map(struct transport *transport,
112
113
const struct ref *remote_refs = transport_get_remote_refs(transport);
114
84
- if (ref_count || tags) {
115
+ if (ref_count || tags == TAGS_SET) {
116
for (i = 0; i < ref_count; i++) {
117
get_fetch_map(remote_refs, &refs[i], &tail, 0);
118
if (refs[i].dst && refs[i].dst[0])
@@ -90,7 +121,7 @@ static struct ref *get_ref_map(struct transport *transport,
121
/* Merge everything on the command line, but not --tags */
122
for (rm = ref_map; rm; rm = rm->next)
123
rm->merge = 1;
93
- if (tags) {
124
+ if (tags == TAGS_SET) {
125
struct refspec refspec;
126
refspec.src = "refs/tags/";
127
refspec.dst = "refs/tags/";
@@ -482,10 +513,10 @@ static int do_fetch(struct transport *transport,
513
struct ref *ref_map, *fetch_map;
514
struct ref *rm;
515
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;
518
if (transport->remote->fetch_tags == -1)
488
- no_tags = 1;
519
+ tags = TAGS_UNSET;
520
521
if (!transport->get_refs_list || !transport->fetch)
522
die("Don't know how to fetch from %s", transport->url);
@@ -515,7 +546,7 @@ static int do_fetch(struct transport *transport,
546
547
/* if neither --no-tags nor --tags was specified, do automated tag
548
* following ... */
518
- if (!(tags || no_tags) && autotags) {
549
+ if (tags == TAGS_DEFAULT && autotags) {
550
ref_map = find_non_local_tags(transport, fetch_map);
551
if (ref_map) {
552
transport_set_option(transport, TRANS_OPT_DEPTH, "0");
@@ -546,80 +577,19 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
577
int i;
578
static const char **refs = NULL;
579
int ref_nr = 0;
549
- const char *upload_pack = NULL;
550
- int keep = 0;
580
581
/* Record the command line for the reflog */
582
strbuf_addstr(&default_rla, "fetch");
583
for (i = 1; i < argc; i++)
584
strbuf_addf(&default_rla, " %s", argv[i]);
585
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);
588
619
- if (i == argc)
589
+ if (argc == 0)
590
remote = remote_get(NULL);
591
else
622
- remote = remote_get(argv[i++]);
592
+ remote = remote_get(argv[0]);
593
594
transport = transport_get(remote, remote->url[0]);
595
if (verbose >= 2)
@@ -636,10 +606,10 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
606
if (!transport->url)
607
die("Where do you want to fetch from today?");
608
639
- if (i < argc) {
609
+ if (argc > 1) {
610
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++) {
613
if (!strcmp(argv[i], "tag")) {
614
char *ref;
615
i++;
@@ -651,7 +621,6 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
621
refs[j++] = ref;
622
} else
623
refs[j++] = argv[i];
654
- i++;
624
}
625
refs[j] = NULL;
626
ref_nr = j;