checkout: check return value of resolve_refdup before using hash
[gitweb.git] / builtin / checkout.c
index 512492aad9099dd2a7aa85391b5c9a8e321c3bfe..5744499098e6c2a4c92879af9118ff3a2c6ba0ae 100644 (file)
@@ -56,8 +56,8 @@ static int post_checkout_hook(struct commit *old, struct commit *new,
                              int changed)
 {
        return run_hook_le(NULL, "post-checkout",
-                          sha1_to_hex(old ? old->object.oid.hash : null_sha1),
-                          sha1_to_hex(new ? new->object.oid.hash : null_sha1),
+                          oid_to_hex(old ? &old->object.oid : &null_oid),
+                          oid_to_hex(new ? &new->object.oid : &null_oid),
                           changed ? "1" : "0", NULL);
        /* "new" can be NULL when checking out from the index before
           a commit exists. */
@@ -274,7 +274,7 @@ static int checkout_paths(const struct checkout_opts *opts,
 
        lock_file = xcalloc(1, sizeof(struct lock_file));
 
-       hold_locked_index(lock_file, 1);
+       hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
        if (read_cache_preload(&opts->pathspec) < 0)
                return error(_("index file corrupt"));
 
@@ -452,7 +452,7 @@ static void setup_branch_path(struct branch_info *branch)
 {
        struct strbuf buf = STRBUF_INIT;
 
-       strbuf_branchname(&buf, branch->name);
+       strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
        if (strcmp(buf.buf, branch->name))
                branch->name = xstrdup(buf.buf);
        strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
@@ -467,7 +467,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
        int ret;
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 
-       hold_locked_index(lock_file, 1);
+       hold_locked_index(lock_file, LOCK_DIE_ON_ERROR);
        if (read_cache_preload(NULL) < 0)
                return error(_("index file corrupt"));
 
@@ -612,22 +612,25 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
        const char *old_desc, *reflog_msg;
        if (opts->new_branch) {
                if (opts->new_orphan_branch) {
-                       if (opts->new_branch_log && !log_all_ref_updates) {
+                       char *refname;
+
+                       refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
+                       if (opts->new_branch_log &&
+                           !should_autocreate_reflog(refname)) {
                                int ret;
-                               char *refname;
                                struct strbuf err = STRBUF_INIT;
 
-                               refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
                                ret = safe_create_reflog(refname, 1, &err);
-                               free(refname);
                                if (ret) {
                                        fprintf(stderr, _("Can not do reflog for '%s': %s\n"),
                                                opts->new_orphan_branch, err.buf);
                                        strbuf_release(&err);
+                                       free(refname);
                                        return;
                                }
                                strbuf_release(&err);
                        }
+                       free(refname);
                }
                else
                        create_branch(opts->new_branch, new->name,
@@ -811,7 +814,8 @@ static int switch_branches(const struct checkout_opts *opts,
        int flag, writeout_error = 0;
        memset(&old, 0, sizeof(old));
        old.path = path_to_free = resolve_refdup("HEAD", 0, rev.hash, &flag);
-       old.commit = lookup_commit_reference_gently(rev.hash, 1);
+       if (old.path)
+               old.commit = lookup_commit_reference_gently(rev.hash, 1);
        if (!(flag & REF_ISSYMREF))
                old.path = NULL;