http-push: use an argv_array for setup_revisions

This drops the magic number for the fixed-size argv arrays, so we do not have to wonder if we are overflowing it. We can also drop some confusing sha1_to_hex memory allocation (which seems to predate the ring of buffers allowing multiple calls), and get rid of an unchecked sprintf call. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 24, 2015 at 17:07 UTC a0355f6bcde6fb62d9629927b2e241353b38aeef
1 file changed +10 -22
http-push.c
+10 -22
@@ -10,6 +10,7 @@
10 #include "remote.h"
11 #include "list-objects.h"
12 #include "sigchain.h"
13 +#include "argv-array.h"
14
15 #ifdef EXPAT_NEEDS_XMLPARSE_H
16 #include <xmlparse.h>
@@ -1856,9 +1857,7 @@ int main(int argc, char **argv)
1857 new_refs = 0;
1858 for (ref = remote_refs; ref; ref = ref->next) {
1859 char old_hex[60], *new_hex;
1859 - const char *commit_argv[5];
1860 - int commit_argc;
1861 - char *new_sha1_hex, *old_sha1_hex;
1860 + struct argv_array commit_argv = ARGV_ARRAY_INIT;
1861
1862 if (!ref->peer_ref)
1863 continue;
@@ -1937,27 +1936,15 @@ int main(int argc, char **argv)
1936 }
1937
1938 /* Set up revision info for this refspec */
1940 - commit_argc = 3;
1941 - new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
1942 - old_sha1_hex = NULL;
1943 - commit_argv[1] = "--objects";
1944 - commit_argv[2] = new_sha1_hex;
1945 - if (!push_all && !is_null_sha1(ref->old_sha1)) {
1946 - old_sha1_hex = xmalloc(42);
1947 - sprintf(old_sha1_hex, "^%s",
1948 - sha1_to_hex(ref->old_sha1));
1949 - commit_argv[3] = old_sha1_hex;
1950 - commit_argc++;
1951 - }
1952 - commit_argv[commit_argc] = NULL;
1939 + argv_array_push(&commit_argv, ""); /* ignored */
1940 + argv_array_push(&commit_argv, "--objects");
1941 + argv_array_push(&commit_argv, sha1_to_hex(ref->new_sha1));
1942 + if (!push_all && !is_null_sha1(ref->old_sha1))
1943 + argv_array_pushf(&commit_argv, "^%s",
1944 + sha1_to_hex(ref->old_sha1));
1945 init_revisions(&revs, setup_git_directory());
1954 - setup_revisions(commit_argc, commit_argv, &revs, NULL);
1946 + setup_revisions(commit_argv.argc, commit_argv.argv, &revs, NULL);
1947 revs.edge_hint = 0; /* just in case */
1956 - free(new_sha1_hex);
1957 - if (old_sha1_hex) {
1958 - free(old_sha1_hex);
1959 - commit_argv[1] = NULL;
1960 - }
1948
1949 /* Generate a list of objects that need to be pushed */
1950 pushing = 0;
@@ -1986,6 +1973,7 @@ int main(int argc, char **argv)
1973 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
1974 unlock_remote(ref_lock);
1975 check_locks();
1976 + argv_array_clear(&commit_argv);
1977 }
1978
1979 /* Update remote server info if appropriate */