send-email: recognize absolute path on Windows
[gitweb.git] / wt-status.c
index 706a6f77a2fd95893cd64c703b7bf407a874547e..9bf96c3af064bcc71ea62c97018312bd084e9273 100644 (file)
 #include "submodule.h"
 #include "column.h"
 #include "strbuf.h"
+#include "utf8.h"
+
+static char cut_line[] =
+"------------------------ >8 ------------------------\n";
 
 static char default_wt_status_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -165,7 +169,7 @@ static void wt_status_print_unmerged_header(struct wt_status *s)
                }
        }
 
-       if (!advice_status_hints)
+       if (!s->hints)
                return;
        if (s->whence != FROM_COMMIT)
                ;
@@ -192,7 +196,7 @@ static void wt_status_print_cached_header(struct wt_status *s)
        const char *c = color(WT_STATUS_HEADER, s);
 
        status_printf_ln(s, c, _("Changes to be committed:"));
-       if (!advice_status_hints)
+       if (!s->hints)
                return;
        if (s->whence != FROM_COMMIT)
                ; /* NEEDSWORK: use "git reset --unresolve"??? */
@@ -210,7 +214,7 @@ static void wt_status_print_dirty_header(struct wt_status *s,
        const char *c = color(WT_STATUS_HEADER, s);
 
        status_printf_ln(s, c, _("Changes not staged for commit:"));
-       if (!advice_status_hints)
+       if (!s->hints)
                return;
        if (!has_deleted)
                status_printf_ln(s, c, _("  (use \"git add <file>...\" to update what will be committed)"));
@@ -228,7 +232,7 @@ static void wt_status_print_other_header(struct wt_status *s,
 {
        const char *c = color(WT_STATUS_HEADER, s);
        status_printf_ln(s, c, "%s:", what);
-       if (!advice_status_hints)
+       if (!s->hints)
                return;
        status_printf_ln(s, c, _("  (use \"git %s <file>...\" to include in what will be committed)"), how);
        status_printf_ln(s, c, "");
@@ -241,26 +245,89 @@ static void wt_status_print_trailer(struct wt_status *s)
 
 #define quote_path quote_path_relative
 
+static const char *wt_status_unmerged_status_string(int stagemask)
+{
+       switch (stagemask) {
+       case 1:
+               return _("both deleted:");
+       case 2:
+               return _("added by us:");
+       case 3:
+               return _("deleted by them:");
+       case 4:
+               return _("added by them:");
+       case 5:
+               return _("deleted by us:");
+       case 6:
+               return _("both added:");
+       case 7:
+               return _("both modified:");
+       default:
+               die(_("bug: unhandled unmerged status %x"), stagemask);
+       }
+}
+
+static const char *wt_status_diff_status_string(int status)
+{
+       switch (status) {
+       case DIFF_STATUS_ADDED:
+               return _("new file:");
+       case DIFF_STATUS_COPIED:
+               return _("copied:");
+       case DIFF_STATUS_DELETED:
+               return _("deleted:");
+       case DIFF_STATUS_MODIFIED:
+               return _("modified:");
+       case DIFF_STATUS_RENAMED:
+               return _("renamed:");
+       case DIFF_STATUS_TYPE_CHANGED:
+               return _("typechange:");
+       case DIFF_STATUS_UNKNOWN:
+               return _("unknown:");
+       case DIFF_STATUS_UNMERGED:
+               return _("unmerged:");
+       default:
+               return NULL;
+       }
+}
+
+static int maxwidth(const char *(*label)(int), int minval, int maxval)
+{
+       int result = 0, i;
+
+       for (i = minval; i <= maxval; i++) {
+               const char *s = label(i);
+               int len = s ? utf8_strwidth(s) : 0;
+               if (len > result)
+                       result = len;
+       }
+       return result;
+}
+
 static void wt_status_print_unmerged_data(struct wt_status *s,
                                          struct string_list_item *it)
 {
        const char *c = color(WT_STATUS_UNMERGED, s);
        struct wt_status_change_data *d = it->util;
        struct strbuf onebuf = STRBUF_INIT;
-       const char *one, *how = _("bug");
+       static char *padding;
+       static int label_width;
+       const char *one, *how;
+       int len;
+
+       if (!padding) {
+               label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
+               label_width += strlen(" ");
+               padding = xmallocz(label_width);
+               memset(padding, ' ', label_width);
+       }
 
        one = quote_path(it->string, s->prefix, &onebuf);
        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
-       switch (d->stagemask) {
-       case 1: how = _("both deleted:"); break;
-       case 2: how = _("added by us:"); break;
-       case 3: how = _("deleted by them:"); break;
-       case 4: how = _("added by them:"); break;
-       case 5: how = _("deleted by us:"); break;
-       case 6: how = _("both added:"); break;
-       case 7: how = _("both modified:"); break;
-       }
-       status_printf_more(s, c, "%-20s%s\n", how, one);
+
+       how = wt_status_unmerged_status_string(d->stagemask);
+       len = label_width - utf8_strwidth(how);
+       status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
        strbuf_release(&onebuf);
 }
 
@@ -276,6 +343,18 @@ static void wt_status_print_change_data(struct wt_status *s,
        const char *one, *two;
        struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
        struct strbuf extra = STRBUF_INIT;
+       static char *padding;
+       static int label_width;
+       const char *what;
+       int len;
+
+       if (!padding) {
+               /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
+               label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
+               label_width += strlen(" ");
+               padding = xmallocz(label_width);
+               memset(padding, ' ', label_width);
+       }
 
        one_name = two_name = it->string;
        switch (change_type) {
@@ -307,34 +386,17 @@ static void wt_status_print_change_data(struct wt_status *s,
        two = quote_path(two_name, s->prefix, &twobuf);
 
        status_printf(s, color(WT_STATUS_HEADER, s), "\t");
-       switch (status) {
-       case DIFF_STATUS_ADDED:
-               status_printf_more(s, c, _("new file:   %s"), one);
-               break;
-       case DIFF_STATUS_COPIED:
-               status_printf_more(s, c, _("copied:     %s -> %s"), one, two);
-               break;
-       case DIFF_STATUS_DELETED:
-               status_printf_more(s, c, _("deleted:    %s"), one);
-               break;
-       case DIFF_STATUS_MODIFIED:
-               status_printf_more(s, c, _("modified:   %s"), one);
-               break;
-       case DIFF_STATUS_RENAMED:
-               status_printf_more(s, c, _("renamed:    %s -> %s"), one, two);
-               break;
-       case DIFF_STATUS_TYPE_CHANGED:
-               status_printf_more(s, c, _("typechange: %s"), one);
-               break;
-       case DIFF_STATUS_UNKNOWN:
-               status_printf_more(s, c, _("unknown:    %s"), one);
-               break;
-       case DIFF_STATUS_UNMERGED:
-               status_printf_more(s, c, _("unmerged:   %s"), one);
-               break;
-       default:
+       what = wt_status_diff_status_string(status);
+       if (!what)
                die(_("bug: unhandled diff status %c"), status);
-       }
+       len = label_width - utf8_strwidth(what);
+       assert(len >= 0);
+       if (status == DIFF_STATUS_COPIED || status == DIFF_STATUS_RENAMED)
+               status_printf_more(s, c, "%s%.*s%s -> %s",
+                                  what, len, padding, one, two);
+       else
+               status_printf_more(s, c, "%s%.*s%s",
+                                  what, len, padding, one);
        if (extra.len) {
                status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
                strbuf_release(&extra);
@@ -481,7 +543,7 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
                struct wt_status_change_data *d;
                const struct cache_entry *ce = active_cache[i];
 
-               if (!ce_path_match(ce, &s->pathspec))
+               if (!ce_path_match(ce, &s->pathspec, NULL))
                        continue;
                it = string_list_insert(&s->change, ce->name);
                d = it->util;
@@ -523,7 +585,7 @@ static void wt_status_collect_untracked(struct wt_status *s)
        for (i = 0; i < dir.nr; i++) {
                struct dir_entry *ent = dir.entries[i];
                if (cache_name_is_other(ent->name, ent->len) &&
-                   match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
+                   dir_path_match(ent, &s->pathspec, 0, NULL))
                        string_list_insert(&s->untracked, ent->name);
                free(ent);
        }
@@ -531,7 +593,7 @@ static void wt_status_collect_untracked(struct wt_status *s)
        for (i = 0; i < dir.ignored_nr; i++) {
                struct dir_entry *ent = dir.ignored[i];
                if (cache_name_is_other(ent->name, ent->len) &&
-                   match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
+                   dir_path_match(ent, &s->pathspec, 0, NULL))
                        string_list_insert(&s->ignored, ent->name);
                free(ent);
        }
@@ -767,6 +829,18 @@ static void wt_status_print_other(struct wt_status *s,
        status_printf_ln(s, GIT_COLOR_NORMAL, "");
 }
 
+void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
+{
+       const char *p;
+       struct strbuf pattern = STRBUF_INIT;
+
+       strbuf_addf(&pattern, "%c %s", comment_line_char, cut_line);
+       p = strstr(buf->buf, pattern.buf);
+       if (p && (p == buf->buf || p[-1] == '\n'))
+               strbuf_setlen(buf, p - buf->buf);
+       strbuf_release(&pattern);
+}
+
 static void wt_status_print_verbose(struct wt_status *s)
 {
        struct rev_info rev;
@@ -787,10 +861,20 @@ static void wt_status_print_verbose(struct wt_status *s)
         * If we're not going to stdout, then we definitely don't
         * want color, since we are going to the commit message
         * file (and even the "auto" setting won't work, since it
-        * will have checked isatty on stdout).
+        * will have checked isatty on stdout). But we then do want
+        * to insert the scissor line here to reliably remove the
+        * diff before committing.
         */
-       if (s->fp != stdout)
+       if (s->fp != stdout) {
+               const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
+               struct strbuf buf = STRBUF_INIT;
+
                rev.diffopt.use_color = 0;
+               fprintf(s->fp, "%c %s", comment_line_char, cut_line);
+               strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
+               fputs(buf.buf, s->fp);
+               strbuf_release(&buf);
+       }
        run_diff_index(&rev, 1);
 }
 
@@ -803,7 +887,7 @@ static void wt_status_print_tracking(struct wt_status *s)
        int i;
 
        assert(s->branch && !s->is_initial);
-       if (prefixcmp(s->branch, "refs/heads/"))
+       if (!starts_with(s->branch, "refs/heads/"))
                return;
        branch = branch_get(s->branch + 11);
        if (!format_tracking_info(branch, &sb))
@@ -846,13 +930,13 @@ static void show_merge_in_progress(struct wt_status *s,
 {
        if (has_unmerged(s)) {
                status_printf_ln(s, color, _("You have unmerged paths."));
-               if (advice_status_hints)
+               if (s->hints)
                        status_printf_ln(s, color,
                                _("  (fix conflicts and run \"git commit\")"));
        } else {
                status_printf_ln(s, color,
                        _("All conflicts fixed but you are still merging."));
-               if (advice_status_hints)
+               if (s->hints)
                        status_printf_ln(s, color,
                                _("  (use \"git commit\" to conclude merge)"));
        }
@@ -868,7 +952,7 @@ static void show_am_in_progress(struct wt_status *s,
        if (state->am_empty_patch)
                status_printf_ln(s, color,
                        _("The current patch is empty."));
-       if (advice_status_hints) {
+       if (s->hints) {
                if (!state->am_empty_patch)
                        status_printf_ln(s, color,
                                _("  (fix conflicts and then run \"git am --continue\")"));
@@ -941,7 +1025,7 @@ static void show_rebase_in_progress(struct wt_status *s,
                else
                        status_printf_ln(s, color,
                                         _("You are currently rebasing."));
-               if (advice_status_hints) {
+               if (s->hints) {
                        status_printf_ln(s, color,
                                _("  (fix conflicts and then run \"git rebase --continue\")"));
                        status_printf_ln(s, color,
@@ -958,7 +1042,7 @@ static void show_rebase_in_progress(struct wt_status *s,
                else
                        status_printf_ln(s, color,
                                         _("You are currently rebasing."));
-               if (advice_status_hints)
+               if (s->hints)
                        status_printf_ln(s, color,
                                _("  (all conflicts fixed: run \"git rebase --continue\")"));
        } else if (split_commit_in_progress(s)) {
@@ -970,7 +1054,7 @@ static void show_rebase_in_progress(struct wt_status *s,
                else
                        status_printf_ln(s, color,
                                         _("You are currently splitting a commit during a rebase."));
-               if (advice_status_hints)
+               if (s->hints)
                        status_printf_ln(s, color,
                                _("  (Once your working directory is clean, run \"git rebase --continue\")"));
        } else {
@@ -982,7 +1066,7 @@ static void show_rebase_in_progress(struct wt_status *s,
                else
                        status_printf_ln(s, color,
                                         _("You are currently editing a commit during a rebase."));
-               if (advice_status_hints && !s->amend) {
+               if (s->hints && !s->amend) {
                        status_printf_ln(s, color,
                                _("  (use \"git commit --amend\" to amend the current commit)"));
                        status_printf_ln(s, color,
@@ -996,8 +1080,9 @@ static void show_cherry_pick_in_progress(struct wt_status *s,
                                        struct wt_status_state *state,
                                        const char *color)
 {
-       status_printf_ln(s, color, _("You are currently cherry-picking."));
-       if (advice_status_hints) {
+       status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
+                       find_unique_abbrev(state->cherry_pick_head_sha1, DEFAULT_ABBREV));
+       if (s->hints) {
                if (has_unmerged(s))
                        status_printf_ln(s, color,
                                _("  (fix conflicts and run \"git cherry-pick --continue\")"));
@@ -1016,7 +1101,7 @@ static void show_revert_in_progress(struct wt_status *s,
 {
        status_printf_ln(s, color, _("You are currently reverting commit %s."),
                         find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
-       if (advice_status_hints) {
+       if (s->hints) {
                if (has_unmerged(s))
                        status_printf_ln(s, color,
                                _("  (fix conflicts and run \"git revert --continue\")"));
@@ -1040,7 +1125,7 @@ static void show_bisect_in_progress(struct wt_status *s,
        else
                status_printf_ln(s, color,
                                 _("You are currently bisecting."));
-       if (advice_status_hints)
+       if (s->hints)
                status_printf_ln(s, color,
                        _("  (use \"git bisect reset\" to get back to the original branch)"));
        wt_status_print_trailer(s);
@@ -1061,9 +1146,9 @@ static char *read_and_strip_branch(const char *path)
                strbuf_setlen(&sb, sb.len - 1);
        if (!sb.len)
                goto got_nothing;
-       if (!prefixcmp(sb.buf, "refs/heads/"))
+       if (starts_with(sb.buf, "refs/heads/"))
                strbuf_remove(&sb,0, strlen("refs/heads/"));
-       else if (!prefixcmp(sb.buf, "refs/"))
+       else if (starts_with(sb.buf, "refs/"))
                ;
        else if (!get_sha1_hex(sb.buf, sha1)) {
                const char *abbrev;
@@ -1093,7 +1178,7 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
        struct grab_1st_switch_cbdata *cb = cb_data;
        const char *target = NULL, *end;
 
-       if (prefixcmp(message, "checkout: moving from "))
+       if (!starts_with(message, "checkout: moving from "))
                return 0;
        message += strlen("checkout: moving from ");
        target = strstr(message, " to ");
@@ -1128,9 +1213,9 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
             ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
              !hashcmp(cb.nsha1, commit->object.sha1)))) {
                int ofs;
-               if (!prefixcmp(ref, "refs/tags/"))
+               if (starts_with(ref, "refs/tags/"))
                        ofs = strlen("refs/tags/");
-               else if (!prefixcmp(ref, "refs/remotes/"))
+               else if (starts_with(ref, "refs/remotes/"))
                        ofs = strlen("refs/remotes/");
                else
                        ofs = 0;
@@ -1169,8 +1254,10 @@ void wt_status_get_state(struct wt_status_state *state,
                        state->rebase_in_progress = 1;
                state->branch = read_and_strip_branch("rebase-merge/head-name");
                state->onto = read_and_strip_branch("rebase-merge/onto");
-       } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
+       } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st) &&
+                       !get_sha1("CHERRY_PICK_HEAD", sha1)) {
                state->cherry_pick_in_progress = 1;
+               hashcpy(state->cherry_pick_head_sha1, sha1);
        }
        if (!stat(git_path("BISECT_LOG"), &st)) {
                state->bisect_in_progress = 1;
@@ -1217,7 +1304,7 @@ void wt_status_print(struct wt_status *s)
        if (s->branch) {
                const char *on_what = _("On branch ");
                const char *branch_name = s->branch;
-               if (!prefixcmp(branch_name, "refs/heads/"))
+               if (starts_with(branch_name, "refs/heads/"))
                        branch_name += 11;
                else if (!strcmp(branch_name, "HEAD")) {
                        branch_status_color = color(WT_STATUS_NOBRANCH, s);
@@ -1278,7 +1365,7 @@ void wt_status_print(struct wt_status *s)
                }
        } else if (s->commitable)
                status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
-                       advice_status_hints
+                       s->hints
                        ? _(" (use -u option to show untracked files)") : "");
 
        if (s->verbose)
@@ -1289,25 +1376,25 @@ void wt_status_print(struct wt_status *s)
                else if (s->nowarn)
                        ; /* nothing */
                else if (s->workdir_dirty) {
-                       if (advice_status_hints)
+                       if (s->hints)
                                printf(_("no changes added to commit "
                                         "(use \"git add\" and/or \"git commit -a\")\n"));
                        else
                                printf(_("no changes added to commit\n"));
                } else if (s->untracked.nr) {
-                       if (advice_status_hints)
+                       if (s->hints)
                                printf(_("nothing added to commit but untracked files "
                                         "present (use \"git add\" to track)\n"));
                        else
                                printf(_("nothing added to commit but untracked files present\n"));
                } else if (s->is_initial) {
-                       if (advice_status_hints)
+                       if (s->hints)
                                printf(_("nothing to commit (create/copy files "
                                         "and use \"git add\" to track)\n"));
                        else
                                printf(_("nothing to commit\n"));
                } else if (!s->show_untracked_files) {
-                       if (advice_status_hints)
+                       if (s->hints)
                                printf(_("nothing to commit (use -u to show untracked files)\n"));
                        else
                                printf(_("nothing to commit\n"));
@@ -1418,7 +1505,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
                return;
        branch_name = s->branch;
 
-       if (!prefixcmp(branch_name, "refs/heads/"))
+       if (starts_with(branch_name, "refs/heads/"))
                branch_name += 11;
        else if (!strcmp(branch_name, "HEAD")) {
                branch_name = _("HEAD (no branch)");
@@ -1455,19 +1542,21 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
                return;
        }
 
+#define LABEL(string) (s->no_gettext ? (string) : _(string))
+
        color_fprintf(s->fp, header_color, " [");
        if (upstream_is_gone) {
-               color_fprintf(s->fp, header_color, _("gone"));
+               color_fprintf(s->fp, header_color, LABEL(N_("gone")));
        } else if (!num_ours) {
-               color_fprintf(s->fp, header_color, _("behind "));
+               color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
        } else if (!num_theirs) {
-               color_fprintf(s->fp, header_color, _("ahead "));
+               color_fprintf(s->fp, header_color, LABEL(N_(("ahead "))));
                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
        } else {
-               color_fprintf(s->fp, header_color, _("ahead "));
+               color_fprintf(s->fp, header_color, LABEL(N_(("ahead "))));
                color_fprintf(s->fp, branch_color_local, "%d", num_ours);
-               color_fprintf(s->fp, header_color, _(", behind "));
+               color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
        }
 
@@ -1512,5 +1601,6 @@ void wt_porcelain_print(struct wt_status *s)
        s->use_color = 0;
        s->relative_paths = 0;
        s->prefix = NULL;
+       s->no_gettext = 1;
        wt_shortstatus_print(s);
 }