Merge branch 'ao/check-resolve-ref-unsafe-result'
authorJunio C Hamano <gitster@pobox.com>
Mon, 6 Nov 2017 04:11:25 +0000 (13:11 +0900)
committerJunio C Hamano <gitster@pobox.com>
Mon, 6 Nov 2017 04:11:25 +0000 (13:11 +0900)
"git commit", after making a commit, did not check for errors when
asking on what branch it made the commit, which has been correted.

* ao/check-resolve-ref-unsafe-result:
commit: check result of resolve_ref_unsafe

1  2 
builtin/commit.c
diff --combined builtin/commit.c
index af034553fcf22c463d2be04f4aafff7c9d6176dc,b528290902da1153849e6cf4560488c553a1e38a..1a4ad725bc9e53fe56a14a779bf7e3e43bb9befa
@@@ -195,6 -195,7 +195,6 @@@ static void determine_whence(struct wt_
  static void status_init_config(struct wt_status *s, config_fn_t fn)
  {
        wt_status_prepare(s);
 -      gitmodules_config();
        git_config(fn, s);
        determine_whence(s);
        init_diff_ui_defaults();
@@@ -335,7 -336,7 +335,7 @@@ static void refresh_cache_or_die(int re
  static const char *prepare_index(int argc, const char **argv, const char *prefix,
                                 const struct commit *current_head, int is_status)
  {
 -      struct string_list partial;
 +      struct string_list partial = STRING_LIST_INIT_DUP;
        struct pathspec pathspec;
        int refresh_flags = REFRESH_QUIET;
        const char *ret;
  
                refresh_cache_or_die(refresh_flags);
  
 -              if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
 +              if (write_locked_index(&the_index, &index_lock, 0))
                        die(_("unable to create temporary index"));
  
                old_index_env = getenv(INDEX_ENVIRONMENT);
                if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
                        if (reopen_lock_file(&index_lock) < 0)
                                die(_("unable to write index file"));
 -                      if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
 +                      if (write_locked_index(&the_index, &index_lock, 0))
                                die(_("unable to update temporary index"));
                } else
                        warning(_("Failed to update main cache tree"));
  
                commit_style = COMMIT_NORMAL;
 -              return get_lock_file_path(&index_lock);
 +              ret = get_lock_file_path(&index_lock);
 +              goto out;
        }
  
        /*
                add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
                refresh_cache_or_die(refresh_flags);
                update_main_cache_tree(WRITE_TREE_SILENT);
 -              if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
 +              if (write_locked_index(&the_index, &index_lock, 0))
                        die(_("unable to write new_index file"));
                commit_style = COMMIT_NORMAL;
 -              return get_lock_file_path(&index_lock);
 +              ret = get_lock_file_path(&index_lock);
 +              goto out;
        }
  
        /*
                        rollback_lock_file(&index_lock);
                }
                commit_style = COMMIT_AS_IS;
 -              return get_index_file();
 +              ret = get_index_file();
 +              goto out;
        }
  
        /*
                        die(_("cannot do a partial commit during a cherry-pick."));
        }
  
 -      string_list_init(&partial, 1);
        if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, &pathspec))
                exit(1);
  
        add_remove_files(&partial);
        refresh_cache(REFRESH_QUIET);
        update_main_cache_tree(WRITE_TREE_SILENT);
 -      if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
 +      if (write_locked_index(&the_index, &index_lock, 0))
                die(_("unable to write new_index file"));
  
        hold_lock_file_for_update(&false_lock,
        add_remove_files(&partial);
        refresh_cache(REFRESH_QUIET);
  
 -      if (write_locked_index(&the_index, &false_lock, CLOSE_LOCK))
 +      if (write_locked_index(&the_index, &false_lock, 0))
                die(_("unable to write temporary index file"));
  
        discard_cache();
        ret = get_lock_file_path(&false_lock);
        read_cache_from(ret);
 +out:
 +      string_list_clear(&partial, 0);
 +      clear_pathspec(&pathspec);
        return ret;
  }
  
@@@ -514,7 -510,7 +514,7 @@@ static int run_status(FILE *fp, const c
        s->index_file = index_file;
        s->fp = fp;
        s->nowarn = nowarn;
 -      s->is_initial = get_sha1(s->reference, oid.hash) ? 1 : 0;
 +      s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
        if (!s->is_initial)
                hashcpy(s->sha1_commit, oid.hash);
        s->status_format = status_format;
@@@ -895,7 -891,7 +895,7 @@@ static int prepare_to_commit(const cha
                if (amend)
                        parent = "HEAD^1";
  
 -              if (get_sha1(parent, oid.hash)) {
 +              if (get_oid(parent, &oid)) {
                        int i, ita_nr = 0;
  
                        for (i = 0; i < active_nr; i++)
                return 0;
        }
  
 -      /*
 -       * Re-read the index as pre-commit hook could have updated it,
 -       * and write it out as a tree.  We must do this before we invoke
 -       * the editor and after we invoke run_status above.
 -       */
 -      discard_cache();
 +      if (!no_verify && find_hook("pre-commit")) {
 +              /*
 +               * Re-read the index as pre-commit hook could have updated it,
 +               * and write it out as a tree.  We must do this before we invoke
 +               * the editor and after we invoke run_status above.
 +               */
 +              discard_cache();
 +      }
        read_cache_from(index_file);
 +
        if (update_main_cache_tree(0)) {
                error(_("Error building trees"));
                return 0;
@@@ -1392,12 -1385,9 +1392,12 @@@ int cmd_status(int argc, const char **a
        read_cache_preload(&s.pathspec);
        refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL);
  
 -      fd = hold_locked_index(&index_lock, 0);
 +      if (use_optional_locks())
 +              fd = hold_locked_index(&index_lock, 0);
 +      else
 +              fd = -1;
  
 -      s.is_initial = get_sha1(s.reference, oid.hash) ? 1 : 0;
 +      s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
        if (!s.is_initial)
                hashcpy(s.sha1_commit, oid.hash);
  
@@@ -1439,6 -1429,7 +1439,6 @@@ static void print_summary(const char *p
        struct rev_info rev;
        struct commit *commit;
        struct strbuf format = STRBUF_INIT;
 -      struct object_id junk_oid;
        const char *head;
        struct pretty_print_context pctx = {0};
        struct strbuf author_ident = STRBUF_INIT;
        rev.diffopt.break_opt = 0;
        diff_setup_done(&rev.diffopt);
  
 -      head = resolve_ref_unsafe("HEAD", 0, junk_oid.hash, NULL);
 +      head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
+       if (!head)
+               die_errno(_("unable to resolve HEAD after creating commit"));
        if (!strcmp(head, "HEAD"))
                head = _("detached HEAD");
        else
@@@ -1666,7 -1659,7 +1668,7 @@@ int cmd_commit(int argc, const char **a
        status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
        s.colopts = 0;
  
 -      if (get_sha1("HEAD", oid.hash))
 +      if (get_oid("HEAD", &oid))
                current_head = NULL;
        else {
                current_head = lookup_commit_or_die(&oid, "HEAD");
        if (!quiet)
                print_summary(prefix, &oid, !current_head);
  
 -      strbuf_release(&err);
 +      UNLEAK(err);
 +      UNLEAK(sb);
        return 0;
  }