blob: 9f4e0ba9c17120ca2903b30b95b6d8fbcc62cd9c [file] [log] [blame]
Junio C Hamano85023572006-12-19 14:34:12 -08001#include "cache.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04002#include "wt-status.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04003#include "object.h"
4#include "dir.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
8#include "diffcore.h"
Dmitry Potapova734d0b2008-03-07 05:30:58 +03009#include "quote.h"
Ping Yinac8d5af2008-04-12 23:05:32 +080010#include "run-command.h"
Junio C Hamanob6975ab2008-07-02 00:52:16 -070011#include "remote.h"
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020012#include "refs.h"
Jens Lehmann46a958b2010-06-25 16:56:47 +020013#include "submodule.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -040014
Junio C Hamano23900a92009-08-09 23:08:40 -070015static char default_wt_status_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010016 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
17 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
18 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
19 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
20 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
Junio C Hamano4d4d5722009-08-05 00:04:51 -070021 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020022 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
23 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
Jeff King148135f2010-12-09 12:27:08 -050024 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
Jeff Kingc91f0d92006-09-08 04:05:34 -040025};
Junio C Hamano4d229652007-01-11 15:34:41 -080026
Junio C Hamanod249b092009-08-09 21:59:30 -070027static const char *color(int slot, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -040028{
Jeff King148135f2010-12-09 12:27:08 -050029 const char *c = s->use_color > 0 ? s->color_palette[slot] : "";
30 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
31 c = s->color_palette[WT_STATUS_HEADER];
32 return c;
Jeff Kingc91f0d92006-09-08 04:05:34 -040033}
34
Jonathan Niederbecbdae2011-02-25 23:09:41 -060035static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
36 const char *fmt, va_list ap, const char *trail)
37{
38 struct strbuf sb = STRBUF_INIT;
39 struct strbuf linebuf = STRBUF_INIT;
40 const char *line, *eol;
41
42 strbuf_vaddf(&sb, fmt, ap);
43 if (!sb.len) {
44 strbuf_addch(&sb, '#');
45 if (!trail)
46 strbuf_addch(&sb, ' ');
47 color_print_strbuf(s->fp, color, &sb);
48 if (trail)
49 fprintf(s->fp, "%s", trail);
50 strbuf_release(&sb);
51 return;
52 }
53 for (line = sb.buf; *line; line = eol + 1) {
54 eol = strchr(line, '\n');
55
56 strbuf_reset(&linebuf);
57 if (at_bol) {
58 strbuf_addch(&linebuf, '#');
59 if (*line != '\n' && *line != '\t')
60 strbuf_addch(&linebuf, ' ');
61 }
62 if (eol)
63 strbuf_add(&linebuf, line, eol - line);
64 else
65 strbuf_addstr(&linebuf, line);
66 color_print_strbuf(s->fp, color, &linebuf);
67 if (eol)
68 fprintf(s->fp, "\n");
69 else
70 break;
71 at_bol = 1;
72 }
73 if (trail)
74 fprintf(s->fp, "%s", trail);
75 strbuf_release(&linebuf);
76 strbuf_release(&sb);
77}
78
79void status_printf_ln(struct wt_status *s, const char *color,
80 const char *fmt, ...)
81{
82 va_list ap;
83
84 va_start(ap, fmt);
85 status_vprintf(s, 1, color, fmt, ap, "\n");
86 va_end(ap);
87}
88
89void status_printf(struct wt_status *s, const char *color,
90 const char *fmt, ...)
91{
92 va_list ap;
93
94 va_start(ap, fmt);
95 status_vprintf(s, 1, color, fmt, ap, NULL);
96 va_end(ap);
97}
98
99void status_printf_more(struct wt_status *s, const char *color,
100 const char *fmt, ...)
101{
102 va_list ap;
103
104 va_start(ap, fmt);
105 status_vprintf(s, 0, color, fmt, ap, NULL);
106 va_end(ap);
107}
108
Jeff Kingc91f0d92006-09-08 04:05:34 -0400109void wt_status_prepare(struct wt_status *s)
110{
111 unsigned char sha1[20];
112 const char *head;
113
Junio C Hamanocc46a742007-02-09 16:22:42 -0800114 memset(s, 0, sizeof(*s));
Junio C Hamano23900a92009-08-09 23:08:40 -0700115 memcpy(s->color_palette, default_wt_status_colors,
116 sizeof(default_wt_status_colors));
Junio C Hamanod249b092009-08-09 21:59:30 -0700117 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
118 s->use_color = -1;
119 s->relative_paths = 1;
Junio C Hamano8da19772006-09-20 22:02:01 -0700120 head = resolve_ref("HEAD", sha1, 0, NULL);
Jeff Kingf62363f2006-09-17 19:13:56 -0700121 s->branch = head ? xstrdup(head) : NULL;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400122 s->reference = "HEAD";
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400123 s->fp = stdout;
Kristian Høgsberg0f729f22007-09-17 20:06:43 -0400124 s->index_file = get_index_file();
Junio C Hamano50b7e702009-08-04 23:49:33 -0700125 s->change.strdup_strings = 1;
Junio C Hamano76378682009-08-10 00:36:33 -0700126 s->untracked.strdup_strings = 1;
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700127 s->ignored.strdup_strings = 1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400128}
129
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700130static void wt_status_print_unmerged_header(struct wt_status *s)
131{
Junio C Hamanod249b092009-08-09 21:59:30 -0700132 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800133
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000134 status_printf_ln(s, c, _("Unmerged paths:"));
Jeff Kingedf563f2009-09-09 07:43:03 -0400135 if (!advice_status_hints)
136 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500137 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800138 ;
139 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000140 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700141 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000142 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
143 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600144 status_printf_ln(s, c, "");
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700145}
146
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400147static void wt_status_print_cached_header(struct wt_status *s)
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100148{
Junio C Hamanod249b092009-08-09 21:59:30 -0700149 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800150
Ævar Arnfjörð Bjarmason919a4ce2011-02-22 23:42:16 +0000151 status_printf_ln(s, c, _("Changes to be committed:"));
Jeff Kingedf563f2009-09-09 07:43:03 -0400152 if (!advice_status_hints)
153 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500154 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800155 ; /* NEEDSWORK: use "git reset --unresolve"??? */
156 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000157 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano3c588452009-12-11 23:53:41 -0800158 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000159 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600160 status_printf_ln(s, c, "");
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100161}
162
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200163static void wt_status_print_dirty_header(struct wt_status *s,
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100164 int has_deleted,
165 int has_dirty_submodules)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400166{
Junio C Hamanod249b092009-08-09 21:59:30 -0700167 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800168
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000169 status_printf_ln(s, c, _("Changes not staged for commit:"));
Jeff Kingedf563f2009-09-09 07:43:03 -0400170 if (!advice_status_hints)
171 return;
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200172 if (!has_deleted)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000173 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200174 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000175 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
176 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100177 if (has_dirty_submodules)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000178 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600179 status_printf_ln(s, c, "");
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200180}
181
Junio C Hamano1b908b62010-04-10 00:19:46 -0700182static void wt_status_print_other_header(struct wt_status *s,
183 const char *what,
184 const char *how)
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200185{
Junio C Hamanod249b092009-08-09 21:59:30 -0700186 const char *c = color(WT_STATUS_HEADER, s);
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000187 status_printf_ln(s, c, _("%s files:"), what);
Jeff Kingedf563f2009-09-09 07:43:03 -0400188 if (!advice_status_hints)
189 return;
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000190 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600191 status_printf_ln(s, c, "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400192}
193
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400194static void wt_status_print_trailer(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400195{
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600196 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400197}
198
Dmitry Potapova734d0b2008-03-07 05:30:58 +0300199#define quote_path quote_path_relative
Junio C Hamano3a946802006-11-08 13:20:46 -0800200
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700201static void wt_status_print_unmerged_data(struct wt_status *s,
202 struct string_list_item *it)
203{
Junio C Hamanod249b092009-08-09 21:59:30 -0700204 const char *c = color(WT_STATUS_UNMERGED, s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700205 struct wt_status_change_data *d = it->util;
206 struct strbuf onebuf = STRBUF_INIT;
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000207 const char *one, *how = _("bug");
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700208
209 one = quote_path(it->string, -1, &onebuf, s->prefix);
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600210 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700211 switch (d->stagemask) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000212 case 1: how = _("both deleted:"); break;
213 case 2: how = _("added by us:"); break;
214 case 3: how = _("deleted by them:"); break;
215 case 4: how = _("added by them:"); break;
216 case 5: how = _("deleted by us:"); break;
217 case 6: how = _("both added:"); break;
218 case 7: how = _("both modified:"); break;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700219 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600220 status_printf_more(s, c, "%-20s%s\n", how, one);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700221 strbuf_release(&onebuf);
222}
223
Junio C Hamano50b7e702009-08-04 23:49:33 -0700224static void wt_status_print_change_data(struct wt_status *s,
225 int change_type,
226 struct string_list_item *it)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400227{
Junio C Hamano50b7e702009-08-04 23:49:33 -0700228 struct wt_status_change_data *d = it->util;
Junio C Hamanod249b092009-08-09 21:59:30 -0700229 const char *c = color(change_type, s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700230 int status = status;
231 char *one_name;
232 char *two_name;
Junio C Hamano3a946802006-11-08 13:20:46 -0800233 const char *one, *two;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500234 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100235 struct strbuf extra = STRBUF_INIT;
Junio C Hamano3a946802006-11-08 13:20:46 -0800236
Junio C Hamano50b7e702009-08-04 23:49:33 -0700237 one_name = two_name = it->string;
238 switch (change_type) {
239 case WT_STATUS_UPDATED:
240 status = d->index_status;
241 if (d->head_path)
242 one_name = d->head_path;
243 break;
244 case WT_STATUS_CHANGED:
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100245 if (d->new_submodule_commits || d->dirty_submodule) {
246 strbuf_addstr(&extra, " (");
247 if (d->new_submodule_commits)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000248 strbuf_addf(&extra, _("new commits, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100249 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000250 strbuf_addf(&extra, _("modified content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100251 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000252 strbuf_addf(&extra, _("untracked content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100253 strbuf_setlen(&extra, extra.len - 2);
254 strbuf_addch(&extra, ')');
255 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700256 status = d->worktree_status;
257 break;
258 }
259
260 one = quote_path(one_name, -1, &onebuf, s->prefix);
261 two = quote_path(two_name, -1, &twobuf, s->prefix);
Junio C Hamano3a946802006-11-08 13:20:46 -0800262
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600263 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
Junio C Hamano50b7e702009-08-04 23:49:33 -0700264 switch (status) {
Jeff Kingc91f0d92006-09-08 04:05:34 -0400265 case DIFF_STATUS_ADDED:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000266 status_printf_more(s, c, _("new file: %s"), one);
Junio C Hamano3a946802006-11-08 13:20:46 -0800267 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400268 case DIFF_STATUS_COPIED:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000269 status_printf_more(s, c, _("copied: %s -> %s"), one, two);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400270 break;
271 case DIFF_STATUS_DELETED:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000272 status_printf_more(s, c, _("deleted: %s"), one);
Junio C Hamano3a946802006-11-08 13:20:46 -0800273 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400274 case DIFF_STATUS_MODIFIED:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000275 status_printf_more(s, c, _("modified: %s"), one);
Junio C Hamano3a946802006-11-08 13:20:46 -0800276 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400277 case DIFF_STATUS_RENAMED:
Ævar Arnfjörð Bjarmasond2b044b2011-02-22 23:42:18 +0000278 status_printf_more(s, c, _("renamed: %s -> %s"), one, two);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400279 break;
280 case DIFF_STATUS_TYPE_CHANGED:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000281 status_printf_more(s, c, _("typechange: %s"), one);
Junio C Hamano3a946802006-11-08 13:20:46 -0800282 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400283 case DIFF_STATUS_UNKNOWN:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000284 status_printf_more(s, c, _("unknown: %s"), one);
Junio C Hamano3a946802006-11-08 13:20:46 -0800285 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400286 case DIFF_STATUS_UNMERGED:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000287 status_printf_more(s, c, _("unmerged: %s"), one);
Junio C Hamano3a946802006-11-08 13:20:46 -0800288 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400289 default:
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000290 die(_("bug: unhandled diff status %c"), status);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400291 }
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100292 if (extra.len) {
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600293 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100294 strbuf_release(&extra);
295 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600296 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
Johannes Schindelin367c9882007-11-11 17:35:41 +0000297 strbuf_release(&onebuf);
298 strbuf_release(&twobuf);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400299}
300
Junio C Hamano50b7e702009-08-04 23:49:33 -0700301static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
302 struct diff_options *options,
303 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400304{
305 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400306 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700307
308 if (!q->nr)
309 return;
310 s->workdir_dirty = 1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400311 for (i = 0; i < q->nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700312 struct diff_filepair *p;
313 struct string_list_item *it;
314 struct wt_status_change_data *d;
315
316 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100317 it = string_list_insert(&s->change, p->one->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700318 d = it->util;
319 if (!d) {
320 d = xcalloc(1, sizeof(*d));
321 it->util = d;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400322 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700323 if (!d->worktree_status)
324 d->worktree_status = p->status;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100325 d->dirty_submodule = p->two->dirty_submodule;
326 if (S_ISGITLINK(p->two->mode))
327 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400328 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400329}
330
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700331static int unmerged_mask(const char *path)
332{
333 int pos, mask;
334 struct cache_entry *ce;
335
336 pos = cache_name_pos(path, strlen(path));
337 if (0 <= pos)
338 return 0;
339
340 mask = 0;
341 pos = -pos-1;
342 while (pos < active_nr) {
343 ce = active_cache[pos++];
344 if (strcmp(ce->name, path) || !ce_stage(ce))
345 break;
346 mask |= (1 << (ce_stage(ce) - 1));
347 }
348 return mask;
349}
350
Junio C Hamano50b7e702009-08-04 23:49:33 -0700351static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
352 struct diff_options *options,
353 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400354{
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100355 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400356 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700357
358 for (i = 0; i < q->nr; i++) {
359 struct diff_filepair *p;
360 struct string_list_item *it;
361 struct wt_status_change_data *d;
362
363 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100364 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700365 d = it->util;
366 if (!d) {
367 d = xcalloc(1, sizeof(*d));
368 it->util = d;
369 }
370 if (!d->index_status)
371 d->index_status = p->status;
372 switch (p->status) {
373 case DIFF_STATUS_COPIED:
374 case DIFF_STATUS_RENAMED:
375 d->head_path = xstrdup(p->one->path);
376 break;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700377 case DIFF_STATUS_UNMERGED:
378 d->stagemask = unmerged_mask(p->two->path);
379 break;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700380 }
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100381 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400382}
383
Junio C Hamano50b7e702009-08-04 23:49:33 -0700384static void wt_status_collect_changes_worktree(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400385{
386 struct rev_info rev;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700387
388 init_revisions(&rev, NULL);
389 setup_revisions(0, NULL, &rev, NULL);
390 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100391 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100392 if (!s->show_untracked_files)
393 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200394 if (s->ignore_submodule_arg) {
395 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
Jens Lehmann46a958b2010-06-25 16:56:47 +0200396 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200397 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700398 rev.diffopt.format_callback = wt_status_collect_changed_cb;
399 rev.diffopt.format_callback_data = s;
Nguyễn Thái Ngọc Duyafe069d2010-12-17 19:43:06 +0700400 init_pathspec(&rev.prune_data, s->pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700401 run_diff_files(&rev, 0);
402}
403
404static void wt_status_collect_changes_index(struct wt_status *s)
405{
406 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800407 struct setup_revision_opt opt;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700408
Jeff Kingc91f0d92006-09-08 04:05:34 -0400409 init_revisions(&rev, NULL);
Junio C Hamano32962c92010-03-08 22:58:09 -0800410 memset(&opt, 0, sizeof(opt));
411 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
412 setup_revisions(0, NULL, &rev, &opt);
413
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200414 if (s->ignore_submodule_arg) {
415 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
Jens Lehmann46a958b2010-06-25 16:56:47 +0200416 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200417 }
Jens Lehmann46a958b2010-06-25 16:56:47 +0200418
Jeff Kingc91f0d92006-09-08 04:05:34 -0400419 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700420 rev.diffopt.format_callback = wt_status_collect_updated_cb;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400421 rev.diffopt.format_callback_data = s;
422 rev.diffopt.detect_rename = 1;
Jeff King50705912008-04-30 13:24:43 -0400423 rev.diffopt.rename_limit = 200;
Jeff Kingf714fb82007-12-02 22:58:37 -0800424 rev.diffopt.break_opt = 0;
Nguyễn Thái Ngọc Duyafe069d2010-12-17 19:43:06 +0700425 init_pathspec(&rev.prune_data, s->pathspec);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400426 run_diff_index(&rev, 1);
427}
428
Junio C Hamano50b7e702009-08-04 23:49:33 -0700429static void wt_status_collect_changes_initial(struct wt_status *s)
430{
Nguyễn Thái Ngọc Duyeb9cb552010-12-17 19:43:07 +0700431 struct pathspec pathspec;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700432 int i;
433
Nguyễn Thái Ngọc Duyeb9cb552010-12-17 19:43:07 +0700434 init_pathspec(&pathspec, s->pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700435 for (i = 0; i < active_nr; i++) {
436 struct string_list_item *it;
437 struct wt_status_change_data *d;
438 struct cache_entry *ce = active_cache[i];
439
Nguyễn Thái Ngọc Duyeb9cb552010-12-17 19:43:07 +0700440 if (!ce_path_match(ce, &pathspec))
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700441 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100442 it = string_list_insert(&s->change, ce->name);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700443 d = it->util;
444 if (!d) {
445 d = xcalloc(1, sizeof(*d));
446 it->util = d;
447 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700448 if (ce_stage(ce)) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700449 d->index_status = DIFF_STATUS_UNMERGED;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700450 d->stagemask |= (1 << (ce_stage(ce) - 1));
451 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700452 else
453 d->index_status = DIFF_STATUS_ADDED;
454 }
Nguyễn Thái Ngọc Duyeb9cb552010-12-17 19:43:07 +0700455 free_pathspec(&pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700456}
457
Junio C Hamano76378682009-08-10 00:36:33 -0700458static void wt_status_collect_untracked(struct wt_status *s)
459{
460 int i;
461 struct dir_struct dir;
462
463 if (!s->show_untracked_files)
464 return;
465 memset(&dir, 0, sizeof(dir));
466 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
467 dir.flags |=
468 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
469 setup_standard_excludes(&dir);
470
Nguyễn Thái Ngọc Duy688cd6d2010-01-14 22:02:21 +0700471 fill_directory(&dir, s->pathspec);
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400472 for (i = 0; i < dir.nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700473 struct dir_entry *ent = dir.entries[i];
Brandon Caseyb8224232010-09-26 21:49:13 -0500474 if (cache_name_is_other(ent->name, ent->len) &&
475 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
476 string_list_insert(&s->untracked, ent->name);
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700477 free(ent);
Junio C Hamano76378682009-08-10 00:36:33 -0700478 }
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700479
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700480 if (s->show_ignored_files) {
481 dir.nr = 0;
482 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
483 fill_directory(&dir, s->pathspec);
484 for (i = 0; i < dir.nr; i++) {
485 struct dir_entry *ent = dir.entries[i];
Brandon Caseyb8224232010-09-26 21:49:13 -0500486 if (cache_name_is_other(ent->name, ent->len) &&
487 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
488 string_list_insert(&s->ignored, ent->name);
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700489 free(ent);
490 }
491 }
492
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700493 free(dir.entries);
Junio C Hamano76378682009-08-10 00:36:33 -0700494}
495
496void wt_status_collect(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700497{
498 wt_status_collect_changes_worktree(s);
499
500 if (s->is_initial)
501 wt_status_collect_changes_initial(s);
502 else
503 wt_status_collect_changes_index(s);
Junio C Hamano76378682009-08-10 00:36:33 -0700504 wt_status_collect_untracked(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700505}
506
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700507static void wt_status_print_unmerged(struct wt_status *s)
508{
509 int shown_header = 0;
510 int i;
511
512 for (i = 0; i < s->change.nr; i++) {
513 struct wt_status_change_data *d;
514 struct string_list_item *it;
515 it = &(s->change.items[i]);
516 d = it->util;
517 if (!d->stagemask)
518 continue;
519 if (!shown_header) {
520 wt_status_print_unmerged_header(s);
521 shown_header = 1;
522 }
523 wt_status_print_unmerged_data(s, it);
524 }
525 if (shown_header)
526 wt_status_print_trailer(s);
527
528}
529
Junio C Hamano50b7e702009-08-04 23:49:33 -0700530static void wt_status_print_updated(struct wt_status *s)
531{
532 int shown_header = 0;
533 int i;
534
535 for (i = 0; i < s->change.nr; i++) {
536 struct wt_status_change_data *d;
537 struct string_list_item *it;
538 it = &(s->change.items[i]);
539 d = it->util;
540 if (!d->index_status ||
541 d->index_status == DIFF_STATUS_UNMERGED)
542 continue;
543 if (!shown_header) {
544 wt_status_print_cached_header(s);
545 s->commitable = 1;
546 shown_header = 1;
547 }
548 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
549 }
550 if (shown_header)
551 wt_status_print_trailer(s);
552}
553
554/*
555 * -1 : has delete
556 * 0 : no change
557 * 1 : some change but no delete
558 */
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100559static int wt_status_check_worktree_changes(struct wt_status *s,
560 int *dirty_submodules)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700561{
562 int i;
563 int changes = 0;
564
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100565 *dirty_submodules = 0;
566
Junio C Hamano50b7e702009-08-04 23:49:33 -0700567 for (i = 0; i < s->change.nr; i++) {
568 struct wt_status_change_data *d;
569 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700570 if (!d->worktree_status ||
571 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700572 continue;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100573 if (!changes)
574 changes = 1;
575 if (d->dirty_submodule)
576 *dirty_submodules = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700577 if (d->worktree_status == DIFF_STATUS_DELETED)
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100578 changes = -1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700579 }
580 return changes;
581}
582
Jeff Kingc91f0d92006-09-08 04:05:34 -0400583static void wt_status_print_changed(struct wt_status *s)
584{
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100585 int i, dirty_submodules;
586 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700587
588 if (!worktree_changes)
589 return;
590
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100591 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700592
593 for (i = 0; i < s->change.nr; i++) {
594 struct wt_status_change_data *d;
595 struct string_list_item *it;
596 it = &(s->change.items[i]);
597 d = it->util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700598 if (!d->worktree_status ||
599 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700600 continue;
601 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
602 }
603 wt_status_print_trailer(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400604}
605
Jens Lehmannf17a5d32010-01-17 20:42:31 +0100606static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
Ping Yinac8d5af2008-04-12 23:05:32 +0800607{
608 struct child_process sm_summary;
609 char summary_limit[64];
610 char index[PATH_MAX];
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000611 const char *env[] = { NULL, NULL };
612 const char *argv[8];
613
614 env[0] = index;
615 argv[0] = "submodule";
616 argv[1] = "summary";
617 argv[2] = uncommitted ? "--files" : "--cached";
618 argv[3] = "--for-status";
619 argv[4] = "--summary-limit";
620 argv[5] = summary_limit;
621 argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
622 argv[7] = NULL;
Ping Yinac8d5af2008-04-12 23:05:32 +0800623
Junio C Hamanod249b092009-08-09 21:59:30 -0700624 sprintf(summary_limit, "%d", s->submodule_summary);
Ping Yinac8d5af2008-04-12 23:05:32 +0800625 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
626
627 memset(&sm_summary, 0, sizeof(sm_summary));
628 sm_summary.argv = argv;
629 sm_summary.env = env;
630 sm_summary.git_cmd = 1;
631 sm_summary.no_stdin = 1;
632 fflush(s->fp);
633 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
634 run_command(&sm_summary);
635}
636
Junio C Hamano1b908b62010-04-10 00:19:46 -0700637static void wt_status_print_other(struct wt_status *s,
638 struct string_list *l,
639 const char *what,
640 const char *how)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400641{
Jeff Kingc91f0d92006-09-08 04:05:34 -0400642 int i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500643 struct strbuf buf = STRBUF_INIT;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400644
Junio C Hamano76378682009-08-10 00:36:33 -0700645 if (!s->untracked.nr)
646 return;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400647
Junio C Hamano1b908b62010-04-10 00:19:46 -0700648 wt_status_print_other_header(s, what, how);
649
650 for (i = 0; i < l->nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700651 struct string_list_item *it;
Junio C Hamano1b908b62010-04-10 00:19:46 -0700652 it = &(l->items[i]);
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600653 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
654 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
655 "%s\n", quote_path(it->string, strlen(it->string),
Junio C Hamano76378682009-08-10 00:36:33 -0700656 &buf, s->prefix));
Jeff Kingc91f0d92006-09-08 04:05:34 -0400657 }
Johannes Schindelin367c9882007-11-11 17:35:41 +0000658 strbuf_release(&buf);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400659}
660
661static void wt_status_print_verbose(struct wt_status *s)
662{
663 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800664 struct setup_revision_opt opt;
Kristian Høgsberg99a12692007-11-21 21:54:49 -0500665
Jeff Kingc91f0d92006-09-08 04:05:34 -0400666 init_revisions(&rev, NULL);
Jeff King5ec11af2008-12-07 21:54:17 -0500667 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
Junio C Hamano32962c92010-03-08 22:58:09 -0800668
669 memset(&opt, 0, sizeof(opt));
670 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
671 setup_revisions(0, NULL, &rev, &opt);
672
Jeff Kingc91f0d92006-09-08 04:05:34 -0400673 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
674 rev.diffopt.detect_rename = 1;
Kristian Høgsberg4ba0cb22008-03-10 13:58:26 -0400675 rev.diffopt.file = s->fp;
676 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 00:49:35 -0400677 /*
678 * If we're not going to stdout, then we definitely don't
679 * want color, since we are going to the commit message
680 * file (and even the "auto" setting won't work, since it
681 * will have checked isatty on stdout).
682 */
683 if (s->fp != stdout)
684 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400685 run_diff_index(&rev, 1);
686}
687
Junio C Hamanob6975ab2008-07-02 00:52:16 -0700688static void wt_status_print_tracking(struct wt_status *s)
689{
690 struct strbuf sb = STRBUF_INIT;
691 const char *cp, *ep;
692 struct branch *branch;
693
694 assert(s->branch && !s->is_initial);
695 if (prefixcmp(s->branch, "refs/heads/"))
696 return;
697 branch = branch_get(s->branch + 11);
698 if (!format_tracking_info(branch, &sb))
699 return;
700
701 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-09 21:59:30 -0700702 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Junio C Hamanob6975ab2008-07-02 00:52:16 -0700703 "# %.*s", (int)(ep - cp), cp);
Junio C Hamanod249b092009-08-09 21:59:30 -0700704 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
Junio C Hamanob6975ab2008-07-02 00:52:16 -0700705}
706
Jeff Kingc91f0d92006-09-08 04:05:34 -0400707void wt_status_print(struct wt_status *s)
708{
Aleksi Aalto1d282322010-11-18 01:40:05 +0200709 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
710 const char *branch_status_color = color(WT_STATUS_HEADER, s);
Jürgen Rühle98bf8a42007-01-02 20:26:23 +0100711
Junio C Hamanobda324c2007-01-03 01:09:34 -0800712 if (s->branch) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000713 const char *on_what = _("On branch ");
Junio C Hamanobda324c2007-01-03 01:09:34 -0800714 const char *branch_name = s->branch;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800715 if (!prefixcmp(branch_name, "refs/heads/"))
Junio C Hamanobda324c2007-01-03 01:09:34 -0800716 branch_name += 11;
717 else if (!strcmp(branch_name, "HEAD")) {
718 branch_name = "";
Aleksi Aalto1d282322010-11-18 01:40:05 +0200719 branch_status_color = color(WT_STATUS_NOBRANCH, s);
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000720 on_what = _("Not currently on any branch.");
Junio C Hamanobda324c2007-01-03 01:09:34 -0800721 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600722 status_printf(s, color(WT_STATUS_HEADER, s), "");
723 status_printf_more(s, branch_status_color, "%s", on_what);
724 status_printf_more(s, branch_color, "%s\n", branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -0700725 if (!s->is_initial)
726 wt_status_print_tracking(s);
Junio C Hamanobda324c2007-01-03 01:09:34 -0800727 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400728
729 if (s->is_initial) {
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600730 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
Ævar Arnfjörð Bjarmasonb3b298a2011-02-22 23:42:17 +0000731 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600732 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400733 }
734
Jeff Kingc1e255b2008-11-12 03:21:39 -0500735 wt_status_print_updated(s);
Johannes Sixt228e7b52009-09-01 22:13:53 +0200736 wt_status_print_unmerged(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400737 wt_status_print_changed(s);
Jens Lehmann46a958b2010-06-25 16:56:47 +0200738 if (s->submodule_summary &&
739 (!s->ignore_submodule_arg ||
740 strcmp(s->ignore_submodule_arg, "all"))) {
Jens Lehmannf17a5d32010-01-17 20:42:31 +0100741 wt_status_print_submodule_summary(s, 0); /* staged */
742 wt_status_print_submodule_summary(s, 1); /* unstaged */
743 }
Junio C Hamano2381e392010-04-10 00:33:17 -0700744 if (s->show_untracked_files) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000745 wt_status_print_other(s, &s->untracked, _("Untracked"), "add");
Junio C Hamano2381e392010-04-10 00:33:17 -0700746 if (s->show_ignored_files)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000747 wt_status_print_other(s, &s->ignored, _("Ignored"), "add -f");
Junio C Hamano2381e392010-04-10 00:33:17 -0700748 } else if (s->commitable)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000749 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
Michael J Gruber980bde32010-04-22 22:30:20 +0200750 advice_status_hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000751 ? _(" (use -u option to show untracked files)") : "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400752
Jeff King1324fb62008-11-12 03:23:37 -0500753 if (s->verbose)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400754 wt_status_print_verbose(s);
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100755 if (!s->commitable) {
756 if (s->amend)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000757 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
Junio C Hamano37d07f82007-12-12 19:09:16 -0800758 else if (s->nowarn)
759 ; /* nothing */
Jürgen Rühle2a3a3c22007-01-10 23:25:03 +0100760 else if (s->workdir_dirty)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000761 printf(_("no changes added to commit%s\n"),
Michael J Gruber980bde32010-04-22 22:30:20 +0200762 advice_status_hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000763 ? _(" (use \"git add\" and/or \"git commit -a\")") : "");
Junio C Hamano76378682009-08-10 00:36:33 -0700764 else if (s->untracked.nr)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000765 printf(_("nothing added to commit but untracked files present%s\n"),
Michael J Gruber980bde32010-04-22 22:30:20 +0200766 advice_status_hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000767 ? _(" (use \"git add\" to track)") : "");
Jürgen Rühle2a3a3c22007-01-10 23:25:03 +0100768 else if (s->is_initial)
Ævar Arnfjörð Bjarmason8ec9bc02011-02-22 23:42:14 +0000769 printf(_("nothing to commit%s\n"), advice_status_hints
770 ? _(" (create/copy files and use \"git add\" to track)") : "");
Junio C Hamanod249b092009-08-09 21:59:30 -0700771 else if (!s->show_untracked_files)
Ævar Arnfjörð Bjarmason8ec9bc02011-02-22 23:42:14 +0000772 printf(_("nothing to commit%s\n"), advice_status_hints
773 ? _(" (use -u to show untracked files)") : "");
Jürgen Rühle2a3a3c22007-01-10 23:25:03 +0100774 else
Ævar Arnfjörð Bjarmason8ec9bc02011-02-22 23:42:14 +0000775 printf(_("nothing to commit%s\n"), advice_status_hints
776 ? _(" (working directory clean)") : "");
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100777 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400778}
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100779
780static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
781 struct wt_status *s)
782{
783 struct wt_status_change_data *d = it->util;
784 const char *how = "??";
785
786 switch (d->stagemask) {
787 case 1: how = "DD"; break; /* both deleted */
788 case 2: how = "AU"; break; /* added by us */
789 case 3: how = "UD"; break; /* deleted by them */
790 case 4: how = "UA"; break; /* added by them */
791 case 5: how = "DU"; break; /* deleted by us */
792 case 6: how = "AA"; break; /* both added */
793 case 7: how = "UU"; break; /* both modified */
794 }
Michael J Gruber3fe2a892009-12-05 16:04:38 +0100795 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100796 if (null_termination) {
Michael J Gruber3fe2a892009-12-05 16:04:38 +0100797 fprintf(stdout, " %s%c", it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100798 } else {
799 struct strbuf onebuf = STRBUF_INIT;
800 const char *one;
801 one = quote_path(it->string, -1, &onebuf, s->prefix);
Michael J Gruber3fe2a892009-12-05 16:04:38 +0100802 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100803 strbuf_release(&onebuf);
804 }
805}
806
807static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
808 struct wt_status *s)
809{
810 struct wt_status_change_data *d = it->util;
811
Michael J Gruber3fe2a892009-12-05 16:04:38 +0100812 if (d->index_status)
813 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
814 else
815 putchar(' ');
816 if (d->worktree_status)
817 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
818 else
819 putchar(' ');
820 putchar(' ');
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100821 if (null_termination) {
822 fprintf(stdout, "%s%c", it->string, 0);
823 if (d->head_path)
824 fprintf(stdout, "%s%c", d->head_path, 0);
825 } else {
826 struct strbuf onebuf = STRBUF_INIT;
827 const char *one;
828 if (d->head_path) {
829 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
Kevin Ballarddbfdc622010-11-08 18:44:38 -0800830 if (*one != '"' && strchr(one, ' ') != NULL) {
831 putchar('"');
832 strbuf_addch(&onebuf, '"');
833 one = onebuf.buf;
834 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100835 printf("%s -> ", one);
836 strbuf_release(&onebuf);
837 }
838 one = quote_path(it->string, -1, &onebuf, s->prefix);
Kevin Ballarddbfdc622010-11-08 18:44:38 -0800839 if (*one != '"' && strchr(one, ' ') != NULL) {
840 putchar('"');
841 strbuf_addch(&onebuf, '"');
842 one = onebuf.buf;
843 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100844 printf("%s\n", one);
845 strbuf_release(&onebuf);
846 }
847}
848
Junio C Hamano2381e392010-04-10 00:33:17 -0700849static void wt_shortstatus_other(int null_termination, struct string_list_item *it,
850 struct wt_status *s, const char *sign)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100851{
852 if (null_termination) {
Junio C Hamano2381e392010-04-10 00:33:17 -0700853 fprintf(stdout, "%s %s%c", sign, it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100854 } else {
855 struct strbuf onebuf = STRBUF_INIT;
856 const char *one;
857 one = quote_path(it->string, -1, &onebuf, s->prefix);
Junio C Hamanoc1909e72010-05-01 22:05:14 -0700858 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
Michael J Gruber3fe2a892009-12-05 16:04:38 +0100859 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100860 strbuf_release(&onebuf);
861 }
862}
863
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200864static void wt_shortstatus_print_tracking(struct wt_status *s)
865{
866 struct branch *branch;
867 const char *header_color = color(WT_STATUS_HEADER, s);
868 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
869 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
870
871 const char *base;
872 const char *branch_name;
873 int num_ours, num_theirs;
874
875 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
876
877 if (!s->branch)
878 return;
879 branch_name = s->branch;
880
881 if (!prefixcmp(branch_name, "refs/heads/"))
882 branch_name += 11;
883 else if (!strcmp(branch_name, "HEAD")) {
Ævar Arnfjörð Bjarmason98f5e242011-02-22 23:42:15 +0000884 branch_name = _("HEAD (no branch)");
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200885 branch_color_local = color(WT_STATUS_NOBRANCH, s);
886 }
887
888 branch = branch_get(s->branch + 11);
889 if (s->is_initial)
Ævar Arnfjörð Bjarmason98f5e242011-02-22 23:42:15 +0000890 color_fprintf(s->fp, header_color, _("Initial commit on "));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200891 if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
892 color_fprintf_ln(s->fp, branch_color_local,
893 "%s", branch_name);
894 return;
895 }
896
897 base = branch->merge[0]->dst;
898 base = shorten_unambiguous_ref(base, 0);
899 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
900 color_fprintf(s->fp, header_color, "...");
901 color_fprintf(s->fp, branch_color_remote, "%s", base);
902
903 color_fprintf(s->fp, header_color, " [");
904 if (!num_ours) {
Ævar Arnfjörð Bjarmason98f5e242011-02-22 23:42:15 +0000905 color_fprintf(s->fp, header_color, _("behind "));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200906 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
907 } else if (!num_theirs) {
Ævar Arnfjörð Bjarmason98f5e242011-02-22 23:42:15 +0000908 color_fprintf(s->fp, header_color, _("ahead "));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200909 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
910 } else {
Ævar Arnfjörð Bjarmason98f5e242011-02-22 23:42:15 +0000911 color_fprintf(s->fp, header_color, _("ahead "));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200912 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
Ævar Arnfjörð Bjarmason98f5e242011-02-22 23:42:15 +0000913 color_fprintf(s->fp, header_color, _(", behind "));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200914 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
915 }
916
917 color_fprintf_ln(s->fp, header_color, "]");
918}
919
920void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100921{
922 int i;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200923
924 if (show_branch)
925 wt_shortstatus_print_tracking(s);
926
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100927 for (i = 0; i < s->change.nr; i++) {
928 struct wt_status_change_data *d;
929 struct string_list_item *it;
930
931 it = &(s->change.items[i]);
932 d = it->util;
933 if (d->stagemask)
934 wt_shortstatus_unmerged(null_termination, it, s);
935 else
936 wt_shortstatus_status(null_termination, it, s);
937 }
938 for (i = 0; i < s->untracked.nr; i++) {
939 struct string_list_item *it;
940
941 it = &(s->untracked.items[i]);
Junio C Hamano2381e392010-04-10 00:33:17 -0700942 wt_shortstatus_other(null_termination, it, s, "??");
943 }
944 for (i = 0; i < s->ignored.nr; i++) {
945 struct string_list_item *it;
946
947 it = &(s->ignored.items[i]);
948 wt_shortstatus_other(null_termination, it, s, "!!");
Michael J Gruber84dbe7b2009-12-05 16:04:37 +0100949 }
950}
Jeff King4a7cc2f2009-12-07 00:17:15 -0500951
952void wt_porcelain_print(struct wt_status *s, int null_termination)
953{
954 s->use_color = 0;
Jeff King86617682009-12-07 00:26:25 -0500955 s->relative_paths = 0;
956 s->prefix = NULL;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +0200957 wt_shortstatus_print(s, null_termination, 0);
Jeff King4a7cc2f2009-12-07 00:17:15 -0500958}