summaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2011-03-15 21:22:16 (GMT)
committerJunio C Hamano <gitster@pobox.com>2011-03-15 21:22:16 (GMT)
commitccf6d62d1e8d9a725b4c5a374c2d0e2cd01dd6a7 (patch)
tree566babe2b10ed9b4d25748777f6002b0c0d2f82e /builtin
parent663ee1a4c9b9caa4c8f446934410ca636f998df6 (diff)
parent8e2dc6ac06ae90a00965e5c00aa4918dfd447639 (diff)
downloadgit-ccf6d62d1e8d9a725b4c5a374c2d0e2cd01dd6a7.zip
git-ccf6d62d1e8d9a725b4c5a374c2d0e2cd01dd6a7.tar.gz
git-ccf6d62d1e8d9a725b4c5a374c2d0e2cd01dd6a7.tar.bz2
Merge branch 'jc/checkout-orphan-warning'
* jc/checkout-orphan-warning: commit: give final warning when reattaching HEAD to leave commits behind
Diffstat (limited to 'builtin')
-rw-r--r--builtin/checkout.c101
1 files changed, 95 insertions, 6 deletions
diff --git a/builtin/checkout.c b/builtin/checkout.c
index cc97dbc..2bf02f2 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -582,6 +582,100 @@ 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"
+ "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",
+ lost, ((1 < lost) ? "s" : ""),
+ sb.buf,
+ sha1_to_hex(commit->object.sha1));
+ strbuf_release(&sb);
+}
+
+/*
+ * 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;
@@ -609,13 +703,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);