Merge branch 'jc/maint-index-pack-keep' into jc/index-pack
[gitweb.git] / builtin / checkout.c
index e5c16af9ef969f9d5de27bb76c6da9e75944e672..cfa66e25eb0838ba70d87d3008c6d3c57e363bd1 100644 (file)
@@ -18,8 +18,8 @@
 #include "xdiff-interface.h"
 #include "ll-merge.h"
 #include "resolve-undo.h"
+#include "submodule-config.h"
 #include "submodule.h"
-#include "argv-array.h"
 
 static const char * const checkout_usage[] = {
        N_("git checkout [<options>] <branch>"),
@@ -36,6 +36,8 @@ struct checkout_opts {
        int writeout_stage;
        int overwrite_ignore;
        int ignore_skipworktree;
+       int ignore_other_worktrees;
+       int show_progress;
 
        const char *new_branch;
        const char *new_branch_force;
@@ -54,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.sha1 : null_sha1),
-                          sha1_to_hex(new ? new->object.sha1 : null_sha1),
+                          sha1_to_hex(old ? old->object.oid.hash : null_sha1),
+                          sha1_to_hex(new ? new->object.oid.hash : null_sha1),
                           changed ? "1" : "0", NULL);
        /* "new" can be NULL when checking out from the index before
           a commit exists. */
@@ -280,7 +282,7 @@ static int checkout_paths(const struct checkout_opts *opts,
        if (opts->source_tree)
                read_tree_some(opts->source_tree, &opts->pathspec);
 
-       ps_matched = xcalloc(1, opts->pathspec.nr);
+       ps_matched = xcalloc(opts->pathspec.nr, 1);
 
        /*
         * Make sure all pathspecs participated in locating the paths
@@ -399,7 +401,7 @@ static void describe_detached_head(const char *msg, struct commit *commit)
        if (!parse_commit(commit))
                pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
        fprintf(stderr, "%s %s... %s\n", msg,
-               find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
+               find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), sb.buf);
        strbuf_release(&sb);
 }
 
@@ -416,7 +418,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o,
        opts.reset = 1;
        opts.merge = 1;
        opts.fn = oneway_merge;
-       opts.verbose_update = !o->quiet && isatty(2);
+       opts.verbose_update = o->show_progress;
        opts.src_index = &the_index;
        opts.dst_index = &the_index;
        parse_tree(tree);
@@ -441,6 +443,11 @@ struct branch_info {
        const char *name; /* The short name used */
        const char *path; /* The full name of a real branch */
        struct commit *commit; /* The named commit */
+       /*
+        * if not null the branch is detached because it's already
+        * checked out in this checkout
+        */
+       char *checkout;
 };
 
 static void setup_branch_path(struct branch_info *branch)
@@ -495,7 +502,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
                topts.update = 1;
                topts.merge = 1;
                topts.gently = opts->merge && old->commit;
-               topts.verbose_update = !opts->quiet && isatty(2);
+               topts.verbose_update = opts->show_progress;
                topts.fn = twoway_merge;
                if (opts->overwrite_ignore) {
                        topts.dir = xcalloc(1, sizeof(*topts.dir));
@@ -503,10 +510,10 @@ static int merge_working_tree(const struct checkout_opts *opts,
                        setup_standard_excludes(topts.dir);
                }
                tree = parse_tree_indirect(old->commit ?
-                                          old->commit->object.sha1 :
+                                          old->commit->object.oid.hash :
                                           EMPTY_TREE_SHA1_BIN);
                init_tree_desc(&trees[0], tree->buffer, tree->size);
-               tree = parse_tree_indirect(new->commit->object.sha1);
+               tree = parse_tree_indirect(new->commit->object.oid.hash);
                init_tree_desc(&trees[1], tree->buffer, tree->size);
 
                ret = unpack_trees(2, trees, &topts);
@@ -605,19 +612,20 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
        if (opts->new_branch) {
                if (opts->new_orphan_branch) {
                        if (opts->new_branch_log && !log_all_ref_updates) {
-                               int temp;
-                               char log_file[PATH_MAX];
-                               char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
-
-                               temp = log_all_ref_updates;
-                               log_all_ref_updates = 1;
-                               if (log_ref_setup(ref_name, log_file, sizeof(log_file))) {
-                                       fprintf(stderr, _("Can not do reflog for '%s'\n"),
-                                           opts->new_orphan_branch);
-                                       log_all_ref_updates = temp;
+                               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);
                                        return;
                                }
-                               log_all_ref_updates = temp;
+                               strbuf_release(&err);
                        }
                }
                else
@@ -633,7 +641,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
 
        old_desc = old->name;
        if (!old_desc && old->commit)
-               old_desc = sha1_to_hex(old->commit->object.sha1);
+               old_desc = oid_to_hex(&old->commit->object.oid);
 
        reflog_msg = getenv("GIT_REFLOG_ACTION");
        if (!reflog_msg)
@@ -645,7 +653,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
        if (!strcmp(new->name, "HEAD") && !new->path && !opts->force_detach) {
                /* Nothing to do. */
        } else if (opts->force_detach || !new->path) {  /* No longer on any branch. */
-               update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
+               update_ref(msg.buf, "HEAD", new->commit->object.oid.hash, NULL,
                           REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
                if (!opts->quiet) {
                        if (old->path && advice_detached_head)
@@ -653,7 +661,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
                        describe_detached_head(_("HEAD is now at"), new->commit);
                }
        } else if (new->path) { /* Switch branches. */
-               create_symref("HEAD", new->path, msg.buf);
+               if (create_symref("HEAD", new->path, msg.buf) < 0)
+                       die("unable to update HEAD");
                if (!opts->quiet) {
                        if (old->path && !strcmp(new->path, old->path)) {
                                if (opts->new_branch_force)
@@ -685,10 +694,10 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
 }
 
 static int add_pending_uninteresting_ref(const char *refname,
-                                        const unsigned char *sha1,
+                                        const struct object_id *oid,
                                         int flags, void *cb_data)
 {
-       add_pending_sha1(cb_data, refname, sha1, UNINTERESTING);
+       add_pending_sha1(cb_data, refname, oid->hash, UNINTERESTING);
        return 0;
 }
 
@@ -696,7 +705,7 @@ static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
 {
        strbuf_addstr(sb, "  ");
        strbuf_addstr(sb,
-               find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
+               find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
        strbuf_addch(sb, ' ');
        if (!parse_commit(commit))
                pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
@@ -743,11 +752,18 @@ static void suggest_reattach(struct commit *commit, struct rev_info *revs)
 
        if (advice_detached_head)
                fprintf(stderr,
-                       _(
+                       Q_(
+                       /* The singular version */
+                       "If you want to keep it by creating a new branch, "
+                       "this may be a good time\nto do so with:\n\n"
+                       " git branch <new-branch-name> %s\n\n",
+                       /* The plural version */
                        "If you want to keep them by creating a new branch, "
                        "this may be a good time\nto do so with:\n\n"
-                       " git branch <new-branch-name> %s\n\n"),
-                       find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
+                       " git branch <new-branch-name> %s\n\n",
+                       /* Give ngettext() the count */
+                       lost),
+                       find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
 }
 
 /*
@@ -765,10 +781,10 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
        setup_revisions(0, NULL, &revs, NULL);
 
        object->flags &= ~UNINTERESTING;
-       add_pending_object(&revs, object, sha1_to_hex(object->sha1));
+       add_pending_object(&revs, object, oid_to_hex(&object->oid));
 
        for_each_ref(add_pending_uninteresting_ref, &revs);
-       add_pending_sha1(&revs, "HEAD", new->object.sha1, UNINTERESTING);
+       add_pending_sha1(&revs, "HEAD", new->object.oid.hash, UNINTERESTING);
 
        refs = revs.pending;
        revs.leak_pending = 1;
@@ -883,10 +899,11 @@ static const char *unique_tracking_name(const char *name, unsigned char *sha1)
 static int parse_branchname_arg(int argc, const char **argv,
                                int dwim_new_local_branch_ok,
                                struct branch_info *new,
-                               struct tree **source_tree,
-                               unsigned char rev[20],
-                               const char **new_branch)
+                               struct checkout_opts *opts,
+                               unsigned char rev[20])
 {
+       struct tree **source_tree = &opts->source_tree;
+       const char **new_branch = &opts->new_branch;
        int argcount = 0;
        unsigned char branch_rev[20];
        const char *arg;
@@ -1087,6 +1104,17 @@ static int checkout_branch(struct checkout_opts *opts,
                die(_("Cannot switch branch to a non-commit '%s'"),
                    new->name);
 
+       if (new->path && !opts->force_detach && !opts->new_branch &&
+           !opts->ignore_other_worktrees) {
+               unsigned char sha1[20];
+               int flag;
+               char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
+               if (head_ref &&
+                   (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)))
+                       die_if_checked_out(new->path);
+               free(head_ref);
+       }
+
        if (!new->commit && opts->new_branch) {
                unsigned char rev[20];
                int flag;
@@ -1129,6 +1157,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                         N_("do not limit pathspecs to sparse entries only")),
                OPT_HIDDEN_BOOL(0, "guess", &dwim_new_local_branch,
                                N_("second guess 'git checkout <no-such-branch>'")),
+               OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees,
+                        N_("do not check if another worktree is holding the given ref")),
+               OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
                OPT_END(),
        };
 
@@ -1136,6 +1167,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        memset(&new, 0, sizeof(new));
        opts.overwrite_ignore = 1;
        opts.prefix = prefix;
+       opts.show_progress = -1;
 
        gitmodules_config();
        git_config(git_checkout_config, &opts);
@@ -1145,6 +1177,13 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, options, checkout_usage,
                             PARSE_OPT_KEEP_DASHDASH);
 
+       if (opts.show_progress < 0) {
+               if (opts.quiet)
+                       opts.show_progress = 0;
+               else
+                       opts.show_progress = isatty(2);
+       }
+
        if (conflict_style) {
                opts.merge = 1; /* implied */
                git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
@@ -1198,8 +1237,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                        opts.track == BRANCH_TRACK_UNSPECIFIED &&
                        !opts.new_branch;
                int n = parse_branchname_arg(argc, argv, dwim_ok,
-                                            &new, &opts.source_tree,
-                                            rev, &opts.new_branch);
+                                            &new, &opts, rev);
                argv += n;
                argc -= n;
        }