From 9d19c6ea52755edec9b8e6d5b838b712fc7e97f6 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 21 May 2012 09:59:56 +0200 Subject: cmd_fetch_pack(): declare dest to be const There is no need for it to be non-const, and this avoids the need for casting away the constness of an argv element. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- builtin/fetch-pack.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index a9b6077af..25c79ca8c 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -900,7 +900,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) { int i, ret, nr_heads; struct ref *ref = NULL; - char *dest = NULL, **heads; + const char *dest = NULL; + char **heads; int fd[2]; char *pack_lockfile = NULL; char **pack_lockfile_ptr = NULL; @@ -970,7 +971,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) } usage(fetch_pack_usage); } - dest = (char *)arg; + dest = arg; heads = (char **)(argv + i + 1); nr_heads = argc - i - 1; break; @@ -1017,7 +1018,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) fd[0] = 0; fd[1] = 1; } else { - conn = git_connect(fd, (char *)dest, args.uploadpack, + conn = git_connect(fd, dest, args.uploadpack, args.verbose ? CONNECT_VERBOSE : 0); } -- cgit v1.2.1 From 4cc00fcf5daf63648b7fd9382ca3e02033a0e1ec Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 21 May 2012 09:59:57 +0200 Subject: cmd_fetch_pack(): handle non-option arguments outside of the loop This makes it more obvious that the code is always executed unless there is an error, and that the first initialization of nr_heads is unnecessary. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- builtin/fetch-pack.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index 25c79ca8c..5c72226c4 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -909,7 +909,6 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) packet_trace_identity("fetch-pack"); - nr_heads = 0; heads = NULL; for (i = 1; i < argc; i++) { const char *arg = argv[i]; @@ -971,14 +970,17 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) } usage(fetch_pack_usage); } - dest = arg; - heads = (char **)(argv + i + 1); - nr_heads = argc - i - 1; break; } - if (!dest) + + if (i < argc) + dest = argv[i++]; + else usage(fetch_pack_usage); + heads = (char **)(argv + i); + nr_heads = argc - i; + if (args.stdin_refs) { /* * Copy refs from cmdline to new growable list, then -- cgit v1.2.1 From ff22ff9909a09d775c740f31443f96edd5b5e006 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 21 May 2012 09:59:58 +0200 Subject: cmd_fetch_pack(): combine the loop termination conditions If an argument that does not start with '-' is found, the loop is terminated. So move that check into the for-loop condition. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- builtin/fetch-pack.c | 113 +++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 58 deletions(-) diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index 5c72226c4..dc4b1dcec 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -910,67 +910,64 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) packet_trace_identity("fetch-pack"); heads = NULL; - for (i = 1; i < argc; i++) { + for (i = 1; i < argc && *argv[i] == '-'; i++) { const char *arg = argv[i]; - if (*arg == '-') { - if (!prefixcmp(arg, "--upload-pack=")) { - args.uploadpack = arg + 14; - continue; - } - if (!prefixcmp(arg, "--exec=")) { - args.uploadpack = arg + 7; - continue; - } - if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { - args.quiet = 1; - continue; - } - if (!strcmp("--keep", arg) || !strcmp("-k", arg)) { - args.lock_pack = args.keep_pack; - args.keep_pack = 1; - continue; - } - if (!strcmp("--thin", arg)) { - args.use_thin_pack = 1; - continue; - } - if (!strcmp("--include-tag", arg)) { - args.include_tag = 1; - continue; - } - if (!strcmp("--all", arg)) { - args.fetch_all = 1; - continue; - } - if (!strcmp("--stdin", arg)) { - args.stdin_refs = 1; - continue; - } - if (!strcmp("-v", arg)) { - args.verbose = 1; - continue; - } - if (!prefixcmp(arg, "--depth=")) { - args.depth = strtol(arg + 8, NULL, 0); - continue; - } - if (!strcmp("--no-progress", arg)) { - args.no_progress = 1; - continue; - } - if (!strcmp("--stateless-rpc", arg)) { - args.stateless_rpc = 1; - continue; - } - if (!strcmp("--lock-pack", arg)) { - args.lock_pack = 1; - pack_lockfile_ptr = &pack_lockfile; - continue; - } - usage(fetch_pack_usage); + if (!prefixcmp(arg, "--upload-pack=")) { + args.uploadpack = arg + 14; + continue; + } + if (!prefixcmp(arg, "--exec=")) { + args.uploadpack = arg + 7; + continue; + } + if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { + args.quiet = 1; + continue; + } + if (!strcmp("--keep", arg) || !strcmp("-k", arg)) { + args.lock_pack = args.keep_pack; + args.keep_pack = 1; + continue; + } + if (!strcmp("--thin", arg)) { + args.use_thin_pack = 1; + continue; + } + if (!strcmp("--include-tag", arg)) { + args.include_tag = 1; + continue; + } + if (!strcmp("--all", arg)) { + args.fetch_all = 1; + continue; } - break; + if (!strcmp("--stdin", arg)) { + args.stdin_refs = 1; + continue; + } + if (!strcmp("-v", arg)) { + args.verbose = 1; + continue; + } + if (!prefixcmp(arg, "--depth=")) { + args.depth = strtol(arg + 8, NULL, 0); + continue; + } + if (!strcmp("--no-progress", arg)) { + args.no_progress = 1; + continue; + } + if (!strcmp("--stateless-rpc", arg)) { + args.stateless_rpc = 1; + continue; + } + if (!strcmp("--lock-pack", arg)) { + args.lock_pack = 1; + pack_lockfile_ptr = &pack_lockfile; + continue; + } + usage(fetch_pack_usage); } if (i < argc) -- cgit v1.2.1 From 57e6fc69588ea5e1b6a151d60f2cf9fa636251c1 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Mon, 21 May 2012 09:59:59 +0200 Subject: cmd_fetch_pack(): respect constness of argv parameter The old code cast away the constness of the strings passed to the function in argument argv[], which could result in their being modified by filter_refs(). Fix by copying reference names from argv and putting them into our own array (similarly to how refnames passed to stdin were already handled). Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- builtin/fetch-pack.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index dc4b1dcec..80e72df49 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -898,10 +898,11 @@ static void fetch_pack_setup(void) int cmd_fetch_pack(int argc, const char **argv, const char *prefix) { - int i, ret, nr_heads; + int i, ret; struct ref *ref = NULL; const char *dest = NULL; - char **heads; + int alloc_heads = 0, nr_heads = 0; + char **heads = NULL; int fd[2]; char *pack_lockfile = NULL; char **pack_lockfile_ptr = NULL; @@ -909,7 +910,6 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) packet_trace_identity("fetch-pack"); - heads = NULL; for (i = 1; i < argc && *argv[i] == '-'; i++) { const char *arg = argv[i]; @@ -975,17 +975,14 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix) else usage(fetch_pack_usage); - heads = (char **)(argv + i); - nr_heads = argc - i; - + /* + * Copy refs from cmdline to growable list, then append any + * refs from the standard input: + */ + ALLOC_GROW(heads, argc - i, alloc_heads); + for (; i < argc; i++) + heads[nr_heads++] = xstrdup(argv[i]); if (args.stdin_refs) { - /* - * Copy refs from cmdline to new growable list, then - * append the refs from the standard input. - */ - int alloc_heads = nr_heads; - int size = nr_heads * sizeof(*heads); - heads = memcpy(xmalloc(size), heads, size); if (args.stateless_rpc) { /* in stateless RPC mode we use pkt-line to read * from stdin, until we get a flush packet -- cgit v1.2.1