checkout: make advice when reattaching the HEAD less loud
[gitweb.git] / builtin / checkout.c
index 9240fafb2ad17505bf407363e058d208a8a70497..90870d9537851bd60363bdc028afbe723046a340 100644 (file)
@@ -578,6 +578,103 @@ static void update_refs_for_switch(struct checkout_opts *opts,
                report_tracking(new);
 }
 
+struct rev_list_args {
+       int argc;
+       int alloc;
+       const char **argv;
+};
+
+static void add_one_rev_list_arg(struct rev_list_args *args, const char *s)
+{
+       ALLOC_GROW(args->argv, args->argc + 1, args->alloc);
+       args->argv[args->argc++] = s;
+}
+
+static int add_one_ref_to_rev_list_arg(const char *refname,
+                                      const unsigned char *sha1,
+                                      int flags,
+                                      void *cb_data)
+{
+       add_one_rev_list_arg(cb_data, refname);
+       return 0;
+}
+
+
+static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
+{
+       struct pretty_print_context ctx = { 0 };
+
+       parse_commit(commit);
+       strbuf_addstr(sb, " - ");
+       pretty_print_commit(CMIT_FMT_ONELINE, commit, sb, &ctx);
+       strbuf_addch(sb, '\n');
+}
+
+#define ORPHAN_CUTOFF 4
+static void suggest_reattach(struct commit *commit, struct rev_info *revs)
+{
+       struct commit *c, *last = NULL;
+       struct strbuf sb = STRBUF_INIT;
+       int lost = 0;
+       while ((c = get_revision(revs)) != NULL) {
+               if (lost < ORPHAN_CUTOFF)
+                       describe_one_orphan(&sb, c);
+               last = c;
+               lost++;
+       }
+       if (ORPHAN_CUTOFF < lost) {
+               int more = lost - ORPHAN_CUTOFF;
+               if (more == 1)
+                       describe_one_orphan(&sb, last);
+               else
+                       strbuf_addf(&sb, " ... and %d more.\n", more);
+       }
+
+       fprintf(stderr,
+               "Warning: you are leaving %d commit%s behind, "
+               "not connected to\n"
+               "any of your branches:\n\n"
+               "%s\n",
+               lost, ((1 < lost) ? "s" : ""),
+               sb.buf);
+       strbuf_release(&sb);
+
+       if (advice_detached_head)
+               fprintf(stderr,
+                       "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",
+                       sha1_to_hex(commit->object.sha1));
+}
+
+/*
+ * We are about to leave commit that was at the tip of a detached
+ * HEAD.  If it is not reachable from any ref, this is the last chance
+ * for the user to do so without resorting to reflog.
+ */
+static void orphaned_commit_warning(struct commit *commit)
+{
+       struct rev_list_args args = { 0, 0, NULL };
+       struct rev_info revs;
+
+       add_one_rev_list_arg(&args, "(internal)");
+       add_one_rev_list_arg(&args, sha1_to_hex(commit->object.sha1));
+       add_one_rev_list_arg(&args, "--not");
+       for_each_ref(add_one_ref_to_rev_list_arg, &args);
+       add_one_rev_list_arg(&args, "--");
+       add_one_rev_list_arg(&args, NULL);
+
+       init_revisions(&revs, NULL);
+       if (setup_revisions(args.argc - 1, args.argv, &revs, NULL) != 1)
+               die("internal error: only -- alone should have been left");
+       if (prepare_revision_walk(&revs))
+               die("internal error in revision walk");
+       if (!(commit->object.flags & UNINTERESTING))
+               suggest_reattach(commit, &revs);
+       else
+               describe_detached_head("Previous HEAD position was", commit);
+}
+
 static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
 {
        int ret = 0;
@@ -605,13 +702,8 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
        if (ret)
                return ret;
 
-       /*
-        * If we were on a detached HEAD, but have now moved to
-        * a new commit, we want to mention the old commit once more
-        * to remind the user that it might be lost.
-        */
        if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
-               describe_detached_head("Previous HEAD position was", old.commit);
+               orphaned_commit_warning(old.commit);
 
        update_refs_for_switch(opts, &old, new);
 
@@ -686,7 +778,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        int patch_mode = 0;
        int dwim_new_local_branch = 1;
        struct option options[] = {
-               OPT__QUIET(&opts.quiet),
+               OPT__QUIET(&opts.quiet, "suppress progress reporting"),
                OPT_STRING('b', NULL, &opts.new_branch, "branch",
                           "create and checkout a new branch"),
                OPT_STRING('B', NULL, &opts.new_branch_force, "branch",
@@ -699,7 +791,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                            2),
                OPT_SET_INT('3', "theirs", &opts.writeout_stage, "checkout their version for unmerged files",
                            3),
-               OPT_BOOLEAN('f', "force", &opts.force, "force checkout (throw away local modifications)"),
+               OPT__FORCE(&opts.force, "force checkout (throw away local modifications)"),
                OPT_BOOLEAN('m', "merge", &opts.merge, "perform a 3-way merge with the new branch"),
                OPT_STRING(0, "conflict", &conflict_style, "style",
                           "conflict style (merge or diff3)"),
@@ -784,9 +876,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
         *   between A and B, A...B names that merge base.
         *
         *   With no paths, if <something> is _not_ a commit, no -t nor -b
-        *   was given, and there is a tracking branch whose name is
+        *   was given, and there is a remote-tracking branch whose name is
         *   <something> in one and only one remote, then this is a short-hand
-        *   to fork local <something> from that remote tracking branch.
+        *   to fork local <something> from that remote-tracking branch.
         *
         *   Otherwise <something> shall not be ambiguous.
         *   - If it's *only* a reference, treat it like case (1).