Merge branch 'rs/resolve-ref-optional-result'
authorJunio C Hamano <gitster@pobox.com>
Thu, 5 Oct 2017 04:48:19 +0000 (13:48 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Oct 2017 04:48:19 +0000 (13:48 +0900)
Code clean-up.

* rs/resolve-ref-optional-result:
refs: pass NULL to resolve_refdup() if hash is not needed
refs: pass NULL to refs_resolve_refdup() if hash is not needed

1  2 
builtin/checkout.c
builtin/receive-pack.c
refs/files-backend.c
wt-status.c
diff --combined builtin/checkout.c
index 3345a0d16f21c38fe5fe6f32fa5b0a139e21c4a3,531f7720f3ecde335bb3c2caed3655f98e573df3..10751585ea38c282d439e4128c895c01a2598985
@@@ -436,7 -436,6 +436,7 @@@ static int reset_tree(struct tree *tree
                 * update paths in the work tree, and we cannot revert
                 * them.
                 */
 +              /* fallthrough */
        case 0:
                return 0;
        default:
@@@ -797,14 -796,9 +797,14 @@@ static void orphaned_commit_warning(str
        for_each_ref(add_pending_uninteresting_ref, &revs);
        add_pending_oid(&revs, "HEAD", &new->object.oid, UNINTERESTING);
  
 +      /* Save pending objects, so they can be cleaned up later. */
        refs = revs.pending;
        revs.leak_pending = 1;
  
 +      /*
 +       * prepare_revision_walk (together with .leak_pending = 1) makes us
 +       * the sole owner of the list of pending objects.
 +       */
        if (prepare_revision_walk(&revs))
                die(_("internal error in revision walk"));
        if (!(old->object.flags & UNINTERESTING))
        else
                describe_detached_head(_("Previous HEAD position was"), old);
  
 +      /* Clean up objects used, as they will be reused. */
        clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
 -      free(refs.objects);
 +
 +      object_array_clear(&refs);
  }
  
  static int switch_branches(const struct checkout_opts *opts,
@@@ -1124,9 -1116,8 +1124,8 @@@ static int checkout_branch(struct check
  
        if (new->path && !opts->force_detach && !opts->new_branch &&
            !opts->ignore_other_worktrees) {
-               struct object_id oid;
                int flag;
-               char *head_ref = resolve_refdup("HEAD", 0, oid.hash, &flag);
+               char *head_ref = resolve_refdup("HEAD", 0, NULL, &flag);
                if (head_ref &&
                    (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)))
                        die_if_checked_out(new->path, 1);
diff --combined builtin/receive-pack.c
index 29a0f3b75fb02dfa2a02c325816453e36baaa55a,65e38a72776c557af3c794c06f4598867a422338..cc4876740569c6e495b7cb32161d019619171d79
@@@ -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);
@@@ -1458,7 -1458,6 +1458,6 @@@ static void execute_commands(struct com
  {
        struct check_connected_options opt = CHECK_CONNECTED_INIT;
        struct command *cmd;
-       struct object_id oid;
        struct iterate_data data;
        struct async muxer;
        int err_fd = 0;
        check_aliased_updates(commands);
  
        free(head_name_to_free);
-       head_name = head_name_to_free = resolve_refdup("HEAD", 0, oid.hash, NULL);
+       head_name = head_name_to_free = resolve_refdup("HEAD", 0, NULL, NULL);
  
        if (use_atomic)
                execute_commands_atomic(commands, si);
diff --combined refs/files-backend.c
index 1b6141e718a5c449226c22fdeefb80b591ca3f7e,a69b70f861d2827a8ea5d20de25071b9a904f579..014dabb0bf3647b9392d6087ebf6c40e1bc68108
@@@ -641,6 -641,43 +641,6 @@@ out
        return ret;
  }
  
 -static int files_peel_ref(struct ref_store *ref_store,
 -                        const char *refname, unsigned char *sha1)
 -{
 -      struct files_ref_store *refs =
 -              files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
 -                             "peel_ref");
 -      int flag;
 -      unsigned char base[20];
 -
 -      if (current_ref_iter && current_ref_iter->refname == refname) {
 -              struct object_id peeled;
 -
 -              if (ref_iterator_peel(current_ref_iter, &peeled))
 -                      return -1;
 -              hashcpy(sha1, peeled.hash);
 -              return 0;
 -      }
 -
 -      if (refs_read_ref_full(ref_store, refname,
 -                             RESOLVE_REF_READING, base, &flag))
 -              return -1;
 -
 -      /*
 -       * If the reference is packed, read its ref_entry from the
 -       * cache in the hope that we already know its peeled value.
 -       * We only try this optimization on packed references because
 -       * (a) forcing the filling of the loose reference cache could
 -       * be expensive and (b) loose references anyway usually do not
 -       * have REF_KNOWS_PEELED.
 -       */
 -      if (flag & REF_ISPACKED &&
 -          !refs_peel_ref(refs->packed_ref_store, refname, sha1))
 -              return 0;
 -
 -      return peel_object(base, sha1);
 -}
 -
  struct files_ref_iterator {
        struct ref_iterator base;
  
@@@ -711,7 -748,7 +711,7 @@@ static struct ref_iterator *files_ref_i
                const char *prefix, unsigned int flags)
  {
        struct files_ref_store *refs;
 -      struct ref_iterator *loose_iter, *packed_iter;
 +      struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
        struct files_ref_iterator *iter;
        struct ref_iterator *ref_iterator;
        unsigned int required_flags = REF_STORE_READ;
  
        refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
  
 -      iter = xcalloc(1, sizeof(*iter));
 -      ref_iterator = &iter->base;
 -      base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
 -
        /*
         * We must make sure that all loose refs are read before
         * accessing the packed-refs file; this avoids a race
                        refs->packed_ref_store, prefix, 0,
                        DO_FOR_EACH_INCLUDE_BROKEN);
  
 -      iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
 +      overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter);
 +
 +      iter = xcalloc(1, sizeof(*iter));
 +      ref_iterator = &iter->base;
 +      base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable,
 +                             overlay_iter->ordered);
 +      iter->iter0 = overlay_iter;
        iter->flags = flags;
  
        return ref_iterator;
@@@ -1223,9 -1258,9 +1223,9 @@@ static int commit_ref_update(struct fil
                             const struct object_id *oid, const char *logmsg,
                             struct strbuf *err);
  
 -static int files_rename_ref(struct ref_store *ref_store,
 +static int files_copy_or_rename_ref(struct ref_store *ref_store,
                            const char *oldrefname, const char *newrefname,
 -                          const char *logmsg)
 +                          const char *logmsg, int copy)
  {
        struct files_ref_store *refs =
                files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
        }
  
        if (flag & REF_ISSYMREF) {
 -              ret = error("refname %s is a symbolic ref, renaming it is not supported",
 -                          oldrefname);
 +              if (copy)
 +                      ret = error("refname %s is a symbolic ref, copying it is not supported",
 +                                  oldrefname);
 +              else
 +                      ret = error("refname %s is a symbolic ref, renaming it is not supported",
 +                                  oldrefname);
                goto out;
        }
        if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
                goto out;
        }
  
 -      if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
 +      if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
                ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
                            oldrefname, strerror(errno));
                goto out;
        }
  
 -      if (refs_delete_ref(&refs->base, logmsg, oldrefname,
 +      if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
 +              ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
 +                          oldrefname, strerror(errno));
 +              goto out;
 +      }
 +
 +      if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,
                            orig_oid.hash, REF_NODEREF)) {
                error("unable to delete old %s", oldrefname);
                goto rollback;
         * the safety anyway; we want to delete the reference whatever
         * its current value.
         */
 -      if (!refs_read_ref_full(&refs->base, newrefname,
 +      if (!copy && !refs_read_ref_full(&refs->base, newrefname,
                                RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
                                oid.hash, NULL) &&
            refs_delete_ref(&refs->base, NULL, newrefname,
        lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
                                   REF_NODEREF, NULL, &err);
        if (!lock) {
 -              error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
 +              if (copy)
 +                      error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);
 +              else
 +                      error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
                strbuf_release(&err);
                goto rollback;
        }
        return ret;
  }
  
 +static int files_rename_ref(struct ref_store *ref_store,
 +                          const char *oldrefname, const char *newrefname,
 +                          const char *logmsg)
 +{
 +      return files_copy_or_rename_ref(ref_store, oldrefname,
 +                               newrefname, logmsg, 0);
 +}
 +
 +static int files_copy_ref(struct ref_store *ref_store,
 +                          const char *oldrefname, const char *newrefname,
 +                          const char *logmsg)
 +{
 +      return files_copy_or_rename_ref(ref_store, oldrefname,
 +                               newrefname, logmsg, 1);
 +}
 +
  static int close_ref_gently(struct ref_lock *lock)
  {
        if (close_lock_file_gently(&lock->lk))
@@@ -1543,7 -1549,7 +1543,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;
@@@ -1622,8 -1628,8 +1622,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));
@@@ -2059,7 -2065,7 +2059,7 @@@ static struct ref_iterator *reflog_iter
        struct ref_iterator *ref_iterator = &iter->base;
        struct strbuf sb = STRBUF_INIT;
  
 -      base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
 +      base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
        strbuf_addf(&sb, "%s/logs", gitdir);
        iter->dir_iterator = dir_iterator_begin(sb.buf);
        iter->ref_store = ref_store;
@@@ -2103,7 -2109,6 +2103,7 @@@ static struct ref_iterator *files_reflo
                return reflog_iterator_begin(ref_store, refs->gitcommondir);
        } else {
                return merge_ref_iterator_begin(
 +                      0,
                        reflog_iterator_begin(ref_store, refs->gitdir),
                        reflog_iterator_begin(ref_store, refs->gitcommondir),
                        reflog_iterator_select, refs);
@@@ -2494,7 -2499,6 +2494,6 @@@ static int files_transaction_prepare(st
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
        char *head_ref = NULL;
        int head_type;
-       struct object_id head_oid;
        struct files_transaction_backend_data *backend_data;
        struct ref_transaction *packed_transaction = NULL;
  
         */
        head_ref = refs_resolve_refdup(ref_store, "HEAD",
                                       RESOLVE_REF_NO_RECURSE,
-                                      head_oid.hash, &head_type);
+                                      NULL, &head_type);
  
        if (head_ref && !(head_type & REF_ISSYMREF)) {
                FREE_AND_NULL(head_ref);
@@@ -3000,8 -3004,8 +2999,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") < 0 ||
                            close_ref_gently(lock) < 0)) {
                        status |= error("couldn't write %s",
                                        get_lock_file_path(&lock->lk));
@@@ -3055,10 -3059,10 +3054,10 @@@ struct ref_storage_be refs_be_files = 
        files_initial_transaction_commit,
  
        files_pack_refs,
 -      files_peel_ref,
        files_create_symref,
        files_delete_refs,
        files_rename_ref,
 +      files_copy_ref,
  
        files_ref_iterator_begin,
        files_read_raw_ref,
diff --combined wt-status.c
index 6f730ee8f22b958c7502656d2e1d01ea1f932396,c97282cf5acd2e2d1351385c32810e96b820b96b..29bc64cc0280675065142bac257930904216d415
@@@ -121,15 -121,13 +121,13 @@@ static void status_printf_more(struct w
  
  void wt_status_prepare(struct wt_status *s)
  {
-       struct object_id oid;
        memset(s, 0, sizeof(*s));
        memcpy(s->color_palette, default_wt_status_colors,
               sizeof(default_wt_status_colors));
        s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
        s->use_color = -1;
        s->relative_paths = 1;
-       s->branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
+       s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
        s->reference = "HEAD";
        s->fp = stdout;
        s->index_file = get_index_file();
@@@ -934,7 -932,7 +932,7 @@@ size_t wt_status_locate_end(const char 
  
  void wt_status_add_cut_line(FILE *fp)
  {
 -      const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
 +      const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
        struct strbuf buf = STRBUF_INIT;
  
        fprintf(fp, "%c %s", comment_line_char, cut_line);