blob: 66f4234af1149618b47786f3b623916be7c02c74 [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;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400141}
142
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400143static void wt_longstatus_print_unmerged_header(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700144{
Lucien Kong96b0ec12012-06-05 22:21:26 +0200145 int i;
146 int del_mod_conflict = 0;
147 int both_deleted = 0;
148 int not_deleted = 0;
Junio C Hamanod249b092009-08-09 21:59:30 -0700149 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800150
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000151 status_printf_ln(s, c, _("Unmerged paths:"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200152
153 for (i = 0; i < s->change.nr; i++) {
154 struct string_list_item *it = &(s->change.items[i]);
155 struct wt_status_change_data *d = it->util;
156
157 switch (d->stagemask) {
158 case 0:
159 break;
160 case 1:
161 both_deleted = 1;
162 break;
163 case 3:
164 case 5:
165 del_mod_conflict = 1;
166 break;
167 default:
168 not_deleted = 1;
169 break;
170 }
171 }
172
Matthieu Moy6a964f52013-09-12 12:50:05 +0200173 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400174 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500175 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800176 ;
177 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000178 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700179 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000180 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200181
182 if (!both_deleted) {
183 if (!del_mod_conflict)
184 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
185 else
186 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
187 } else if (!del_mod_conflict && !not_deleted) {
188 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
189 } else {
190 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
191 }
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500192 status_printf_ln(s, c, "%s", "");
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700193}
194
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400195static void wt_longstatus_print_cached_header(struct wt_status *s)
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100196{
Junio C Hamanod249b092009-08-09 21:59:30 -0700197 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800198
Ævar Arnfjörð Bjarmason919a4ce2011-02-22 23:42:16 +0000199 status_printf_ln(s, c, _("Changes to be committed:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200200 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400201 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500202 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800203 ; /* NEEDSWORK: use "git reset --unresolve"??? */
204 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000205 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano3c588452009-12-11 23:53:41 -0800206 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000207 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500208 status_printf_ln(s, c, "%s", "");
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100209}
210
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400211static void wt_longstatus_print_dirty_header(struct wt_status *s,
212 int has_deleted,
213 int has_dirty_submodules)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400214{
Junio C Hamanod249b092009-08-09 21:59:30 -0700215 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800216
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000217 status_printf_ln(s, c, _("Changes not staged for commit:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200218 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400219 return;
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200220 if (!has_deleted)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000221 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200222 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000223 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
224 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100225 if (has_dirty_submodules)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000226 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500227 status_printf_ln(s, c, "%s", "");
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200228}
229
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400230static void wt_longstatus_print_other_header(struct wt_status *s,
231 const char *what,
232 const char *how)
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200233{
Junio C Hamanod249b092009-08-09 21:59:30 -0700234 const char *c = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +0700235 status_printf_ln(s, c, "%s:", what);
Matthieu Moy6a964f52013-09-12 12:50:05 +0200236 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400237 return;
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000238 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500239 status_printf_ln(s, c, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400240}
241
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400242static void wt_longstatus_print_trailer(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400243{
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500244 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400245}
246
Dmitry Potapova734d0b2008-03-07 05:30:58 +0300247#define quote_path quote_path_relative
Junio C Hamano3a946802006-11-08 13:20:46 -0800248
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800249static const char *wt_status_unmerged_status_string(int stagemask)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700250{
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800251 switch (stagemask) {
252 case 1:
253 return _("both deleted:");
254 case 2:
255 return _("added by us:");
256 case 3:
257 return _("deleted by them:");
258 case 4:
259 return _("added by them:");
260 case 5:
261 return _("deleted by us:");
262 case 6:
263 return _("both added:");
264 case 7:
265 return _("both modified:");
266 default:
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200267 die("BUG: unhandled unmerged status %x", stagemask);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700268 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700269}
270
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700271static const char *wt_status_diff_status_string(int status)
272{
273 switch (status) {
274 case DIFF_STATUS_ADDED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700275 return _("new file:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700276 case DIFF_STATUS_COPIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700277 return _("copied:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700278 case DIFF_STATUS_DELETED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700279 return _("deleted:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700280 case DIFF_STATUS_MODIFIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700281 return _("modified:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700282 case DIFF_STATUS_RENAMED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700283 return _("renamed:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700284 case DIFF_STATUS_TYPE_CHANGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700285 return _("typechange:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700286 case DIFF_STATUS_UNKNOWN:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700287 return _("unknown:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700288 case DIFF_STATUS_UNMERGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700289 return _("unmerged:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700290 default:
291 return NULL;
292 }
293}
294
Jonathan Nieder335e8252013-12-19 11:43:19 -0800295static int maxwidth(const char *(*label)(int), int minval, int maxval)
296{
297 int result = 0, i;
298
299 for (i = minval; i <= maxval; i++) {
300 const char *s = label(i);
301 int len = s ? utf8_strwidth(s) : 0;
302 if (len > result)
303 result = len;
304 }
305 return result;
306}
307
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400308static void wt_longstatus_print_unmerged_data(struct wt_status *s,
309 struct string_list_item *it)
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800310{
311 const char *c = color(WT_STATUS_UNMERGED, s);
312 struct wt_status_change_data *d = it->util;
313 struct strbuf onebuf = STRBUF_INIT;
314 static char *padding;
315 static int label_width;
316 const char *one, *how;
317 int len;
318
319 if (!padding) {
320 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
321 label_width += strlen(" ");
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800322 padding = xmallocz(label_width);
323 memset(padding, ' ', label_width);
324 }
325
326 one = quote_path(it->string, s->prefix, &onebuf);
327 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
328
329 how = wt_status_unmerged_status_string(d->stagemask);
330 len = label_width - utf8_strwidth(how);
331 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
332 strbuf_release(&onebuf);
333}
334
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400335static void wt_longstatus_print_change_data(struct wt_status *s,
336 int change_type,
337 struct string_list_item *it)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400338{
Junio C Hamano50b7e702009-08-04 23:49:33 -0700339 struct wt_status_change_data *d = it->util;
Junio C Hamanod249b092009-08-09 21:59:30 -0700340 const char *c = color(change_type, s);
Jeff Kingb8527d52013-03-21 07:05:28 -0400341 int status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700342 char *one_name;
343 char *two_name;
Junio C Hamano3a946802006-11-08 13:20:46 -0800344 const char *one, *two;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500345 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100346 struct strbuf extra = STRBUF_INIT;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700347 static char *padding;
Junio C Hamanod52cb572014-03-12 13:51:22 -0700348 static int label_width;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700349 const char *what;
350 int len;
351
352 if (!padding) {
Jonathan Nieder335e8252013-12-19 11:43:19 -0800353 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
354 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
Junio C Hamanod52cb572014-03-12 13:51:22 -0700355 label_width += strlen(" ");
356 padding = xmallocz(label_width);
357 memset(padding, ' ', label_width);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700358 }
Junio C Hamano3a946802006-11-08 13:20:46 -0800359
Junio C Hamano50b7e702009-08-04 23:49:33 -0700360 one_name = two_name = it->string;
361 switch (change_type) {
362 case WT_STATUS_UPDATED:
363 status = d->index_status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700364 break;
365 case WT_STATUS_CHANGED:
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100366 if (d->new_submodule_commits || d->dirty_submodule) {
367 strbuf_addstr(&extra, " (");
368 if (d->new_submodule_commits)
René Scharfea22ae752016-09-15 20:31:00 +0200369 strbuf_addstr(&extra, _("new commits, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100370 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
René Scharfea22ae752016-09-15 20:31:00 +0200371 strbuf_addstr(&extra, _("modified content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100372 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
René Scharfea22ae752016-09-15 20:31:00 +0200373 strbuf_addstr(&extra, _("untracked content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100374 strbuf_setlen(&extra, extra.len - 2);
375 strbuf_addch(&extra, ')');
376 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700377 status = d->worktree_status;
378 break;
Jeff Kingb8527d52013-03-21 07:05:28 -0400379 default:
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400380 die("BUG: unhandled change_type %d in wt_longstatus_print_change_data",
Jeff Kingb8527d52013-03-21 07:05:28 -0400381 change_type);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700382 }
383
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700384 /*
385 * Only pick up the rename it's relevant. If the rename is for
386 * the changed section and we're printing the updated section,
387 * ignore it.
388 */
389 if (d->rename_status == status)
390 one_name = d->rename_source;
391
Jiang Xin39598f92013-06-25 23:53:45 +0800392 one = quote_path(one_name, s->prefix, &onebuf);
393 two = quote_path(two_name, s->prefix, &twobuf);
Junio C Hamano3a946802006-11-08 13:20:46 -0800394
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600395 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700396 what = wt_status_diff_status_string(status);
397 if (!what)
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200398 die("BUG: unhandled diff status %c", status);
Junio C Hamanod52cb572014-03-12 13:51:22 -0700399 len = label_width - utf8_strwidth(what);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700400 assert(len >= 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700401 if (one_name != two_name)
Junio C Hamanod52cb572014-03-12 13:51:22 -0700402 status_printf_more(s, c, "%s%.*s%s -> %s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700403 what, len, padding, one, two);
404 else
Junio C Hamanod52cb572014-03-12 13:51:22 -0700405 status_printf_more(s, c, "%s%.*s%s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700406 what, len, padding, one);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100407 if (extra.len) {
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600408 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100409 strbuf_release(&extra);
410 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600411 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
Johannes Schindelin367c9882007-11-11 17:35:41 +0000412 strbuf_release(&onebuf);
413 strbuf_release(&twobuf);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400414}
415
Nguyễn Thái Ngọc Duy98bc94e2017-12-27 17:18:36 +0700416static char short_submodule_status(struct wt_status_change_data *d)
417{
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700418 if (d->new_submodule_commits)
419 return 'M';
420 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
421 return 'm';
422 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
423 return '?';
424 return d->worktree_status;
425}
426
Junio C Hamano50b7e702009-08-04 23:49:33 -0700427static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
428 struct diff_options *options,
429 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400430{
431 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400432 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700433
434 if (!q->nr)
435 return;
436 s->workdir_dirty = 1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400437 for (i = 0; i < q->nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700438 struct diff_filepair *p;
439 struct string_list_item *it;
440 struct wt_status_change_data *d;
441
442 p = q->queue[i];
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700443 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700444 d = it->util;
445 if (!d) {
446 d = xcalloc(1, sizeof(*d));
447 it->util = d;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400448 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700449 if (!d->worktree_status)
450 d->worktree_status = p->status;
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700451 if (S_ISGITLINK(p->two->mode)) {
452 d->dirty_submodule = p->two->dirty_submodule;
brian m. carlsona0d12c42016-06-24 23:09:23 +0000453 d->new_submodule_commits = !!oidcmp(&p->one->oid,
454 &p->two->oid);
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700455 if (s->status_format == STATUS_FORMAT_SHORT)
456 d->worktree_status = short_submodule_status(d);
457 }
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400458
459 switch (p->status) {
460 case DIFF_STATUS_ADDED:
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700461 d->mode_worktree = p->two->mode;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400462 break;
463
464 case DIFF_STATUS_DELETED:
465 d->mode_index = p->one->mode;
466 oidcpy(&d->oid_index, &p->one->oid);
467 /* mode_worktree is zero for a delete. */
468 break;
469
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700470 case DIFF_STATUS_COPIED:
471 case DIFF_STATUS_RENAMED:
472 if (d->rename_status)
473 die("BUG: multiple renames on the same target? how?");
474 d->rename_source = xstrdup(p->one->path);
475 d->rename_score = p->score * 100 / MAX_SCORE;
476 d->rename_status = p->status;
477 /* fallthru */
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400478 case DIFF_STATUS_MODIFIED:
479 case DIFF_STATUS_TYPE_CHANGED:
480 case DIFF_STATUS_UNMERGED:
481 d->mode_index = p->one->mode;
482 d->mode_worktree = p->two->mode;
483 oidcpy(&d->oid_index, &p->one->oid);
484 break;
485
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700486 default:
487 die("BUG: unhandled diff-files status '%c'", p->status);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400488 break;
489 }
490
Jeff Kingc91f0d92006-09-08 04:05:34 -0400491 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400492}
493
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700494static int unmerged_mask(const char *path)
495{
496 int pos, mask;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700497 const struct cache_entry *ce;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700498
499 pos = cache_name_pos(path, strlen(path));
500 if (0 <= pos)
501 return 0;
502
503 mask = 0;
504 pos = -pos-1;
505 while (pos < active_nr) {
506 ce = active_cache[pos++];
507 if (strcmp(ce->name, path) || !ce_stage(ce))
508 break;
509 mask |= (1 << (ce_stage(ce) - 1));
510 }
511 return mask;
512}
513
Junio C Hamano50b7e702009-08-04 23:49:33 -0700514static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
515 struct diff_options *options,
516 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400517{
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100518 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400519 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700520
521 for (i = 0; i < q->nr; i++) {
522 struct diff_filepair *p;
523 struct string_list_item *it;
524 struct wt_status_change_data *d;
525
526 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100527 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700528 d = it->util;
529 if (!d) {
530 d = xcalloc(1, sizeof(*d));
531 it->util = d;
532 }
533 if (!d->index_status)
534 d->index_status = p->status;
535 switch (p->status) {
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400536 case DIFF_STATUS_ADDED:
537 /* Leave {mode,oid}_head zero for an add. */
538 d->mode_index = p->two->mode;
539 oidcpy(&d->oid_index, &p->two->oid);
540 break;
541 case DIFF_STATUS_DELETED:
542 d->mode_head = p->one->mode;
543 oidcpy(&d->oid_head, &p->one->oid);
544 /* Leave {mode,oid}_index zero for a delete. */
545 break;
546
Junio C Hamano50b7e702009-08-04 23:49:33 -0700547 case DIFF_STATUS_COPIED:
548 case DIFF_STATUS_RENAMED:
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700549 if (d->rename_status)
550 die("BUG: multiple renames on the same target? how?");
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700551 d->rename_source = xstrdup(p->one->path);
552 d->rename_score = p->score * 100 / MAX_SCORE;
553 d->rename_status = p->status;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400554 /* fallthru */
555 case DIFF_STATUS_MODIFIED:
556 case DIFF_STATUS_TYPE_CHANGED:
557 d->mode_head = p->one->mode;
558 d->mode_index = p->two->mode;
559 oidcpy(&d->oid_head, &p->one->oid);
560 oidcpy(&d->oid_index, &p->two->oid);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700561 break;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700562 case DIFF_STATUS_UNMERGED:
563 d->stagemask = unmerged_mask(p->two->path);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400564 /*
565 * Don't bother setting {mode,oid}_{head,index} since the print
566 * code will output the stage values directly and not use the
567 * values in these fields.
568 */
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700569 break;
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700570
571 default:
572 die("BUG: unhandled diff-index status '%c'", p->status);
573 break;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700574 }
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100575 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400576}
577
Junio C Hamano50b7e702009-08-04 23:49:33 -0700578static void wt_status_collect_changes_worktree(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400579{
580 struct rev_info rev;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700581
582 init_revisions(&rev, NULL);
583 setup_revisions(0, NULL, &rev, NULL);
584 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700585 rev.diffopt.flags.dirty_submodules = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700586 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100587 if (!s->show_untracked_files)
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700588 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200589 if (s->ignore_submodule_arg) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700590 rev.diffopt.flags.override_submodule_config = 1;
Jens Lehmann46a958b2010-06-25 16:56:47 +0200591 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Nguyễn Thái Ngọc Duyc4c42f22011-10-24 15:24:51 +1100592 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700593 rev.diffopt.format_callback = wt_status_collect_changed_cb;
594 rev.diffopt.format_callback_data = s;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700595 copy_pathspec(&rev.prune_data, &s->pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700596 run_diff_files(&rev, 0);
597}
598
599static void wt_status_collect_changes_index(struct wt_status *s)
600{
601 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800602 struct setup_revision_opt opt;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700603
Jeff Kingc91f0d92006-09-08 04:05:34 -0400604 init_revisions(&rev, NULL);
Junio C Hamano32962c92010-03-08 22:58:09 -0800605 memset(&opt, 0, sizeof(opt));
606 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
607 setup_revisions(0, NULL, &rev, &opt);
608
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700609 rev.diffopt.flags.override_submodule_config = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700610 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200611 if (s->ignore_submodule_arg) {
Jens Lehmann46a958b2010-06-25 16:56:47 +0200612 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Jens Lehmann1d2f3932014-04-05 18:59:03 +0200613 } else {
614 /*
615 * Unless the user did explicitly request a submodule ignore
616 * mode by passing a command line option we do not ignore any
617 * changed submodule SHA-1s when comparing index and HEAD, no
618 * matter what is configured. Otherwise the user won't be
619 * shown any submodules she manually added (and which are
620 * staged to be committed), which would be really confusing.
621 */
622 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200623 }
Jens Lehmann46a958b2010-06-25 16:56:47 +0200624
Jeff Kingc91f0d92006-09-08 04:05:34 -0400625 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700626 rev.diffopt.format_callback = wt_status_collect_updated_cb;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400627 rev.diffopt.format_callback_data = s;
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 17:18:35 +0700628 rev.diffopt.detect_rename = DIFF_DETECT_RENAME;
Jeff King50705912008-04-30 13:24:43 -0400629 rev.diffopt.rename_limit = 200;
Jeff Kingf714fb82007-12-02 22:58:37 -0800630 rev.diffopt.break_opt = 0;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700631 copy_pathspec(&rev.prune_data, &s->pathspec);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400632 run_diff_index(&rev, 1);
633}
634
Junio C Hamano50b7e702009-08-04 23:49:33 -0700635static void wt_status_collect_changes_initial(struct wt_status *s)
636{
637 int i;
638
639 for (i = 0; i < active_nr; i++) {
640 struct string_list_item *it;
641 struct wt_status_change_data *d;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700642 const struct cache_entry *ce = active_cache[i];
Junio C Hamano50b7e702009-08-04 23:49:33 -0700643
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +0700644 if (!ce_path_match(ce, &s->pathspec, NULL))
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700645 continue;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700646 if (ce_intent_to_add(ce))
647 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100648 it = string_list_insert(&s->change, ce->name);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700649 d = it->util;
650 if (!d) {
651 d = xcalloc(1, sizeof(*d));
652 it->util = d;
653 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700654 if (ce_stage(ce)) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700655 d->index_status = DIFF_STATUS_UNMERGED;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700656 d->stagemask |= (1 << (ce_stage(ce) - 1));
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400657 /*
658 * Don't bother setting {mode,oid}_{head,index} since the print
659 * code will output the stage values directly and not use the
660 * values in these fields.
661 */
662 } else {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700663 d->index_status = DIFF_STATUS_ADDED;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400664 /* Leave {mode,oid}_head zero for adds. */
665 d->mode_index = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +0100666 oidcpy(&d->oid_index, &ce->oid);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400667 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700668 }
669}
670
Junio C Hamano76378682009-08-10 00:36:33 -0700671static void wt_status_collect_untracked(struct wt_status *s)
672{
673 int i;
674 struct dir_struct dir;
Karsten Blees132d41e2014-07-12 02:07:36 +0200675 uint64_t t_begin = getnanotime();
Junio C Hamano76378682009-08-10 00:36:33 -0700676
677 if (!s->show_untracked_files)
678 return;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700679
Junio C Hamano76378682009-08-10 00:36:33 -0700680 memset(&dir, 0, sizeof(dir));
681 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
682 dir.flags |=
683 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400684 if (s->show_ignored_mode) {
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200685 dir.flags |= DIR_SHOW_IGNORED_TOO;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400686
687 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
688 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
689 } else {
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +0700690 dir.untracked = the_index.untracked;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400691 }
692
Junio C Hamano76378682009-08-10 00:36:33 -0700693 setup_standard_excludes(&dir);
694
Brandon Williams0d32c182017-05-05 12:53:34 -0700695 fill_directory(&dir, &the_index, &s->pathspec);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200696
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400697 for (i = 0; i < dir.nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700698 struct dir_entry *ent = dir.entries[i];
Brandon Caseyb8224232010-09-26 21:49:13 -0500699 if (cache_name_is_other(ent->name, ent->len) &&
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700700 dir_path_match(ent, &s->pathspec, 0, NULL))
Brandon Caseyb8224232010-09-26 21:49:13 -0500701 string_list_insert(&s->untracked, ent->name);
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700702 free(ent);
Junio C Hamano76378682009-08-10 00:36:33 -0700703 }
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700704
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200705 for (i = 0; i < dir.ignored_nr; i++) {
706 struct dir_entry *ent = dir.ignored[i];
707 if (cache_name_is_other(ent->name, ent->len) &&
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700708 dir_path_match(ent, &s->pathspec, 0, NULL))
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200709 string_list_insert(&s->ignored, ent->name);
710 free(ent);
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700711 }
712
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700713 free(dir.entries);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200714 free(dir.ignored);
715 clear_directory(&dir);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700716
Karsten Blees132d41e2014-07-12 02:07:36 +0200717 if (advice_status_u_option)
718 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
Junio C Hamano76378682009-08-10 00:36:33 -0700719}
720
721void wt_status_collect(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700722{
723 wt_status_collect_changes_worktree(s);
724
725 if (s->is_initial)
726 wt_status_collect_changes_initial(s);
727 else
728 wt_status_collect_changes_index(s);
Junio C Hamano76378682009-08-10 00:36:33 -0700729 wt_status_collect_untracked(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700730}
731
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400732static void wt_longstatus_print_unmerged(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700733{
734 int shown_header = 0;
735 int i;
736
737 for (i = 0; i < s->change.nr; i++) {
738 struct wt_status_change_data *d;
739 struct string_list_item *it;
740 it = &(s->change.items[i]);
741 d = it->util;
742 if (!d->stagemask)
743 continue;
744 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400745 wt_longstatus_print_unmerged_header(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700746 shown_header = 1;
747 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400748 wt_longstatus_print_unmerged_data(s, it);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700749 }
750 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400751 wt_longstatus_print_trailer(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700752
753}
754
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400755static void wt_longstatus_print_updated(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700756{
757 int shown_header = 0;
758 int i;
759
760 for (i = 0; i < s->change.nr; i++) {
761 struct wt_status_change_data *d;
762 struct string_list_item *it;
763 it = &(s->change.items[i]);
764 d = it->util;
765 if (!d->index_status ||
766 d->index_status == DIFF_STATUS_UNMERGED)
767 continue;
768 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400769 wt_longstatus_print_cached_header(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700770 s->commitable = 1;
771 shown_header = 1;
772 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400773 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700774 }
775 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400776 wt_longstatus_print_trailer(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700777}
778
779/*
780 * -1 : has delete
781 * 0 : no change
782 * 1 : some change but no delete
783 */
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100784static int wt_status_check_worktree_changes(struct wt_status *s,
785 int *dirty_submodules)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700786{
787 int i;
788 int changes = 0;
789
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100790 *dirty_submodules = 0;
791
Junio C Hamano50b7e702009-08-04 23:49:33 -0700792 for (i = 0; i < s->change.nr; i++) {
793 struct wt_status_change_data *d;
794 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700795 if (!d->worktree_status ||
796 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700797 continue;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100798 if (!changes)
799 changes = 1;
800 if (d->dirty_submodule)
801 *dirty_submodules = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700802 if (d->worktree_status == DIFF_STATUS_DELETED)
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100803 changes = -1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700804 }
805 return changes;
806}
807
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400808static void wt_longstatus_print_changed(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400809{
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100810 int i, dirty_submodules;
811 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700812
813 if (!worktree_changes)
814 return;
815
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400816 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700817
818 for (i = 0; i < s->change.nr; i++) {
819 struct wt_status_change_data *d;
820 struct string_list_item *it;
821 it = &(s->change.items[i]);
822 d = it->util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700823 if (!d->worktree_status ||
824 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700825 continue;
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400826 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700827 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400828 wt_longstatus_print_trailer(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400829}
830
Liam Beguinc1b5d012017-06-17 18:30:51 -0400831static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
832 const char *email, timestamp_t timestamp, int tz,
833 const char *message, void *cb_data)
834{
835 int *c = cb_data;
836 (*c)++;
837 return 0;
838}
839
840static void wt_longstatus_print_stash_summary(struct wt_status *s)
841{
842 int stash_count = 0;
843
844 for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
845 if (stash_count > 0)
846 status_printf_ln(s, GIT_COLOR_NORMAL,
847 Q_("Your stash currently has %d entry",
848 "Your stash currently has %d entries", stash_count),
849 stash_count);
850}
851
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400852static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
Ping Yinac8d5af2008-04-12 23:05:32 +0800853{
René Scharfed3180272014-08-19 21:09:35 +0200854 struct child_process sm_summary = CHILD_PROCESS_INIT;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200855 struct strbuf cmd_stdout = STRBUF_INIT;
856 struct strbuf summary = STRBUF_INIT;
857 char *summary_content;
Ping Yinac8d5af2008-04-12 23:05:32 +0800858
René Scharfea9154592014-10-19 13:14:20 +0200859 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
860 s->index_file);
Ping Yinac8d5af2008-04-12 23:05:32 +0800861
René Scharfea2bae2d2014-11-09 14:49:54 +0100862 argv_array_push(&sm_summary.args, "submodule");
863 argv_array_push(&sm_summary.args, "summary");
864 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
865 argv_array_push(&sm_summary.args, "--for-status");
866 argv_array_push(&sm_summary.args, "--summary-limit");
867 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200868 if (!uncommitted)
René Scharfea2bae2d2014-11-09 14:49:54 +0100869 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200870
Ping Yinac8d5af2008-04-12 23:05:32 +0800871 sm_summary.git_cmd = 1;
872 sm_summary.no_stdin = 1;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200873
Jeff King5c950e92015-03-22 23:53:52 -0400874 capture_command(&sm_summary, &cmd_stdout, 1024);
Matthieu Moy3ba74072013-09-06 19:43:06 +0200875
876 /* prepend header, only if there's an actual output */
Jeff Kingd56d9662015-03-22 06:00:32 -0400877 if (cmd_stdout.len) {
Matthieu Moy3ba74072013-09-06 19:43:06 +0200878 if (uncommitted)
879 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
880 else
881 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
882 strbuf_addstr(&summary, "\n\n");
883 }
884 strbuf_addbuf(&summary, &cmd_stdout);
885 strbuf_release(&cmd_stdout);
886
Matthieu Moy2556b992013-09-06 19:43:07 +0200887 if (s->display_comment_prefix) {
Jeff Kingd56d9662015-03-22 06:00:32 -0400888 size_t len;
Matthieu Moy2556b992013-09-06 19:43:07 +0200889 summary_content = strbuf_detach(&summary, &len);
890 strbuf_add_commented_lines(&summary, summary_content, len);
891 free(summary_content);
892 }
Matthieu Moy3ba74072013-09-06 19:43:06 +0200893
894 fputs(summary.buf, s->fp);
895 strbuf_release(&summary);
Ping Yinac8d5af2008-04-12 23:05:32 +0800896}
897
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400898static void wt_longstatus_print_other(struct wt_status *s,
899 struct string_list *l,
900 const char *what,
901 const char *how)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400902{
Jeff Kingc91f0d92006-09-08 04:05:34 -0400903 int i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500904 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700905 static struct string_list output = STRING_LIST_INIT_DUP;
906 struct column_options copts;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400907
Jeff King12829882011-06-02 01:54:49 -0400908 if (!l->nr)
Junio C Hamano76378682009-08-10 00:36:33 -0700909 return;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400910
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400911 wt_longstatus_print_other_header(s, what, how);
Junio C Hamano1b908b62010-04-10 00:19:46 -0700912
913 for (i = 0; i < l->nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700914 struct string_list_item *it;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700915 const char *path;
Junio C Hamano1b908b62010-04-10 00:19:46 -0700916 it = &(l->items[i]);
Jiang Xin39598f92013-06-25 23:53:45 +0800917 path = quote_path(it->string, s->prefix, &buf);
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700918 if (column_active(s->colopts)) {
919 string_list_append(&output, path);
920 continue;
921 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600922 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
923 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700924 "%s\n", path);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400925 }
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700926
927 strbuf_release(&buf);
928 if (!column_active(s->colopts))
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200929 goto conclude;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700930
Matthieu Moy2556b992013-09-06 19:43:07 +0200931 strbuf_addf(&buf, "%s%s\t%s",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700932 color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +0200933 s->display_comment_prefix ? "#" : "",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700934 color(WT_STATUS_UNTRACKED, s));
935 memset(&copts, 0, sizeof(copts));
936 copts.padding = 1;
937 copts.indent = buf.buf;
938 if (want_color(s->use_color))
939 copts.nl = GIT_COLOR_RESET "\n";
940 print_columns(&output, s->colopts, &copts);
941 string_list_clear(&output, 0);
Johannes Schindelin367c9882007-11-11 17:35:41 +0000942 strbuf_release(&buf);
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200943conclude:
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500944 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400945}
946
Brian Malehornd76650b2017-05-15 23:06:49 -0700947size_t wt_status_locate_end(const char *s, size_t len)
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100948{
949 const char *p;
950 struct strbuf pattern = STRBUF_INIT;
951
SZEDER Gáborfbfa0972015-06-09 02:28:34 +0200952 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
Brian Malehornd76650b2017-05-15 23:06:49 -0700953 if (starts_with(s, pattern.buf + 1))
954 len = 0;
955 else if ((p = strstr(s, pattern.buf)))
956 len = p - s + 1;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100957 strbuf_release(&pattern);
Brian Malehornd76650b2017-05-15 23:06:49 -0700958 return len;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100959}
960
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +0700961void wt_status_add_cut_line(FILE *fp)
962{
Kaartic Sivaraam8c4b1a32017-09-13 13:05:38 +0000963 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 +0700964 struct strbuf buf = STRBUF_INIT;
965
966 fprintf(fp, "%c %s", comment_line_char, cut_line);
967 strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
968 fputs(buf.buf, fp);
969 strbuf_release(&buf);
970}
971
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400972static void wt_longstatus_print_verbose(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400973{
974 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800975 struct setup_revision_opt opt;
Michael J Gruber40555002015-03-06 10:43:35 +0100976 int dirty_submodules;
977 const char *c = color(WT_STATUS_HEADER, s);
Kristian Høgsberg99a12692007-11-21 21:54:49 -0500978
Jeff Kingc91f0d92006-09-08 04:05:34 -0400979 init_revisions(&rev, NULL);
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700980 rev.diffopt.flags.allow_textconv = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700981 rev.diffopt.ita_invisible_in_index = 1;
Junio C Hamano32962c92010-03-08 22:58:09 -0800982
983 memset(&opt, 0, sizeof(opt));
984 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
985 setup_revisions(0, NULL, &rev, &opt);
986
Jeff Kingc91f0d92006-09-08 04:05:34 -0400987 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 17:18:35 +0700988 rev.diffopt.detect_rename = DIFF_DETECT_RENAME;
Kristian Høgsberg4ba0cb22008-03-10 13:58:26 -0400989 rev.diffopt.file = s->fp;
990 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 00:49:35 -0400991 /*
992 * If we're not going to stdout, then we definitely don't
993 * want color, since we are going to the commit message
994 * file (and even the "auto" setting won't work, since it
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100995 * will have checked isatty on stdout). But we then do want
996 * to insert the scissor line here to reliably remove the
997 * diff before committing.
Jeff King4f672ad2008-10-26 00:49:35 -0400998 */
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100999 if (s->fp != stdout) {
Jeff Kingf1c96262011-08-17 22:03:12 -07001000 rev.diffopt.use_color = 0;
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001001 wt_status_add_cut_line(s->fp);
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001002 }
Michael J Gruber40555002015-03-06 10:43:35 +01001003 if (s->verbose > 1 && s->commitable) {
1004 /* print_updated() printed a header, so do we */
1005 if (s->fp != stdout)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001006 wt_longstatus_print_trailer(s);
Michael J Gruber40555002015-03-06 10:43:35 +01001007 status_printf_ln(s, c, _("Changes to be committed:"));
1008 rev.diffopt.a_prefix = "c/";
1009 rev.diffopt.b_prefix = "i/";
1010 } /* else use prefix as per user config */
Jeff Kingc91f0d92006-09-08 04:05:34 -04001011 run_diff_index(&rev, 1);
Michael J Gruber40555002015-03-06 10:43:35 +01001012 if (s->verbose > 1 &&
1013 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1014 status_printf_ln(s, c,
1015 "--------------------------------------------------");
1016 status_printf_ln(s, c, _("Changes not staged for commit:"));
1017 setup_work_tree();
1018 rev.diffopt.a_prefix = "i/";
1019 rev.diffopt.b_prefix = "w/";
1020 run_diff_files(&rev, 0);
1021 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001022}
1023
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001024static void wt_longstatus_print_tracking(struct wt_status *s)
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001025{
1026 struct strbuf sb = STRBUF_INIT;
René Scharfec72b49d2015-10-31 18:37:43 +01001027 const char *cp, *ep, *branch_name;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001028 struct branch *branch;
Matthieu Moy2556b992013-09-06 19:43:07 +02001029 char comment_line_string[3];
1030 int i;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001031
1032 assert(s->branch && !s->is_initial);
René Scharfec72b49d2015-10-31 18:37:43 +01001033 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001034 return;
René Scharfec72b49d2015-10-31 18:37:43 +01001035 branch = branch_get(branch_name);
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001036 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001037 return;
1038
Matthieu Moy2556b992013-09-06 19:43:07 +02001039 i = 0;
1040 if (s->display_comment_prefix) {
1041 comment_line_string[i++] = comment_line_char;
1042 comment_line_string[i++] = ' ';
1043 }
1044 comment_line_string[i] = '\0';
1045
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001046 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-09 21:59:30 -07001047 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001048 "%s%.*s", comment_line_string,
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001049 (int)(ep - cp), cp);
Matthieu Moy2556b992013-09-06 19:43:07 +02001050 if (s->display_comment_prefix)
1051 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1052 comment_line_char);
1053 else
Jeff King75177c82017-04-27 05:01:05 -04001054 fputs("\n", s->fp);
Rene Scharfeb6ec3072017-08-30 20:20:18 +02001055 strbuf_release(&sb);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001056}
1057
Lucien Kong83c750a2012-06-05 22:21:24 +02001058static int has_unmerged(struct wt_status *s)
1059{
1060 int i;
1061
1062 for (i = 0; i < s->change.nr; i++) {
1063 struct wt_status_change_data *d;
1064 d = s->change.items[i].util;
1065 if (d->stagemask)
1066 return 1;
1067 }
1068 return 0;
1069}
1070
1071static void show_merge_in_progress(struct wt_status *s,
1072 struct wt_status_state *state,
1073 const char *color)
1074{
1075 if (has_unmerged(s)) {
1076 status_printf_ln(s, color, _("You have unmerged paths."));
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001077 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001078 status_printf_ln(s, color,
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001079 _(" (fix conflicts and run \"git commit\")"));
1080 status_printf_ln(s, color,
1081 _(" (use \"git merge --abort\" to abort the merge)"));
1082 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001083 } else {
Stephen P. Smith8dc874b2016-02-15 19:38:25 -07001084 s-> commitable = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001085 status_printf_ln(s, color,
1086 _("All conflicts fixed but you are still merging."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001087 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001088 status_printf_ln(s, color,
1089 _(" (use \"git commit\" to conclude merge)"));
1090 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001091 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001092}
1093
1094static void show_am_in_progress(struct wt_status *s,
1095 struct wt_status_state *state,
1096 const char *color)
1097{
1098 status_printf_ln(s, color,
1099 _("You are in the middle of an am session."));
1100 if (state->am_empty_patch)
1101 status_printf_ln(s, color,
1102 _("The current patch is empty."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001103 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001104 if (!state->am_empty_patch)
1105 status_printf_ln(s, color,
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +03001106 _(" (fix conflicts and then run \"git am --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001107 status_printf_ln(s, color,
1108 _(" (use \"git am --skip\" to skip this patch)"));
1109 status_printf_ln(s, color,
1110 _(" (use \"git am --abort\" to restore the original branch)"));
1111 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001112 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001113}
1114
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001115static char *read_line_from_git_path(const char *filename)
1116{
1117 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +07001118 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1119
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001120 if (!fp) {
1121 strbuf_release(&buf);
1122 return NULL;
1123 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001124 strbuf_getline_lf(&buf, fp);
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001125 if (!fclose(fp)) {
1126 return strbuf_detach(&buf, NULL);
1127 } else {
1128 strbuf_release(&buf);
1129 return NULL;
1130 }
1131}
1132
1133static int split_commit_in_progress(struct wt_status *s)
1134{
1135 int split_in_progress = 0;
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001136 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001137
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001138 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001139 !s->branch || strcmp(s->branch, "HEAD"))
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001140 return 0;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001141
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001142 head = read_line_from_git_path("HEAD");
1143 orig_head = read_line_from_git_path("ORIG_HEAD");
1144 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1145 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1146
1147 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1148 ; /* fall through, no split in progress */
1149 else if (!strcmp(rebase_amend, rebase_orig_head))
1150 split_in_progress = !!strcmp(head, rebase_amend);
1151 else if (strcmp(orig_head, rebase_orig_head))
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001152 split_in_progress = 1;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001153
1154 free(head);
1155 free(orig_head);
1156 free(rebase_amend);
1157 free(rebase_orig_head);
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001158
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001159 return split_in_progress;
1160}
1161
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001162/*
1163 * Turn
1164 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1165 * into
1166 * "pick d6a2f03 some message"
1167 *
1168 * The function assumes that the line does not contain useless spaces
1169 * before or after the command.
1170 */
1171static void abbrev_sha1_in_line(struct strbuf *line)
1172{
1173 struct strbuf **split;
1174 int i;
1175
1176 if (starts_with(line->buf, "exec ") ||
1177 starts_with(line->buf, "x "))
1178 return;
1179
1180 split = strbuf_split_max(line, ' ', 3);
1181 if (split[0] && split[1]) {
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001182 struct object_id oid;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001183
1184 /*
1185 * strbuf_split_max left a space. Trim it and re-add
1186 * it after abbreviation.
1187 */
1188 strbuf_trim(split[1]);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001189 if (!get_oid(split[1]->buf, &oid)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001190 strbuf_reset(split[1]);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001191 strbuf_add_unique_abbrev(split[1], oid.hash,
René Scharfea94bb682016-10-08 17:38:47 +02001192 DEFAULT_ABBREV);
1193 strbuf_addch(split[1], ' ');
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001194 strbuf_reset(line);
1195 for (i = 0; split[i]; i++)
René Scharfe81099842016-07-19 20:36:29 +02001196 strbuf_addbuf(line, split[i]);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001197 }
1198 }
Stefan Beller6eb60782016-03-31 17:35:44 -07001199 strbuf_list_free(split);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001200}
1201
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001202static int read_rebase_todolist(const char *fname, struct string_list *lines)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001203{
1204 struct strbuf line = STRBUF_INIT;
1205 FILE *f = fopen(git_path("%s", fname), "r");
1206
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001207 if (!f) {
1208 if (errno == ENOENT)
1209 return -1;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001210 die_errno("Could not open file %s for reading",
1211 git_path("%s", fname));
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001212 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001213 while (!strbuf_getline_lf(&line, f)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001214 if (line.len && line.buf[0] == comment_line_char)
1215 continue;
1216 strbuf_trim(&line);
1217 if (!line.len)
1218 continue;
1219 abbrev_sha1_in_line(&line);
1220 string_list_append(lines, line.buf);
1221 }
Johannes Schindeline7b65e22017-05-04 15:55:52 +02001222 fclose(f);
Rene Scharfe6f495412017-08-30 20:20:17 +02001223 strbuf_release(&line);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001224 return 0;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001225}
1226
1227static void show_rebase_information(struct wt_status *s,
1228 struct wt_status_state *state,
1229 const char *color)
1230{
1231 if (state->rebase_interactive_in_progress) {
1232 int i;
1233 int nr_lines_to_show = 2;
1234
1235 struct string_list have_done = STRING_LIST_INIT_DUP;
1236 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1237
1238 read_rebase_todolist("rebase-merge/done", &have_done);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001239 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1240 &yet_to_do))
1241 status_printf_ln(s, color,
1242 _("git-rebase-todo is missing."));
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001243 if (have_done.nr == 0)
1244 status_printf_ln(s, color, _("No commands done."));
1245 else {
1246 status_printf_ln(s, color,
1247 Q_("Last command done (%d command done):",
1248 "Last commands done (%d commands done):",
1249 have_done.nr),
1250 have_done.nr);
1251 for (i = (have_done.nr > nr_lines_to_show)
1252 ? have_done.nr - nr_lines_to_show : 0;
1253 i < have_done.nr;
1254 i++)
1255 status_printf_ln(s, color, " %s", have_done.items[i].string);
1256 if (have_done.nr > nr_lines_to_show && s->hints)
1257 status_printf_ln(s, color,
1258 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1259 }
1260
1261 if (yet_to_do.nr == 0)
1262 status_printf_ln(s, color,
1263 _("No commands remaining."));
1264 else {
1265 status_printf_ln(s, color,
1266 Q_("Next command to do (%d remaining command):",
1267 "Next commands to do (%d remaining commands):",
1268 yet_to_do.nr),
1269 yet_to_do.nr);
1270 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1271 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1272 if (s->hints)
1273 status_printf_ln(s, color,
1274 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1275 }
1276 string_list_clear(&yet_to_do, 0);
1277 string_list_clear(&have_done, 0);
1278 }
1279}
1280
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001281static void print_rebase_state(struct wt_status *s,
1282 struct wt_status_state *state,
1283 const char *color)
1284{
1285 if (state->branch)
1286 status_printf_ln(s, color,
1287 _("You are currently rebasing branch '%s' on '%s'."),
1288 state->branch,
1289 state->onto);
1290 else
1291 status_printf_ln(s, color,
1292 _("You are currently rebasing."));
1293}
1294
Lucien Kong83c750a2012-06-05 22:21:24 +02001295static void show_rebase_in_progress(struct wt_status *s,
1296 struct wt_status_state *state,
1297 const char *color)
1298{
1299 struct stat st;
1300
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001301 show_rebase_information(s, state, color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001302 if (has_unmerged(s)) {
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001303 print_rebase_state(s, state, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001304 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001305 status_printf_ln(s, color,
1306 _(" (fix conflicts and then run \"git rebase --continue\")"));
1307 status_printf_ln(s, color,
1308 _(" (use \"git rebase --skip\" to skip this patch)"));
1309 status_printf_ln(s, color,
1310 _(" (use \"git rebase --abort\" to check out the original branch)"));
1311 }
Jeff Kingf9327292015-08-10 05:38:57 -04001312 } else if (state->rebase_in_progress || !stat(git_path_merge_msg(), &st)) {
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001313 print_rebase_state(s, state, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001314 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001315 status_printf_ln(s, color,
1316 _(" (all conflicts fixed: run \"git rebase --continue\")"));
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001317 } else if (split_commit_in_progress(s)) {
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001318 if (state->branch)
1319 status_printf_ln(s, color,
1320 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1321 state->branch,
1322 state->onto);
1323 else
1324 status_printf_ln(s, color,
1325 _("You are currently splitting a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001326 if (s->hints)
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001327 status_printf_ln(s, color,
1328 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001329 } else {
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001330 if (state->branch)
1331 status_printf_ln(s, color,
1332 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1333 state->branch,
1334 state->onto);
1335 else
1336 status_printf_ln(s, color,
1337 _("You are currently editing a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001338 if (s->hints && !s->amend) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001339 status_printf_ln(s, color,
1340 _(" (use \"git commit --amend\" to amend the current commit)"));
1341 status_printf_ln(s, color,
1342 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1343 }
1344 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001345 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001346}
1347
1348static void show_cherry_pick_in_progress(struct wt_status *s,
1349 struct wt_status_state *state,
1350 const char *color)
1351{
Ralf Thielowbffd8092013-10-11 17:58:37 +02001352 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1353 find_unique_abbrev(state->cherry_pick_head_sha1, DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001354 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001355 if (has_unmerged(s))
1356 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001357 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001358 else
1359 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001360 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1361 status_printf_ln(s, color,
1362 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001363 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001364 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001365}
1366
Matthieu Moydb4ef442013-04-02 16:20:21 +02001367static void show_revert_in_progress(struct wt_status *s,
1368 struct wt_status_state *state,
1369 const char *color)
1370{
Matthieu Moy87e139c2013-04-02 16:20:22 +02001371 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1372 find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001373 if (s->hints) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001374 if (has_unmerged(s))
1375 status_printf_ln(s, color,
1376 _(" (fix conflicts and run \"git revert --continue\")"));
1377 else
1378 status_printf_ln(s, color,
1379 _(" (all conflicts fixed: run \"git revert --continue\")"));
1380 status_printf_ln(s, color,
1381 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1382 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001383 wt_longstatus_print_trailer(s);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001384}
1385
Lucien Kong83c750a2012-06-05 22:21:24 +02001386static void show_bisect_in_progress(struct wt_status *s,
1387 struct wt_status_state *state,
1388 const char *color)
1389{
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001390 if (state->branch)
1391 status_printf_ln(s, color,
Nguyễn Thái Ngọc Duy6deab242013-03-23 10:52:44 +07001392 _("You are currently bisecting, started from branch '%s'."),
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001393 state->branch);
1394 else
1395 status_printf_ln(s, color,
1396 _("You are currently bisecting."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001397 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001398 status_printf_ln(s, color,
1399 _(" (use \"git bisect reset\" to get back to the original branch)"));
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001400 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001401}
1402
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001403/*
1404 * Extract branch information from rebase/bisect
1405 */
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001406static char *get_branch(const struct worktree *wt, const char *path)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001407{
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001408 struct strbuf sb = STRBUF_INIT;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001409 struct object_id oid;
René Scharfec72b49d2015-10-31 18:37:43 +01001410 const char *branch_name;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001411
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001412 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 +07001413 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001414
Jeff King66ec9042015-01-28 12:57:35 -05001415 while (sb.len && sb.buf[sb.len - 1] == '\n')
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001416 strbuf_setlen(&sb, sb.len - 1);
1417 if (!sb.len)
1418 goto got_nothing;
René Scharfec72b49d2015-10-31 18:37:43 +01001419 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1420 strbuf_remove(&sb, 0, branch_name - sb.buf);
Christian Couder59556542013-11-30 21:55:40 +01001421 else if (starts_with(sb.buf, "refs/"))
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001422 ;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001423 else if (!get_oid_hex(sb.buf, &oid)) {
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001424 strbuf_reset(&sb);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001425 strbuf_add_unique_abbrev(&sb, oid.hash, DEFAULT_ABBREV);
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001426 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1427 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001428 else /* bisect */
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001429 ;
1430 return strbuf_detach(&sb, NULL);
1431
1432got_nothing:
1433 strbuf_release(&sb);
1434 return NULL;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001435}
1436
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001437struct grab_1st_switch_cbdata {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001438 struct strbuf buf;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001439 struct object_id noid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001440};
1441
brian m. carlson9461d272017-02-21 23:47:32 +00001442static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +02001443 const char *email, timestamp_t timestamp, int tz,
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001444 const char *message, void *cb_data)
Lucien Kong83c750a2012-06-05 22:21:24 +02001445{
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001446 struct grab_1st_switch_cbdata *cb = cb_data;
1447 const char *target = NULL, *end;
1448
René Scharfec72b49d2015-10-31 18:37:43 +01001449 if (!skip_prefix(message, "checkout: moving from ", &message))
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001450 return 0;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001451 target = strstr(message, " to ");
1452 if (!target)
1453 return 0;
1454 target += strlen(" to ");
1455 strbuf_reset(&cb->buf);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001456 oidcpy(&cb->noid, noid);
René Scharfe904de442015-11-25 15:10:18 +01001457 end = strchrnul(target, '\n');
1458 strbuf_add(&cb->buf, target, end - target);
1459 if (!strcmp(cb->buf.buf, "HEAD")) {
Matthieu Moy0eb85482015-09-27 17:13:42 +02001460 /* HEAD is relative. Resolve it to the right reflog entry. */
René Scharfe904de442015-11-25 15:10:18 +01001461 strbuf_reset(&cb->buf);
brian m. carlson9461d272017-02-21 23:47:32 +00001462 strbuf_add_unique_abbrev(&cb->buf, noid->hash, DEFAULT_ABBREV);
Matthieu Moy0eb85482015-09-27 17:13:42 +02001463 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001464 return 1;
1465}
1466
1467static void wt_status_get_detached_from(struct wt_status_state *state)
1468{
1469 struct grab_1st_switch_cbdata cb;
1470 struct commit *commit;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001471 struct object_id oid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001472 char *ref = NULL;
1473
1474 strbuf_init(&cb.buf, 0);
1475 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1476 strbuf_release(&cb.buf);
1477 return;
1478 }
1479
brian m. carlsoncca5fa62017-10-15 22:06:57 +00001480 if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref) == 1 &&
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001481 /* sha1 is a commit? match without further lookup */
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001482 (!oidcmp(&cb.noid, &oid) ||
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001483 /* perhaps sha1 is a tag, try to dereference to a commit */
brian m. carlsonbc832662017-05-06 22:10:10 +00001484 ((commit = lookup_commit_reference_gently(&oid, 1)) != NULL &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001485 !oidcmp(&cb.noid, &commit->object.oid)))) {
René Scharfec72b49d2015-10-31 18:37:43 +01001486 const char *from = ref;
1487 if (!skip_prefix(from, "refs/tags/", &from))
1488 skip_prefix(from, "refs/remotes/", &from);
1489 state->detached_from = xstrdup(from);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001490 } else
1491 state->detached_from =
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001492 xstrdup(find_unique_abbrev(cb.noid.hash, DEFAULT_ABBREV));
1493 hashcpy(state->detached_sha1, cb.noid.hash);
1494 state->detached_at = !get_oid("HEAD", &oid) &&
1495 !hashcmp(oid.hash, state->detached_sha1);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001496
1497 free(ref);
1498 strbuf_release(&cb.buf);
1499}
1500
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001501int wt_status_check_rebase(const struct worktree *wt,
1502 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001503{
Lucien Kong83c750a2012-06-05 22:21:24 +02001504 struct stat st;
1505
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001506 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1507 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001508 state->am_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001509 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 +07001510 state->am_empty_patch = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001511 } else {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001512 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001513 state->branch = get_branch(wt, "rebase-apply/head-name");
1514 state->onto = get_branch(wt, "rebase-apply/onto");
Lucien Kong83c750a2012-06-05 22:21:24 +02001515 }
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001516 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1517 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001518 state->rebase_interactive_in_progress = 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-merge/head-name");
1522 state->onto = get_branch(wt, "rebase-merge/onto");
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001523 } else
1524 return 0;
1525 return 1;
1526}
1527
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001528int wt_status_check_bisect(const struct worktree *wt,
1529 struct wt_status_state *state)
1530{
1531 struct stat st;
1532
1533 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1534 state->bisect_in_progress = 1;
1535 state->branch = get_branch(wt, "BISECT_START");
1536 return 1;
1537 }
1538 return 0;
1539}
1540
Lucien Kong83c750a2012-06-05 22:21:24 +02001541void wt_status_get_state(struct wt_status_state *state,
1542 int get_detached_from)
1543{
1544 struct stat st;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001545 struct object_id oid;
Lucien Kong83c750a2012-06-05 22:21:24 +02001546
1547 if (!stat(git_path_merge_head(), &st)) {
1548 state->merge_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001549 } else if (wt_status_check_rebase(NULL, state)) {
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001550 ; /* all set */
Jeff Kingf9327292015-08-10 05:38:57 -04001551 } else if (!stat(git_path_cherry_pick_head(), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001552 !get_oid("CHERRY_PICK_HEAD", &oid)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001553 state->cherry_pick_in_progress = 1;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001554 hashcpy(state->cherry_pick_head_sha1, oid.hash);
Lucien Kong83c750a2012-06-05 22:21:24 +02001555 }
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001556 wt_status_check_bisect(NULL, state);
Jeff Kingf9327292015-08-10 05:38:57 -04001557 if (!stat(git_path_revert_head(), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001558 !get_oid("REVERT_HEAD", &oid)) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001559 state->revert_in_progress = 1;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001560 hashcpy(state->revert_head_sha1, oid.hash);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001561 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001562
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001563 if (get_detached_from)
1564 wt_status_get_detached_from(state);
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001565}
1566
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001567static void wt_longstatus_print_state(struct wt_status *s,
1568 struct wt_status_state *state)
Lucien Kong83c750a2012-06-05 22:21:24 +02001569{
1570 const char *state_color = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001571 if (state->merge_in_progress)
1572 show_merge_in_progress(s, state, state_color);
1573 else if (state->am_in_progress)
1574 show_am_in_progress(s, state, state_color);
1575 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1576 show_rebase_in_progress(s, state, state_color);
1577 else if (state->cherry_pick_in_progress)
1578 show_cherry_pick_in_progress(s, state, state_color);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001579 else if (state->revert_in_progress)
1580 show_revert_in_progress(s, state, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001581 if (state->bisect_in_progress)
1582 show_bisect_in_progress(s, state, state_color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001583}
1584
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001585static void wt_longstatus_print(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001586{
Aleksi Aalto1d282322010-11-18 01:40:05 +02001587 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1588 const char *branch_status_color = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001589 struct wt_status_state state;
1590
1591 memset(&state, 0, sizeof(state));
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001592 wt_status_get_state(&state,
1593 s->branch && !strcmp(s->branch, "HEAD"));
Jürgen Rühle98bf8a42007-01-02 20:26:23 +01001594
Junio C Hamanobda324c2007-01-03 01:09:34 -08001595 if (s->branch) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001596 const char *on_what = _("On branch ");
Junio C Hamanobda324c2007-01-03 01:09:34 -08001597 const char *branch_name = s->branch;
René Scharfec72b49d2015-10-31 18:37:43 +01001598 if (!strcmp(branch_name, "HEAD")) {
Aleksi Aalto1d282322010-11-18 01:40:05 +02001599 branch_status_color = color(WT_STATUS_NOBRANCH, s);
Ramkumar Ramachandraec506312013-06-16 14:15:15 +05301600 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
Guillaume Pagèsdf25e942015-06-30 15:01:13 +02001601 if (state.rebase_interactive_in_progress)
1602 on_what = _("interactive rebase in progress; onto ");
1603 else
1604 on_what = _("rebase in progress; onto ");
Ramkumar Ramachandraec506312013-06-16 14:15:15 +05301605 branch_name = state.onto;
1606 } else if (state.detached_from) {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001607 branch_name = state.detached_from;
Michael J Gruber970399e2015-03-06 16:04:06 +01001608 if (state.detached_at)
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001609 on_what = _("HEAD detached at ");
1610 else
1611 on_what = _("HEAD detached from ");
1612 } else {
1613 branch_name = "";
1614 on_what = _("Not currently on any branch.");
1615 }
René Scharfec72b49d2015-10-31 18:37:43 +01001616 } else
1617 skip_prefix(branch_name, "refs/heads/", &branch_name);
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001618 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001619 status_printf_more(s, branch_status_color, "%s", on_what);
1620 status_printf_more(s, branch_color, "%s\n", branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001621 if (!s->is_initial)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001622 wt_longstatus_print_tracking(s);
Junio C Hamanobda324c2007-01-03 01:09:34 -08001623 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001624
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001625 wt_longstatus_print_state(s, &state);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001626 free(state.branch);
1627 free(state.onto);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001628 free(state.detached_from);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001629
Jeff Kingc91f0d92006-09-08 04:05:34 -04001630 if (s->is_initial) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001631 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301632 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1633 s->commit_template
1634 ? _("Initial commit")
1635 : _("No commits yet"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001636 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001637 }
1638
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001639 wt_longstatus_print_updated(s);
1640 wt_longstatus_print_unmerged(s);
1641 wt_longstatus_print_changed(s);
Jens Lehmann46a958b2010-06-25 16:56:47 +02001642 if (s->submodule_summary &&
1643 (!s->ignore_submodule_arg ||
1644 strcmp(s->ignore_submodule_arg, "all"))) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001645 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1646 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
Jens Lehmannf17a5d32010-01-17 20:42:31 +01001647 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001648 if (s->show_untracked_files) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001649 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
Jameson Millereec0f7f2017-10-30 13:21:37 -04001650 if (s->show_ignored_mode)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001651 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001652 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001653 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001654 status_printf_ln(s, GIT_COLOR_NORMAL,
Jiang Xin62901172013-04-12 11:53:01 +08001655 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1656 "may speed it up, but you have to be careful not to forget to add\n"
1657 "new files yourself (see 'git help status')."),
1658 s->untracked_in_ms / 1000.0);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001659 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001660 } else if (s->commitable)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001661 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
Matthieu Moy6a964f52013-09-12 12:50:05 +02001662 s->hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001663 ? _(" (use -u option to show untracked files)") : "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001664
Jeff King1324fb62008-11-12 03:23:37 -05001665 if (s->verbose)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001666 wt_longstatus_print_verbose(s);
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001667 if (!s->commitable) {
1668 if (s->amend)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001669 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
Junio C Hamano37d07f82007-12-12 19:09:16 -08001670 else if (s->nowarn)
1671 ; /* nothing */
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001672 else if (s->workdir_dirty) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001673 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001674 printf(_("no changes added to commit "
1675 "(use \"git add\" and/or \"git commit -a\")\n"));
1676 else
1677 printf(_("no changes added to commit\n"));
1678 } else if (s->untracked.nr) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001679 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001680 printf(_("nothing added to commit but untracked files "
1681 "present (use \"git add\" to track)\n"));
1682 else
1683 printf(_("nothing added to commit but untracked files present\n"));
1684 } else if (s->is_initial) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001685 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001686 printf(_("nothing to commit (create/copy files "
1687 "and use \"git add\" to track)\n"));
1688 else
1689 printf(_("nothing to commit\n"));
1690 } else if (!s->show_untracked_files) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001691 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001692 printf(_("nothing to commit (use -u to show untracked files)\n"));
1693 else
1694 printf(_("nothing to commit\n"));
1695 } else
Lars Vogel2a0e6cd2016-06-09 20:19:30 +02001696 printf(_("nothing to commit, working tree clean\n"));
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001697 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001698 if(s->show_stash)
1699 wt_longstatus_print_stash_summary(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001700}
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001701
Jeff King3207a3a2012-05-07 15:44:44 -04001702static void wt_shortstatus_unmerged(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001703 struct wt_status *s)
1704{
1705 struct wt_status_change_data *d = it->util;
1706 const char *how = "??";
1707
1708 switch (d->stagemask) {
1709 case 1: how = "DD"; break; /* both deleted */
1710 case 2: how = "AU"; break; /* added by us */
1711 case 3: how = "UD"; break; /* deleted by them */
1712 case 4: how = "UA"; break; /* added by them */
1713 case 5: how = "DU"; break; /* deleted by us */
1714 case 6: how = "AA"; break; /* both added */
1715 case 7: how = "UU"; break; /* both modified */
1716 }
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001717 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
Jeff King3207a3a2012-05-07 15:44:44 -04001718 if (s->null_termination) {
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001719 fprintf(stdout, " %s%c", it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001720 } else {
1721 struct strbuf onebuf = STRBUF_INIT;
1722 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001723 one = quote_path(it->string, s->prefix, &onebuf);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001724 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001725 strbuf_release(&onebuf);
1726 }
1727}
1728
Jeff King3207a3a2012-05-07 15:44:44 -04001729static void wt_shortstatus_status(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001730 struct wt_status *s)
1731{
1732 struct wt_status_change_data *d = it->util;
1733
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001734 if (d->index_status)
1735 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1736 else
1737 putchar(' ');
1738 if (d->worktree_status)
1739 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1740 else
1741 putchar(' ');
1742 putchar(' ');
Jeff King3207a3a2012-05-07 15:44:44 -04001743 if (s->null_termination) {
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001744 fprintf(stdout, "%s%c", it->string, 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07001745 if (d->rename_source)
1746 fprintf(stdout, "%s%c", d->rename_source, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001747 } else {
1748 struct strbuf onebuf = STRBUF_INIT;
1749 const char *one;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07001750
1751 if (d->rename_source) {
1752 one = quote_path(d->rename_source, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001753 if (*one != '"' && strchr(one, ' ') != NULL) {
1754 putchar('"');
1755 strbuf_addch(&onebuf, '"');
1756 one = onebuf.buf;
1757 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001758 printf("%s -> ", one);
1759 strbuf_release(&onebuf);
1760 }
Jiang Xin39598f92013-06-25 23:53:45 +08001761 one = quote_path(it->string, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001762 if (*one != '"' && strchr(one, ' ') != NULL) {
1763 putchar('"');
1764 strbuf_addch(&onebuf, '"');
1765 one = onebuf.buf;
1766 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001767 printf("%s\n", one);
1768 strbuf_release(&onebuf);
1769 }
1770}
1771
Jeff King3207a3a2012-05-07 15:44:44 -04001772static void wt_shortstatus_other(struct string_list_item *it,
Junio C Hamano2381e392010-04-10 00:33:17 -07001773 struct wt_status *s, const char *sign)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001774{
Jeff King3207a3a2012-05-07 15:44:44 -04001775 if (s->null_termination) {
Junio C Hamano2381e392010-04-10 00:33:17 -07001776 fprintf(stdout, "%s %s%c", sign, it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001777 } else {
1778 struct strbuf onebuf = STRBUF_INIT;
1779 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001780 one = quote_path(it->string, s->prefix, &onebuf);
Junio C Hamanoc1909e72010-05-01 22:05:14 -07001781 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001782 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001783 strbuf_release(&onebuf);
1784 }
1785}
1786
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001787static void wt_shortstatus_print_tracking(struct wt_status *s)
1788{
1789 struct branch *branch;
1790 const char *header_color = color(WT_STATUS_HEADER, s);
1791 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1792 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1793
1794 const char *base;
René Scharfe5e8d2722017-07-08 12:51:01 +02001795 char *short_base;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001796 const char *branch_name;
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001797 int num_ours, num_theirs, sti;
Jiang Xinf2e08732013-08-26 15:02:48 +08001798 int upstream_is_gone = 0;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001799
1800 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1801
1802 if (!s->branch)
1803 return;
1804 branch_name = s->branch;
1805
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001806#define LABEL(string) (s->no_gettext ? (string) : _(string))
1807
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001808 if (s->is_initial)
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301809 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001810
René Scharfebaf0a3e2015-10-31 18:36:35 +01001811 if (!strcmp(s->branch, "HEAD")) {
1812 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001813 LABEL(N_("HEAD (no branch)")));
René Scharfebaf0a3e2015-10-31 18:36:35 +01001814 goto conclude;
1815 }
1816
René Scharfec72b49d2015-10-31 18:37:43 +01001817 skip_prefix(branch_name, "refs/heads/", &branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001818
René Scharfe8d8325f2015-10-31 18:37:12 +01001819 branch = branch_get(branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001820
Jiang Xinf2e08732013-08-26 15:02:48 +08001821 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1822
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001823 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
1824 s->ahead_behind_flags);
1825 if (sti < 0) {
René Scharfebcf8cc22015-10-31 18:36:01 +01001826 if (!base)
1827 goto conclude;
Jeff King979cb242015-05-21 20:49:11 -04001828
Jiang Xinf2e08732013-08-26 15:02:48 +08001829 upstream_is_gone = 1;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001830 }
1831
René Scharfe5e8d2722017-07-08 12:51:01 +02001832 short_base = shorten_unambiguous_ref(base, 0);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001833 color_fprintf(s->fp, header_color, "...");
René Scharfe5e8d2722017-07-08 12:51:01 +02001834 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
1835 free(short_base);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001836
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001837 if (!upstream_is_gone && !sti)
René Scharfebcf8cc22015-10-31 18:36:01 +01001838 goto conclude;
Jiang Xinf2234592013-08-26 15:02:49 +08001839
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001840 color_fprintf(s->fp, header_color, " [");
Jiang Xinf2e08732013-08-26 15:02:48 +08001841 if (upstream_is_gone) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001842 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001843 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
1844 color_fprintf(s->fp, header_color, LABEL(N_("different")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001845 } else if (!num_ours) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001846 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001847 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1848 } else if (!num_theirs) {
Michael J Gruberdf227242016-03-14 16:30:33 +01001849 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001850 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1851 } else {
Michael J Gruberdf227242016-03-14 16:30:33 +01001852 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001853 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
Matthieu Moy7a76c282014-03-20 13:12:41 +01001854 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001855 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1856 }
1857
Jeff Kinga5985232012-05-07 17:02:18 -04001858 color_fprintf(s->fp, header_color, "]");
René Scharfebcf8cc22015-10-31 18:36:01 +01001859 conclude:
Jeff Kinga5985232012-05-07 17:02:18 -04001860 fputc(s->null_termination ? '\0' : '\n', s->fp);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001861}
1862
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001863static void wt_shortstatus_print(struct wt_status *s)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001864{
Stefan Bellerd4aae452017-03-16 14:36:19 -07001865 struct string_list_item *it;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001866
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001867 if (s->show_branch)
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001868 wt_shortstatus_print_tracking(s);
1869
Stefan Bellerd4aae452017-03-16 14:36:19 -07001870 for_each_string_list_item(it, &s->change) {
1871 struct wt_status_change_data *d = it->util;
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001872
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001873 if (d->stagemask)
Jeff King3207a3a2012-05-07 15:44:44 -04001874 wt_shortstatus_unmerged(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001875 else
Jeff King3207a3a2012-05-07 15:44:44 -04001876 wt_shortstatus_status(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001877 }
Stefan Bellerd4aae452017-03-16 14:36:19 -07001878 for_each_string_list_item(it, &s->untracked)
Jeff King3207a3a2012-05-07 15:44:44 -04001879 wt_shortstatus_other(it, s, "??");
Junio C Hamano2381e392010-04-10 00:33:17 -07001880
Stefan Bellerd4aae452017-03-16 14:36:19 -07001881 for_each_string_list_item(it, &s->ignored)
Jeff King3207a3a2012-05-07 15:44:44 -04001882 wt_shortstatus_other(it, s, "!!");
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001883}
Jeff King4a7cc2f2009-12-07 00:17:15 -05001884
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001885static void wt_porcelain_print(struct wt_status *s)
Jeff King4a7cc2f2009-12-07 00:17:15 -05001886{
1887 s->use_color = 0;
Jeff King86617682009-12-07 00:26:25 -05001888 s->relative_paths = 0;
1889 s->prefix = NULL;
Matthieu Moy7a76c282014-03-20 13:12:41 +01001890 s->no_gettext = 1;
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001891 wt_shortstatus_print(s);
Jeff King4a7cc2f2009-12-07 00:17:15 -05001892}
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001893
Jeff Hostetler24959ba2016-08-11 10:45:58 -04001894/*
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001895 * Print branch information for porcelain v2 output. These lines
1896 * are printed when the '--branch' parameter is given.
1897 *
1898 * # branch.oid <commit><eol>
1899 * # branch.head <head><eol>
1900 * [# branch.upstream <upstream><eol>
1901 * [# branch.ab +<ahead> -<behind><eol>]]
1902 *
1903 * <commit> ::= the current commit hash or the the literal
1904 * "(initial)" to indicate an initialized repo
1905 * with no commits.
1906 *
1907 * <head> ::= <branch_name> the current branch name or
1908 * "(detached)" literal when detached head or
1909 * "(unknown)" when something is wrong.
1910 *
1911 * <upstream> ::= the upstream branch name, when set.
1912 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001913 * <ahead> ::= integer ahead value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001914 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001915 * <behind> ::= integer behind value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001916 *
1917 * The end-of-line is defined by the -z flag.
1918 *
1919 * <eol> ::= NUL when -z,
1920 * LF when NOT -z.
1921 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001922 * When an upstream is set and present, the 'branch.ab' line will
1923 * be printed with the ahead/behind counts for the branch and the
1924 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1925 * are different, '?' will be substituted for the actual count.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001926 */
1927static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1928{
1929 struct branch *branch;
1930 const char *base;
1931 const char *branch_name;
1932 struct wt_status_state state;
1933 int ab_info, nr_ahead, nr_behind;
1934 char eol = s->null_termination ? '\0' : '\n';
1935
1936 memset(&state, 0, sizeof(state));
1937 wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1938
1939 fprintf(s->fp, "# branch.oid %s%c",
1940 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1941 eol);
1942
1943 if (!s->branch)
1944 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1945 else {
1946 if (!strcmp(s->branch, "HEAD")) {
1947 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1948
1949 if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1950 branch_name = state.onto;
1951 else if (state.detached_from)
1952 branch_name = state.detached_from;
1953 else
1954 branch_name = "";
1955 } else {
1956 branch_name = NULL;
1957 skip_prefix(s->branch, "refs/heads/", &branch_name);
1958
1959 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1960 }
1961
1962 /* Lookup stats on the upstream tracking branch, if set. */
1963 branch = branch_get(branch_name);
1964 base = NULL;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001965 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
1966 &base, s->ahead_behind_flags);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001967 if (base) {
1968 base = shorten_unambiguous_ref(base, 0);
1969 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1970 free((char *)base);
1971
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001972 if (ab_info > 0) {
1973 /* different */
1974 if (nr_ahead || nr_behind)
1975 fprintf(s->fp, "# branch.ab +%d -%d%c",
1976 nr_ahead, nr_behind, eol);
1977 else
1978 fprintf(s->fp, "# branch.ab +? -?%c",
1979 eol);
1980 } else if (!ab_info) {
1981 /* same */
1982 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
1983 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001984 }
1985 }
1986
1987 free(state.branch);
1988 free(state.onto);
1989 free(state.detached_from);
1990}
1991
1992/*
Jeff Hostetler24959ba2016-08-11 10:45:58 -04001993 * Convert various submodule status values into a
1994 * fixed-length string of characters in the buffer provided.
1995 */
1996static void wt_porcelain_v2_submodule_state(
1997 struct wt_status_change_data *d,
1998 char sub[5])
1999{
2000 if (S_ISGITLINK(d->mode_head) ||
2001 S_ISGITLINK(d->mode_index) ||
2002 S_ISGITLINK(d->mode_worktree)) {
2003 sub[0] = 'S';
2004 sub[1] = d->new_submodule_commits ? 'C' : '.';
2005 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2006 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2007 } else {
2008 sub[0] = 'N';
2009 sub[1] = '.';
2010 sub[2] = '.';
2011 sub[3] = '.';
2012 }
2013 sub[4] = 0;
2014}
2015
2016/*
2017 * Fix-up changed entries before we print them.
2018 */
2019static void wt_porcelain_v2_fix_up_changed(
2020 struct string_list_item *it,
2021 struct wt_status *s)
2022{
2023 struct wt_status_change_data *d = it->util;
2024
2025 if (!d->index_status) {
2026 /*
2027 * This entry is unchanged in the index (relative to the head).
2028 * Therefore, the collect_updated_cb was never called for this
2029 * entry (during the head-vs-index scan) and so the head column
2030 * fields were never set.
2031 *
2032 * We must have data for the index column (from the
2033 * index-vs-worktree scan (otherwise, this entry should not be
2034 * in the list of changes)).
2035 *
2036 * Copy index column fields to the head column, so that our
2037 * output looks complete.
2038 */
2039 assert(d->mode_head == 0);
2040 d->mode_head = d->mode_index;
2041 oidcpy(&d->oid_head, &d->oid_index);
2042 }
2043
2044 if (!d->worktree_status) {
2045 /*
2046 * This entry is unchanged in the worktree (relative to the index).
2047 * Therefore, the collect_changed_cb was never called for this entry
2048 * (during the index-vs-worktree scan) and so the worktree column
2049 * fields were never set.
2050 *
2051 * We must have data for the index column (from the head-vs-index
2052 * scan).
2053 *
2054 * Copy the index column fields to the worktree column so that
2055 * our output looks complete.
2056 *
2057 * Note that we only have a mode field in the worktree column
2058 * because the scan code tries really hard to not have to compute it.
2059 */
2060 assert(d->mode_worktree == 0);
2061 d->mode_worktree = d->mode_index;
2062 }
2063}
2064
2065/*
2066 * Print porcelain v2 info for tracked entries with changes.
2067 */
2068static void wt_porcelain_v2_print_changed_entry(
2069 struct string_list_item *it,
2070 struct wt_status *s)
2071{
2072 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002073 struct strbuf buf = STRBUF_INIT;
2074 struct strbuf buf_from = STRBUF_INIT;
2075 const char *path = NULL;
2076 const char *path_from = NULL;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002077 char key[3];
2078 char submodule_token[5];
2079 char sep_char, eol_char;
2080
2081 wt_porcelain_v2_fix_up_changed(it, s);
2082 wt_porcelain_v2_submodule_state(d, submodule_token);
2083
2084 key[0] = d->index_status ? d->index_status : '.';
2085 key[1] = d->worktree_status ? d->worktree_status : '.';
2086 key[2] = 0;
2087
2088 if (s->null_termination) {
2089 /*
2090 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2091 * A single NUL character separates them.
2092 */
2093 sep_char = '\0';
2094 eol_char = '\0';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002095 path = it->string;
2096 path_from = d->rename_source;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002097 } else {
2098 /*
2099 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2100 * The source path is only present when necessary.
2101 * A single TAB separates them (because paths can contain spaces
2102 * which are not escaped and C-quoting does escape TAB characters).
2103 */
2104 sep_char = '\t';
2105 eol_char = '\n';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002106 path = quote_path(it->string, s->prefix, &buf);
2107 if (d->rename_source)
2108 path_from = quote_path(d->rename_source, s->prefix, &buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002109 }
2110
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002111 if (path_from)
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002112 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2113 key, submodule_token,
2114 d->mode_head, d->mode_index, d->mode_worktree,
2115 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002116 d->rename_status, d->rename_score,
2117 path, sep_char, path_from, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002118 else
2119 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2120 key, submodule_token,
2121 d->mode_head, d->mode_index, d->mode_worktree,
2122 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002123 path, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002124
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002125 strbuf_release(&buf);
2126 strbuf_release(&buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002127}
2128
2129/*
2130 * Print porcelain v2 status info for unmerged entries.
2131 */
2132static void wt_porcelain_v2_print_unmerged_entry(
2133 struct string_list_item *it,
2134 struct wt_status *s)
2135{
2136 struct wt_status_change_data *d = it->util;
2137 const struct cache_entry *ce;
2138 struct strbuf buf_index = STRBUF_INIT;
2139 const char *path_index = NULL;
2140 int pos, stage, sum;
2141 struct {
2142 int mode;
2143 struct object_id oid;
2144 } stages[3];
2145 char *key;
2146 char submodule_token[5];
2147 char unmerged_prefix = 'u';
2148 char eol_char = s->null_termination ? '\0' : '\n';
2149
2150 wt_porcelain_v2_submodule_state(d, submodule_token);
2151
2152 switch (d->stagemask) {
2153 case 1: key = "DD"; break; /* both deleted */
2154 case 2: key = "AU"; break; /* added by us */
2155 case 3: key = "UD"; break; /* deleted by them */
2156 case 4: key = "UA"; break; /* added by them */
2157 case 5: key = "DU"; break; /* deleted by us */
2158 case 6: key = "AA"; break; /* both added */
2159 case 7: key = "UU"; break; /* both modified */
2160 default:
2161 die("BUG: unhandled unmerged status %x", d->stagemask);
2162 }
2163
2164 /*
2165 * Disregard d.aux.porcelain_v2 data that we accumulated
2166 * for the head and index columns during the scans and
2167 * replace with the actual stage data.
2168 *
2169 * Note that this is a last-one-wins for each the individual
2170 * stage [123] columns in the event of multiple cache entries
2171 * for same stage.
2172 */
2173 memset(stages, 0, sizeof(stages));
2174 sum = 0;
2175 pos = cache_name_pos(it->string, strlen(it->string));
2176 assert(pos < 0);
2177 pos = -pos-1;
2178 while (pos < active_nr) {
2179 ce = active_cache[pos++];
2180 stage = ce_stage(ce);
2181 if (strcmp(ce->name, it->string) || !stage)
2182 break;
2183 stages[stage - 1].mode = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +01002184 oidcpy(&stages[stage - 1].oid, &ce->oid);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002185 sum |= (1 << (stage - 1));
2186 }
2187 if (sum != d->stagemask)
2188 die("BUG: observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2189
2190 if (s->null_termination)
2191 path_index = it->string;
2192 else
2193 path_index = quote_path(it->string, s->prefix, &buf_index);
2194
2195 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2196 unmerged_prefix, key, submodule_token,
2197 stages[0].mode, /* stage 1 */
2198 stages[1].mode, /* stage 2 */
2199 stages[2].mode, /* stage 3 */
2200 d->mode_worktree,
2201 oid_to_hex(&stages[0].oid), /* stage 1 */
2202 oid_to_hex(&stages[1].oid), /* stage 2 */
2203 oid_to_hex(&stages[2].oid), /* stage 3 */
2204 path_index,
2205 eol_char);
2206
2207 strbuf_release(&buf_index);
2208}
2209
2210/*
2211 * Print porcelain V2 status info for untracked and ignored entries.
2212 */
2213static void wt_porcelain_v2_print_other(
2214 struct string_list_item *it,
2215 struct wt_status *s,
2216 char prefix)
2217{
2218 struct strbuf buf = STRBUF_INIT;
2219 const char *path;
2220 char eol_char;
2221
2222 if (s->null_termination) {
2223 path = it->string;
2224 eol_char = '\0';
2225 } else {
2226 path = quote_path(it->string, s->prefix, &buf);
2227 eol_char = '\n';
2228 }
2229
2230 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2231
2232 strbuf_release(&buf);
2233}
2234
2235/*
2236 * Print porcelain V2 status.
2237 *
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002238 * [<v2_branch>]
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002239 * [<v2_changed_items>]*
2240 * [<v2_unmerged_items>]*
2241 * [<v2_untracked_items>]*
2242 * [<v2_ignored_items>]*
2243 *
2244 */
2245static void wt_porcelain_v2_print(struct wt_status *s)
2246{
2247 struct wt_status_change_data *d;
2248 struct string_list_item *it;
2249 int i;
2250
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002251 if (s->show_branch)
2252 wt_porcelain_v2_print_tracking(s);
2253
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002254 for (i = 0; i < s->change.nr; i++) {
2255 it = &(s->change.items[i]);
2256 d = it->util;
2257 if (!d->stagemask)
2258 wt_porcelain_v2_print_changed_entry(it, s);
2259 }
2260
2261 for (i = 0; i < s->change.nr; i++) {
2262 it = &(s->change.items[i]);
2263 d = it->util;
2264 if (d->stagemask)
2265 wt_porcelain_v2_print_unmerged_entry(it, s);
2266 }
2267
2268 for (i = 0; i < s->untracked.nr; i++) {
2269 it = &(s->untracked.items[i]);
2270 wt_porcelain_v2_print_other(it, s, '?');
2271 }
2272
2273 for (i = 0; i < s->ignored.nr; i++) {
2274 it = &(s->ignored.items[i]);
2275 wt_porcelain_v2_print_other(it, s, '!');
2276 }
2277}
2278
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002279void wt_status_print(struct wt_status *s)
2280{
2281 switch (s->status_format) {
2282 case STATUS_FORMAT_SHORT:
2283 wt_shortstatus_print(s);
2284 break;
2285 case STATUS_FORMAT_PORCELAIN:
2286 wt_porcelain_print(s);
2287 break;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002288 case STATUS_FORMAT_PORCELAIN_V2:
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002289 wt_porcelain_v2_print(s);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002290 break;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002291 case STATUS_FORMAT_UNSPECIFIED:
2292 die("BUG: finalize_deferred_config() should have been called");
2293 break;
2294 case STATUS_FORMAT_NONE:
2295 case STATUS_FORMAT_LONG:
2296 wt_longstatus_print(s);
2297 break;
2298 }
2299}
Johannes Schindelinfd849862016-10-07 18:08:38 +02002300
2301/**
2302 * Returns 1 if there are unstaged changes, 0 otherwise.
2303 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002304int has_unstaged_changes(int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002305{
2306 struct rev_info rev_info;
2307 int result;
2308
2309 init_revisions(&rev_info, NULL);
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002310 if (ignore_submodules) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002311 rev_info.diffopt.flags.ignore_submodules = 1;
Junio C Hamanob50d82b2017-11-15 12:14:30 +09002312 rev_info.diffopt.flags.override_submodule_config = 1;
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002313 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002314 rev_info.diffopt.flags.quick = 1;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002315 diff_setup_done(&rev_info.diffopt);
2316 result = run_diff_files(&rev_info, 0);
2317 return diff_result_code(&rev_info.diffopt, result);
2318}
2319
2320/**
2321 * Returns 1 if there are uncommitted changes, 0 otherwise.
2322 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002323int has_uncommitted_changes(int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002324{
2325 struct rev_info rev_info;
2326 int result;
2327
2328 if (is_cache_unborn())
2329 return 0;
2330
2331 init_revisions(&rev_info, NULL);
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002332 if (ignore_submodules)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002333 rev_info.diffopt.flags.ignore_submodules = 1;
2334 rev_info.diffopt.flags.quick = 1;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002335 add_head_to_pending(&rev_info);
2336 diff_setup_done(&rev_info.diffopt);
2337 result = run_diff_index(&rev_info, 1);
2338 return diff_result_code(&rev_info.diffopt, result);
2339}
2340
2341/**
2342 * If the work tree has unstaged or uncommitted changes, dies with the
2343 * appropriate message.
2344 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002345int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002346{
Martin Ågren837e34e2017-10-05 22:32:04 +02002347 struct lock_file lock_file = LOCK_INIT;
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002348 int err = 0, fd;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002349
Martin Ågren837e34e2017-10-05 22:32:04 +02002350 fd = hold_locked_index(&lock_file, 0);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002351 refresh_cache(REFRESH_QUIET);
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002352 if (0 <= fd)
Martin Ågren837e34e2017-10-05 22:32:04 +02002353 update_index_if_able(&the_index, &lock_file);
2354 rollback_lock_file(&lock_file);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002355
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002356 if (has_unstaged_changes(ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002357 /* TRANSLATORS: the action is e.g. "pull with rebase" */
Johannes Schindelin4777e172016-10-07 18:09:04 +02002358 error(_("cannot %s: You have unstaged changes."), _(action));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002359 err = 1;
2360 }
2361
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002362 if (has_uncommitted_changes(ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002363 if (err)
Johannes Schindelin4777e172016-10-07 18:09:04 +02002364 error(_("additionally, your index contains uncommitted changes."));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002365 else
Johannes Schindelin4777e172016-10-07 18:09:04 +02002366 error(_("cannot %s: Your index contains uncommitted changes."),
Johannes Schindelinfd849862016-10-07 18:08:38 +02002367 _(action));
2368 err = 1;
2369 }
2370
2371 if (err) {
2372 if (hint)
2373 error("%s", hint);
2374 if (!gently)
2375 exit(128);
2376 }
2377
2378 return err;
2379}