blob: 6c0e400f8130743b0c0fef2764bd804451b016a6 [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"
Matthieu Moybb7e32e2013-09-06 19:43:05 +020011#include "argv-array.h"
Junio C Hamanob6975ab2008-07-02 00:52:16 -070012#include "remote.h"
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020013#include "refs.h"
Jens Lehmann46a958b2010-06-25 16:56:47 +020014#include "submodule.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070015#include "column.h"
Lucien Kong2d1cceb2012-06-10 13:17:38 +020016#include "strbuf.h"
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +070017#include "utf8.h"
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +070018#include "worktree.h"
Johannes Schindelinfd849862016-10-07 18:08:38 +020019#include "lockfile.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -040020
Nguyễn Thái Ngọc Duy983dc692014-02-17 19:15:30 +070021static const char cut_line[] =
Jens Lehmann1a72cfd2013-12-05 20:44:14 +010022"------------------------ >8 ------------------------\n";
23
Junio C Hamano23900a92009-08-09 23:08:40 -070024static char default_wt_status_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010025 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
26 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
27 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
28 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
29 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
Junio C Hamano4d4d5722009-08-05 00:04:51 -070030 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020031 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
32 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
Jeff King148135f2010-12-09 12:27:08 -050033 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
Jeff Kingc91f0d92006-09-08 04:05:34 -040034};
Junio C Hamano4d229652007-01-11 15:34:41 -080035
Junio C Hamanod249b092009-08-09 21:59:30 -070036static const char *color(int slot, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -040037{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070038 const char *c = "";
39 if (want_color(s->use_color))
40 c = s->color_palette[slot];
Jeff King148135f2010-12-09 12:27:08 -050041 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
42 c = s->color_palette[WT_STATUS_HEADER];
43 return c;
Jeff Kingc91f0d92006-09-08 04:05:34 -040044}
45
Jonathan Niederbecbdae2011-02-25 23:09:41 -060046static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
47 const char *fmt, va_list ap, const char *trail)
48{
49 struct strbuf sb = STRBUF_INIT;
50 struct strbuf linebuf = STRBUF_INIT;
51 const char *line, *eol;
52
53 strbuf_vaddf(&sb, fmt, ap);
54 if (!sb.len) {
Matthieu Moy2556b992013-09-06 19:43:07 +020055 if (s->display_comment_prefix) {
56 strbuf_addch(&sb, comment_line_char);
57 if (!trail)
58 strbuf_addch(&sb, ' ');
59 }
Jonathan Niederbecbdae2011-02-25 23:09:41 -060060 color_print_strbuf(s->fp, color, &sb);
61 if (trail)
62 fprintf(s->fp, "%s", trail);
63 strbuf_release(&sb);
64 return;
65 }
66 for (line = sb.buf; *line; line = eol + 1) {
67 eol = strchr(line, '\n');
68
69 strbuf_reset(&linebuf);
Matthieu Moy2556b992013-09-06 19:43:07 +020070 if (at_bol && s->display_comment_prefix) {
Junio C Hamanoeff80a92013-01-16 20:18:48 +010071 strbuf_addch(&linebuf, comment_line_char);
Jonathan Niederbecbdae2011-02-25 23:09:41 -060072 if (*line != '\n' && *line != '\t')
73 strbuf_addch(&linebuf, ' ');
74 }
75 if (eol)
76 strbuf_add(&linebuf, line, eol - line);
77 else
78 strbuf_addstr(&linebuf, line);
79 color_print_strbuf(s->fp, color, &linebuf);
80 if (eol)
81 fprintf(s->fp, "\n");
82 else
83 break;
84 at_bol = 1;
85 }
86 if (trail)
87 fprintf(s->fp, "%s", trail);
88 strbuf_release(&linebuf);
89 strbuf_release(&sb);
90}
91
92void status_printf_ln(struct wt_status *s, const char *color,
93 const char *fmt, ...)
94{
95 va_list ap;
96
97 va_start(ap, fmt);
98 status_vprintf(s, 1, color, fmt, ap, "\n");
99 va_end(ap);
100}
101
102void status_printf(struct wt_status *s, const char *color,
103 const char *fmt, ...)
104{
105 va_list ap;
106
107 va_start(ap, fmt);
108 status_vprintf(s, 1, color, fmt, ap, NULL);
109 va_end(ap);
110}
111
Junio C Hamano1e248452012-09-15 22:46:26 -0700112static void status_printf_more(struct wt_status *s, const char *color,
113 const char *fmt, ...)
Jonathan Niederbecbdae2011-02-25 23:09:41 -0600114{
115 va_list ap;
116
117 va_start(ap, fmt);
118 status_vprintf(s, 0, color, fmt, ap, NULL);
119 va_end(ap);
120}
121
Jeff Kingc91f0d92006-09-08 04:05:34 -0400122void wt_status_prepare(struct wt_status *s)
123{
Junio C Hamanocc46a742007-02-09 16:22:42 -0800124 memset(s, 0, sizeof(*s));
Junio C Hamano23900a92009-08-09 23:08:40 -0700125 memcpy(s->color_palette, default_wt_status_colors,
126 sizeof(default_wt_status_colors));
Junio C Hamanod249b092009-08-09 21:59:30 -0700127 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
128 s->use_color = -1;
129 s->relative_paths = 1;
René Scharfeefbd4fd2017-10-01 09:29:03 +0200130 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400131 s->reference = "HEAD";
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400132 s->fp = stdout;
Kristian Høgsberg0f729f22007-09-17 20:06:43 -0400133 s->index_file = get_index_file();
Junio C Hamano50b7e702009-08-04 23:49:33 -0700134 s->change.strdup_strings = 1;
Junio C Hamano76378682009-08-10 00:36:33 -0700135 s->untracked.strdup_strings = 1;
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700136 s->ignored.strdup_strings = 1;
Junio C Hamano84b42022013-06-24 11:41:40 -0700137 s->show_branch = -1; /* unspecified */
Liam Beguinc1b5d012017-06-17 18:30:51 -0400138 s->show_stash = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +0000139 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
Matthieu Moy2556b992013-09-06 19:43:07 +0200140 s->display_comment_prefix = 0;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000141 s->detect_rename = -1;
142 s->rename_score = -1;
143 s->rename_limit = -1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400144}
145
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400146static void wt_longstatus_print_unmerged_header(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700147{
Lucien Kong96b0ec12012-06-05 22:21:26 +0200148 int i;
149 int del_mod_conflict = 0;
150 int both_deleted = 0;
151 int not_deleted = 0;
Junio C Hamanod249b092009-08-09 21:59:30 -0700152 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800153
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000154 status_printf_ln(s, c, _("Unmerged paths:"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200155
156 for (i = 0; i < s->change.nr; i++) {
157 struct string_list_item *it = &(s->change.items[i]);
158 struct wt_status_change_data *d = it->util;
159
160 switch (d->stagemask) {
161 case 0:
162 break;
163 case 1:
164 both_deleted = 1;
165 break;
166 case 3:
167 case 5:
168 del_mod_conflict = 1;
169 break;
170 default:
171 not_deleted = 1;
172 break;
173 }
174 }
175
Matthieu Moy6a964f52013-09-12 12:50:05 +0200176 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400177 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500178 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800179 ;
180 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000181 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700182 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000183 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200184
185 if (!both_deleted) {
186 if (!del_mod_conflict)
187 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
188 else
189 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
190 } else if (!del_mod_conflict && !not_deleted) {
191 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
192 } else {
193 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
194 }
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500195 status_printf_ln(s, c, "%s", "");
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700196}
197
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400198static void wt_longstatus_print_cached_header(struct wt_status *s)
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100199{
Junio C Hamanod249b092009-08-09 21:59:30 -0700200 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800201
Ævar Arnfjörð Bjarmason919a4ce2011-02-22 23:42:16 +0000202 status_printf_ln(s, c, _("Changes to be committed:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200203 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400204 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500205 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800206 ; /* NEEDSWORK: use "git reset --unresolve"??? */
207 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000208 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano3c588452009-12-11 23:53:41 -0800209 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000210 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500211 status_printf_ln(s, c, "%s", "");
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100212}
213
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400214static void wt_longstatus_print_dirty_header(struct wt_status *s,
215 int has_deleted,
216 int has_dirty_submodules)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400217{
Junio C Hamanod249b092009-08-09 21:59:30 -0700218 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800219
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000220 status_printf_ln(s, c, _("Changes not staged for commit:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200221 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400222 return;
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200223 if (!has_deleted)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000224 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200225 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000226 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
227 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100228 if (has_dirty_submodules)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000229 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500230 status_printf_ln(s, c, "%s", "");
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200231}
232
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400233static void wt_longstatus_print_other_header(struct wt_status *s,
234 const char *what,
235 const char *how)
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200236{
Junio C Hamanod249b092009-08-09 21:59:30 -0700237 const char *c = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +0700238 status_printf_ln(s, c, "%s:", what);
Matthieu Moy6a964f52013-09-12 12:50:05 +0200239 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400240 return;
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000241 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500242 status_printf_ln(s, c, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400243}
244
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400245static void wt_longstatus_print_trailer(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400246{
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500247 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400248}
249
Dmitry Potapova734d0b2008-03-07 05:30:58 +0300250#define quote_path quote_path_relative
Junio C Hamano3a946802006-11-08 13:20:46 -0800251
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800252static const char *wt_status_unmerged_status_string(int stagemask)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700253{
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800254 switch (stagemask) {
255 case 1:
256 return _("both deleted:");
257 case 2:
258 return _("added by us:");
259 case 3:
260 return _("deleted by them:");
261 case 4:
262 return _("added by them:");
263 case 5:
264 return _("deleted by us:");
265 case 6:
266 return _("both added:");
267 case 7:
268 return _("both modified:");
269 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200270 BUG("unhandled unmerged status %x", stagemask);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700271 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700272}
273
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700274static const char *wt_status_diff_status_string(int status)
275{
276 switch (status) {
277 case DIFF_STATUS_ADDED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700278 return _("new file:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700279 case DIFF_STATUS_COPIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700280 return _("copied:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700281 case DIFF_STATUS_DELETED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700282 return _("deleted:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700283 case DIFF_STATUS_MODIFIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700284 return _("modified:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700285 case DIFF_STATUS_RENAMED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700286 return _("renamed:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700287 case DIFF_STATUS_TYPE_CHANGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700288 return _("typechange:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700289 case DIFF_STATUS_UNKNOWN:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700290 return _("unknown:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700291 case DIFF_STATUS_UNMERGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700292 return _("unmerged:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700293 default:
294 return NULL;
295 }
296}
297
Jonathan Nieder335e8252013-12-19 11:43:19 -0800298static int maxwidth(const char *(*label)(int), int minval, int maxval)
299{
300 int result = 0, i;
301
302 for (i = minval; i <= maxval; i++) {
303 const char *s = label(i);
304 int len = s ? utf8_strwidth(s) : 0;
305 if (len > result)
306 result = len;
307 }
308 return result;
309}
310
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400311static void wt_longstatus_print_unmerged_data(struct wt_status *s,
312 struct string_list_item *it)
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800313{
314 const char *c = color(WT_STATUS_UNMERGED, s);
315 struct wt_status_change_data *d = it->util;
316 struct strbuf onebuf = STRBUF_INIT;
317 static char *padding;
318 static int label_width;
319 const char *one, *how;
320 int len;
321
322 if (!padding) {
323 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
324 label_width += strlen(" ");
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800325 padding = xmallocz(label_width);
326 memset(padding, ' ', label_width);
327 }
328
329 one = quote_path(it->string, s->prefix, &onebuf);
330 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
331
332 how = wt_status_unmerged_status_string(d->stagemask);
333 len = label_width - utf8_strwidth(how);
334 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
335 strbuf_release(&onebuf);
336}
337
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400338static void wt_longstatus_print_change_data(struct wt_status *s,
339 int change_type,
340 struct string_list_item *it)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400341{
Junio C Hamano50b7e702009-08-04 23:49:33 -0700342 struct wt_status_change_data *d = it->util;
Junio C Hamanod249b092009-08-09 21:59:30 -0700343 const char *c = color(change_type, s);
Jeff Kingb8527d52013-03-21 07:05:28 -0400344 int status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700345 char *one_name;
346 char *two_name;
Junio C Hamano3a946802006-11-08 13:20:46 -0800347 const char *one, *two;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500348 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100349 struct strbuf extra = STRBUF_INIT;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700350 static char *padding;
Junio C Hamanod52cb572014-03-12 13:51:22 -0700351 static int label_width;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700352 const char *what;
353 int len;
354
355 if (!padding) {
Jonathan Nieder335e8252013-12-19 11:43:19 -0800356 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
357 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
Junio C Hamanod52cb572014-03-12 13:51:22 -0700358 label_width += strlen(" ");
359 padding = xmallocz(label_width);
360 memset(padding, ' ', label_width);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700361 }
Junio C Hamano3a946802006-11-08 13:20:46 -0800362
Junio C Hamano50b7e702009-08-04 23:49:33 -0700363 one_name = two_name = it->string;
364 switch (change_type) {
365 case WT_STATUS_UPDATED:
366 status = d->index_status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700367 break;
368 case WT_STATUS_CHANGED:
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100369 if (d->new_submodule_commits || d->dirty_submodule) {
370 strbuf_addstr(&extra, " (");
371 if (d->new_submodule_commits)
René Scharfea22ae752016-09-15 20:31:00 +0200372 strbuf_addstr(&extra, _("new commits, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100373 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
René Scharfea22ae752016-09-15 20:31:00 +0200374 strbuf_addstr(&extra, _("modified content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100375 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
René Scharfea22ae752016-09-15 20:31:00 +0200376 strbuf_addstr(&extra, _("untracked content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100377 strbuf_setlen(&extra, extra.len - 2);
378 strbuf_addch(&extra, ')');
379 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700380 status = d->worktree_status;
381 break;
Jeff Kingb8527d52013-03-21 07:05:28 -0400382 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200383 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
Jeff Kingb8527d52013-03-21 07:05:28 -0400384 change_type);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700385 }
386
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700387 /*
388 * Only pick up the rename it's relevant. If the rename is for
389 * the changed section and we're printing the updated section,
390 * ignore it.
391 */
392 if (d->rename_status == status)
393 one_name = d->rename_source;
394
Jiang Xin39598f92013-06-25 23:53:45 +0800395 one = quote_path(one_name, s->prefix, &onebuf);
396 two = quote_path(two_name, s->prefix, &twobuf);
Junio C Hamano3a946802006-11-08 13:20:46 -0800397
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600398 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700399 what = wt_status_diff_status_string(status);
400 if (!what)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200401 BUG("unhandled diff status %c", status);
Junio C Hamanod52cb572014-03-12 13:51:22 -0700402 len = label_width - utf8_strwidth(what);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700403 assert(len >= 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700404 if (one_name != two_name)
Junio C Hamanod52cb572014-03-12 13:51:22 -0700405 status_printf_more(s, c, "%s%.*s%s -> %s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700406 what, len, padding, one, two);
407 else
Junio C Hamanod52cb572014-03-12 13:51:22 -0700408 status_printf_more(s, c, "%s%.*s%s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700409 what, len, padding, one);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100410 if (extra.len) {
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600411 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100412 strbuf_release(&extra);
413 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600414 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
Johannes Schindelin367c9882007-11-11 17:35:41 +0000415 strbuf_release(&onebuf);
416 strbuf_release(&twobuf);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400417}
418
Nguyễn Thái Ngọc Duy98bc94e2017-12-27 17:18:36 +0700419static char short_submodule_status(struct wt_status_change_data *d)
420{
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700421 if (d->new_submodule_commits)
422 return 'M';
423 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
424 return 'm';
425 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
426 return '?';
427 return d->worktree_status;
428}
429
Junio C Hamano50b7e702009-08-04 23:49:33 -0700430static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
431 struct diff_options *options,
432 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400433{
434 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400435 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700436
437 if (!q->nr)
438 return;
439 s->workdir_dirty = 1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400440 for (i = 0; i < q->nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700441 struct diff_filepair *p;
442 struct string_list_item *it;
443 struct wt_status_change_data *d;
444
445 p = q->queue[i];
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700446 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700447 d = it->util;
448 if (!d) {
449 d = xcalloc(1, sizeof(*d));
450 it->util = d;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400451 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700452 if (!d->worktree_status)
453 d->worktree_status = p->status;
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700454 if (S_ISGITLINK(p->two->mode)) {
455 d->dirty_submodule = p->two->dirty_submodule;
brian m. carlsona0d12c42016-06-24 23:09:23 +0000456 d->new_submodule_commits = !!oidcmp(&p->one->oid,
457 &p->two->oid);
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700458 if (s->status_format == STATUS_FORMAT_SHORT)
459 d->worktree_status = short_submodule_status(d);
460 }
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400461
462 switch (p->status) {
463 case DIFF_STATUS_ADDED:
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700464 d->mode_worktree = p->two->mode;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400465 break;
466
467 case DIFF_STATUS_DELETED:
468 d->mode_index = p->one->mode;
469 oidcpy(&d->oid_index, &p->one->oid);
470 /* mode_worktree is zero for a delete. */
471 break;
472
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700473 case DIFF_STATUS_COPIED:
474 case DIFF_STATUS_RENAMED:
475 if (d->rename_status)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200476 BUG("multiple renames on the same target? how?");
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700477 d->rename_source = xstrdup(p->one->path);
478 d->rename_score = p->score * 100 / MAX_SCORE;
479 d->rename_status = p->status;
480 /* fallthru */
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400481 case DIFF_STATUS_MODIFIED:
482 case DIFF_STATUS_TYPE_CHANGED:
483 case DIFF_STATUS_UNMERGED:
484 d->mode_index = p->one->mode;
485 d->mode_worktree = p->two->mode;
486 oidcpy(&d->oid_index, &p->one->oid);
487 break;
488
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700489 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200490 BUG("unhandled diff-files status '%c'", p->status);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400491 break;
492 }
493
Jeff Kingc91f0d92006-09-08 04:05:34 -0400494 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400495}
496
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700497static int unmerged_mask(const char *path)
498{
499 int pos, mask;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700500 const struct cache_entry *ce;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700501
502 pos = cache_name_pos(path, strlen(path));
503 if (0 <= pos)
504 return 0;
505
506 mask = 0;
507 pos = -pos-1;
508 while (pos < active_nr) {
509 ce = active_cache[pos++];
510 if (strcmp(ce->name, path) || !ce_stage(ce))
511 break;
512 mask |= (1 << (ce_stage(ce) - 1));
513 }
514 return mask;
515}
516
Junio C Hamano50b7e702009-08-04 23:49:33 -0700517static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
518 struct diff_options *options,
519 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400520{
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100521 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400522 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700523
524 for (i = 0; i < q->nr; i++) {
525 struct diff_filepair *p;
526 struct string_list_item *it;
527 struct wt_status_change_data *d;
528
529 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100530 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700531 d = it->util;
532 if (!d) {
533 d = xcalloc(1, sizeof(*d));
534 it->util = d;
535 }
536 if (!d->index_status)
537 d->index_status = p->status;
538 switch (p->status) {
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400539 case DIFF_STATUS_ADDED:
540 /* Leave {mode,oid}_head zero for an add. */
541 d->mode_index = p->two->mode;
542 oidcpy(&d->oid_index, &p->two->oid);
543 break;
544 case DIFF_STATUS_DELETED:
545 d->mode_head = p->one->mode;
546 oidcpy(&d->oid_head, &p->one->oid);
547 /* Leave {mode,oid}_index zero for a delete. */
548 break;
549
Junio C Hamano50b7e702009-08-04 23:49:33 -0700550 case DIFF_STATUS_COPIED:
551 case DIFF_STATUS_RENAMED:
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700552 if (d->rename_status)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200553 BUG("multiple renames on the same target? how?");
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700554 d->rename_source = xstrdup(p->one->path);
555 d->rename_score = p->score * 100 / MAX_SCORE;
556 d->rename_status = p->status;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400557 /* fallthru */
558 case DIFF_STATUS_MODIFIED:
559 case DIFF_STATUS_TYPE_CHANGED:
560 d->mode_head = p->one->mode;
561 d->mode_index = p->two->mode;
562 oidcpy(&d->oid_head, &p->one->oid);
563 oidcpy(&d->oid_index, &p->two->oid);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700564 break;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700565 case DIFF_STATUS_UNMERGED:
566 d->stagemask = unmerged_mask(p->two->path);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400567 /*
568 * Don't bother setting {mode,oid}_{head,index} since the print
569 * code will output the stage values directly and not use the
570 * values in these fields.
571 */
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700572 break;
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700573
574 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200575 BUG("unhandled diff-index status '%c'", p->status);
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700576 break;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700577 }
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100578 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400579}
580
Junio C Hamano50b7e702009-08-04 23:49:33 -0700581static void wt_status_collect_changes_worktree(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400582{
583 struct rev_info rev;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700584
585 init_revisions(&rev, NULL);
586 setup_revisions(0, NULL, &rev, NULL);
587 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700588 rev.diffopt.flags.dirty_submodules = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700589 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100590 if (!s->show_untracked_files)
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700591 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200592 if (s->ignore_submodule_arg) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700593 rev.diffopt.flags.override_submodule_config = 1;
Jens Lehmann46a958b2010-06-25 16:56:47 +0200594 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Nguyễn Thái Ngọc Duyc4c42f22011-10-24 15:24:51 +1100595 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700596 rev.diffopt.format_callback = wt_status_collect_changed_cb;
597 rev.diffopt.format_callback_data = s;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000598 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
599 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
600 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700601 copy_pathspec(&rev.prune_data, &s->pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700602 run_diff_files(&rev, 0);
603}
604
605static void wt_status_collect_changes_index(struct wt_status *s)
606{
607 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800608 struct setup_revision_opt opt;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700609
Jeff Kingc91f0d92006-09-08 04:05:34 -0400610 init_revisions(&rev, NULL);
Junio C Hamano32962c92010-03-08 22:58:09 -0800611 memset(&opt, 0, sizeof(opt));
brian m. carlsonf2e51192018-05-02 00:26:00 +0000612 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
Junio C Hamano32962c92010-03-08 22:58:09 -0800613 setup_revisions(0, NULL, &rev, &opt);
614
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700615 rev.diffopt.flags.override_submodule_config = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700616 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200617 if (s->ignore_submodule_arg) {
Jens Lehmann46a958b2010-06-25 16:56:47 +0200618 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Jens Lehmann1d2f3932014-04-05 18:59:03 +0200619 } else {
620 /*
621 * Unless the user did explicitly request a submodule ignore
622 * mode by passing a command line option we do not ignore any
623 * changed submodule SHA-1s when comparing index and HEAD, no
624 * matter what is configured. Otherwise the user won't be
625 * shown any submodules she manually added (and which are
626 * staged to be committed), which would be really confusing.
627 */
628 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200629 }
Jens Lehmann46a958b2010-06-25 16:56:47 +0200630
Jeff Kingc91f0d92006-09-08 04:05:34 -0400631 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700632 rev.diffopt.format_callback = wt_status_collect_updated_cb;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400633 rev.diffopt.format_callback_data = s;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000634 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
635 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
636 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700637 copy_pathspec(&rev.prune_data, &s->pathspec);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400638 run_diff_index(&rev, 1);
639}
640
Junio C Hamano50b7e702009-08-04 23:49:33 -0700641static void wt_status_collect_changes_initial(struct wt_status *s)
642{
643 int i;
644
645 for (i = 0; i < active_nr; i++) {
646 struct string_list_item *it;
647 struct wt_status_change_data *d;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700648 const struct cache_entry *ce = active_cache[i];
Junio C Hamano50b7e702009-08-04 23:49:33 -0700649
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +0700650 if (!ce_path_match(ce, &s->pathspec, NULL))
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700651 continue;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700652 if (ce_intent_to_add(ce))
653 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100654 it = string_list_insert(&s->change, ce->name);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700655 d = it->util;
656 if (!d) {
657 d = xcalloc(1, sizeof(*d));
658 it->util = d;
659 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700660 if (ce_stage(ce)) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700661 d->index_status = DIFF_STATUS_UNMERGED;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700662 d->stagemask |= (1 << (ce_stage(ce) - 1));
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400663 /*
664 * Don't bother setting {mode,oid}_{head,index} since the print
665 * code will output the stage values directly and not use the
666 * values in these fields.
667 */
668 } else {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700669 d->index_status = DIFF_STATUS_ADDED;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400670 /* Leave {mode,oid}_head zero for adds. */
671 d->mode_index = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +0100672 oidcpy(&d->oid_index, &ce->oid);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400673 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700674 }
675}
676
Junio C Hamano76378682009-08-10 00:36:33 -0700677static void wt_status_collect_untracked(struct wt_status *s)
678{
679 int i;
680 struct dir_struct dir;
Karsten Blees132d41e2014-07-12 02:07:36 +0200681 uint64_t t_begin = getnanotime();
Junio C Hamano76378682009-08-10 00:36:33 -0700682
683 if (!s->show_untracked_files)
684 return;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700685
Junio C Hamano76378682009-08-10 00:36:33 -0700686 memset(&dir, 0, sizeof(dir));
687 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
688 dir.flags |=
689 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400690 if (s->show_ignored_mode) {
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200691 dir.flags |= DIR_SHOW_IGNORED_TOO;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400692
693 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
694 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
695 } else {
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +0700696 dir.untracked = the_index.untracked;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400697 }
698
Junio C Hamano76378682009-08-10 00:36:33 -0700699 setup_standard_excludes(&dir);
700
Brandon Williams0d32c182017-05-05 12:53:34 -0700701 fill_directory(&dir, &the_index, &s->pathspec);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200702
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400703 for (i = 0; i < dir.nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700704 struct dir_entry *ent = dir.entries[i];
Brandon Caseyb8224232010-09-26 21:49:13 -0500705 if (cache_name_is_other(ent->name, ent->len) &&
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700706 dir_path_match(ent, &s->pathspec, 0, NULL))
Brandon Caseyb8224232010-09-26 21:49:13 -0500707 string_list_insert(&s->untracked, ent->name);
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700708 free(ent);
Junio C Hamano76378682009-08-10 00:36:33 -0700709 }
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700710
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200711 for (i = 0; i < dir.ignored_nr; i++) {
712 struct dir_entry *ent = dir.ignored[i];
713 if (cache_name_is_other(ent->name, ent->len) &&
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700714 dir_path_match(ent, &s->pathspec, 0, NULL))
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200715 string_list_insert(&s->ignored, ent->name);
716 free(ent);
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700717 }
718
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700719 free(dir.entries);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200720 free(dir.ignored);
721 clear_directory(&dir);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700722
Karsten Blees132d41e2014-07-12 02:07:36 +0200723 if (advice_status_u_option)
724 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
Junio C Hamano76378682009-08-10 00:36:33 -0700725}
726
727void wt_status_collect(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700728{
729 wt_status_collect_changes_worktree(s);
730
731 if (s->is_initial)
732 wt_status_collect_changes_initial(s);
733 else
734 wt_status_collect_changes_index(s);
Junio C Hamano76378682009-08-10 00:36:33 -0700735 wt_status_collect_untracked(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700736}
737
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400738static void wt_longstatus_print_unmerged(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700739{
740 int shown_header = 0;
741 int i;
742
743 for (i = 0; i < s->change.nr; i++) {
744 struct wt_status_change_data *d;
745 struct string_list_item *it;
746 it = &(s->change.items[i]);
747 d = it->util;
748 if (!d->stagemask)
749 continue;
750 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400751 wt_longstatus_print_unmerged_header(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700752 shown_header = 1;
753 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400754 wt_longstatus_print_unmerged_data(s, it);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700755 }
756 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400757 wt_longstatus_print_trailer(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700758
759}
760
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400761static void wt_longstatus_print_updated(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700762{
763 int shown_header = 0;
764 int i;
765
766 for (i = 0; i < s->change.nr; i++) {
767 struct wt_status_change_data *d;
768 struct string_list_item *it;
769 it = &(s->change.items[i]);
770 d = it->util;
771 if (!d->index_status ||
772 d->index_status == DIFF_STATUS_UNMERGED)
773 continue;
774 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400775 wt_longstatus_print_cached_header(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700776 s->commitable = 1;
777 shown_header = 1;
778 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400779 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700780 }
781 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400782 wt_longstatus_print_trailer(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700783}
784
785/*
786 * -1 : has delete
787 * 0 : no change
788 * 1 : some change but no delete
789 */
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100790static int wt_status_check_worktree_changes(struct wt_status *s,
791 int *dirty_submodules)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700792{
793 int i;
794 int changes = 0;
795
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100796 *dirty_submodules = 0;
797
Junio C Hamano50b7e702009-08-04 23:49:33 -0700798 for (i = 0; i < s->change.nr; i++) {
799 struct wt_status_change_data *d;
800 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700801 if (!d->worktree_status ||
802 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700803 continue;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100804 if (!changes)
805 changes = 1;
806 if (d->dirty_submodule)
807 *dirty_submodules = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700808 if (d->worktree_status == DIFF_STATUS_DELETED)
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100809 changes = -1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700810 }
811 return changes;
812}
813
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400814static void wt_longstatus_print_changed(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400815{
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100816 int i, dirty_submodules;
817 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700818
819 if (!worktree_changes)
820 return;
821
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400822 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700823
824 for (i = 0; i < s->change.nr; i++) {
825 struct wt_status_change_data *d;
826 struct string_list_item *it;
827 it = &(s->change.items[i]);
828 d = it->util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700829 if (!d->worktree_status ||
830 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700831 continue;
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400832 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700833 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400834 wt_longstatus_print_trailer(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400835}
836
Liam Beguinc1b5d012017-06-17 18:30:51 -0400837static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
838 const char *email, timestamp_t timestamp, int tz,
839 const char *message, void *cb_data)
840{
841 int *c = cb_data;
842 (*c)++;
843 return 0;
844}
845
846static void wt_longstatus_print_stash_summary(struct wt_status *s)
847{
848 int stash_count = 0;
849
850 for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
851 if (stash_count > 0)
852 status_printf_ln(s, GIT_COLOR_NORMAL,
853 Q_("Your stash currently has %d entry",
854 "Your stash currently has %d entries", stash_count),
855 stash_count);
856}
857
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400858static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
Ping Yinac8d5af2008-04-12 23:05:32 +0800859{
René Scharfed3180272014-08-19 21:09:35 +0200860 struct child_process sm_summary = CHILD_PROCESS_INIT;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200861 struct strbuf cmd_stdout = STRBUF_INIT;
862 struct strbuf summary = STRBUF_INIT;
863 char *summary_content;
Ping Yinac8d5af2008-04-12 23:05:32 +0800864
René Scharfea9154592014-10-19 13:14:20 +0200865 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
866 s->index_file);
Ping Yinac8d5af2008-04-12 23:05:32 +0800867
René Scharfea2bae2d2014-11-09 14:49:54 +0100868 argv_array_push(&sm_summary.args, "submodule");
869 argv_array_push(&sm_summary.args, "summary");
870 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
871 argv_array_push(&sm_summary.args, "--for-status");
872 argv_array_push(&sm_summary.args, "--summary-limit");
873 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200874 if (!uncommitted)
René Scharfea2bae2d2014-11-09 14:49:54 +0100875 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200876
Ping Yinac8d5af2008-04-12 23:05:32 +0800877 sm_summary.git_cmd = 1;
878 sm_summary.no_stdin = 1;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200879
Jeff King5c950e92015-03-22 23:53:52 -0400880 capture_command(&sm_summary, &cmd_stdout, 1024);
Matthieu Moy3ba74072013-09-06 19:43:06 +0200881
882 /* prepend header, only if there's an actual output */
Jeff Kingd56d9662015-03-22 06:00:32 -0400883 if (cmd_stdout.len) {
Matthieu Moy3ba74072013-09-06 19:43:06 +0200884 if (uncommitted)
885 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
886 else
887 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
888 strbuf_addstr(&summary, "\n\n");
889 }
890 strbuf_addbuf(&summary, &cmd_stdout);
891 strbuf_release(&cmd_stdout);
892
Matthieu Moy2556b992013-09-06 19:43:07 +0200893 if (s->display_comment_prefix) {
Jeff Kingd56d9662015-03-22 06:00:32 -0400894 size_t len;
Matthieu Moy2556b992013-09-06 19:43:07 +0200895 summary_content = strbuf_detach(&summary, &len);
896 strbuf_add_commented_lines(&summary, summary_content, len);
897 free(summary_content);
898 }
Matthieu Moy3ba74072013-09-06 19:43:06 +0200899
900 fputs(summary.buf, s->fp);
901 strbuf_release(&summary);
Ping Yinac8d5af2008-04-12 23:05:32 +0800902}
903
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400904static void wt_longstatus_print_other(struct wt_status *s,
905 struct string_list *l,
906 const char *what,
907 const char *how)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400908{
Jeff Kingc91f0d92006-09-08 04:05:34 -0400909 int i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500910 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700911 static struct string_list output = STRING_LIST_INIT_DUP;
912 struct column_options copts;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400913
Jeff King12829882011-06-02 01:54:49 -0400914 if (!l->nr)
Junio C Hamano76378682009-08-10 00:36:33 -0700915 return;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400916
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400917 wt_longstatus_print_other_header(s, what, how);
Junio C Hamano1b908b62010-04-10 00:19:46 -0700918
919 for (i = 0; i < l->nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700920 struct string_list_item *it;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700921 const char *path;
Junio C Hamano1b908b62010-04-10 00:19:46 -0700922 it = &(l->items[i]);
Jiang Xin39598f92013-06-25 23:53:45 +0800923 path = quote_path(it->string, s->prefix, &buf);
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700924 if (column_active(s->colopts)) {
925 string_list_append(&output, path);
926 continue;
927 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600928 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
929 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700930 "%s\n", path);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400931 }
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700932
933 strbuf_release(&buf);
934 if (!column_active(s->colopts))
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200935 goto conclude;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700936
Matthieu Moy2556b992013-09-06 19:43:07 +0200937 strbuf_addf(&buf, "%s%s\t%s",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700938 color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +0200939 s->display_comment_prefix ? "#" : "",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700940 color(WT_STATUS_UNTRACKED, s));
941 memset(&copts, 0, sizeof(copts));
942 copts.padding = 1;
943 copts.indent = buf.buf;
944 if (want_color(s->use_color))
945 copts.nl = GIT_COLOR_RESET "\n";
946 print_columns(&output, s->colopts, &copts);
947 string_list_clear(&output, 0);
Johannes Schindelin367c9882007-11-11 17:35:41 +0000948 strbuf_release(&buf);
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200949conclude:
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500950 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400951}
952
Brian Malehornd76650b2017-05-15 23:06:49 -0700953size_t wt_status_locate_end(const char *s, size_t len)
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100954{
955 const char *p;
956 struct strbuf pattern = STRBUF_INIT;
957
SZEDER Gáborfbfa0972015-06-09 02:28:34 +0200958 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
Brian Malehornd76650b2017-05-15 23:06:49 -0700959 if (starts_with(s, pattern.buf + 1))
960 len = 0;
961 else if ((p = strstr(s, pattern.buf)))
962 len = p - s + 1;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100963 strbuf_release(&pattern);
Brian Malehornd76650b2017-05-15 23:06:49 -0700964 return len;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100965}
966
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +0700967void wt_status_add_cut_line(FILE *fp)
968{
Kaartic Sivaraam8c4b1a32017-09-13 13:05:38 +0000969 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +0700970 struct strbuf buf = STRBUF_INIT;
971
972 fprintf(fp, "%c %s", comment_line_char, cut_line);
973 strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
974 fputs(buf.buf, fp);
975 strbuf_release(&buf);
976}
977
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400978static void wt_longstatus_print_verbose(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400979{
980 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800981 struct setup_revision_opt opt;
Michael J Gruber40555002015-03-06 10:43:35 +0100982 int dirty_submodules;
983 const char *c = color(WT_STATUS_HEADER, s);
Kristian Høgsberg99a12692007-11-21 21:54:49 -0500984
Jeff Kingc91f0d92006-09-08 04:05:34 -0400985 init_revisions(&rev, NULL);
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700986 rev.diffopt.flags.allow_textconv = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700987 rev.diffopt.ita_invisible_in_index = 1;
Junio C Hamano32962c92010-03-08 22:58:09 -0800988
989 memset(&opt, 0, sizeof(opt));
brian m. carlsonf2e51192018-05-02 00:26:00 +0000990 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
Junio C Hamano32962c92010-03-08 22:58:09 -0800991 setup_revisions(0, NULL, &rev, &opt);
992
Jeff Kingc91f0d92006-09-08 04:05:34 -0400993 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000994 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
995 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
996 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Kristian Høgsberg4ba0cb22008-03-10 13:58:26 -0400997 rev.diffopt.file = s->fp;
998 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 00:49:35 -0400999 /*
1000 * If we're not going to stdout, then we definitely don't
1001 * want color, since we are going to the commit message
1002 * file (and even the "auto" setting won't work, since it
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001003 * will have checked isatty on stdout). But we then do want
1004 * to insert the scissor line here to reliably remove the
1005 * diff before committing.
Jeff King4f672ad2008-10-26 00:49:35 -04001006 */
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001007 if (s->fp != stdout) {
Jeff Kingf1c96262011-08-17 22:03:12 -07001008 rev.diffopt.use_color = 0;
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001009 wt_status_add_cut_line(s->fp);
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001010 }
Michael J Gruber40555002015-03-06 10:43:35 +01001011 if (s->verbose > 1 && s->commitable) {
1012 /* print_updated() printed a header, so do we */
1013 if (s->fp != stdout)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001014 wt_longstatus_print_trailer(s);
Michael J Gruber40555002015-03-06 10:43:35 +01001015 status_printf_ln(s, c, _("Changes to be committed:"));
1016 rev.diffopt.a_prefix = "c/";
1017 rev.diffopt.b_prefix = "i/";
1018 } /* else use prefix as per user config */
Jeff Kingc91f0d92006-09-08 04:05:34 -04001019 run_diff_index(&rev, 1);
Michael J Gruber40555002015-03-06 10:43:35 +01001020 if (s->verbose > 1 &&
1021 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1022 status_printf_ln(s, c,
1023 "--------------------------------------------------");
1024 status_printf_ln(s, c, _("Changes not staged for commit:"));
1025 setup_work_tree();
1026 rev.diffopt.a_prefix = "i/";
1027 rev.diffopt.b_prefix = "w/";
1028 run_diff_files(&rev, 0);
1029 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001030}
1031
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001032static void wt_longstatus_print_tracking(struct wt_status *s)
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001033{
1034 struct strbuf sb = STRBUF_INIT;
René Scharfec72b49d2015-10-31 18:37:43 +01001035 const char *cp, *ep, *branch_name;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001036 struct branch *branch;
Matthieu Moy2556b992013-09-06 19:43:07 +02001037 char comment_line_string[3];
1038 int i;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001039
1040 assert(s->branch && !s->is_initial);
René Scharfec72b49d2015-10-31 18:37:43 +01001041 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001042 return;
René Scharfec72b49d2015-10-31 18:37:43 +01001043 branch = branch_get(branch_name);
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001044 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001045 return;
1046
Matthieu Moy2556b992013-09-06 19:43:07 +02001047 i = 0;
1048 if (s->display_comment_prefix) {
1049 comment_line_string[i++] = comment_line_char;
1050 comment_line_string[i++] = ' ';
1051 }
1052 comment_line_string[i] = '\0';
1053
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001054 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-09 21:59:30 -07001055 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001056 "%s%.*s", comment_line_string,
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001057 (int)(ep - cp), cp);
Matthieu Moy2556b992013-09-06 19:43:07 +02001058 if (s->display_comment_prefix)
1059 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1060 comment_line_char);
1061 else
Jeff King75177c82017-04-27 05:01:05 -04001062 fputs("\n", s->fp);
Rene Scharfeb6ec3072017-08-30 20:20:18 +02001063 strbuf_release(&sb);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001064}
1065
Lucien Kong83c750a2012-06-05 22:21:24 +02001066static int has_unmerged(struct wt_status *s)
1067{
1068 int i;
1069
1070 for (i = 0; i < s->change.nr; i++) {
1071 struct wt_status_change_data *d;
1072 d = s->change.items[i].util;
1073 if (d->stagemask)
1074 return 1;
1075 }
1076 return 0;
1077}
1078
1079static void show_merge_in_progress(struct wt_status *s,
1080 struct wt_status_state *state,
1081 const char *color)
1082{
1083 if (has_unmerged(s)) {
1084 status_printf_ln(s, color, _("You have unmerged paths."));
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001085 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001086 status_printf_ln(s, color,
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001087 _(" (fix conflicts and run \"git commit\")"));
1088 status_printf_ln(s, color,
1089 _(" (use \"git merge --abort\" to abort the merge)"));
1090 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001091 } else {
Stephen P. Smith8dc874b2016-02-15 19:38:25 -07001092 s-> commitable = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001093 status_printf_ln(s, color,
1094 _("All conflicts fixed but you are still merging."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001095 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001096 status_printf_ln(s, color,
1097 _(" (use \"git commit\" to conclude merge)"));
1098 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001099 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001100}
1101
1102static void show_am_in_progress(struct wt_status *s,
1103 struct wt_status_state *state,
1104 const char *color)
1105{
1106 status_printf_ln(s, color,
1107 _("You are in the middle of an am session."));
1108 if (state->am_empty_patch)
1109 status_printf_ln(s, color,
1110 _("The current patch is empty."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001111 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001112 if (!state->am_empty_patch)
1113 status_printf_ln(s, color,
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +03001114 _(" (fix conflicts and then run \"git am --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001115 status_printf_ln(s, color,
1116 _(" (use \"git am --skip\" to skip this patch)"));
1117 status_printf_ln(s, color,
1118 _(" (use \"git am --abort\" to restore the original branch)"));
1119 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001120 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001121}
1122
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001123static char *read_line_from_git_path(const char *filename)
1124{
1125 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +07001126 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1127
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001128 if (!fp) {
1129 strbuf_release(&buf);
1130 return NULL;
1131 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001132 strbuf_getline_lf(&buf, fp);
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001133 if (!fclose(fp)) {
1134 return strbuf_detach(&buf, NULL);
1135 } else {
1136 strbuf_release(&buf);
1137 return NULL;
1138 }
1139}
1140
1141static int split_commit_in_progress(struct wt_status *s)
1142{
1143 int split_in_progress = 0;
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001144 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001145
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001146 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001147 !s->branch || strcmp(s->branch, "HEAD"))
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001148 return 0;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001149
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001150 head = read_line_from_git_path("HEAD");
1151 orig_head = read_line_from_git_path("ORIG_HEAD");
1152 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1153 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1154
1155 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1156 ; /* fall through, no split in progress */
1157 else if (!strcmp(rebase_amend, rebase_orig_head))
1158 split_in_progress = !!strcmp(head, rebase_amend);
1159 else if (strcmp(orig_head, rebase_orig_head))
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001160 split_in_progress = 1;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001161
1162 free(head);
1163 free(orig_head);
1164 free(rebase_amend);
1165 free(rebase_orig_head);
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001166
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001167 return split_in_progress;
1168}
1169
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001170/*
1171 * Turn
1172 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1173 * into
1174 * "pick d6a2f03 some message"
1175 *
1176 * The function assumes that the line does not contain useless spaces
1177 * before or after the command.
1178 */
1179static void abbrev_sha1_in_line(struct strbuf *line)
1180{
1181 struct strbuf **split;
1182 int i;
1183
1184 if (starts_with(line->buf, "exec ") ||
1185 starts_with(line->buf, "x "))
1186 return;
1187
1188 split = strbuf_split_max(line, ' ', 3);
1189 if (split[0] && split[1]) {
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001190 struct object_id oid;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001191
1192 /*
1193 * strbuf_split_max left a space. Trim it and re-add
1194 * it after abbreviation.
1195 */
1196 strbuf_trim(split[1]);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001197 if (!get_oid(split[1]->buf, &oid)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001198 strbuf_reset(split[1]);
brian m. carlson30e677e2018-03-12 02:27:28 +00001199 strbuf_add_unique_abbrev(split[1], &oid,
René Scharfea94bb682016-10-08 17:38:47 +02001200 DEFAULT_ABBREV);
1201 strbuf_addch(split[1], ' ');
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001202 strbuf_reset(line);
1203 for (i = 0; split[i]; i++)
René Scharfe81099842016-07-19 20:36:29 +02001204 strbuf_addbuf(line, split[i]);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001205 }
1206 }
Stefan Beller6eb60782016-03-31 17:35:44 -07001207 strbuf_list_free(split);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001208}
1209
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001210static int read_rebase_todolist(const char *fname, struct string_list *lines)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001211{
1212 struct strbuf line = STRBUF_INIT;
1213 FILE *f = fopen(git_path("%s", fname), "r");
1214
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001215 if (!f) {
1216 if (errno == ENOENT)
1217 return -1;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001218 die_errno("Could not open file %s for reading",
1219 git_path("%s", fname));
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001220 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001221 while (!strbuf_getline_lf(&line, f)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001222 if (line.len && line.buf[0] == comment_line_char)
1223 continue;
1224 strbuf_trim(&line);
1225 if (!line.len)
1226 continue;
1227 abbrev_sha1_in_line(&line);
1228 string_list_append(lines, line.buf);
1229 }
Johannes Schindeline7b65e22017-05-04 15:55:52 +02001230 fclose(f);
Rene Scharfe6f495412017-08-30 20:20:17 +02001231 strbuf_release(&line);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001232 return 0;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001233}
1234
1235static void show_rebase_information(struct wt_status *s,
1236 struct wt_status_state *state,
1237 const char *color)
1238{
1239 if (state->rebase_interactive_in_progress) {
1240 int i;
1241 int nr_lines_to_show = 2;
1242
1243 struct string_list have_done = STRING_LIST_INIT_DUP;
1244 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1245
1246 read_rebase_todolist("rebase-merge/done", &have_done);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001247 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1248 &yet_to_do))
1249 status_printf_ln(s, color,
1250 _("git-rebase-todo is missing."));
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001251 if (have_done.nr == 0)
1252 status_printf_ln(s, color, _("No commands done."));
1253 else {
1254 status_printf_ln(s, color,
1255 Q_("Last command done (%d command done):",
1256 "Last commands done (%d commands done):",
1257 have_done.nr),
1258 have_done.nr);
1259 for (i = (have_done.nr > nr_lines_to_show)
1260 ? have_done.nr - nr_lines_to_show : 0;
1261 i < have_done.nr;
1262 i++)
1263 status_printf_ln(s, color, " %s", have_done.items[i].string);
1264 if (have_done.nr > nr_lines_to_show && s->hints)
1265 status_printf_ln(s, color,
1266 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1267 }
1268
1269 if (yet_to_do.nr == 0)
1270 status_printf_ln(s, color,
1271 _("No commands remaining."));
1272 else {
1273 status_printf_ln(s, color,
1274 Q_("Next command to do (%d remaining command):",
1275 "Next commands to do (%d remaining commands):",
1276 yet_to_do.nr),
1277 yet_to_do.nr);
1278 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1279 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1280 if (s->hints)
1281 status_printf_ln(s, color,
1282 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1283 }
1284 string_list_clear(&yet_to_do, 0);
1285 string_list_clear(&have_done, 0);
1286 }
1287}
1288
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001289static void print_rebase_state(struct wt_status *s,
1290 struct wt_status_state *state,
1291 const char *color)
1292{
1293 if (state->branch)
1294 status_printf_ln(s, color,
1295 _("You are currently rebasing branch '%s' on '%s'."),
1296 state->branch,
1297 state->onto);
1298 else
1299 status_printf_ln(s, color,
1300 _("You are currently rebasing."));
1301}
1302
Lucien Kong83c750a2012-06-05 22:21:24 +02001303static void show_rebase_in_progress(struct wt_status *s,
1304 struct wt_status_state *state,
1305 const char *color)
1306{
1307 struct stat st;
1308
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001309 show_rebase_information(s, state, color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001310 if (has_unmerged(s)) {
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001311 print_rebase_state(s, state, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001312 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001313 status_printf_ln(s, color,
1314 _(" (fix conflicts and then run \"git rebase --continue\")"));
1315 status_printf_ln(s, color,
1316 _(" (use \"git rebase --skip\" to skip this patch)"));
1317 status_printf_ln(s, color,
1318 _(" (use \"git rebase --abort\" to check out the original branch)"));
1319 }
Stefan Beller102de882018-05-17 15:51:51 -07001320 } else if (state->rebase_in_progress || !stat(git_path_merge_msg(the_repository), &st)) {
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001321 print_rebase_state(s, state, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001322 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001323 status_printf_ln(s, color,
1324 _(" (all conflicts fixed: run \"git rebase --continue\")"));
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001325 } else if (split_commit_in_progress(s)) {
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001326 if (state->branch)
1327 status_printf_ln(s, color,
1328 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1329 state->branch,
1330 state->onto);
1331 else
1332 status_printf_ln(s, color,
1333 _("You are currently splitting a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001334 if (s->hints)
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001335 status_printf_ln(s, color,
1336 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001337 } else {
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001338 if (state->branch)
1339 status_printf_ln(s, color,
1340 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1341 state->branch,
1342 state->onto);
1343 else
1344 status_printf_ln(s, color,
1345 _("You are currently editing a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001346 if (s->hints && !s->amend) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001347 status_printf_ln(s, color,
1348 _(" (use \"git commit --amend\" to amend the current commit)"));
1349 status_printf_ln(s, color,
1350 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1351 }
1352 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001353 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001354}
1355
1356static void show_cherry_pick_in_progress(struct wt_status *s,
1357 struct wt_status_state *state,
1358 const char *color)
1359{
Ralf Thielowbffd8092013-10-11 17:58:37 +02001360 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
brian m. carlsonaab95832018-03-12 02:27:30 +00001361 find_unique_abbrev(&state->cherry_pick_head_oid, DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001362 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001363 if (has_unmerged(s))
1364 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001365 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001366 else
1367 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001368 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1369 status_printf_ln(s, color,
1370 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001371 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001372 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001373}
1374
Matthieu Moydb4ef442013-04-02 16:20:21 +02001375static void show_revert_in_progress(struct wt_status *s,
1376 struct wt_status_state *state,
1377 const char *color)
1378{
Matthieu Moy87e139c2013-04-02 16:20:22 +02001379 status_printf_ln(s, color, _("You are currently reverting commit %s."),
brian m. carlsonaab95832018-03-12 02:27:30 +00001380 find_unique_abbrev(&state->revert_head_oid, DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001381 if (s->hints) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001382 if (has_unmerged(s))
1383 status_printf_ln(s, color,
1384 _(" (fix conflicts and run \"git revert --continue\")"));
1385 else
1386 status_printf_ln(s, color,
1387 _(" (all conflicts fixed: run \"git revert --continue\")"));
1388 status_printf_ln(s, color,
1389 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1390 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001391 wt_longstatus_print_trailer(s);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001392}
1393
Lucien Kong83c750a2012-06-05 22:21:24 +02001394static void show_bisect_in_progress(struct wt_status *s,
1395 struct wt_status_state *state,
1396 const char *color)
1397{
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001398 if (state->branch)
1399 status_printf_ln(s, color,
Nguyễn Thái Ngọc Duy6deab242013-03-23 10:52:44 +07001400 _("You are currently bisecting, started from branch '%s'."),
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001401 state->branch);
1402 else
1403 status_printf_ln(s, color,
1404 _("You are currently bisecting."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001405 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001406 status_printf_ln(s, color,
1407 _(" (use \"git bisect reset\" to get back to the original branch)"));
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001408 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001409}
1410
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001411/*
1412 * Extract branch information from rebase/bisect
1413 */
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001414static char *get_branch(const struct worktree *wt, const char *path)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001415{
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001416 struct strbuf sb = STRBUF_INIT;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001417 struct object_id oid;
René Scharfec72b49d2015-10-31 18:37:43 +01001418 const char *branch_name;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001419
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001420 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001421 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001422
Jeff King66ec9042015-01-28 12:57:35 -05001423 while (sb.len && sb.buf[sb.len - 1] == '\n')
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001424 strbuf_setlen(&sb, sb.len - 1);
1425 if (!sb.len)
1426 goto got_nothing;
René Scharfec72b49d2015-10-31 18:37:43 +01001427 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1428 strbuf_remove(&sb, 0, branch_name - sb.buf);
Christian Couder59556542013-11-30 21:55:40 +01001429 else if (starts_with(sb.buf, "refs/"))
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001430 ;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001431 else if (!get_oid_hex(sb.buf, &oid)) {
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001432 strbuf_reset(&sb);
brian m. carlson30e677e2018-03-12 02:27:28 +00001433 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001434 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1435 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001436 else /* bisect */
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001437 ;
1438 return strbuf_detach(&sb, NULL);
1439
1440got_nothing:
1441 strbuf_release(&sb);
1442 return NULL;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001443}
1444
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001445struct grab_1st_switch_cbdata {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001446 struct strbuf buf;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001447 struct object_id noid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001448};
1449
brian m. carlson9461d272017-02-21 23:47:32 +00001450static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +02001451 const char *email, timestamp_t timestamp, int tz,
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001452 const char *message, void *cb_data)
Lucien Kong83c750a2012-06-05 22:21:24 +02001453{
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001454 struct grab_1st_switch_cbdata *cb = cb_data;
1455 const char *target = NULL, *end;
1456
René Scharfec72b49d2015-10-31 18:37:43 +01001457 if (!skip_prefix(message, "checkout: moving from ", &message))
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001458 return 0;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001459 target = strstr(message, " to ");
1460 if (!target)
1461 return 0;
1462 target += strlen(" to ");
1463 strbuf_reset(&cb->buf);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001464 oidcpy(&cb->noid, noid);
René Scharfe904de442015-11-25 15:10:18 +01001465 end = strchrnul(target, '\n');
1466 strbuf_add(&cb->buf, target, end - target);
1467 if (!strcmp(cb->buf.buf, "HEAD")) {
Matthieu Moy0eb85482015-09-27 17:13:42 +02001468 /* HEAD is relative. Resolve it to the right reflog entry. */
René Scharfe904de442015-11-25 15:10:18 +01001469 strbuf_reset(&cb->buf);
brian m. carlson30e677e2018-03-12 02:27:28 +00001470 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
Matthieu Moy0eb85482015-09-27 17:13:42 +02001471 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001472 return 1;
1473}
1474
1475static void wt_status_get_detached_from(struct wt_status_state *state)
1476{
1477 struct grab_1st_switch_cbdata cb;
1478 struct commit *commit;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001479 struct object_id oid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001480 char *ref = NULL;
1481
1482 strbuf_init(&cb.buf, 0);
1483 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1484 strbuf_release(&cb.buf);
1485 return;
1486 }
1487
brian m. carlsoncca5fa62017-10-15 22:06:57 +00001488 if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref) == 1 &&
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001489 /* sha1 is a commit? match without further lookup */
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001490 (!oidcmp(&cb.noid, &oid) ||
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001491 /* perhaps sha1 is a tag, try to dereference to a commit */
Stefan Beller21e1ee82018-06-28 18:21:57 -07001492 ((commit = lookup_commit_reference_gently(the_repository, &oid, 1)) != NULL &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001493 !oidcmp(&cb.noid, &commit->object.oid)))) {
René Scharfec72b49d2015-10-31 18:37:43 +01001494 const char *from = ref;
1495 if (!skip_prefix(from, "refs/tags/", &from))
1496 skip_prefix(from, "refs/remotes/", &from);
1497 state->detached_from = xstrdup(from);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001498 } else
1499 state->detached_from =
brian m. carlsonaab95832018-03-12 02:27:30 +00001500 xstrdup(find_unique_abbrev(&cb.noid, DEFAULT_ABBREV));
brian m. carlson40f55552018-03-12 02:27:29 +00001501 oidcpy(&state->detached_oid, &cb.noid);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001502 state->detached_at = !get_oid("HEAD", &oid) &&
brian m. carlson40f55552018-03-12 02:27:29 +00001503 !oidcmp(&oid, &state->detached_oid);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001504
1505 free(ref);
1506 strbuf_release(&cb.buf);
1507}
1508
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001509int wt_status_check_rebase(const struct worktree *wt,
1510 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001511{
Lucien Kong83c750a2012-06-05 22:21:24 +02001512 struct stat st;
1513
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001514 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1515 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001516 state->am_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001517 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001518 state->am_empty_patch = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001519 } else {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001520 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001521 state->branch = get_branch(wt, "rebase-apply/head-name");
1522 state->onto = get_branch(wt, "rebase-apply/onto");
Lucien Kong83c750a2012-06-05 22:21:24 +02001523 }
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001524 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1525 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001526 state->rebase_interactive_in_progress = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001527 else
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001528 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001529 state->branch = get_branch(wt, "rebase-merge/head-name");
1530 state->onto = get_branch(wt, "rebase-merge/onto");
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001531 } else
1532 return 0;
1533 return 1;
1534}
1535
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001536int wt_status_check_bisect(const struct worktree *wt,
1537 struct wt_status_state *state)
1538{
1539 struct stat st;
1540
1541 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1542 state->bisect_in_progress = 1;
1543 state->branch = get_branch(wt, "BISECT_START");
1544 return 1;
1545 }
1546 return 0;
1547}
1548
Lucien Kong83c750a2012-06-05 22:21:24 +02001549void wt_status_get_state(struct wt_status_state *state,
1550 int get_detached_from)
1551{
1552 struct stat st;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001553 struct object_id oid;
Lucien Kong83c750a2012-06-05 22:21:24 +02001554
Stefan Beller102de882018-05-17 15:51:51 -07001555 if (!stat(git_path_merge_head(the_repository), &st)) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001556 state->merge_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001557 } else if (wt_status_check_rebase(NULL, state)) {
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001558 ; /* all set */
Stefan Beller102de882018-05-17 15:51:51 -07001559 } else if (!stat(git_path_cherry_pick_head(the_repository), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001560 !get_oid("CHERRY_PICK_HEAD", &oid)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001561 state->cherry_pick_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001562 oidcpy(&state->cherry_pick_head_oid, &oid);
Lucien Kong83c750a2012-06-05 22:21:24 +02001563 }
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001564 wt_status_check_bisect(NULL, state);
Stefan Beller102de882018-05-17 15:51:51 -07001565 if (!stat(git_path_revert_head(the_repository), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001566 !get_oid("REVERT_HEAD", &oid)) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001567 state->revert_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001568 oidcpy(&state->revert_head_oid, &oid);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001569 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001570
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001571 if (get_detached_from)
1572 wt_status_get_detached_from(state);
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001573}
1574
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001575static void wt_longstatus_print_state(struct wt_status *s,
1576 struct wt_status_state *state)
Lucien Kong83c750a2012-06-05 22:21:24 +02001577{
1578 const char *state_color = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001579 if (state->merge_in_progress)
1580 show_merge_in_progress(s, state, state_color);
1581 else if (state->am_in_progress)
1582 show_am_in_progress(s, state, state_color);
1583 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1584 show_rebase_in_progress(s, state, state_color);
1585 else if (state->cherry_pick_in_progress)
1586 show_cherry_pick_in_progress(s, state, state_color);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001587 else if (state->revert_in_progress)
1588 show_revert_in_progress(s, state, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001589 if (state->bisect_in_progress)
1590 show_bisect_in_progress(s, state, state_color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001591}
1592
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001593static void wt_longstatus_print(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001594{
Aleksi Aalto1d282322010-11-18 01:40:05 +02001595 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1596 const char *branch_status_color = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001597 struct wt_status_state state;
1598
1599 memset(&state, 0, sizeof(state));
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001600 wt_status_get_state(&state,
1601 s->branch && !strcmp(s->branch, "HEAD"));
Jürgen Rühle98bf8a42007-01-02 20:26:23 +01001602
Junio C Hamanobda324c2007-01-03 01:09:34 -08001603 if (s->branch) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001604 const char *on_what = _("On branch ");
Junio C Hamanobda324c2007-01-03 01:09:34 -08001605 const char *branch_name = s->branch;
René Scharfec72b49d2015-10-31 18:37:43 +01001606 if (!strcmp(branch_name, "HEAD")) {
Aleksi Aalto1d282322010-11-18 01:40:05 +02001607 branch_status_color = color(WT_STATUS_NOBRANCH, s);
Ramkumar Ramachandraec506312013-06-16 14:15:15 +05301608 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
Guillaume Pagèsdf25e942015-06-30 15:01:13 +02001609 if (state.rebase_interactive_in_progress)
1610 on_what = _("interactive rebase in progress; onto ");
1611 else
1612 on_what = _("rebase in progress; onto ");
Ramkumar Ramachandraec506312013-06-16 14:15:15 +05301613 branch_name = state.onto;
1614 } else if (state.detached_from) {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001615 branch_name = state.detached_from;
Michael J Gruber970399e2015-03-06 16:04:06 +01001616 if (state.detached_at)
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001617 on_what = _("HEAD detached at ");
1618 else
1619 on_what = _("HEAD detached from ");
1620 } else {
1621 branch_name = "";
1622 on_what = _("Not currently on any branch.");
1623 }
René Scharfec72b49d2015-10-31 18:37:43 +01001624 } else
1625 skip_prefix(branch_name, "refs/heads/", &branch_name);
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001626 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001627 status_printf_more(s, branch_status_color, "%s", on_what);
1628 status_printf_more(s, branch_color, "%s\n", branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001629 if (!s->is_initial)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001630 wt_longstatus_print_tracking(s);
Junio C Hamanobda324c2007-01-03 01:09:34 -08001631 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001632
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001633 wt_longstatus_print_state(s, &state);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001634 free(state.branch);
1635 free(state.onto);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001636 free(state.detached_from);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001637
Jeff Kingc91f0d92006-09-08 04:05:34 -04001638 if (s->is_initial) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001639 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301640 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1641 s->commit_template
1642 ? _("Initial commit")
1643 : _("No commits yet"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001644 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001645 }
1646
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001647 wt_longstatus_print_updated(s);
1648 wt_longstatus_print_unmerged(s);
1649 wt_longstatus_print_changed(s);
Jens Lehmann46a958b2010-06-25 16:56:47 +02001650 if (s->submodule_summary &&
1651 (!s->ignore_submodule_arg ||
1652 strcmp(s->ignore_submodule_arg, "all"))) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001653 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1654 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
Jens Lehmannf17a5d32010-01-17 20:42:31 +01001655 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001656 if (s->show_untracked_files) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001657 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
Jameson Millereec0f7f2017-10-30 13:21:37 -04001658 if (s->show_ignored_mode)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001659 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001660 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001661 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001662 status_printf_ln(s, GIT_COLOR_NORMAL,
Jiang Xin62901172013-04-12 11:53:01 +08001663 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1664 "may speed it up, but you have to be careful not to forget to add\n"
1665 "new files yourself (see 'git help status')."),
1666 s->untracked_in_ms / 1000.0);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001667 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001668 } else if (s->commitable)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001669 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
Matthieu Moy6a964f52013-09-12 12:50:05 +02001670 s->hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001671 ? _(" (use -u option to show untracked files)") : "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001672
Jeff King1324fb62008-11-12 03:23:37 -05001673 if (s->verbose)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001674 wt_longstatus_print_verbose(s);
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001675 if (!s->commitable) {
1676 if (s->amend)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001677 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
Junio C Hamano37d07f82007-12-12 19:09:16 -08001678 else if (s->nowarn)
1679 ; /* nothing */
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001680 else if (s->workdir_dirty) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001681 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001682 printf(_("no changes added to commit "
1683 "(use \"git add\" and/or \"git commit -a\")\n"));
1684 else
1685 printf(_("no changes added to commit\n"));
1686 } else if (s->untracked.nr) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001687 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001688 printf(_("nothing added to commit but untracked files "
1689 "present (use \"git add\" to track)\n"));
1690 else
1691 printf(_("nothing added to commit but untracked files present\n"));
1692 } else if (s->is_initial) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001693 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001694 printf(_("nothing to commit (create/copy files "
1695 "and use \"git add\" to track)\n"));
1696 else
1697 printf(_("nothing to commit\n"));
1698 } else if (!s->show_untracked_files) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001699 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001700 printf(_("nothing to commit (use -u to show untracked files)\n"));
1701 else
1702 printf(_("nothing to commit\n"));
1703 } else
Lars Vogel2a0e6cd2016-06-09 20:19:30 +02001704 printf(_("nothing to commit, working tree clean\n"));
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001705 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001706 if(s->show_stash)
1707 wt_longstatus_print_stash_summary(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001708}
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001709
Jeff King3207a3a2012-05-07 15:44:44 -04001710static void wt_shortstatus_unmerged(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001711 struct wt_status *s)
1712{
1713 struct wt_status_change_data *d = it->util;
1714 const char *how = "??";
1715
1716 switch (d->stagemask) {
1717 case 1: how = "DD"; break; /* both deleted */
1718 case 2: how = "AU"; break; /* added by us */
1719 case 3: how = "UD"; break; /* deleted by them */
1720 case 4: how = "UA"; break; /* added by them */
1721 case 5: how = "DU"; break; /* deleted by us */
1722 case 6: how = "AA"; break; /* both added */
1723 case 7: how = "UU"; break; /* both modified */
1724 }
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001725 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
Jeff King3207a3a2012-05-07 15:44:44 -04001726 if (s->null_termination) {
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001727 fprintf(stdout, " %s%c", it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001728 } else {
1729 struct strbuf onebuf = STRBUF_INIT;
1730 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001731 one = quote_path(it->string, s->prefix, &onebuf);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001732 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001733 strbuf_release(&onebuf);
1734 }
1735}
1736
Jeff King3207a3a2012-05-07 15:44:44 -04001737static void wt_shortstatus_status(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001738 struct wt_status *s)
1739{
1740 struct wt_status_change_data *d = it->util;
1741
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001742 if (d->index_status)
1743 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1744 else
1745 putchar(' ');
1746 if (d->worktree_status)
1747 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1748 else
1749 putchar(' ');
1750 putchar(' ');
Jeff King3207a3a2012-05-07 15:44:44 -04001751 if (s->null_termination) {
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001752 fprintf(stdout, "%s%c", it->string, 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07001753 if (d->rename_source)
1754 fprintf(stdout, "%s%c", d->rename_source, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001755 } else {
1756 struct strbuf onebuf = STRBUF_INIT;
1757 const char *one;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07001758
1759 if (d->rename_source) {
1760 one = quote_path(d->rename_source, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001761 if (*one != '"' && strchr(one, ' ') != NULL) {
1762 putchar('"');
1763 strbuf_addch(&onebuf, '"');
1764 one = onebuf.buf;
1765 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001766 printf("%s -> ", one);
1767 strbuf_release(&onebuf);
1768 }
Jiang Xin39598f92013-06-25 23:53:45 +08001769 one = quote_path(it->string, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001770 if (*one != '"' && strchr(one, ' ') != NULL) {
1771 putchar('"');
1772 strbuf_addch(&onebuf, '"');
1773 one = onebuf.buf;
1774 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001775 printf("%s\n", one);
1776 strbuf_release(&onebuf);
1777 }
1778}
1779
Jeff King3207a3a2012-05-07 15:44:44 -04001780static void wt_shortstatus_other(struct string_list_item *it,
Junio C Hamano2381e392010-04-10 00:33:17 -07001781 struct wt_status *s, const char *sign)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001782{
Jeff King3207a3a2012-05-07 15:44:44 -04001783 if (s->null_termination) {
Junio C Hamano2381e392010-04-10 00:33:17 -07001784 fprintf(stdout, "%s %s%c", sign, it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001785 } else {
1786 struct strbuf onebuf = STRBUF_INIT;
1787 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001788 one = quote_path(it->string, s->prefix, &onebuf);
Junio C Hamanoc1909e72010-05-01 22:05:14 -07001789 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001790 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001791 strbuf_release(&onebuf);
1792 }
1793}
1794
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001795static void wt_shortstatus_print_tracking(struct wt_status *s)
1796{
1797 struct branch *branch;
1798 const char *header_color = color(WT_STATUS_HEADER, s);
1799 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1800 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1801
1802 const char *base;
René Scharfe5e8d2722017-07-08 12:51:01 +02001803 char *short_base;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001804 const char *branch_name;
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001805 int num_ours, num_theirs, sti;
Jiang Xinf2e08732013-08-26 15:02:48 +08001806 int upstream_is_gone = 0;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001807
1808 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1809
1810 if (!s->branch)
1811 return;
1812 branch_name = s->branch;
1813
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001814#define LABEL(string) (s->no_gettext ? (string) : _(string))
1815
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001816 if (s->is_initial)
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301817 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001818
René Scharfebaf0a3e2015-10-31 18:36:35 +01001819 if (!strcmp(s->branch, "HEAD")) {
1820 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001821 LABEL(N_("HEAD (no branch)")));
René Scharfebaf0a3e2015-10-31 18:36:35 +01001822 goto conclude;
1823 }
1824
René Scharfec72b49d2015-10-31 18:37:43 +01001825 skip_prefix(branch_name, "refs/heads/", &branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001826
René Scharfe8d8325f2015-10-31 18:37:12 +01001827 branch = branch_get(branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001828
Jiang Xinf2e08732013-08-26 15:02:48 +08001829 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1830
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001831 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
1832 s->ahead_behind_flags);
1833 if (sti < 0) {
René Scharfebcf8cc22015-10-31 18:36:01 +01001834 if (!base)
1835 goto conclude;
Jeff King979cb242015-05-21 20:49:11 -04001836
Jiang Xinf2e08732013-08-26 15:02:48 +08001837 upstream_is_gone = 1;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001838 }
1839
René Scharfe5e8d2722017-07-08 12:51:01 +02001840 short_base = shorten_unambiguous_ref(base, 0);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001841 color_fprintf(s->fp, header_color, "...");
René Scharfe5e8d2722017-07-08 12:51:01 +02001842 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
1843 free(short_base);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001844
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001845 if (!upstream_is_gone && !sti)
René Scharfebcf8cc22015-10-31 18:36:01 +01001846 goto conclude;
Jiang Xinf2234592013-08-26 15:02:49 +08001847
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001848 color_fprintf(s->fp, header_color, " [");
Jiang Xinf2e08732013-08-26 15:02:48 +08001849 if (upstream_is_gone) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001850 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001851 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
1852 color_fprintf(s->fp, header_color, LABEL(N_("different")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001853 } else if (!num_ours) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001854 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001855 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1856 } else if (!num_theirs) {
Michael J Gruberdf227242016-03-14 16:30:33 +01001857 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001858 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1859 } else {
Michael J Gruberdf227242016-03-14 16:30:33 +01001860 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001861 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
Matthieu Moy7a76c282014-03-20 13:12:41 +01001862 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001863 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1864 }
1865
Jeff Kinga5985232012-05-07 17:02:18 -04001866 color_fprintf(s->fp, header_color, "]");
René Scharfebcf8cc22015-10-31 18:36:01 +01001867 conclude:
Jeff Kinga5985232012-05-07 17:02:18 -04001868 fputc(s->null_termination ? '\0' : '\n', s->fp);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001869}
1870
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001871static void wt_shortstatus_print(struct wt_status *s)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001872{
Stefan Bellerd4aae452017-03-16 14:36:19 -07001873 struct string_list_item *it;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001874
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001875 if (s->show_branch)
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001876 wt_shortstatus_print_tracking(s);
1877
Stefan Bellerd4aae452017-03-16 14:36:19 -07001878 for_each_string_list_item(it, &s->change) {
1879 struct wt_status_change_data *d = it->util;
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001880
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001881 if (d->stagemask)
Jeff King3207a3a2012-05-07 15:44:44 -04001882 wt_shortstatus_unmerged(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001883 else
Jeff King3207a3a2012-05-07 15:44:44 -04001884 wt_shortstatus_status(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001885 }
Stefan Bellerd4aae452017-03-16 14:36:19 -07001886 for_each_string_list_item(it, &s->untracked)
Jeff King3207a3a2012-05-07 15:44:44 -04001887 wt_shortstatus_other(it, s, "??");
Junio C Hamano2381e392010-04-10 00:33:17 -07001888
Stefan Bellerd4aae452017-03-16 14:36:19 -07001889 for_each_string_list_item(it, &s->ignored)
Jeff King3207a3a2012-05-07 15:44:44 -04001890 wt_shortstatus_other(it, s, "!!");
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001891}
Jeff King4a7cc2f2009-12-07 00:17:15 -05001892
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001893static void wt_porcelain_print(struct wt_status *s)
Jeff King4a7cc2f2009-12-07 00:17:15 -05001894{
1895 s->use_color = 0;
Jeff King86617682009-12-07 00:26:25 -05001896 s->relative_paths = 0;
1897 s->prefix = NULL;
Matthieu Moy7a76c282014-03-20 13:12:41 +01001898 s->no_gettext = 1;
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001899 wt_shortstatus_print(s);
Jeff King4a7cc2f2009-12-07 00:17:15 -05001900}
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001901
Jeff Hostetler24959ba2016-08-11 10:45:58 -04001902/*
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001903 * Print branch information for porcelain v2 output. These lines
1904 * are printed when the '--branch' parameter is given.
1905 *
1906 * # branch.oid <commit><eol>
1907 * # branch.head <head><eol>
1908 * [# branch.upstream <upstream><eol>
1909 * [# branch.ab +<ahead> -<behind><eol>]]
1910 *
1911 * <commit> ::= the current commit hash or the the literal
1912 * "(initial)" to indicate an initialized repo
1913 * with no commits.
1914 *
1915 * <head> ::= <branch_name> the current branch name or
1916 * "(detached)" literal when detached head or
1917 * "(unknown)" when something is wrong.
1918 *
1919 * <upstream> ::= the upstream branch name, when set.
1920 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001921 * <ahead> ::= integer ahead value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001922 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001923 * <behind> ::= integer behind value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001924 *
1925 * The end-of-line is defined by the -z flag.
1926 *
1927 * <eol> ::= NUL when -z,
1928 * LF when NOT -z.
1929 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001930 * When an upstream is set and present, the 'branch.ab' line will
1931 * be printed with the ahead/behind counts for the branch and the
1932 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1933 * are different, '?' will be substituted for the actual count.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001934 */
1935static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1936{
1937 struct branch *branch;
1938 const char *base;
1939 const char *branch_name;
1940 struct wt_status_state state;
1941 int ab_info, nr_ahead, nr_behind;
1942 char eol = s->null_termination ? '\0' : '\n';
1943
1944 memset(&state, 0, sizeof(state));
1945 wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1946
1947 fprintf(s->fp, "# branch.oid %s%c",
1948 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1949 eol);
1950
1951 if (!s->branch)
1952 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1953 else {
1954 if (!strcmp(s->branch, "HEAD")) {
1955 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1956
1957 if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1958 branch_name = state.onto;
1959 else if (state.detached_from)
1960 branch_name = state.detached_from;
1961 else
1962 branch_name = "";
1963 } else {
1964 branch_name = NULL;
1965 skip_prefix(s->branch, "refs/heads/", &branch_name);
1966
1967 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1968 }
1969
1970 /* Lookup stats on the upstream tracking branch, if set. */
1971 branch = branch_get(branch_name);
1972 base = NULL;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001973 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
1974 &base, s->ahead_behind_flags);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001975 if (base) {
1976 base = shorten_unambiguous_ref(base, 0);
1977 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1978 free((char *)base);
1979
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001980 if (ab_info > 0) {
1981 /* different */
1982 if (nr_ahead || nr_behind)
1983 fprintf(s->fp, "# branch.ab +%d -%d%c",
1984 nr_ahead, nr_behind, eol);
1985 else
1986 fprintf(s->fp, "# branch.ab +? -?%c",
1987 eol);
1988 } else if (!ab_info) {
1989 /* same */
1990 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
1991 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001992 }
1993 }
1994
1995 free(state.branch);
1996 free(state.onto);
1997 free(state.detached_from);
1998}
1999
2000/*
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002001 * Convert various submodule status values into a
2002 * fixed-length string of characters in the buffer provided.
2003 */
2004static void wt_porcelain_v2_submodule_state(
2005 struct wt_status_change_data *d,
2006 char sub[5])
2007{
2008 if (S_ISGITLINK(d->mode_head) ||
2009 S_ISGITLINK(d->mode_index) ||
2010 S_ISGITLINK(d->mode_worktree)) {
2011 sub[0] = 'S';
2012 sub[1] = d->new_submodule_commits ? 'C' : '.';
2013 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2014 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2015 } else {
2016 sub[0] = 'N';
2017 sub[1] = '.';
2018 sub[2] = '.';
2019 sub[3] = '.';
2020 }
2021 sub[4] = 0;
2022}
2023
2024/*
2025 * Fix-up changed entries before we print them.
2026 */
2027static void wt_porcelain_v2_fix_up_changed(
2028 struct string_list_item *it,
2029 struct wt_status *s)
2030{
2031 struct wt_status_change_data *d = it->util;
2032
2033 if (!d->index_status) {
2034 /*
2035 * This entry is unchanged in the index (relative to the head).
2036 * Therefore, the collect_updated_cb was never called for this
2037 * entry (during the head-vs-index scan) and so the head column
2038 * fields were never set.
2039 *
2040 * We must have data for the index column (from the
2041 * index-vs-worktree scan (otherwise, this entry should not be
2042 * in the list of changes)).
2043 *
2044 * Copy index column fields to the head column, so that our
2045 * output looks complete.
2046 */
2047 assert(d->mode_head == 0);
2048 d->mode_head = d->mode_index;
2049 oidcpy(&d->oid_head, &d->oid_index);
2050 }
2051
2052 if (!d->worktree_status) {
2053 /*
2054 * This entry is unchanged in the worktree (relative to the index).
2055 * Therefore, the collect_changed_cb was never called for this entry
2056 * (during the index-vs-worktree scan) and so the worktree column
2057 * fields were never set.
2058 *
2059 * We must have data for the index column (from the head-vs-index
2060 * scan).
2061 *
2062 * Copy the index column fields to the worktree column so that
2063 * our output looks complete.
2064 *
2065 * Note that we only have a mode field in the worktree column
2066 * because the scan code tries really hard to not have to compute it.
2067 */
2068 assert(d->mode_worktree == 0);
2069 d->mode_worktree = d->mode_index;
2070 }
2071}
2072
2073/*
2074 * Print porcelain v2 info for tracked entries with changes.
2075 */
2076static void wt_porcelain_v2_print_changed_entry(
2077 struct string_list_item *it,
2078 struct wt_status *s)
2079{
2080 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002081 struct strbuf buf = STRBUF_INIT;
2082 struct strbuf buf_from = STRBUF_INIT;
2083 const char *path = NULL;
2084 const char *path_from = NULL;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002085 char key[3];
2086 char submodule_token[5];
2087 char sep_char, eol_char;
2088
2089 wt_porcelain_v2_fix_up_changed(it, s);
2090 wt_porcelain_v2_submodule_state(d, submodule_token);
2091
2092 key[0] = d->index_status ? d->index_status : '.';
2093 key[1] = d->worktree_status ? d->worktree_status : '.';
2094 key[2] = 0;
2095
2096 if (s->null_termination) {
2097 /*
2098 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2099 * A single NUL character separates them.
2100 */
2101 sep_char = '\0';
2102 eol_char = '\0';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002103 path = it->string;
2104 path_from = d->rename_source;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002105 } else {
2106 /*
2107 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2108 * The source path is only present when necessary.
2109 * A single TAB separates them (because paths can contain spaces
2110 * which are not escaped and C-quoting does escape TAB characters).
2111 */
2112 sep_char = '\t';
2113 eol_char = '\n';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002114 path = quote_path(it->string, s->prefix, &buf);
2115 if (d->rename_source)
2116 path_from = quote_path(d->rename_source, s->prefix, &buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002117 }
2118
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002119 if (path_from)
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002120 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2121 key, submodule_token,
2122 d->mode_head, d->mode_index, d->mode_worktree,
2123 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002124 d->rename_status, d->rename_score,
2125 path, sep_char, path_from, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002126 else
2127 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2128 key, submodule_token,
2129 d->mode_head, d->mode_index, d->mode_worktree,
2130 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002131 path, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002132
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002133 strbuf_release(&buf);
2134 strbuf_release(&buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002135}
2136
2137/*
2138 * Print porcelain v2 status info for unmerged entries.
2139 */
2140static void wt_porcelain_v2_print_unmerged_entry(
2141 struct string_list_item *it,
2142 struct wt_status *s)
2143{
2144 struct wt_status_change_data *d = it->util;
2145 const struct cache_entry *ce;
2146 struct strbuf buf_index = STRBUF_INIT;
2147 const char *path_index = NULL;
2148 int pos, stage, sum;
2149 struct {
2150 int mode;
2151 struct object_id oid;
2152 } stages[3];
2153 char *key;
2154 char submodule_token[5];
2155 char unmerged_prefix = 'u';
2156 char eol_char = s->null_termination ? '\0' : '\n';
2157
2158 wt_porcelain_v2_submodule_state(d, submodule_token);
2159
2160 switch (d->stagemask) {
2161 case 1: key = "DD"; break; /* both deleted */
2162 case 2: key = "AU"; break; /* added by us */
2163 case 3: key = "UD"; break; /* deleted by them */
2164 case 4: key = "UA"; break; /* added by them */
2165 case 5: key = "DU"; break; /* deleted by us */
2166 case 6: key = "AA"; break; /* both added */
2167 case 7: key = "UU"; break; /* both modified */
2168 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002169 BUG("unhandled unmerged status %x", d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002170 }
2171
2172 /*
2173 * Disregard d.aux.porcelain_v2 data that we accumulated
2174 * for the head and index columns during the scans and
2175 * replace with the actual stage data.
2176 *
2177 * Note that this is a last-one-wins for each the individual
2178 * stage [123] columns in the event of multiple cache entries
2179 * for same stage.
2180 */
2181 memset(stages, 0, sizeof(stages));
2182 sum = 0;
2183 pos = cache_name_pos(it->string, strlen(it->string));
2184 assert(pos < 0);
2185 pos = -pos-1;
2186 while (pos < active_nr) {
2187 ce = active_cache[pos++];
2188 stage = ce_stage(ce);
2189 if (strcmp(ce->name, it->string) || !stage)
2190 break;
2191 stages[stage - 1].mode = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +01002192 oidcpy(&stages[stage - 1].oid, &ce->oid);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002193 sum |= (1 << (stage - 1));
2194 }
2195 if (sum != d->stagemask)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002196 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002197
2198 if (s->null_termination)
2199 path_index = it->string;
2200 else
2201 path_index = quote_path(it->string, s->prefix, &buf_index);
2202
2203 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2204 unmerged_prefix, key, submodule_token,
2205 stages[0].mode, /* stage 1 */
2206 stages[1].mode, /* stage 2 */
2207 stages[2].mode, /* stage 3 */
2208 d->mode_worktree,
2209 oid_to_hex(&stages[0].oid), /* stage 1 */
2210 oid_to_hex(&stages[1].oid), /* stage 2 */
2211 oid_to_hex(&stages[2].oid), /* stage 3 */
2212 path_index,
2213 eol_char);
2214
2215 strbuf_release(&buf_index);
2216}
2217
2218/*
2219 * Print porcelain V2 status info for untracked and ignored entries.
2220 */
2221static void wt_porcelain_v2_print_other(
2222 struct string_list_item *it,
2223 struct wt_status *s,
2224 char prefix)
2225{
2226 struct strbuf buf = STRBUF_INIT;
2227 const char *path;
2228 char eol_char;
2229
2230 if (s->null_termination) {
2231 path = it->string;
2232 eol_char = '\0';
2233 } else {
2234 path = quote_path(it->string, s->prefix, &buf);
2235 eol_char = '\n';
2236 }
2237
2238 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2239
2240 strbuf_release(&buf);
2241}
2242
2243/*
2244 * Print porcelain V2 status.
2245 *
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002246 * [<v2_branch>]
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002247 * [<v2_changed_items>]*
2248 * [<v2_unmerged_items>]*
2249 * [<v2_untracked_items>]*
2250 * [<v2_ignored_items>]*
2251 *
2252 */
2253static void wt_porcelain_v2_print(struct wt_status *s)
2254{
2255 struct wt_status_change_data *d;
2256 struct string_list_item *it;
2257 int i;
2258
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002259 if (s->show_branch)
2260 wt_porcelain_v2_print_tracking(s);
2261
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002262 for (i = 0; i < s->change.nr; i++) {
2263 it = &(s->change.items[i]);
2264 d = it->util;
2265 if (!d->stagemask)
2266 wt_porcelain_v2_print_changed_entry(it, s);
2267 }
2268
2269 for (i = 0; i < s->change.nr; i++) {
2270 it = &(s->change.items[i]);
2271 d = it->util;
2272 if (d->stagemask)
2273 wt_porcelain_v2_print_unmerged_entry(it, s);
2274 }
2275
2276 for (i = 0; i < s->untracked.nr; i++) {
2277 it = &(s->untracked.items[i]);
2278 wt_porcelain_v2_print_other(it, s, '?');
2279 }
2280
2281 for (i = 0; i < s->ignored.nr; i++) {
2282 it = &(s->ignored.items[i]);
2283 wt_porcelain_v2_print_other(it, s, '!');
2284 }
2285}
2286
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002287void wt_status_print(struct wt_status *s)
2288{
2289 switch (s->status_format) {
2290 case STATUS_FORMAT_SHORT:
2291 wt_shortstatus_print(s);
2292 break;
2293 case STATUS_FORMAT_PORCELAIN:
2294 wt_porcelain_print(s);
2295 break;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002296 case STATUS_FORMAT_PORCELAIN_V2:
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002297 wt_porcelain_v2_print(s);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002298 break;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002299 case STATUS_FORMAT_UNSPECIFIED:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002300 BUG("finalize_deferred_config() should have been called");
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002301 break;
2302 case STATUS_FORMAT_NONE:
2303 case STATUS_FORMAT_LONG:
2304 wt_longstatus_print(s);
2305 break;
2306 }
2307}
Johannes Schindelinfd849862016-10-07 18:08:38 +02002308
2309/**
2310 * Returns 1 if there are unstaged changes, 0 otherwise.
2311 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002312int has_unstaged_changes(int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002313{
2314 struct rev_info rev_info;
2315 int result;
2316
2317 init_revisions(&rev_info, NULL);
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002318 if (ignore_submodules) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002319 rev_info.diffopt.flags.ignore_submodules = 1;
Junio C Hamanob50d82b2017-11-15 12:14:30 +09002320 rev_info.diffopt.flags.override_submodule_config = 1;
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002321 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002322 rev_info.diffopt.flags.quick = 1;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002323 diff_setup_done(&rev_info.diffopt);
2324 result = run_diff_files(&rev_info, 0);
2325 return diff_result_code(&rev_info.diffopt, result);
2326}
2327
2328/**
2329 * Returns 1 if there are uncommitted changes, 0 otherwise.
2330 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002331int has_uncommitted_changes(int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002332{
2333 struct rev_info rev_info;
2334 int result;
2335
2336 if (is_cache_unborn())
2337 return 0;
2338
2339 init_revisions(&rev_info, NULL);
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002340 if (ignore_submodules)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002341 rev_info.diffopt.flags.ignore_submodules = 1;
2342 rev_info.diffopt.flags.quick = 1;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002343 add_head_to_pending(&rev_info);
2344 diff_setup_done(&rev_info.diffopt);
2345 result = run_diff_index(&rev_info, 1);
2346 return diff_result_code(&rev_info.diffopt, result);
2347}
2348
2349/**
2350 * If the work tree has unstaged or uncommitted changes, dies with the
2351 * appropriate message.
2352 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002353int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002354{
Martin Ågren837e34e2017-10-05 22:32:04 +02002355 struct lock_file lock_file = LOCK_INIT;
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002356 int err = 0, fd;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002357
Martin Ågren837e34e2017-10-05 22:32:04 +02002358 fd = hold_locked_index(&lock_file, 0);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002359 refresh_cache(REFRESH_QUIET);
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002360 if (0 <= fd)
Martin Ågren837e34e2017-10-05 22:32:04 +02002361 update_index_if_able(&the_index, &lock_file);
2362 rollback_lock_file(&lock_file);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002363
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002364 if (has_unstaged_changes(ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002365 /* TRANSLATORS: the action is e.g. "pull with rebase" */
Johannes Schindelin4777e172016-10-07 18:09:04 +02002366 error(_("cannot %s: You have unstaged changes."), _(action));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002367 err = 1;
2368 }
2369
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002370 if (has_uncommitted_changes(ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002371 if (err)
Johannes Schindelin4777e172016-10-07 18:09:04 +02002372 error(_("additionally, your index contains uncommitted changes."));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002373 else
Johannes Schindelin4777e172016-10-07 18:09:04 +02002374 error(_("cannot %s: Your index contains uncommitted changes."),
Johannes Schindelinfd849862016-10-07 18:08:38 +02002375 _(action));
2376 err = 1;
2377 }
2378
2379 if (err) {
2380 if (hint)
2381 error("%s", hint);
2382 if (!gently)
2383 exit(128);
2384 }
2385
2386 return err;
2387}