Merge branch 'js/checkout-detach-count'
authorJunio C Hamano <gitster@pobox.com>
Thu, 10 May 2012 17:49:25 +0000 (10:49 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 10 May 2012 17:49:25 +0000 (10:49 -0700)
When checking out another commit from an already detached state, we used
to report all commits that are not reachable from any of the refs as
lossage, but some of them might be reachable from the new HEAD, and there
is no need to warn about them.

By Johannes Sixt
* js/checkout-detach-count:
checkout (detached): truncate list of orphaned commits at the new HEAD
t2020-checkout-detach: check for the number of orphaned commits

1  2 
builtin/checkout.c
t/t2020-checkout-detach.sh
diff --combined builtin/checkout.c
index 23fc56d88d478593727fe1cd6d9694226b0ad72a,160f678b8cae006953ce7b4f9bdd424e53ce47f7..c93efe4d9815c8fbbb3ff0ee994e1736b26a3efd
@@@ -543,7 -543,6 +543,7 @@@ static void update_refs_for_switch(stru
                                      opts->new_branch_force ? 1 : 0,
                                      opts->new_branch_log,
                                      opts->new_branch_force ? 1 : 0,
 +                                    opts->quiet,
                                      opts->track);
                new->name = opts->new_branch;
                setup_branch_path(new);
@@@ -672,10 -671,10 +672,10 @@@ static void suggest_reattach(struct com
   * 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)
+ static void orphaned_commit_warning(struct commit *old, struct commit *new)
  {
        struct rev_info revs;
-       struct object *object = &commit->object;
+       struct object *object = &old->object;
        struct object_array refs;
  
        init_revisions(&revs, NULL);
        add_pending_object(&revs, object, sha1_to_hex(object->sha1));
  
        for_each_ref(add_pending_uninteresting_ref, &revs);
+       add_pending_sha1(&revs, "HEAD", new->object.sha1, UNINTERESTING);
  
        refs = revs.pending;
        revs.leak_pending = 1;
  
        if (prepare_revision_walk(&revs))
                die(_("internal error in revision walk"));
-       if (!(commit->object.flags & UNINTERESTING))
-               suggest_reattach(commit, &revs);
+       if (!(old->object.flags & UNINTERESTING))
+               suggest_reattach(old, &revs);
        else
-               describe_detached_head(_("Previous HEAD position was"), commit);
+               describe_detached_head(_("Previous HEAD position was"), old);
  
        clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
        free(refs.objects);
@@@ -731,7 -731,7 +732,7 @@@ static int switch_branches(struct check
        }
  
        if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
-               orphaned_commit_warning(old.commit);
+               orphaned_commit_warning(old.commit, new->commit);
  
        update_refs_for_switch(opts, &old, new);
  
index b37ce25c42cd2fb0e2868c19f66902b319526de2,f63333b64e2984c609bde2a562bfcdbf05f8b88e..81005373d728f09e1a9289048cc721a0300f1cf9
@@@ -11,14 -11,13 +11,13 @@@ check_not_detached () 
        git symbolic-ref -q HEAD >/dev/null
  }
  
- ORPHAN_WARNING='you are leaving .* commit.*behind'
  PREV_HEAD_DESC='Previous HEAD position was'
  check_orphan_warning() {
-       test_i18ngrep "$ORPHAN_WARNING" "$1" &&
+       test_i18ngrep "you are leaving $2 behind" "$1" &&
        test_i18ngrep ! "$PREV_HEAD_DESC" "$1"
  }
  check_no_orphan_warning() {
-       test_i18ngrep ! "$ORPHAN_WARNING" "$1" &&
+       test_i18ngrep ! "you are leaving .* commit.*behind" "$1" &&
        test_i18ngrep "$PREV_HEAD_DESC" "$1"
  }
  
@@@ -110,12 -109,24 +109,24 @@@ test_expect_success 'checkout warns on 
        git checkout --detach two &&
        echo content >orphan &&
        git add orphan &&
-       git commit -a -m orphan &&
+       git commit -a -m orphan1 &&
+       echo new content >orphan &&
+       git commit -a -m orphan2 &&
+       orphan2=$(git rev-parse HEAD) &&
        git checkout master 2>stderr
  '
  
  test_expect_success 'checkout warns on orphan commits: output' '
-       check_orphan_warning stderr
+       check_orphan_warning stderr "2 commits"
+ '
+ test_expect_success 'checkout warns orphaning 1 of 2 commits' '
+       git checkout "$orphan2" &&
+       git checkout HEAD^ 2>stderr
+ '
+ test_expect_success 'checkout warns orphaning 1 of 2 commits: output' '
+       check_orphan_warning stderr "1 commit"
  '
  
  test_expect_success 'checkout does not warn leaving ref tip' '
@@@ -148,7 -159,7 +159,7 @@@ test_expect_success 'tracking count is 
        git config branch.child.merge refs/heads/master &&
        git checkout child^ &&
        git checkout child >stdout &&
 -      test_cmp expect stdout
 +      test_i18ncmp expect stdout
  '
  
  test_done