Merge branch 'rs/resolve-ref-optional-result'
authorJunio C Hamano <gitster@pobox.com>
Thu, 28 Sep 2017 05:47:56 +0000 (14:47 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Sep 2017 05:47:56 +0000 (14:47 +0900)
Code clean-up.

* rs/resolve-ref-optional-result:
refs: pass NULL to resolve_ref_unsafe() if hash is not needed
refs: pass NULL to refs_resolve_ref_unsafe() if hash is not needed
refs: make sha1 output parameter of refs_resolve_ref_unsafe() optional

1  2 
builtin/receive-pack.c
builtin/submodule--helper.c
http-backend.c
refs.c
refs.h
refs/files-backend.c
revision.c
diff --combined builtin/receive-pack.c
index dd06b3fb4f9ee3e4da46d807b183541ec14c5481,65d89078ab885dce2838a4e01bca7205d804108a..29a0f3b75fb02dfa2a02c325816453e36baaa55a
@@@ -743,7 -743,7 +743,7 @@@ static int run_and_feed_hook(const cha
                size_t n;
                if (feed(feed_state, &buf, &n))
                        break;
 -              if (write_in_full(proc.in, buf, n) != n)
 +              if (write_in_full(proc.in, buf, n) < 0)
                        break;
        }
        close(proc.in);
@@@ -1207,11 -1207,10 +1207,10 @@@ static void check_aliased_update(struc
        const char *dst_name;
        struct string_list_item *item;
        struct command *dst_cmd;
-       unsigned char sha1[GIT_MAX_RAWSZ];
        int flag;
  
        strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
-       dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
+       dst_name = resolve_ref_unsafe(buf.buf, 0, NULL, &flag);
        strbuf_release(&buf);
  
        if (!(flag & REF_ISSYMREF))
index f6a5e1af5d7c2fdb827f139e899e96cb9c4d91b5,ea49710893a1194691c43450657abc9b74eebaf9..06ed02f99402be6a7edb36ff0eb08b903c513c6c
@@@ -17,9 -17,8 +17,8 @@@
  static char *get_default_remote(void)
  {
        char *dest = NULL, *ret;
-       unsigned char sha1[20];
        struct strbuf sb = STRBUF_INIT;
-       const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
+       const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
  
        if (!refname)
                die(_("No such ref: %s"), "HEAD");
@@@ -1089,8 -1088,7 +1088,7 @@@ static const char *remote_submodule_bra
                return "master";
  
        if (!strcmp(branch, ".")) {
-               unsigned char sha1[20];
-               const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
+               const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
  
                if (!refname)
                        die(_("No such ref: %s"), "HEAD");
@@@ -1189,7 -1187,6 +1187,7 @@@ static int push_check(int argc, const c
                                                break;
                                        die("HEAD does not match the named branch in the superproject");
                                }
 +                              /* fallthrough */
                        default:
                                die("src refspec '%s' must name a ref",
                                    rs->src);
diff --combined http-backend.c
index e51c7805c805ac6c1234665f5f90fcdd51d39231,b8ce960a35986f03303df047a2be286966dc5390..f3dc218b2a3d2662efda56fb6c8878a37a4e67a4
@@@ -358,7 -358,7 +358,7 @@@ static void inflate_request(const char 
                                die("zlib error inflating request, result %d", ret);
  
                        n = stream.total_out - cnt;
 -                      if (write_in_full(out, out_buf, n) != n)
 +                      if (write_in_full(out, out_buf, n) < 0)
                                die("%s aborted reading request", prog_name);
                        cnt += n;
  
@@@ -379,7 -379,7 +379,7 @@@ static void copy_request(const char *pr
        ssize_t n = read_request(0, &buf);
        if (n < 0)
                die_errno("error reading request body");
 -      if (write_in_full(out, buf, n) != n)
 +      if (write_in_full(out, buf, n) < 0)
                die("%s aborted reading request", prog_name);
        close(out);
        free(buf);
@@@ -486,10 -486,9 +486,9 @@@ static int show_head_ref(const char *re
        struct strbuf *buf = cb_data;
  
        if (flag & REF_ISSYMREF) {
-               struct object_id unused;
                const char *target = resolve_ref_unsafe(refname,
                                                        RESOLVE_REF_READING,
-                                                       unused.hash, NULL);
+                                                       NULL, NULL);
  
                if (target)
                        strbuf_addf(buf, "ref: %s\n", strip_namespace(target));
diff --combined refs.c
index 83f0e687857dfaee3b91ce4d47b16f2f16945ce2,97e33fdc7b3bc6ebb17937585d2348911db5723c..6042645c40614992bbd8ea0b06c53a350449f413
--- 1/refs.c
--- 2/refs.c
+++ b/refs.c
@@@ -239,8 -239,7 +239,7 @@@ int read_ref(const char *refname, unsig
  
  int ref_exists(const char *refname)
  {
-       unsigned char sha1[20];
-       return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
+       return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, NULL, NULL);
  }
  
  static int filter_refs(const char *refname, const struct object_id *oid,
@@@ -286,12 -285,11 +285,11 @@@ static int warn_if_dangling_symref(cons
  {
        struct warn_if_dangling_data *d = cb_data;
        const char *resolves_to;
-       struct object_id junk;
  
        if (!(flags & REF_ISSYMREF))
                return 0;
  
-       resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
+       resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL);
        if (!resolves_to
            || (d->refname
                ? strcmp(resolves_to, d->refname)
@@@ -609,7 -607,7 +607,7 @@@ static int write_pseudoref(const char *
                }
        }
  
 -      if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
 +      if (write_in_full(fd, buf.buf, buf.len) < 0) {
                strbuf_addf(err, "could not write to '%s'", filename);
                rollback_lock_file(&lock);
                goto done;
@@@ -939,8 -937,6 +937,8 @@@ int ref_transaction_update(struct ref_t
                return -1;
        }
  
 +      flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
 +
        flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
  
        ref_transaction_add_update(transaction, refname, flags,
@@@ -1359,7 -1355,7 +1357,7 @@@ int for_each_replace_ref(each_ref_fn fn
        return do_for_each_ref(get_main_ref_store(),
                               git_replace_ref_base, fn,
                               strlen(git_replace_ref_base),
 -                             0, cb_data);
 +                             DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
  }
  
  int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
@@@ -1398,9 -1394,12 +1396,12 @@@ const char *refs_resolve_ref_unsafe(str
                                    unsigned char *sha1, int *flags)
  {
        static struct strbuf sb_refname = STRBUF_INIT;
+       struct object_id unused_oid;
        int unused_flags;
        int symref_count;
  
+       if (!sha1)
+               sha1 = unused_oid.hash;
        if (!flags)
                flags = &unused_flags;
  
diff --combined refs.h
index ae33a5f74d65dda62c84deb1eaae1774a0d21214,996d4ea54d02323e79f3a96241efd1cafdbfa727..e1c5803f9a5aba0b6ba18f5a9bd5192cba661542
--- 1/refs.h
--- 2/refs.h
+++ b/refs.h
@@@ -10,10 -10,11 +10,11 @@@ struct worktree
  /*
   * Resolve a reference, recursively following symbolic refererences.
   *
-  * Store the referred-to object's name in sha1 and return the name of
-  * the non-symbolic reference that ultimately pointed at it.  The
-  * return value, if not NULL, is a pointer into either a static buffer
-  * or the input ref.
+  * Return the name of the non-symbolic reference that ultimately pointed
+  * at the resolved object name.  The return value, if not NULL, is a
+  * pointer into either a static buffer or the input ref.
+  *
+  * If sha1 is non-NULL, store the referred-to object's name in it.
   *
   * If the reference cannot be resolved to an object, the behavior
   * depends on the RESOLVE_REF_READING flag:
@@@ -344,14 -345,6 +345,14 @@@ int refs_pack_refs(struct ref_store *re
  #define REF_NODEREF   0x01
  #define REF_FORCE_CREATE_REFLOG 0x40
  
 +/*
 + * Flags that can be passed in to ref_transaction_update
 + */
 +#define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
 +      REF_ISPRUNING |                      \
 +      REF_FORCE_CREATE_REFLOG |            \
 +      REF_NODEREF
 +
  /*
   * Setup reflog before using. Fill in err and return -1 on failure.
   */
diff --combined refs/files-backend.c
index dac33628b37d4e19ea15e6706f1ab67c9b3a3e6c,a3134d23ab11f177d0e5f482848cd9d7086f0b71..fec77744b40179b1454093bd37cf0815f3b755ae
@@@ -1549,7 -1549,7 +1549,7 @@@ static int log_ref_write_fd(int fd, con
  
        written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
        free(logrec);
 -      if (written != len)
 +      if (written < 0)
                return -1;
  
        return 0;
@@@ -1628,8 -1628,8 +1628,8 @@@ static int write_ref_to_lockfile(struc
                return -1;
        }
        fd = get_lock_file_fd(&lock->lk);
 -      if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
 -          write_in_full(fd, &term, 1) != 1 ||
 +      if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 ||
 +          write_in_full(fd, &term, 1) < 0 ||
            close_ref_gently(lock) < 0) {
                strbuf_addf(err,
                            "couldn't write '%s'", get_lock_file_path(&lock->lk));
@@@ -1676,13 -1676,12 +1676,12 @@@ static int commit_ref_update(struct fil
                 * check with HEAD only which should cover 99% of all usage
                 * scenarios (even 100% of the default ones).
                 */
-               struct object_id head_oid;
                int head_flag;
                const char *head_ref;
  
                head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
                                                   RESOLVE_REF_READING,
-                                                  head_oid.hash, &head_flag);
+                                                  NULL, &head_flag);
                if (head_ref && (head_flag & REF_ISSYMREF) &&
                    !strcmp(head_ref, lock->ref_name)) {
                        struct strbuf log_err = STRBUF_INIT;
@@@ -3006,8 -3005,8 +3005,8 @@@ static int files_reflog_expire(struct r
                        rollback_lock_file(&reflog_lock);
                } else if (update &&
                           (write_in_full(get_lock_file_fd(&lock->lk),
 -                              oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
 -                          write_str_in_full(get_lock_file_fd(&lock->lk), "\n") != 1 ||
 +                              oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 ||
 +                          write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 1 ||
                            close_ref_gently(lock) < 0)) {
                        status |= error("couldn't write %s",
                                        get_lock_file_path(&lock->lk));
diff --combined revision.c
index 1520f69d93583972a71d5ece7870b598c9060555,9582a727ca98bafbc269a48224744ea314861281..613168d397bcce6f2ddb2cf074707f3aff79f8c3
@@@ -21,7 -21,6 +21,7 @@@
  #include "bisect.h"
  #include "packfile.h"
  #include "worktree.h"
 +#include "argv-array.h"
  
  volatile show_early_output_fn_t show_early_output;
  
@@@ -1673,15 -1672,31 +1673,15 @@@ int handle_revision_arg(const char *arg
        return 0;
  }
  
 -struct cmdline_pathspec {
 -      int alloc;
 -      int nr;
 -      const char **path;
 -};
 -
 -static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
 -{
 -      while (*av) {
 -              ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
 -              prune->path[prune->nr++] = *(av++);
 -      }
 -}
 -
  static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
 -                                   struct cmdline_pathspec *prune)
 +                                   struct argv_array *prune)
  {
 -      while (strbuf_getline(sb, stdin) != EOF) {
 -              ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
 -              prune->path[prune->nr++] = xstrdup(sb->buf);
 -      }
 +      while (strbuf_getline(sb, stdin) != EOF)
 +              argv_array_push(prune, sb->buf);
  }
  
  static void read_revisions_from_stdin(struct rev_info *revs,
 -                                    struct cmdline_pathspec *prune)
 +                                    struct argv_array *prune)
  {
        struct strbuf sb;
        int seen_dashdash = 0;
@@@ -2248,11 -2263,10 +2248,10 @@@ static int handle_revision_pseudo_opt(c
  
  static void NORETURN diagnose_missing_default(const char *def)
  {
-       unsigned char sha1[20];
        int flags;
        const char *refname;
  
-       refname = resolve_ref_unsafe(def, 0, sha1, &flags);
+       refname = resolve_ref_unsafe(def, 0, NULL, &flags);
        if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
                die(_("your current branch appears to be broken"));
  
  int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
  {
        int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
 -      struct cmdline_pathspec prune_data;
 +      struct argv_array prune_data = ARGV_ARRAY_INIT;
        const char *submodule = NULL;
  
 -      memset(&prune_data, 0, sizeof(prune_data));
        if (opt)
                submodule = opt->submodule;
  
                        argv[i] = NULL;
                        argc = i;
                        if (argv[i + 1])
 -                              append_prune_data(&prune_data, argv + i + 1);
 +                              argv_array_pushv(&prune_data, argv + i + 1);
                        seen_dashdash = 1;
                        break;
                }
                        for (j = i; j < argc; j++)
                                verify_filename(revs->prefix, argv[j], j == i);
  
 -                      append_prune_data(&prune_data, argv + i);
 +                      argv_array_pushv(&prune_data, argv + i);
                        break;
                }
                else
                        got_rev_arg = 1;
        }
  
 -      if (prune_data.nr) {
 +      if (prune_data.argc) {
                /*
                 * If we need to introduce the magic "a lone ':' means no
                 * pathspec whatsoever", here is the place to do so.
                 *      call init_pathspec() to set revs->prune_data here.
                 * }
                 */
 -              ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
 -              prune_data.path[prune_data.nr++] = NULL;
                parse_pathspec(&revs->prune_data, 0, 0,
 -                             revs->prefix, prune_data.path);
 +                             revs->prefix, prune_data.argv);
        }
 +      argv_array_clear(&prune_data);
  
        if (revs->def == NULL)
                revs->def = opt ? opt->def : NULL;