builtin/merge.c: drop a parameter that is never used
[gitweb.git] / wt-status.c
index 885ee666eec900e40a7f72516362abbf839c1801..85390b813f12e2392b73dd6dffe8d1b41e838614 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "pathspec.h"
 #include "wt-status.h"
 #include "object.h"
 #include "dir.h"
@@ -8,12 +9,16 @@
 #include "diffcore.h"
 #include "quote.h"
 #include "run-command.h"
+#include "argv-array.h"
 #include "remote.h"
 #include "refs.h"
 #include "submodule.h"
 #include "column.h"
 #include "strbuf.h"
 
+static char cut_line[] =
+"------------------------ >8 ------------------------\n";
+
 static char default_wt_status_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
        GIT_COLOR_GREEN,  /* WT_STATUS_UPDATED */
@@ -45,9 +50,11 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
 
        strbuf_vaddf(&sb, fmt, ap);
        if (!sb.len) {
-               strbuf_addch(&sb, comment_line_char);
-               if (!trail)
-                       strbuf_addch(&sb, ' ');
+               if (s->display_comment_prefix) {
+                       strbuf_addch(&sb, comment_line_char);
+                       if (!trail)
+                               strbuf_addch(&sb, ' ');
+               }
                color_print_strbuf(s->fp, color, &sb);
                if (trail)
                        fprintf(s->fp, "%s", trail);
@@ -58,7 +65,7 @@ static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
                eol = strchr(line, '\n');
 
                strbuf_reset(&linebuf);
-               if (at_bol) {
+               if (at_bol && s->display_comment_prefix) {
                        strbuf_addch(&linebuf, comment_line_char);
                        if (*line != '\n' && *line != '\t')
                                strbuf_addch(&linebuf, ' ');
@@ -128,6 +135,7 @@ void wt_status_prepare(struct wt_status *s)
        s->untracked.strdup_strings = 1;
        s->ignored.strdup_strings = 1;
        s->show_branch = -1;  /* unspecified */
+       s->display_comment_prefix = 0;
 }
 
 static void wt_status_print_unmerged_header(struct wt_status *s)
@@ -438,7 +446,7 @@ static void wt_status_collect_changes_worktree(struct wt_status *s)
        }
        rev.diffopt.format_callback = wt_status_collect_changed_cb;
        rev.diffopt.format_callback_data = s;
-       init_pathspec(&rev.prune_data, s->pathspec);
+       copy_pathspec(&rev.prune_data, &s->pathspec);
        run_diff_files(&rev, 0);
 }
 
@@ -463,22 +471,20 @@ static void wt_status_collect_changes_index(struct wt_status *s)
        rev.diffopt.detect_rename = 1;
        rev.diffopt.rename_limit = 200;
        rev.diffopt.break_opt = 0;
-       init_pathspec(&rev.prune_data, s->pathspec);
+       copy_pathspec(&rev.prune_data, &s->pathspec);
        run_diff_index(&rev, 1);
 }
 
 static void wt_status_collect_changes_initial(struct wt_status *s)
 {
-       struct pathspec pathspec;
        int i;
 
-       init_pathspec(&pathspec, s->pathspec);
        for (i = 0; i < active_nr; i++) {
                struct string_list_item *it;
                struct wt_status_change_data *d;
                const struct cache_entry *ce = active_cache[i];
 
-               if (!ce_path_match(ce, &pathspec))
+               if (!ce_path_match(ce, &s->pathspec))
                        continue;
                it = string_list_insert(&s->change, ce->name);
                d = it->util;
@@ -493,7 +499,6 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
                else
                        d->index_status = DIFF_STATUS_ADDED;
        }
-       free_pathspec(&pathspec);
 }
 
 static void wt_status_collect_untracked(struct wt_status *s)
@@ -516,12 +521,12 @@ static void wt_status_collect_untracked(struct wt_status *s)
                dir.flags |= DIR_SHOW_IGNORED_TOO;
        setup_standard_excludes(&dir);
 
-       fill_directory(&dir, s->pathspec);
+       fill_directory(&dir, &s->pathspec);
 
        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(s->pathspec, ent->name, ent->len, 0, NULL))
+                   match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
                        string_list_insert(&s->untracked, ent->name);
                free(ent);
        }
@@ -529,7 +534,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(s->pathspec, ent->name, ent->len, 0, NULL))
+                   match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
                        string_list_insert(&s->ignored, ent->name);
                free(ent);
        }
@@ -663,29 +668,57 @@ static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitt
        char summary_limit[64];
        char index[PATH_MAX];
        const char *env[] = { NULL, NULL };
-       const char *argv[8];
-
-       env[0] =        index;
-       argv[0] =       "submodule";
-       argv[1] =       "summary";
-       argv[2] =       uncommitted ? "--files" : "--cached";
-       argv[3] =       "--for-status";
-       argv[4] =       "--summary-limit";
-       argv[5] =       summary_limit;
-       argv[6] =       uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
-       argv[7] =       NULL;
+       struct argv_array argv = ARGV_ARRAY_INIT;
+       struct strbuf cmd_stdout = STRBUF_INIT;
+       struct strbuf summary = STRBUF_INIT;
+       char *summary_content;
+       size_t len;
 
        sprintf(summary_limit, "%d", s->submodule_summary);
        snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
 
+       env[0] = index;
+       argv_array_push(&argv, "submodule");
+       argv_array_push(&argv, "summary");
+       argv_array_push(&argv, uncommitted ? "--files" : "--cached");
+       argv_array_push(&argv, "--for-status");
+       argv_array_push(&argv, "--summary-limit");
+       argv_array_push(&argv, summary_limit);
+       if (!uncommitted)
+               argv_array_push(&argv, s->amend ? "HEAD^" : "HEAD");
+
        memset(&sm_summary, 0, sizeof(sm_summary));
-       sm_summary.argv = argv;
+       sm_summary.argv = argv.argv;
        sm_summary.env = env;
        sm_summary.git_cmd = 1;
        sm_summary.no_stdin = 1;
        fflush(s->fp);
-       sm_summary.out = dup(fileno(s->fp));    /* run_command closes it */
+       sm_summary.out = -1;
+
        run_command(&sm_summary);
+       argv_array_clear(&argv);
+
+       len = strbuf_read(&cmd_stdout, sm_summary.out, 1024);
+
+       /* prepend header, only if there's an actual output */
+       if (len) {
+               if (uncommitted)
+                       strbuf_addstr(&summary, _("Submodules changed but not updated:"));
+               else
+                       strbuf_addstr(&summary, _("Submodule changes to be committed:"));
+               strbuf_addstr(&summary, "\n\n");
+       }
+       strbuf_addbuf(&summary, &cmd_stdout);
+       strbuf_release(&cmd_stdout);
+
+       if (s->display_comment_prefix) {
+               summary_content = strbuf_detach(&summary, &len);
+               strbuf_add_commented_lines(&summary, summary_content, len);
+               free(summary_content);
+       }
+
+       fputs(summary.buf, s->fp);
+       strbuf_release(&summary);
 }
 
 static void wt_status_print_other(struct wt_status *s,
@@ -719,10 +752,11 @@ static void wt_status_print_other(struct wt_status *s,
 
        strbuf_release(&buf);
        if (!column_active(s->colopts))
-               return;
+               goto conclude;
 
-       strbuf_addf(&buf, "%s#\t%s",
+       strbuf_addf(&buf, "%s%s\t%s",
                    color(WT_STATUS_HEADER, s),
+                   s->display_comment_prefix ? "#" : "",
                    color(WT_STATUS_UNTRACKED, s));
        memset(&copts, 0, sizeof(copts));
        copts.padding = 1;
@@ -732,6 +766,20 @@ static void wt_status_print_other(struct wt_status *s,
        print_columns(&output, s->colopts, &copts);
        string_list_clear(&output, 0);
        strbuf_release(&buf);
+conclude:
+       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)
@@ -754,10 +802,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);
 }
 
@@ -766,6 +824,8 @@ static void wt_status_print_tracking(struct wt_status *s)
        struct strbuf sb = STRBUF_INIT;
        const char *cp, *ep;
        struct branch *branch;
+       char comment_line_string[3];
+       int i;
 
        assert(s->branch && !s->is_initial);
        if (prefixcmp(s->branch, "refs/heads/"))
@@ -774,12 +834,22 @@ static void wt_status_print_tracking(struct wt_status *s)
        if (!format_tracking_info(branch, &sb))
                return;
 
+       i = 0;
+       if (s->display_comment_prefix) {
+               comment_line_string[i++] = comment_line_char;
+               comment_line_string[i++] = ' ';
+       }
+       comment_line_string[i] = '\0';
+
        for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
                color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
-                                "%c %.*s", comment_line_char,
+                                "%s%.*s", comment_line_string,
                                 (int)(ep - cp), cp);
-       color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
-                        comment_line_char);
+       if (s->display_comment_prefix)
+               color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
+                                comment_line_char);
+       else
+               fprintf_ln(s->fp, "");
 }
 
 static int has_unmerged(struct wt_status *s)
@@ -951,7 +1021,8 @@ 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."));
+       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,
@@ -1124,8 +1195,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;
@@ -1365,6 +1438,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
        const char *base;
        const char *branch_name;
        int num_ours, num_theirs;
+       int upstream_is_gone = 0;
 
        color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
 
@@ -1382,20 +1456,37 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
        branch = branch_get(s->branch + 11);
        if (s->is_initial)
                color_fprintf(s->fp, header_color, _("Initial commit on "));
-       if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
-               color_fprintf(s->fp, branch_color_local, "%s", branch_name);
+
+       color_fprintf(s->fp, branch_color_local, "%s", branch_name);
+
+       switch (stat_tracking_info(branch, &num_ours, &num_theirs)) {
+       case 0:
+               /* no base */
                fputc(s->null_termination ? '\0' : '\n', s->fp);
                return;
+       case -1:
+               /* with "gone" base */
+               upstream_is_gone = 1;
+               break;
+       default:
+               /* with base */
+               break;
        }
 
        base = branch->merge[0]->dst;
        base = shorten_unambiguous_ref(base, 0);
-       color_fprintf(s->fp, branch_color_local, "%s", branch_name);
        color_fprintf(s->fp, header_color, "...");
        color_fprintf(s->fp, branch_color_remote, "%s", base);
 
+       if (!upstream_is_gone && !num_ours && !num_theirs) {
+               fputc(s->null_termination ? '\0' : '\n', s->fp);
+               return;
+       }
+
        color_fprintf(s->fp, header_color, " [");
-       if (!num_ours) {
+       if (upstream_is_gone) {
+               color_fprintf(s->fp, header_color, _("gone"));
+       } else if (!num_ours) {
                color_fprintf(s->fp, header_color, _("behind "));
                color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
        } else if (!num_theirs) {