Merge branch 'mg/detached-head-report'
authorJunio C Hamano <gitster@pobox.com>
Fri, 20 Mar 2015 20:11:46 +0000 (13:11 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Mar 2015 20:11:46 +0000 (13:11 -0700)
"git branch" on a detached HEAD always said "(detached from xyz)",
even when "git status" would report "detached at xyz". The HEAD is
actually at xyz and haven't been moved since it was detached in
such a case, but the user cannot read what the current value of
HEAD is when "detached from" is used.

* mg/detached-head-report:
branch: name detached HEAD analogous to status
wt-status: refactor detached HEAD analysis

1  2 
builtin/branch.c
wt-status.c
diff --combined builtin/branch.c
index 6a25957e9f9daff6ab127f8feb91d9349075741e,46ad6f5b0d6f0cd3ef47024b522ba304057331a9..1d150378e91cd47c4cd555bfc858ec0b7ea9d604
  #include "wt-status.h"
  
  static const char * const builtin_branch_usage[] = {
 -      N_("git branch [options] [-r | -a] [--merged | --no-merged]"),
 -      N_("git branch [options] [-l] [-f] <branchname> [<start-point>]"),
 -      N_("git branch [options] [-r] (-d | -D) <branchname>..."),
 -      N_("git branch [options] (-m | -M) [<oldbranch>] <newbranch>"),
 +      N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
 +      N_("git branch [<options>] [-l] [-f] <branch-name> [<start-point>]"),
 +      N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
 +      N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
        NULL
  };
  
@@@ -589,9 -589,16 +589,16 @@@ static char *get_head_description(void
        else if (state.bisect_in_progress)
                strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
                            state.branch);
-       else if (state.detached_from)
-               strbuf_addf(&desc, _("(detached from %s)"),
-                           state.detached_from);
+       else if (state.detached_from) {
+               /* TRANSLATORS: make sure these match _("HEAD detached at ")
+                  and _("HEAD detached from ") in wt-status.c */
+               if (state.detached_at)
+                       strbuf_addf(&desc, _("(HEAD detached at %s)"),
+                               state.detached_from);
+               else
+                       strbuf_addf(&desc, _("(HEAD detached from %s)"),
+                               state.detached_from);
+       }
        else
                strbuf_addstr(&desc, _("(no branch)"));
        free(state.branch);
diff --combined wt-status.c
index 422dc0e70cf465c21e99375d32cb76c6f0acafae,8b7543b0be4bcabb78fa152a3f8e1083cc17d8ea..7036fa28dc39f72ed2064ca0ad15f2e381808020
@@@ -849,8 -849,6 +849,8 @@@ static void wt_status_print_verbose(str
  {
        struct rev_info rev;
        struct setup_revision_opt opt;
 +      int dirty_submodules;
 +      const char *c = color(WT_STATUS_HEADER, s);
  
        init_revisions(&rev, NULL);
        DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
                rev.diffopt.use_color = 0;
                wt_status_add_cut_line(s->fp);
        }
 +      if (s->verbose > 1 && s->commitable) {
 +              /* print_updated() printed a header, so do we */
 +              if (s->fp != stdout)
 +                      wt_status_print_trailer(s);
 +              status_printf_ln(s, c, _("Changes to be committed:"));
 +              rev.diffopt.a_prefix = "c/";
 +              rev.diffopt.b_prefix = "i/";
 +      } /* else use prefix as per user config */
        run_diff_index(&rev, 1);
 +      if (s->verbose > 1 &&
 +          wt_status_check_worktree_changes(s, &dirty_submodules)) {
 +              status_printf_ln(s, c,
 +                      "--------------------------------------------------");
 +              status_printf_ln(s, c, _("Changes not staged for commit:"));
 +              setup_work_tree();
 +              rev.diffopt.a_prefix = "i/";
 +              rev.diffopt.b_prefix = "w/";
 +              run_diff_files(&rev, 0);
 +      }
  }
  
  static void wt_status_print_tracking(struct wt_status *s)
@@@ -1160,7 -1140,7 +1160,7 @@@ static char *read_and_strip_branch(cons
        if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
                goto got_nothing;
  
 -      while (&sb.len && sb.buf[sb.len - 1] == '\n')
 +      while (sb.len && sb.buf[sb.len - 1] == '\n')
                strbuf_setlen(&sb, sb.len - 1);
        if (!sb.len)
                goto got_nothing;
@@@ -1242,6 -1222,8 +1242,8 @@@ static void wt_status_get_detached_from
                state->detached_from =
                        xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
        hashcpy(state->detached_sha1, cb.nsha1);
+       state->detached_at = !get_sha1("HEAD", sha1) &&
+                            !hashcmp(sha1, state->detached_sha1);
  
        free(ref);
        strbuf_release(&cb.buf);
@@@ -1330,10 -1312,8 +1332,8 @@@ void wt_status_print(struct wt_status *
                                on_what = _("rebase in progress; onto ");
                                branch_name = state.onto;
                        } else if (state.detached_from) {
-                               unsigned char sha1[20];
                                branch_name = state.detached_from;
-                               if (!get_sha1("HEAD", sha1) &&
-                                   !hashcmp(sha1, state.detached_sha1))
+                               if (state.detached_at)
                                        on_what = _("HEAD detached at ");
                                else
                                        on_what = _("HEAD detached from ");