blob: d2a1bec226ba0dfef9e3ddd96c0b3a83ea6884bb [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"
Phillip Wood4a724862019-04-16 11:18:42 +010020#include "sequencer.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -040021
Nguyễn Thái Ngọc Duy983dc692014-02-17 19:15:30 +070022static const char cut_line[] =
Jens Lehmann1a72cfd2013-12-05 20:44:14 +010023"------------------------ >8 ------------------------\n";
24
Junio C Hamano23900a92009-08-09 23:08:40 -070025static char default_wt_status_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010026 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
27 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
28 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
29 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
30 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
Junio C Hamano4d4d5722009-08-05 00:04:51 -070031 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020032 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
33 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
Jeff King148135f2010-12-09 12:27:08 -050034 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
Jeff Kingc91f0d92006-09-08 04:05:34 -040035};
Junio C Hamano4d229652007-01-11 15:34:41 -080036
Junio C Hamanod249b092009-08-09 21:59:30 -070037static const char *color(int slot, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -040038{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070039 const char *c = "";
40 if (want_color(s->use_color))
41 c = s->color_palette[slot];
Jeff King148135f2010-12-09 12:27:08 -050042 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
43 c = s->color_palette[WT_STATUS_HEADER];
44 return c;
Jeff Kingc91f0d92006-09-08 04:05:34 -040045}
46
Jonathan Niederbecbdae2011-02-25 23:09:41 -060047static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
48 const char *fmt, va_list ap, const char *trail)
49{
50 struct strbuf sb = STRBUF_INIT;
51 struct strbuf linebuf = STRBUF_INIT;
52 const char *line, *eol;
53
54 strbuf_vaddf(&sb, fmt, ap);
55 if (!sb.len) {
Matthieu Moy2556b992013-09-06 19:43:07 +020056 if (s->display_comment_prefix) {
57 strbuf_addch(&sb, comment_line_char);
58 if (!trail)
59 strbuf_addch(&sb, ' ');
60 }
Jonathan Niederbecbdae2011-02-25 23:09:41 -060061 color_print_strbuf(s->fp, color, &sb);
62 if (trail)
63 fprintf(s->fp, "%s", trail);
64 strbuf_release(&sb);
65 return;
66 }
67 for (line = sb.buf; *line; line = eol + 1) {
68 eol = strchr(line, '\n');
69
70 strbuf_reset(&linebuf);
Matthieu Moy2556b992013-09-06 19:43:07 +020071 if (at_bol && s->display_comment_prefix) {
Junio C Hamanoeff80a92013-01-16 20:18:48 +010072 strbuf_addch(&linebuf, comment_line_char);
Jonathan Niederbecbdae2011-02-25 23:09:41 -060073 if (*line != '\n' && *line != '\t')
74 strbuf_addch(&linebuf, ' ');
75 }
76 if (eol)
77 strbuf_add(&linebuf, line, eol - line);
78 else
79 strbuf_addstr(&linebuf, line);
80 color_print_strbuf(s->fp, color, &linebuf);
81 if (eol)
82 fprintf(s->fp, "\n");
83 else
84 break;
85 at_bol = 1;
86 }
87 if (trail)
88 fprintf(s->fp, "%s", trail);
89 strbuf_release(&linebuf);
90 strbuf_release(&sb);
91}
92
93void status_printf_ln(struct wt_status *s, const char *color,
94 const char *fmt, ...)
95{
96 va_list ap;
97
98 va_start(ap, fmt);
99 status_vprintf(s, 1, color, fmt, ap, "\n");
100 va_end(ap);
101}
102
103void status_printf(struct wt_status *s, const char *color,
104 const char *fmt, ...)
105{
106 va_list ap;
107
108 va_start(ap, fmt);
109 status_vprintf(s, 1, color, fmt, ap, NULL);
110 va_end(ap);
111}
112
Junio C Hamano1e248452012-09-15 22:46:26 -0700113static void status_printf_more(struct wt_status *s, const char *color,
114 const char *fmt, ...)
Jonathan Niederbecbdae2011-02-25 23:09:41 -0600115{
116 va_list ap;
117
118 va_start(ap, fmt);
119 status_vprintf(s, 0, color, fmt, ap, NULL);
120 va_end(ap);
121}
122
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100123void wt_status_prepare(struct repository *r, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400124{
Junio C Hamanocc46a742007-02-09 16:22:42 -0800125 memset(s, 0, sizeof(*s));
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100126 s->repo = r;
Junio C Hamano23900a92009-08-09 23:08:40 -0700127 memcpy(s->color_palette, default_wt_status_colors,
128 sizeof(default_wt_status_colors));
Junio C Hamanod249b092009-08-09 21:59:30 -0700129 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
130 s->use_color = -1;
131 s->relative_paths = 1;
René Scharfeefbd4fd2017-10-01 09:29:03 +0200132 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400133 s->reference = "HEAD";
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400134 s->fp = stdout;
Kristian Høgsberg0f729f22007-09-17 20:06:43 -0400135 s->index_file = get_index_file();
Junio C Hamano50b7e702009-08-04 23:49:33 -0700136 s->change.strdup_strings = 1;
Junio C Hamano76378682009-08-10 00:36:33 -0700137 s->untracked.strdup_strings = 1;
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700138 s->ignored.strdup_strings = 1;
Junio C Hamano84b42022013-06-24 11:41:40 -0700139 s->show_branch = -1; /* unspecified */
Liam Beguinc1b5d012017-06-17 18:30:51 -0400140 s->show_stash = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +0000141 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
Matthieu Moy2556b992013-09-06 19:43:07 +0200142 s->display_comment_prefix = 0;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000143 s->detect_rename = -1;
144 s->rename_score = -1;
145 s->rename_limit = -1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400146}
147
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400148static void wt_longstatus_print_unmerged_header(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700149{
Lucien Kong96b0ec12012-06-05 22:21:26 +0200150 int i;
151 int del_mod_conflict = 0;
152 int both_deleted = 0;
153 int not_deleted = 0;
Junio C Hamanod249b092009-08-09 21:59:30 -0700154 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800155
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000156 status_printf_ln(s, c, _("Unmerged paths:"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200157
158 for (i = 0; i < s->change.nr; i++) {
159 struct string_list_item *it = &(s->change.items[i]);
160 struct wt_status_change_data *d = it->util;
161
162 switch (d->stagemask) {
163 case 0:
164 break;
165 case 1:
166 both_deleted = 1;
167 break;
168 case 3:
169 case 5:
170 del_mod_conflict = 1;
171 break;
172 default:
173 not_deleted = 1;
174 break;
175 }
176 }
177
Matthieu Moy6a964f52013-09-12 12:50:05 +0200178 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400179 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500180 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800181 ;
182 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000183 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700184 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000185 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200186
187 if (!both_deleted) {
188 if (!del_mod_conflict)
189 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
190 else
191 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
192 } else if (!del_mod_conflict && !not_deleted) {
193 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
194 } else {
195 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
196 }
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500197 status_printf_ln(s, c, "%s", "");
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700198}
199
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400200static void wt_longstatus_print_cached_header(struct wt_status *s)
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100201{
Junio C Hamanod249b092009-08-09 21:59:30 -0700202 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800203
Ævar Arnfjörð Bjarmason919a4ce2011-02-22 23:42:16 +0000204 status_printf_ln(s, c, _("Changes to be committed:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200205 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400206 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500207 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800208 ; /* NEEDSWORK: use "git reset --unresolve"??? */
209 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000210 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano3c588452009-12-11 23:53:41 -0800211 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000212 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500213 status_printf_ln(s, c, "%s", "");
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100214}
215
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400216static void wt_longstatus_print_dirty_header(struct wt_status *s,
217 int has_deleted,
218 int has_dirty_submodules)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400219{
Junio C Hamanod249b092009-08-09 21:59:30 -0700220 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800221
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000222 status_printf_ln(s, c, _("Changes not staged for commit:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200223 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400224 return;
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200225 if (!has_deleted)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000226 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200227 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000228 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
229 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100230 if (has_dirty_submodules)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000231 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500232 status_printf_ln(s, c, "%s", "");
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200233}
234
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400235static void wt_longstatus_print_other_header(struct wt_status *s,
236 const char *what,
237 const char *how)
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200238{
Junio C Hamanod249b092009-08-09 21:59:30 -0700239 const char *c = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +0700240 status_printf_ln(s, c, "%s:", what);
Matthieu Moy6a964f52013-09-12 12:50:05 +0200241 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400242 return;
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000243 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500244 status_printf_ln(s, c, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400245}
246
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400247static void wt_longstatus_print_trailer(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400248{
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500249 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400250}
251
Dmitry Potapova734d0b2008-03-07 05:30:58 +0300252#define quote_path quote_path_relative
Junio C Hamano3a946802006-11-08 13:20:46 -0800253
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800254static const char *wt_status_unmerged_status_string(int stagemask)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700255{
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800256 switch (stagemask) {
257 case 1:
258 return _("both deleted:");
259 case 2:
260 return _("added by us:");
261 case 3:
262 return _("deleted by them:");
263 case 4:
264 return _("added by them:");
265 case 5:
266 return _("deleted by us:");
267 case 6:
268 return _("both added:");
269 case 7:
270 return _("both modified:");
271 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200272 BUG("unhandled unmerged status %x", stagemask);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700273 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700274}
275
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700276static const char *wt_status_diff_status_string(int status)
277{
278 switch (status) {
279 case DIFF_STATUS_ADDED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700280 return _("new file:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700281 case DIFF_STATUS_COPIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700282 return _("copied:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700283 case DIFF_STATUS_DELETED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700284 return _("deleted:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700285 case DIFF_STATUS_MODIFIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700286 return _("modified:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700287 case DIFF_STATUS_RENAMED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700288 return _("renamed:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700289 case DIFF_STATUS_TYPE_CHANGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700290 return _("typechange:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700291 case DIFF_STATUS_UNKNOWN:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700292 return _("unknown:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700293 case DIFF_STATUS_UNMERGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700294 return _("unmerged:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700295 default:
296 return NULL;
297 }
298}
299
Jonathan Nieder335e8252013-12-19 11:43:19 -0800300static int maxwidth(const char *(*label)(int), int minval, int maxval)
301{
302 int result = 0, i;
303
304 for (i = minval; i <= maxval; i++) {
305 const char *s = label(i);
306 int len = s ? utf8_strwidth(s) : 0;
307 if (len > result)
308 result = len;
309 }
310 return result;
311}
312
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400313static void wt_longstatus_print_unmerged_data(struct wt_status *s,
314 struct string_list_item *it)
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800315{
316 const char *c = color(WT_STATUS_UNMERGED, s);
317 struct wt_status_change_data *d = it->util;
318 struct strbuf onebuf = STRBUF_INIT;
319 static char *padding;
320 static int label_width;
321 const char *one, *how;
322 int len;
323
324 if (!padding) {
325 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
326 label_width += strlen(" ");
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800327 padding = xmallocz(label_width);
328 memset(padding, ' ', label_width);
329 }
330
331 one = quote_path(it->string, s->prefix, &onebuf);
332 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
333
334 how = wt_status_unmerged_status_string(d->stagemask);
335 len = label_width - utf8_strwidth(how);
336 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
337 strbuf_release(&onebuf);
338}
339
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400340static void wt_longstatus_print_change_data(struct wt_status *s,
341 int change_type,
342 struct string_list_item *it)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400343{
Junio C Hamano50b7e702009-08-04 23:49:33 -0700344 struct wt_status_change_data *d = it->util;
Junio C Hamanod249b092009-08-09 21:59:30 -0700345 const char *c = color(change_type, s);
Jeff Kingb8527d52013-03-21 07:05:28 -0400346 int status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700347 char *one_name;
348 char *two_name;
Junio C Hamano3a946802006-11-08 13:20:46 -0800349 const char *one, *two;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500350 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100351 struct strbuf extra = STRBUF_INIT;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700352 static char *padding;
Junio C Hamanod52cb572014-03-12 13:51:22 -0700353 static int label_width;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700354 const char *what;
355 int len;
356
357 if (!padding) {
Jonathan Nieder335e8252013-12-19 11:43:19 -0800358 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
359 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
Junio C Hamanod52cb572014-03-12 13:51:22 -0700360 label_width += strlen(" ");
361 padding = xmallocz(label_width);
362 memset(padding, ' ', label_width);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700363 }
Junio C Hamano3a946802006-11-08 13:20:46 -0800364
Junio C Hamano50b7e702009-08-04 23:49:33 -0700365 one_name = two_name = it->string;
366 switch (change_type) {
367 case WT_STATUS_UPDATED:
368 status = d->index_status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700369 break;
370 case WT_STATUS_CHANGED:
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100371 if (d->new_submodule_commits || d->dirty_submodule) {
372 strbuf_addstr(&extra, " (");
373 if (d->new_submodule_commits)
René Scharfea22ae752016-09-15 20:31:00 +0200374 strbuf_addstr(&extra, _("new commits, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100375 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
René Scharfea22ae752016-09-15 20:31:00 +0200376 strbuf_addstr(&extra, _("modified content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100377 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
René Scharfea22ae752016-09-15 20:31:00 +0200378 strbuf_addstr(&extra, _("untracked content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100379 strbuf_setlen(&extra, extra.len - 2);
380 strbuf_addch(&extra, ')');
381 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700382 status = d->worktree_status;
383 break;
Jeff Kingb8527d52013-03-21 07:05:28 -0400384 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200385 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
Jeff Kingb8527d52013-03-21 07:05:28 -0400386 change_type);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700387 }
388
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700389 /*
390 * Only pick up the rename it's relevant. If the rename is for
391 * the changed section and we're printing the updated section,
392 * ignore it.
393 */
394 if (d->rename_status == status)
395 one_name = d->rename_source;
396
Jiang Xin39598f92013-06-25 23:53:45 +0800397 one = quote_path(one_name, s->prefix, &onebuf);
398 two = quote_path(two_name, s->prefix, &twobuf);
Junio C Hamano3a946802006-11-08 13:20:46 -0800399
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600400 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700401 what = wt_status_diff_status_string(status);
402 if (!what)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200403 BUG("unhandled diff status %c", status);
Junio C Hamanod52cb572014-03-12 13:51:22 -0700404 len = label_width - utf8_strwidth(what);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700405 assert(len >= 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700406 if (one_name != two_name)
Junio C Hamanod52cb572014-03-12 13:51:22 -0700407 status_printf_more(s, c, "%s%.*s%s -> %s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700408 what, len, padding, one, two);
409 else
Junio C Hamanod52cb572014-03-12 13:51:22 -0700410 status_printf_more(s, c, "%s%.*s%s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700411 what, len, padding, one);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100412 if (extra.len) {
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600413 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100414 strbuf_release(&extra);
415 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600416 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
Johannes Schindelin367c9882007-11-11 17:35:41 +0000417 strbuf_release(&onebuf);
418 strbuf_release(&twobuf);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400419}
420
Nguyễn Thái Ngọc Duy98bc94e2017-12-27 17:18:36 +0700421static char short_submodule_status(struct wt_status_change_data *d)
422{
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700423 if (d->new_submodule_commits)
424 return 'M';
425 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
426 return 'm';
427 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
428 return '?';
429 return d->worktree_status;
430}
431
Junio C Hamano50b7e702009-08-04 23:49:33 -0700432static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
433 struct diff_options *options,
434 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400435{
436 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400437 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700438
439 if (!q->nr)
440 return;
441 s->workdir_dirty = 1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400442 for (i = 0; i < q->nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700443 struct diff_filepair *p;
444 struct string_list_item *it;
445 struct wt_status_change_data *d;
446
447 p = q->queue[i];
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700448 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700449 d = it->util;
450 if (!d) {
451 d = xcalloc(1, sizeof(*d));
452 it->util = d;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400453 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700454 if (!d->worktree_status)
455 d->worktree_status = p->status;
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700456 if (S_ISGITLINK(p->two->mode)) {
457 d->dirty_submodule = p->two->dirty_submodule;
Jeff King4a7e27e2018-08-28 17:22:40 -0400458 d->new_submodule_commits = !oideq(&p->one->oid,
459 &p->two->oid);
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700460 if (s->status_format == STATUS_FORMAT_SHORT)
461 d->worktree_status = short_submodule_status(d);
462 }
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400463
464 switch (p->status) {
465 case DIFF_STATUS_ADDED:
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700466 d->mode_worktree = p->two->mode;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400467 break;
468
469 case DIFF_STATUS_DELETED:
470 d->mode_index = p->one->mode;
471 oidcpy(&d->oid_index, &p->one->oid);
472 /* mode_worktree is zero for a delete. */
473 break;
474
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700475 case DIFF_STATUS_COPIED:
476 case DIFF_STATUS_RENAMED:
477 if (d->rename_status)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200478 BUG("multiple renames on the same target? how?");
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700479 d->rename_source = xstrdup(p->one->path);
480 d->rename_score = p->score * 100 / MAX_SCORE;
481 d->rename_status = p->status;
482 /* fallthru */
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400483 case DIFF_STATUS_MODIFIED:
484 case DIFF_STATUS_TYPE_CHANGED:
485 case DIFF_STATUS_UNMERGED:
486 d->mode_index = p->one->mode;
487 d->mode_worktree = p->two->mode;
488 oidcpy(&d->oid_index, &p->one->oid);
489 break;
490
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700491 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200492 BUG("unhandled diff-files status '%c'", p->status);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400493 break;
494 }
495
Jeff Kingc91f0d92006-09-08 04:05:34 -0400496 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400497}
498
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100499static int unmerged_mask(struct index_state *istate, const char *path)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700500{
501 int pos, mask;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700502 const struct cache_entry *ce;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700503
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100504 pos = index_name_pos(istate, path, strlen(path));
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700505 if (0 <= pos)
506 return 0;
507
508 mask = 0;
509 pos = -pos-1;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100510 while (pos < istate->cache_nr) {
511 ce = istate->cache[pos++];
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700512 if (strcmp(ce->name, path) || !ce_stage(ce))
513 break;
514 mask |= (1 << (ce_stage(ce) - 1));
515 }
516 return mask;
517}
518
Junio C Hamano50b7e702009-08-04 23:49:33 -0700519static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
520 struct diff_options *options,
521 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400522{
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100523 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400524 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700525
526 for (i = 0; i < q->nr; i++) {
527 struct diff_filepair *p;
528 struct string_list_item *it;
529 struct wt_status_change_data *d;
530
531 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100532 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700533 d = it->util;
534 if (!d) {
535 d = xcalloc(1, sizeof(*d));
536 it->util = d;
537 }
538 if (!d->index_status)
539 d->index_status = p->status;
540 switch (p->status) {
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400541 case DIFF_STATUS_ADDED:
542 /* Leave {mode,oid}_head zero for an add. */
543 d->mode_index = p->two->mode;
544 oidcpy(&d->oid_index, &p->two->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700545 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400546 break;
547 case DIFF_STATUS_DELETED:
548 d->mode_head = p->one->mode;
549 oidcpy(&d->oid_head, &p->one->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700550 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400551 /* Leave {mode,oid}_index zero for a delete. */
552 break;
553
Junio C Hamano50b7e702009-08-04 23:49:33 -0700554 case DIFF_STATUS_COPIED:
555 case DIFF_STATUS_RENAMED:
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700556 if (d->rename_status)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200557 BUG("multiple renames on the same target? how?");
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700558 d->rename_source = xstrdup(p->one->path);
559 d->rename_score = p->score * 100 / MAX_SCORE;
560 d->rename_status = p->status;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400561 /* fallthru */
562 case DIFF_STATUS_MODIFIED:
563 case DIFF_STATUS_TYPE_CHANGED:
564 d->mode_head = p->one->mode;
565 d->mode_index = p->two->mode;
566 oidcpy(&d->oid_head, &p->one->oid);
567 oidcpy(&d->oid_index, &p->two->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700568 s->committable = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700569 break;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700570 case DIFF_STATUS_UNMERGED:
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100571 d->stagemask = unmerged_mask(s->repo->index,
572 p->two->path);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400573 /*
574 * Don't bother setting {mode,oid}_{head,index} since the print
575 * code will output the stage values directly and not use the
576 * values in these fields.
577 */
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700578 break;
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700579
580 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200581 BUG("unhandled diff-index status '%c'", p->status);
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700582 break;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700583 }
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100584 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400585}
586
Junio C Hamano50b7e702009-08-04 23:49:33 -0700587static void wt_status_collect_changes_worktree(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400588{
589 struct rev_info rev;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700590
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100591 repo_init_revisions(s->repo, &rev, NULL);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700592 setup_revisions(0, NULL, &rev, NULL);
593 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700594 rev.diffopt.flags.dirty_submodules = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700595 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100596 if (!s->show_untracked_files)
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700597 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200598 if (s->ignore_submodule_arg) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700599 rev.diffopt.flags.override_submodule_config = 1;
Jens Lehmann46a958b2010-06-25 16:56:47 +0200600 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Nguyễn Thái Ngọc Duyc4c42f22011-10-24 15:24:51 +1100601 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700602 rev.diffopt.format_callback = wt_status_collect_changed_cb;
603 rev.diffopt.format_callback_data = s;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000604 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
605 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
606 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700607 copy_pathspec(&rev.prune_data, &s->pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700608 run_diff_files(&rev, 0);
609}
610
611static void wt_status_collect_changes_index(struct wt_status *s)
612{
613 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800614 struct setup_revision_opt opt;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700615
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100616 repo_init_revisions(s->repo, &rev, NULL);
Junio C Hamano32962c92010-03-08 22:58:09 -0800617 memset(&opt, 0, sizeof(opt));
brian m. carlsonf2e51192018-05-02 00:26:00 +0000618 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
Junio C Hamano32962c92010-03-08 22:58:09 -0800619 setup_revisions(0, NULL, &rev, &opt);
620
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700621 rev.diffopt.flags.override_submodule_config = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700622 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200623 if (s->ignore_submodule_arg) {
Jens Lehmann46a958b2010-06-25 16:56:47 +0200624 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Jens Lehmann1d2f3932014-04-05 18:59:03 +0200625 } else {
626 /*
627 * Unless the user did explicitly request a submodule ignore
628 * mode by passing a command line option we do not ignore any
629 * changed submodule SHA-1s when comparing index and HEAD, no
630 * matter what is configured. Otherwise the user won't be
631 * shown any submodules she manually added (and which are
632 * staged to be committed), which would be really confusing.
633 */
634 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200635 }
Jens Lehmann46a958b2010-06-25 16:56:47 +0200636
Jeff Kingc91f0d92006-09-08 04:05:34 -0400637 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700638 rev.diffopt.format_callback = wt_status_collect_updated_cb;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400639 rev.diffopt.format_callback_data = s;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000640 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
641 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
642 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700643 copy_pathspec(&rev.prune_data, &s->pathspec);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400644 run_diff_index(&rev, 1);
645}
646
Junio C Hamano50b7e702009-08-04 23:49:33 -0700647static void wt_status_collect_changes_initial(struct wt_status *s)
648{
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100649 struct index_state *istate = s->repo->index;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700650 int i;
651
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100652 for (i = 0; i < istate->cache_nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700653 struct string_list_item *it;
654 struct wt_status_change_data *d;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100655 const struct cache_entry *ce = istate->cache[i];
Junio C Hamano50b7e702009-08-04 23:49:33 -0700656
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100657 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700658 continue;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700659 if (ce_intent_to_add(ce))
660 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100661 it = string_list_insert(&s->change, ce->name);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700662 d = it->util;
663 if (!d) {
664 d = xcalloc(1, sizeof(*d));
665 it->util = d;
666 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700667 if (ce_stage(ce)) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700668 d->index_status = DIFF_STATUS_UNMERGED;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700669 d->stagemask |= (1 << (ce_stage(ce) - 1));
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400670 /*
671 * Don't bother setting {mode,oid}_{head,index} since the print
672 * code will output the stage values directly and not use the
673 * values in these fields.
674 */
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700675 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400676 } else {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700677 d->index_status = DIFF_STATUS_ADDED;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400678 /* Leave {mode,oid}_head zero for adds. */
679 d->mode_index = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +0100680 oidcpy(&d->oid_index, &ce->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700681 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400682 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700683 }
684}
685
Junio C Hamano76378682009-08-10 00:36:33 -0700686static void wt_status_collect_untracked(struct wt_status *s)
687{
688 int i;
689 struct dir_struct dir;
Karsten Blees132d41e2014-07-12 02:07:36 +0200690 uint64_t t_begin = getnanotime();
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100691 struct index_state *istate = s->repo->index;
Junio C Hamano76378682009-08-10 00:36:33 -0700692
693 if (!s->show_untracked_files)
694 return;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700695
Junio C Hamano76378682009-08-10 00:36:33 -0700696 memset(&dir, 0, sizeof(dir));
697 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
698 dir.flags |=
699 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400700 if (s->show_ignored_mode) {
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200701 dir.flags |= DIR_SHOW_IGNORED_TOO;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400702
703 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
704 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
705 } else {
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100706 dir.untracked = istate->untracked;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400707 }
708
Junio C Hamano76378682009-08-10 00:36:33 -0700709 setup_standard_excludes(&dir);
710
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100711 fill_directory(&dir, istate, &s->pathspec);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200712
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400713 for (i = 0; i < dir.nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700714 struct dir_entry *ent = dir.entries[i];
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100715 if (index_name_is_other(istate, ent->name, ent->len) &&
716 dir_path_match(istate, ent, &s->pathspec, 0, NULL))
Brandon Caseyb8224232010-09-26 21:49:13 -0500717 string_list_insert(&s->untracked, ent->name);
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700718 free(ent);
Junio C Hamano76378682009-08-10 00:36:33 -0700719 }
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700720
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200721 for (i = 0; i < dir.ignored_nr; i++) {
722 struct dir_entry *ent = dir.ignored[i];
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100723 if (index_name_is_other(istate, ent->name, ent->len) &&
724 dir_path_match(istate, ent, &s->pathspec, 0, NULL))
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200725 string_list_insert(&s->ignored, ent->name);
726 free(ent);
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700727 }
728
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700729 free(dir.entries);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200730 free(dir.ignored);
731 clear_directory(&dir);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700732
Karsten Blees132d41e2014-07-12 02:07:36 +0200733 if (advice_status_u_option)
734 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
Junio C Hamano76378682009-08-10 00:36:33 -0700735}
736
Stephen P. Smithc01d8f92018-09-05 17:53:26 -0700737static int has_unmerged(struct wt_status *s)
738{
739 int i;
740
741 for (i = 0; i < s->change.nr; i++) {
742 struct wt_status_change_data *d;
743 d = s->change.items[i].util;
744 if (d->stagemask)
745 return 1;
746 }
747 return 0;
748}
749
Junio C Hamano76378682009-08-10 00:36:33 -0700750void wt_status_collect(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700751{
Jeff Hostetler942b2742019-02-22 14:25:03 -0800752 trace2_region_enter("status", "worktrees", s->repo);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700753 wt_status_collect_changes_worktree(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800754 trace2_region_leave("status", "worktrees", s->repo);
755
756 if (s->is_initial) {
757 trace2_region_enter("status", "initial", s->repo);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700758 wt_status_collect_changes_initial(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800759 trace2_region_leave("status", "initial", s->repo);
760 } else {
761 trace2_region_enter("status", "index", s->repo);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700762 wt_status_collect_changes_index(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800763 trace2_region_leave("status", "index", s->repo);
764 }
765
766 trace2_region_enter("status", "untracked", s->repo);
Junio C Hamano76378682009-08-10 00:36:33 -0700767 wt_status_collect_untracked(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800768 trace2_region_leave("status", "untracked", s->repo);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700769
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +0100770 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700771 if (s->state.merge_in_progress && !has_unmerged(s))
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700772 s->committable = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700773}
774
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700775void wt_status_collect_free_buffers(struct wt_status *s)
776{
777 free(s->state.branch);
778 free(s->state.onto);
779 free(s->state.detached_from);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700780}
781
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400782static void wt_longstatus_print_unmerged(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700783{
784 int shown_header = 0;
785 int i;
786
787 for (i = 0; i < s->change.nr; i++) {
788 struct wt_status_change_data *d;
789 struct string_list_item *it;
790 it = &(s->change.items[i]);
791 d = it->util;
792 if (!d->stagemask)
793 continue;
794 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400795 wt_longstatus_print_unmerged_header(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700796 shown_header = 1;
797 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400798 wt_longstatus_print_unmerged_data(s, it);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700799 }
800 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400801 wt_longstatus_print_trailer(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700802
803}
804
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400805static void wt_longstatus_print_updated(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700806{
807 int shown_header = 0;
808 int i;
809
810 for (i = 0; i < s->change.nr; i++) {
811 struct wt_status_change_data *d;
812 struct string_list_item *it;
813 it = &(s->change.items[i]);
814 d = it->util;
815 if (!d->index_status ||
816 d->index_status == DIFF_STATUS_UNMERGED)
817 continue;
818 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400819 wt_longstatus_print_cached_header(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700820 shown_header = 1;
821 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400822 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700823 }
824 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400825 wt_longstatus_print_trailer(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700826}
827
828/*
829 * -1 : has delete
830 * 0 : no change
831 * 1 : some change but no delete
832 */
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100833static int wt_status_check_worktree_changes(struct wt_status *s,
834 int *dirty_submodules)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700835{
836 int i;
837 int changes = 0;
838
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100839 *dirty_submodules = 0;
840
Junio C Hamano50b7e702009-08-04 23:49:33 -0700841 for (i = 0; i < s->change.nr; i++) {
842 struct wt_status_change_data *d;
843 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700844 if (!d->worktree_status ||
845 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700846 continue;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100847 if (!changes)
848 changes = 1;
849 if (d->dirty_submodule)
850 *dirty_submodules = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700851 if (d->worktree_status == DIFF_STATUS_DELETED)
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100852 changes = -1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700853 }
854 return changes;
855}
856
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400857static void wt_longstatus_print_changed(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400858{
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100859 int i, dirty_submodules;
860 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700861
862 if (!worktree_changes)
863 return;
864
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400865 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700866
867 for (i = 0; i < s->change.nr; i++) {
868 struct wt_status_change_data *d;
869 struct string_list_item *it;
870 it = &(s->change.items[i]);
871 d = it->util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700872 if (!d->worktree_status ||
873 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700874 continue;
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400875 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700876 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400877 wt_longstatus_print_trailer(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400878}
879
Liam Beguinc1b5d012017-06-17 18:30:51 -0400880static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
881 const char *email, timestamp_t timestamp, int tz,
882 const char *message, void *cb_data)
883{
884 int *c = cb_data;
885 (*c)++;
886 return 0;
887}
888
889static void wt_longstatus_print_stash_summary(struct wt_status *s)
890{
891 int stash_count = 0;
892
893 for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
894 if (stash_count > 0)
895 status_printf_ln(s, GIT_COLOR_NORMAL,
896 Q_("Your stash currently has %d entry",
897 "Your stash currently has %d entries", stash_count),
898 stash_count);
899}
900
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400901static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
Ping Yinac8d5af2008-04-12 23:05:32 +0800902{
René Scharfed3180272014-08-19 21:09:35 +0200903 struct child_process sm_summary = CHILD_PROCESS_INIT;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200904 struct strbuf cmd_stdout = STRBUF_INIT;
905 struct strbuf summary = STRBUF_INIT;
906 char *summary_content;
Ping Yinac8d5af2008-04-12 23:05:32 +0800907
René Scharfea9154592014-10-19 13:14:20 +0200908 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
909 s->index_file);
Ping Yinac8d5af2008-04-12 23:05:32 +0800910
René Scharfea2bae2d2014-11-09 14:49:54 +0100911 argv_array_push(&sm_summary.args, "submodule");
912 argv_array_push(&sm_summary.args, "summary");
913 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
914 argv_array_push(&sm_summary.args, "--for-status");
915 argv_array_push(&sm_summary.args, "--summary-limit");
916 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200917 if (!uncommitted)
René Scharfea2bae2d2014-11-09 14:49:54 +0100918 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200919
Ping Yinac8d5af2008-04-12 23:05:32 +0800920 sm_summary.git_cmd = 1;
921 sm_summary.no_stdin = 1;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200922
Jeff King5c950e92015-03-22 23:53:52 -0400923 capture_command(&sm_summary, &cmd_stdout, 1024);
Matthieu Moy3ba74072013-09-06 19:43:06 +0200924
925 /* prepend header, only if there's an actual output */
Jeff Kingd56d9662015-03-22 06:00:32 -0400926 if (cmd_stdout.len) {
Matthieu Moy3ba74072013-09-06 19:43:06 +0200927 if (uncommitted)
928 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
929 else
930 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
931 strbuf_addstr(&summary, "\n\n");
932 }
933 strbuf_addbuf(&summary, &cmd_stdout);
934 strbuf_release(&cmd_stdout);
935
Matthieu Moy2556b992013-09-06 19:43:07 +0200936 if (s->display_comment_prefix) {
Jeff Kingd56d9662015-03-22 06:00:32 -0400937 size_t len;
Matthieu Moy2556b992013-09-06 19:43:07 +0200938 summary_content = strbuf_detach(&summary, &len);
939 strbuf_add_commented_lines(&summary, summary_content, len);
940 free(summary_content);
941 }
Matthieu Moy3ba74072013-09-06 19:43:06 +0200942
943 fputs(summary.buf, s->fp);
944 strbuf_release(&summary);
Ping Yinac8d5af2008-04-12 23:05:32 +0800945}
946
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400947static void wt_longstatus_print_other(struct wt_status *s,
948 struct string_list *l,
949 const char *what,
950 const char *how)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400951{
Jeff Kingc91f0d92006-09-08 04:05:34 -0400952 int i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500953 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700954 static struct string_list output = STRING_LIST_INIT_DUP;
955 struct column_options copts;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400956
Jeff King12829882011-06-02 01:54:49 -0400957 if (!l->nr)
Junio C Hamano76378682009-08-10 00:36:33 -0700958 return;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400959
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400960 wt_longstatus_print_other_header(s, what, how);
Junio C Hamano1b908b62010-04-10 00:19:46 -0700961
962 for (i = 0; i < l->nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700963 struct string_list_item *it;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700964 const char *path;
Junio C Hamano1b908b62010-04-10 00:19:46 -0700965 it = &(l->items[i]);
Jiang Xin39598f92013-06-25 23:53:45 +0800966 path = quote_path(it->string, s->prefix, &buf);
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700967 if (column_active(s->colopts)) {
968 string_list_append(&output, path);
969 continue;
970 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600971 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
972 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700973 "%s\n", path);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400974 }
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700975
976 strbuf_release(&buf);
977 if (!column_active(s->colopts))
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200978 goto conclude;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700979
Matthieu Moy2556b992013-09-06 19:43:07 +0200980 strbuf_addf(&buf, "%s%s\t%s",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700981 color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +0200982 s->display_comment_prefix ? "#" : "",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700983 color(WT_STATUS_UNTRACKED, s));
984 memset(&copts, 0, sizeof(copts));
985 copts.padding = 1;
986 copts.indent = buf.buf;
987 if (want_color(s->use_color))
988 copts.nl = GIT_COLOR_RESET "\n";
989 print_columns(&output, s->colopts, &copts);
990 string_list_clear(&output, 0);
Johannes Schindelin367c9882007-11-11 17:35:41 +0000991 strbuf_release(&buf);
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200992conclude:
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500993 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400994}
995
Brian Malehornd76650b2017-05-15 23:06:49 -0700996size_t wt_status_locate_end(const char *s, size_t len)
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100997{
998 const char *p;
999 struct strbuf pattern = STRBUF_INIT;
1000
SZEDER Gáborfbfa0972015-06-09 02:28:34 +02001001 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
Brian Malehornd76650b2017-05-15 23:06:49 -07001002 if (starts_with(s, pattern.buf + 1))
1003 len = 0;
1004 else if ((p = strstr(s, pattern.buf)))
1005 len = p - s + 1;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001006 strbuf_release(&pattern);
Brian Malehornd76650b2017-05-15 23:06:49 -07001007 return len;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001008}
1009
Denton Liud540b702019-04-17 11:23:27 +01001010void wt_status_append_cut_line(struct strbuf *buf)
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001011{
Kaartic Sivaraam8c4b1a32017-09-13 13:05:38 +00001012 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
Denton Liud540b702019-04-17 11:23:27 +01001013
1014 strbuf_commented_addf(buf, "%s", cut_line);
1015 strbuf_add_commented_lines(buf, explanation, strlen(explanation));
1016}
1017
1018void wt_status_add_cut_line(FILE *fp)
1019{
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001020 struct strbuf buf = STRBUF_INIT;
1021
Denton Liud540b702019-04-17 11:23:27 +01001022 wt_status_append_cut_line(&buf);
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001023 fputs(buf.buf, fp);
1024 strbuf_release(&buf);
1025}
1026
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001027static void wt_longstatus_print_verbose(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001028{
1029 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -08001030 struct setup_revision_opt opt;
Michael J Gruber40555002015-03-06 10:43:35 +01001031 int dirty_submodules;
1032 const char *c = color(WT_STATUS_HEADER, s);
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001033
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01001034 repo_init_revisions(s->repo, &rev, NULL);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001035 rev.diffopt.flags.allow_textconv = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +07001036 rev.diffopt.ita_invisible_in_index = 1;
Junio C Hamano32962c92010-03-08 22:58:09 -08001037
1038 memset(&opt, 0, sizeof(opt));
brian m. carlsonf2e51192018-05-02 00:26:00 +00001039 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
Junio C Hamano32962c92010-03-08 22:58:09 -08001040 setup_revisions(0, NULL, &rev, &opt);
1041
Jeff Kingc91f0d92006-09-08 04:05:34 -04001042 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001043 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1044 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1045 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Kristian Høgsberg4ba0cb22008-03-10 13:58:26 -04001046 rev.diffopt.file = s->fp;
1047 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 00:49:35 -04001048 /*
1049 * If we're not going to stdout, then we definitely don't
1050 * want color, since we are going to the commit message
1051 * file (and even the "auto" setting won't work, since it
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001052 * will have checked isatty on stdout). But we then do want
1053 * to insert the scissor line here to reliably remove the
1054 * diff before committing.
Jeff King4f672ad2008-10-26 00:49:35 -04001055 */
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001056 if (s->fp != stdout) {
Jeff Kingf1c96262011-08-17 22:03:12 -07001057 rev.diffopt.use_color = 0;
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001058 wt_status_add_cut_line(s->fp);
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001059 }
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001060 if (s->verbose > 1 && s->committable) {
Michael J Gruber40555002015-03-06 10:43:35 +01001061 /* print_updated() printed a header, so do we */
1062 if (s->fp != stdout)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001063 wt_longstatus_print_trailer(s);
Michael J Gruber40555002015-03-06 10:43:35 +01001064 status_printf_ln(s, c, _("Changes to be committed:"));
1065 rev.diffopt.a_prefix = "c/";
1066 rev.diffopt.b_prefix = "i/";
1067 } /* else use prefix as per user config */
Jeff Kingc91f0d92006-09-08 04:05:34 -04001068 run_diff_index(&rev, 1);
Michael J Gruber40555002015-03-06 10:43:35 +01001069 if (s->verbose > 1 &&
1070 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1071 status_printf_ln(s, c,
1072 "--------------------------------------------------");
1073 status_printf_ln(s, c, _("Changes not staged for commit:"));
1074 setup_work_tree();
1075 rev.diffopt.a_prefix = "i/";
1076 rev.diffopt.b_prefix = "w/";
1077 run_diff_files(&rev, 0);
1078 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001079}
1080
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001081static void wt_longstatus_print_tracking(struct wt_status *s)
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001082{
1083 struct strbuf sb = STRBUF_INIT;
René Scharfec72b49d2015-10-31 18:37:43 +01001084 const char *cp, *ep, *branch_name;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001085 struct branch *branch;
Matthieu Moy2556b992013-09-06 19:43:07 +02001086 char comment_line_string[3];
1087 int i;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001088
1089 assert(s->branch && !s->is_initial);
René Scharfec72b49d2015-10-31 18:37:43 +01001090 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001091 return;
René Scharfec72b49d2015-10-31 18:37:43 +01001092 branch = branch_get(branch_name);
Jeff Hostetlerf39a7572018-01-09 18:50:18 +00001093 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001094 return;
1095
Matthieu Moy2556b992013-09-06 19:43:07 +02001096 i = 0;
1097 if (s->display_comment_prefix) {
1098 comment_line_string[i++] = comment_line_char;
1099 comment_line_string[i++] = ' ';
1100 }
1101 comment_line_string[i] = '\0';
1102
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001103 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-09 21:59:30 -07001104 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001105 "%s%.*s", comment_line_string,
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001106 (int)(ep - cp), cp);
Matthieu Moy2556b992013-09-06 19:43:07 +02001107 if (s->display_comment_prefix)
1108 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1109 comment_line_char);
1110 else
Jeff King75177c82017-04-27 05:01:05 -04001111 fputs("\n", s->fp);
Rene Scharfeb6ec3072017-08-30 20:20:18 +02001112 strbuf_release(&sb);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001113}
1114
Lucien Kong83c750a2012-06-05 22:21:24 +02001115static void show_merge_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001116 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001117{
1118 if (has_unmerged(s)) {
1119 status_printf_ln(s, color, _("You have unmerged paths."));
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001120 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001121 status_printf_ln(s, color,
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001122 _(" (fix conflicts and run \"git commit\")"));
1123 status_printf_ln(s, color,
1124 _(" (use \"git merge --abort\" to abort the merge)"));
1125 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001126 } else {
1127 status_printf_ln(s, color,
1128 _("All conflicts fixed but you are still merging."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001129 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001130 status_printf_ln(s, color,
1131 _(" (use \"git commit\" to conclude merge)"));
1132 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001133 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001134}
1135
1136static void show_am_in_progress(struct wt_status *s,
Lucien Kong83c750a2012-06-05 22:21:24 +02001137 const char *color)
1138{
1139 status_printf_ln(s, color,
1140 _("You are in the middle of an am session."));
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001141 if (s->state.am_empty_patch)
Lucien Kong83c750a2012-06-05 22:21:24 +02001142 status_printf_ln(s, color,
1143 _("The current patch is empty."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001144 if (s->hints) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001145 if (!s->state.am_empty_patch)
Lucien Kong83c750a2012-06-05 22:21:24 +02001146 status_printf_ln(s, color,
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +03001147 _(" (fix conflicts and then run \"git am --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001148 status_printf_ln(s, color,
1149 _(" (use \"git am --skip\" to skip this patch)"));
1150 status_printf_ln(s, color,
1151 _(" (use \"git am --abort\" to restore the original branch)"));
1152 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001153 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001154}
1155
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001156static char *read_line_from_git_path(const char *filename)
1157{
1158 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +07001159 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1160
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001161 if (!fp) {
1162 strbuf_release(&buf);
1163 return NULL;
1164 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001165 strbuf_getline_lf(&buf, fp);
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001166 if (!fclose(fp)) {
1167 return strbuf_detach(&buf, NULL);
1168 } else {
1169 strbuf_release(&buf);
1170 return NULL;
1171 }
1172}
1173
1174static int split_commit_in_progress(struct wt_status *s)
1175{
1176 int split_in_progress = 0;
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001177 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001178
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001179 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001180 !s->branch || strcmp(s->branch, "HEAD"))
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001181 return 0;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001182
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001183 head = read_line_from_git_path("HEAD");
1184 orig_head = read_line_from_git_path("ORIG_HEAD");
1185 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1186 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1187
1188 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1189 ; /* fall through, no split in progress */
1190 else if (!strcmp(rebase_amend, rebase_orig_head))
1191 split_in_progress = !!strcmp(head, rebase_amend);
1192 else if (strcmp(orig_head, rebase_orig_head))
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001193 split_in_progress = 1;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001194
1195 free(head);
1196 free(orig_head);
1197 free(rebase_amend);
1198 free(rebase_orig_head);
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001199
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001200 return split_in_progress;
1201}
1202
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001203/*
1204 * Turn
1205 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1206 * into
1207 * "pick d6a2f03 some message"
1208 *
1209 * The function assumes that the line does not contain useless spaces
1210 * before or after the command.
1211 */
1212static void abbrev_sha1_in_line(struct strbuf *line)
1213{
1214 struct strbuf **split;
1215 int i;
1216
1217 if (starts_with(line->buf, "exec ") ||
Johannes Schindelin225a7772019-05-10 13:23:14 -07001218 starts_with(line->buf, "x ") ||
1219 starts_with(line->buf, "label ") ||
1220 starts_with(line->buf, "l "))
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001221 return;
1222
1223 split = strbuf_split_max(line, ' ', 3);
1224 if (split[0] && split[1]) {
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001225 struct object_id oid;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001226
1227 /*
1228 * strbuf_split_max left a space. Trim it and re-add
1229 * it after abbreviation.
1230 */
1231 strbuf_trim(split[1]);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001232 if (!get_oid(split[1]->buf, &oid)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001233 strbuf_reset(split[1]);
brian m. carlson30e677e2018-03-12 02:27:28 +00001234 strbuf_add_unique_abbrev(split[1], &oid,
René Scharfea94bb682016-10-08 17:38:47 +02001235 DEFAULT_ABBREV);
1236 strbuf_addch(split[1], ' ');
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001237 strbuf_reset(line);
1238 for (i = 0; split[i]; i++)
René Scharfe81099842016-07-19 20:36:29 +02001239 strbuf_addbuf(line, split[i]);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001240 }
1241 }
Stefan Beller6eb60782016-03-31 17:35:44 -07001242 strbuf_list_free(split);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001243}
1244
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001245static int read_rebase_todolist(const char *fname, struct string_list *lines)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001246{
1247 struct strbuf line = STRBUF_INIT;
1248 FILE *f = fopen(git_path("%s", fname), "r");
1249
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001250 if (!f) {
1251 if (errno == ENOENT)
1252 return -1;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001253 die_errno("Could not open file %s for reading",
1254 git_path("%s", fname));
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001255 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001256 while (!strbuf_getline_lf(&line, f)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001257 if (line.len && line.buf[0] == comment_line_char)
1258 continue;
1259 strbuf_trim(&line);
1260 if (!line.len)
1261 continue;
1262 abbrev_sha1_in_line(&line);
1263 string_list_append(lines, line.buf);
1264 }
Johannes Schindeline7b65e22017-05-04 15:55:52 +02001265 fclose(f);
Rene Scharfe6f495412017-08-30 20:20:17 +02001266 strbuf_release(&line);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001267 return 0;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001268}
1269
1270static void show_rebase_information(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001271 const char *color)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001272{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001273 if (s->state.rebase_interactive_in_progress) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001274 int i;
1275 int nr_lines_to_show = 2;
1276
1277 struct string_list have_done = STRING_LIST_INIT_DUP;
1278 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1279
1280 read_rebase_todolist("rebase-merge/done", &have_done);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001281 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1282 &yet_to_do))
1283 status_printf_ln(s, color,
1284 _("git-rebase-todo is missing."));
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001285 if (have_done.nr == 0)
1286 status_printf_ln(s, color, _("No commands done."));
1287 else {
1288 status_printf_ln(s, color,
1289 Q_("Last command done (%d command done):",
1290 "Last commands done (%d commands done):",
1291 have_done.nr),
1292 have_done.nr);
1293 for (i = (have_done.nr > nr_lines_to_show)
1294 ? have_done.nr - nr_lines_to_show : 0;
1295 i < have_done.nr;
1296 i++)
1297 status_printf_ln(s, color, " %s", have_done.items[i].string);
1298 if (have_done.nr > nr_lines_to_show && s->hints)
1299 status_printf_ln(s, color,
1300 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1301 }
1302
1303 if (yet_to_do.nr == 0)
1304 status_printf_ln(s, color,
1305 _("No commands remaining."));
1306 else {
1307 status_printf_ln(s, color,
1308 Q_("Next command to do (%d remaining command):",
1309 "Next commands to do (%d remaining commands):",
1310 yet_to_do.nr),
1311 yet_to_do.nr);
1312 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1313 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1314 if (s->hints)
1315 status_printf_ln(s, color,
1316 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1317 }
1318 string_list_clear(&yet_to_do, 0);
1319 string_list_clear(&have_done, 0);
1320 }
1321}
1322
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001323static void print_rebase_state(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001324 const char *color)
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001325{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001326 if (s->state.branch)
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001327 status_printf_ln(s, color,
1328 _("You are currently rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001329 s->state.branch,
1330 s->state.onto);
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001331 else
1332 status_printf_ln(s, color,
1333 _("You are currently rebasing."));
1334}
1335
Lucien Kong83c750a2012-06-05 22:21:24 +02001336static void show_rebase_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001337 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001338{
1339 struct stat st;
1340
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001341 show_rebase_information(s, color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001342 if (has_unmerged(s)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001343 print_rebase_state(s, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001344 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001345 status_printf_ln(s, color,
1346 _(" (fix conflicts and then run \"git rebase --continue\")"));
1347 status_printf_ln(s, color,
1348 _(" (use \"git rebase --skip\" to skip this patch)"));
1349 status_printf_ln(s, color,
1350 _(" (use \"git rebase --abort\" to check out the original branch)"));
1351 }
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001352 } else if (s->state.rebase_in_progress ||
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01001353 !stat(git_path_merge_msg(s->repo), &st)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001354 print_rebase_state(s, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001355 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001356 status_printf_ln(s, color,
1357 _(" (all conflicts fixed: run \"git rebase --continue\")"));
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001358 } else if (split_commit_in_progress(s)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001359 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001360 status_printf_ln(s, color,
1361 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001362 s->state.branch,
1363 s->state.onto);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001364 else
1365 status_printf_ln(s, color,
1366 _("You are currently splitting a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001367 if (s->hints)
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001368 status_printf_ln(s, color,
1369 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001370 } else {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001371 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001372 status_printf_ln(s, color,
1373 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001374 s->state.branch,
1375 s->state.onto);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001376 else
1377 status_printf_ln(s, color,
1378 _("You are currently editing a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001379 if (s->hints && !s->amend) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001380 status_printf_ln(s, color,
1381 _(" (use \"git commit --amend\" to amend the current commit)"));
1382 status_printf_ln(s, color,
1383 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1384 }
1385 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001386 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001387}
1388
1389static void show_cherry_pick_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001390 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001391{
Phillip Wood4a724862019-04-16 11:18:42 +01001392 if (is_null_oid(&s->state.cherry_pick_head_oid))
1393 status_printf_ln(s, color,
1394 _("Cherry-pick currently in progress."));
1395 else
1396 status_printf_ln(s, color,
1397 _("You are currently cherry-picking commit %s."),
1398 find_unique_abbrev(&s->state.cherry_pick_head_oid,
1399 DEFAULT_ABBREV));
1400
Matthieu Moy6a964f52013-09-12 12:50:05 +02001401 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001402 if (has_unmerged(s))
1403 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001404 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
Phillip Wood4a724862019-04-16 11:18:42 +01001405 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1406 status_printf_ln(s, color,
1407 _(" (run \"git cherry-pick --continue\" to continue)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001408 else
1409 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001410 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1411 status_printf_ln(s, color,
1412 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001413 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001414 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001415}
1416
Matthieu Moydb4ef442013-04-02 16:20:21 +02001417static void show_revert_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001418 const char *color)
Matthieu Moydb4ef442013-04-02 16:20:21 +02001419{
Phillip Wood4a724862019-04-16 11:18:42 +01001420 if (is_null_oid(&s->state.revert_head_oid))
1421 status_printf_ln(s, color,
1422 _("Revert currently in progress."));
1423 else
1424 status_printf_ln(s, color,
1425 _("You are currently reverting commit %s."),
1426 find_unique_abbrev(&s->state.revert_head_oid,
1427 DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001428 if (s->hints) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001429 if (has_unmerged(s))
1430 status_printf_ln(s, color,
1431 _(" (fix conflicts and run \"git revert --continue\")"));
Phillip Wood4a724862019-04-16 11:18:42 +01001432 else if (is_null_oid(&s->state.revert_head_oid))
1433 status_printf_ln(s, color,
1434 _(" (run \"git revert --continue\" to continue)"));
Matthieu Moydb4ef442013-04-02 16:20:21 +02001435 else
1436 status_printf_ln(s, color,
1437 _(" (all conflicts fixed: run \"git revert --continue\")"));
1438 status_printf_ln(s, color,
1439 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1440 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001441 wt_longstatus_print_trailer(s);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001442}
1443
Lucien Kong83c750a2012-06-05 22:21:24 +02001444static void show_bisect_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001445 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001446{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001447 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001448 status_printf_ln(s, color,
Nguyễn Thái Ngọc Duy6deab242013-03-23 10:52:44 +07001449 _("You are currently bisecting, started from branch '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001450 s->state.branch);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001451 else
1452 status_printf_ln(s, color,
1453 _("You are currently bisecting."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001454 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001455 status_printf_ln(s, color,
1456 _(" (use \"git bisect reset\" to get back to the original branch)"));
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001457 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001458}
1459
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001460/*
1461 * Extract branch information from rebase/bisect
1462 */
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001463static char *get_branch(const struct worktree *wt, const char *path)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001464{
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001465 struct strbuf sb = STRBUF_INIT;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001466 struct object_id oid;
René Scharfec72b49d2015-10-31 18:37:43 +01001467 const char *branch_name;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001468
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001469 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 +07001470 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001471
Jeff King66ec9042015-01-28 12:57:35 -05001472 while (sb.len && sb.buf[sb.len - 1] == '\n')
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001473 strbuf_setlen(&sb, sb.len - 1);
1474 if (!sb.len)
1475 goto got_nothing;
René Scharfec72b49d2015-10-31 18:37:43 +01001476 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1477 strbuf_remove(&sb, 0, branch_name - sb.buf);
Christian Couder59556542013-11-30 21:55:40 +01001478 else if (starts_with(sb.buf, "refs/"))
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001479 ;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001480 else if (!get_oid_hex(sb.buf, &oid)) {
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001481 strbuf_reset(&sb);
brian m. carlson30e677e2018-03-12 02:27:28 +00001482 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001483 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1484 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001485 else /* bisect */
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001486 ;
1487 return strbuf_detach(&sb, NULL);
1488
1489got_nothing:
1490 strbuf_release(&sb);
1491 return NULL;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001492}
1493
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001494struct grab_1st_switch_cbdata {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001495 struct strbuf buf;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001496 struct object_id noid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001497};
1498
brian m. carlson9461d272017-02-21 23:47:32 +00001499static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +02001500 const char *email, timestamp_t timestamp, int tz,
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001501 const char *message, void *cb_data)
Lucien Kong83c750a2012-06-05 22:21:24 +02001502{
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001503 struct grab_1st_switch_cbdata *cb = cb_data;
1504 const char *target = NULL, *end;
1505
René Scharfec72b49d2015-10-31 18:37:43 +01001506 if (!skip_prefix(message, "checkout: moving from ", &message))
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001507 return 0;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001508 target = strstr(message, " to ");
1509 if (!target)
1510 return 0;
1511 target += strlen(" to ");
1512 strbuf_reset(&cb->buf);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001513 oidcpy(&cb->noid, noid);
René Scharfe904de442015-11-25 15:10:18 +01001514 end = strchrnul(target, '\n');
1515 strbuf_add(&cb->buf, target, end - target);
1516 if (!strcmp(cb->buf.buf, "HEAD")) {
Matthieu Moy0eb85482015-09-27 17:13:42 +02001517 /* HEAD is relative. Resolve it to the right reflog entry. */
René Scharfe904de442015-11-25 15:10:18 +01001518 strbuf_reset(&cb->buf);
brian m. carlson30e677e2018-03-12 02:27:28 +00001519 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
Matthieu Moy0eb85482015-09-27 17:13:42 +02001520 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001521 return 1;
1522}
1523
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001524static void wt_status_get_detached_from(struct repository *r,
1525 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001526{
1527 struct grab_1st_switch_cbdata cb;
1528 struct commit *commit;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001529 struct object_id oid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001530 char *ref = NULL;
1531
1532 strbuf_init(&cb.buf, 0);
1533 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1534 strbuf_release(&cb.buf);
1535 return;
1536 }
1537
brian m. carlsoncca5fa62017-10-15 22:06:57 +00001538 if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref) == 1 &&
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001539 /* sha1 is a commit? match without further lookup */
Jeff King4a7e27e2018-08-28 17:22:40 -04001540 (oideq(&cb.noid, &oid) ||
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001541 /* perhaps sha1 is a tag, try to dereference to a commit */
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001542 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001543 oideq(&cb.noid, &commit->object.oid)))) {
René Scharfec72b49d2015-10-31 18:37:43 +01001544 const char *from = ref;
1545 if (!skip_prefix(from, "refs/tags/", &from))
1546 skip_prefix(from, "refs/remotes/", &from);
1547 state->detached_from = xstrdup(from);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001548 } else
1549 state->detached_from =
brian m. carlsonaab95832018-03-12 02:27:30 +00001550 xstrdup(find_unique_abbrev(&cb.noid, DEFAULT_ABBREV));
brian m. carlson40f55552018-03-12 02:27:29 +00001551 oidcpy(&state->detached_oid, &cb.noid);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001552 state->detached_at = !get_oid("HEAD", &oid) &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001553 oideq(&oid, &state->detached_oid);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001554
1555 free(ref);
1556 strbuf_release(&cb.buf);
1557}
1558
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001559int wt_status_check_rebase(const struct worktree *wt,
1560 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001561{
Lucien Kong83c750a2012-06-05 22:21:24 +02001562 struct stat st;
1563
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001564 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1565 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001566 state->am_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001567 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 +07001568 state->am_empty_patch = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001569 } else {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001570 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001571 state->branch = get_branch(wt, "rebase-apply/head-name");
1572 state->onto = get_branch(wt, "rebase-apply/onto");
Lucien Kong83c750a2012-06-05 22:21:24 +02001573 }
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001574 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1575 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001576 state->rebase_interactive_in_progress = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001577 else
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001578 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001579 state->branch = get_branch(wt, "rebase-merge/head-name");
1580 state->onto = get_branch(wt, "rebase-merge/onto");
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001581 } else
1582 return 0;
1583 return 1;
1584}
1585
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001586int wt_status_check_bisect(const struct worktree *wt,
1587 struct wt_status_state *state)
1588{
1589 struct stat st;
1590
1591 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1592 state->bisect_in_progress = 1;
1593 state->branch = get_branch(wt, "BISECT_START");
1594 return 1;
1595 }
1596 return 0;
1597}
1598
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001599void wt_status_get_state(struct repository *r,
1600 struct wt_status_state *state,
Lucien Kong83c750a2012-06-05 22:21:24 +02001601 int get_detached_from)
1602{
1603 struct stat st;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001604 struct object_id oid;
Phillip Wood4a724862019-04-16 11:18:42 +01001605 enum replay_action action;
Lucien Kong83c750a2012-06-05 22:21:24 +02001606
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001607 if (!stat(git_path_merge_head(r), &st)) {
Johannes Schindelin982288e2018-11-12 15:26:02 -08001608 wt_status_check_rebase(NULL, state);
Lucien Kong83c750a2012-06-05 22:21:24 +02001609 state->merge_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001610 } else if (wt_status_check_rebase(NULL, state)) {
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001611 ; /* all set */
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001612 } else if (!stat(git_path_cherry_pick_head(r), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001613 !get_oid("CHERRY_PICK_HEAD", &oid)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001614 state->cherry_pick_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001615 oidcpy(&state->cherry_pick_head_oid, &oid);
Lucien Kong83c750a2012-06-05 22:21:24 +02001616 }
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001617 wt_status_check_bisect(NULL, state);
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001618 if (!stat(git_path_revert_head(r), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001619 !get_oid("REVERT_HEAD", &oid)) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001620 state->revert_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001621 oidcpy(&state->revert_head_oid, &oid);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001622 }
Phillip Wood4a724862019-04-16 11:18:42 +01001623 if (!sequencer_get_last_command(r, &action)) {
1624 if (action == REPLAY_PICK) {
1625 state->cherry_pick_in_progress = 1;
1626 oidcpy(&state->cherry_pick_head_oid, &null_oid);
1627 } else {
1628 state->revert_in_progress = 1;
1629 oidcpy(&state->revert_head_oid, &null_oid);
1630 }
1631 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001632 if (get_detached_from)
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001633 wt_status_get_detached_from(r, state);
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001634}
1635
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001636static void wt_longstatus_print_state(struct wt_status *s)
Lucien Kong83c750a2012-06-05 22:21:24 +02001637{
1638 const char *state_color = color(WT_STATUS_HEADER, s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001639 struct wt_status_state *state = &s->state;
1640
Johannes Schindelin982288e2018-11-12 15:26:02 -08001641 if (state->merge_in_progress) {
1642 if (state->rebase_interactive_in_progress) {
1643 show_rebase_information(s, state_color);
1644 fputs("\n", s->fp);
1645 }
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001646 show_merge_in_progress(s, state_color);
Johannes Schindelin982288e2018-11-12 15:26:02 -08001647 } else if (state->am_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001648 show_am_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001649 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001650 show_rebase_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001651 else if (state->cherry_pick_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001652 show_cherry_pick_in_progress(s, state_color);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001653 else if (state->revert_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001654 show_revert_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001655 if (state->bisect_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001656 show_bisect_in_progress(s, state_color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001657}
1658
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001659static void wt_longstatus_print(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001660{
Aleksi Aalto1d282322010-11-18 01:40:05 +02001661 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1662 const char *branch_status_color = color(WT_STATUS_HEADER, s);
Jürgen Rühle98bf8a42007-01-02 20:26:23 +01001663
Junio C Hamanobda324c2007-01-03 01:09:34 -08001664 if (s->branch) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001665 const char *on_what = _("On branch ");
Junio C Hamanobda324c2007-01-03 01:09:34 -08001666 const char *branch_name = s->branch;
René Scharfec72b49d2015-10-31 18:37:43 +01001667 if (!strcmp(branch_name, "HEAD")) {
Aleksi Aalto1d282322010-11-18 01:40:05 +02001668 branch_status_color = color(WT_STATUS_NOBRANCH, s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001669 if (s->state.rebase_in_progress ||
1670 s->state.rebase_interactive_in_progress) {
1671 if (s->state.rebase_interactive_in_progress)
Guillaume Pagèsdf25e942015-06-30 15:01:13 +02001672 on_what = _("interactive rebase in progress; onto ");
1673 else
1674 on_what = _("rebase in progress; onto ");
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001675 branch_name = s->state.onto;
1676 } else if (s->state.detached_from) {
1677 branch_name = s->state.detached_from;
1678 if (s->state.detached_at)
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001679 on_what = _("HEAD detached at ");
1680 else
1681 on_what = _("HEAD detached from ");
1682 } else {
1683 branch_name = "";
1684 on_what = _("Not currently on any branch.");
1685 }
René Scharfec72b49d2015-10-31 18:37:43 +01001686 } else
1687 skip_prefix(branch_name, "refs/heads/", &branch_name);
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001688 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001689 status_printf_more(s, branch_status_color, "%s", on_what);
1690 status_printf_more(s, branch_color, "%s\n", branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001691 if (!s->is_initial)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001692 wt_longstatus_print_tracking(s);
Junio C Hamanobda324c2007-01-03 01:09:34 -08001693 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001694
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001695 wt_longstatus_print_state(s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001696
Jeff Kingc91f0d92006-09-08 04:05:34 -04001697 if (s->is_initial) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001698 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301699 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1700 s->commit_template
1701 ? _("Initial commit")
1702 : _("No commits yet"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001703 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001704 }
1705
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001706 wt_longstatus_print_updated(s);
1707 wt_longstatus_print_unmerged(s);
1708 wt_longstatus_print_changed(s);
Jens Lehmann46a958b2010-06-25 16:56:47 +02001709 if (s->submodule_summary &&
1710 (!s->ignore_submodule_arg ||
1711 strcmp(s->ignore_submodule_arg, "all"))) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001712 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1713 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
Jens Lehmannf17a5d32010-01-17 20:42:31 +01001714 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001715 if (s->show_untracked_files) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001716 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
Jameson Millereec0f7f2017-10-30 13:21:37 -04001717 if (s->show_ignored_mode)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001718 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001719 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001720 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001721 status_printf_ln(s, GIT_COLOR_NORMAL,
Jiang Xin62901172013-04-12 11:53:01 +08001722 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1723 "may speed it up, but you have to be careful not to forget to add\n"
1724 "new files yourself (see 'git help status')."),
1725 s->untracked_in_ms / 1000.0);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001726 }
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001727 } else if (s->committable)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001728 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
Matthieu Moy6a964f52013-09-12 12:50:05 +02001729 s->hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001730 ? _(" (use -u option to show untracked files)") : "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001731
Jeff King1324fb62008-11-12 03:23:37 -05001732 if (s->verbose)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001733 wt_longstatus_print_verbose(s);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001734 if (!s->committable) {
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001735 if (s->amend)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001736 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
Junio C Hamano37d07f82007-12-12 19:09:16 -08001737 else if (s->nowarn)
1738 ; /* nothing */
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001739 else if (s->workdir_dirty) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001740 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001741 printf(_("no changes added to commit "
1742 "(use \"git add\" and/or \"git commit -a\")\n"));
1743 else
1744 printf(_("no changes added to commit\n"));
1745 } else if (s->untracked.nr) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001746 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001747 printf(_("nothing added to commit but untracked files "
1748 "present (use \"git add\" to track)\n"));
1749 else
1750 printf(_("nothing added to commit but untracked files present\n"));
1751 } else if (s->is_initial) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001752 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001753 printf(_("nothing to commit (create/copy files "
1754 "and use \"git add\" to track)\n"));
1755 else
1756 printf(_("nothing to commit\n"));
1757 } else if (!s->show_untracked_files) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001758 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001759 printf(_("nothing to commit (use -u to show untracked files)\n"));
1760 else
1761 printf(_("nothing to commit\n"));
1762 } else
Lars Vogel2a0e6cd2016-06-09 20:19:30 +02001763 printf(_("nothing to commit, working tree clean\n"));
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001764 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001765 if(s->show_stash)
1766 wt_longstatus_print_stash_summary(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001767}
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001768
Jeff King3207a3a2012-05-07 15:44:44 -04001769static void wt_shortstatus_unmerged(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001770 struct wt_status *s)
1771{
1772 struct wt_status_change_data *d = it->util;
1773 const char *how = "??";
1774
1775 switch (d->stagemask) {
1776 case 1: how = "DD"; break; /* both deleted */
1777 case 2: how = "AU"; break; /* added by us */
1778 case 3: how = "UD"; break; /* deleted by them */
1779 case 4: how = "UA"; break; /* added by them */
1780 case 5: how = "DU"; break; /* deleted by us */
1781 case 6: how = "AA"; break; /* both added */
1782 case 7: how = "UU"; break; /* both modified */
1783 }
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001784 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
Jeff King3207a3a2012-05-07 15:44:44 -04001785 if (s->null_termination) {
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001786 fprintf(stdout, " %s%c", it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001787 } else {
1788 struct strbuf onebuf = STRBUF_INIT;
1789 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001790 one = quote_path(it->string, s->prefix, &onebuf);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001791 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001792 strbuf_release(&onebuf);
1793 }
1794}
1795
Jeff King3207a3a2012-05-07 15:44:44 -04001796static void wt_shortstatus_status(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001797 struct wt_status *s)
1798{
1799 struct wt_status_change_data *d = it->util;
1800
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001801 if (d->index_status)
1802 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1803 else
1804 putchar(' ');
1805 if (d->worktree_status)
1806 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1807 else
1808 putchar(' ');
1809 putchar(' ');
Jeff King3207a3a2012-05-07 15:44:44 -04001810 if (s->null_termination) {
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001811 fprintf(stdout, "%s%c", it->string, 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07001812 if (d->rename_source)
1813 fprintf(stdout, "%s%c", d->rename_source, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001814 } else {
1815 struct strbuf onebuf = STRBUF_INIT;
1816 const char *one;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07001817
1818 if (d->rename_source) {
1819 one = quote_path(d->rename_source, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001820 if (*one != '"' && strchr(one, ' ') != NULL) {
1821 putchar('"');
1822 strbuf_addch(&onebuf, '"');
1823 one = onebuf.buf;
1824 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001825 printf("%s -> ", one);
1826 strbuf_release(&onebuf);
1827 }
Jiang Xin39598f92013-06-25 23:53:45 +08001828 one = quote_path(it->string, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001829 if (*one != '"' && strchr(one, ' ') != NULL) {
1830 putchar('"');
1831 strbuf_addch(&onebuf, '"');
1832 one = onebuf.buf;
1833 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001834 printf("%s\n", one);
1835 strbuf_release(&onebuf);
1836 }
1837}
1838
Jeff King3207a3a2012-05-07 15:44:44 -04001839static void wt_shortstatus_other(struct string_list_item *it,
Junio C Hamano2381e392010-04-10 00:33:17 -07001840 struct wt_status *s, const char *sign)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001841{
Jeff King3207a3a2012-05-07 15:44:44 -04001842 if (s->null_termination) {
Junio C Hamano2381e392010-04-10 00:33:17 -07001843 fprintf(stdout, "%s %s%c", sign, it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001844 } else {
1845 struct strbuf onebuf = STRBUF_INIT;
1846 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001847 one = quote_path(it->string, s->prefix, &onebuf);
Junio C Hamanoc1909e72010-05-01 22:05:14 -07001848 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001849 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001850 strbuf_release(&onebuf);
1851 }
1852}
1853
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001854static void wt_shortstatus_print_tracking(struct wt_status *s)
1855{
1856 struct branch *branch;
1857 const char *header_color = color(WT_STATUS_HEADER, s);
1858 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1859 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1860
1861 const char *base;
René Scharfe5e8d2722017-07-08 12:51:01 +02001862 char *short_base;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001863 const char *branch_name;
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001864 int num_ours, num_theirs, sti;
Jiang Xinf2e08732013-08-26 15:02:48 +08001865 int upstream_is_gone = 0;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001866
1867 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1868
1869 if (!s->branch)
1870 return;
1871 branch_name = s->branch;
1872
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001873#define LABEL(string) (s->no_gettext ? (string) : _(string))
1874
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001875 if (s->is_initial)
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301876 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001877
René Scharfebaf0a3e2015-10-31 18:36:35 +01001878 if (!strcmp(s->branch, "HEAD")) {
1879 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001880 LABEL(N_("HEAD (no branch)")));
René Scharfebaf0a3e2015-10-31 18:36:35 +01001881 goto conclude;
1882 }
1883
René Scharfec72b49d2015-10-31 18:37:43 +01001884 skip_prefix(branch_name, "refs/heads/", &branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001885
René Scharfe8d8325f2015-10-31 18:37:12 +01001886 branch = branch_get(branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001887
Jiang Xinf2e08732013-08-26 15:02:48 +08001888 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1889
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001890 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
Damien Robertc646d092019-04-16 14:16:46 +02001891 0, s->ahead_behind_flags);
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001892 if (sti < 0) {
René Scharfebcf8cc22015-10-31 18:36:01 +01001893 if (!base)
1894 goto conclude;
Jeff King979cb242015-05-21 20:49:11 -04001895
Jiang Xinf2e08732013-08-26 15:02:48 +08001896 upstream_is_gone = 1;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001897 }
1898
René Scharfe5e8d2722017-07-08 12:51:01 +02001899 short_base = shorten_unambiguous_ref(base, 0);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001900 color_fprintf(s->fp, header_color, "...");
René Scharfe5e8d2722017-07-08 12:51:01 +02001901 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
1902 free(short_base);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001903
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001904 if (!upstream_is_gone && !sti)
René Scharfebcf8cc22015-10-31 18:36:01 +01001905 goto conclude;
Jiang Xinf2234592013-08-26 15:02:49 +08001906
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001907 color_fprintf(s->fp, header_color, " [");
Jiang Xinf2e08732013-08-26 15:02:48 +08001908 if (upstream_is_gone) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001909 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
Jeff Hostetler3ca18972018-01-09 18:50:17 +00001910 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
1911 color_fprintf(s->fp, header_color, LABEL(N_("different")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001912 } else if (!num_ours) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001913 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001914 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1915 } else if (!num_theirs) {
Michael J Gruberdf227242016-03-14 16:30:33 +01001916 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001917 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1918 } else {
Michael J Gruberdf227242016-03-14 16:30:33 +01001919 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001920 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
Matthieu Moy7a76c282014-03-20 13:12:41 +01001921 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001922 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1923 }
1924
Jeff Kinga5985232012-05-07 17:02:18 -04001925 color_fprintf(s->fp, header_color, "]");
René Scharfebcf8cc22015-10-31 18:36:01 +01001926 conclude:
Jeff Kinga5985232012-05-07 17:02:18 -04001927 fputc(s->null_termination ? '\0' : '\n', s->fp);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001928}
1929
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001930static void wt_shortstatus_print(struct wt_status *s)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001931{
Stefan Bellerd4aae452017-03-16 14:36:19 -07001932 struct string_list_item *it;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001933
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001934 if (s->show_branch)
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001935 wt_shortstatus_print_tracking(s);
1936
Stefan Bellerd4aae452017-03-16 14:36:19 -07001937 for_each_string_list_item(it, &s->change) {
1938 struct wt_status_change_data *d = it->util;
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001939
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001940 if (d->stagemask)
Jeff King3207a3a2012-05-07 15:44:44 -04001941 wt_shortstatus_unmerged(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001942 else
Jeff King3207a3a2012-05-07 15:44:44 -04001943 wt_shortstatus_status(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001944 }
Stefan Bellerd4aae452017-03-16 14:36:19 -07001945 for_each_string_list_item(it, &s->untracked)
Jeff King3207a3a2012-05-07 15:44:44 -04001946 wt_shortstatus_other(it, s, "??");
Junio C Hamano2381e392010-04-10 00:33:17 -07001947
Stefan Bellerd4aae452017-03-16 14:36:19 -07001948 for_each_string_list_item(it, &s->ignored)
Jeff King3207a3a2012-05-07 15:44:44 -04001949 wt_shortstatus_other(it, s, "!!");
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001950}
Jeff King4a7cc2f2009-12-07 00:17:15 -05001951
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001952static void wt_porcelain_print(struct wt_status *s)
Jeff King4a7cc2f2009-12-07 00:17:15 -05001953{
1954 s->use_color = 0;
Jeff King86617682009-12-07 00:26:25 -05001955 s->relative_paths = 0;
1956 s->prefix = NULL;
Matthieu Moy7a76c282014-03-20 13:12:41 +01001957 s->no_gettext = 1;
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001958 wt_shortstatus_print(s);
Jeff King4a7cc2f2009-12-07 00:17:15 -05001959}
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001960
Jeff Hostetler24959ba2016-08-11 10:45:58 -04001961/*
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001962 * Print branch information for porcelain v2 output. These lines
1963 * are printed when the '--branch' parameter is given.
1964 *
1965 * # branch.oid <commit><eol>
1966 * # branch.head <head><eol>
1967 * [# branch.upstream <upstream><eol>
1968 * [# branch.ab +<ahead> -<behind><eol>]]
1969 *
1970 * <commit> ::= the current commit hash or the the literal
1971 * "(initial)" to indicate an initialized repo
1972 * with no commits.
1973 *
1974 * <head> ::= <branch_name> the current branch name or
1975 * "(detached)" literal when detached head or
1976 * "(unknown)" when something is wrong.
1977 *
1978 * <upstream> ::= the upstream branch name, when set.
1979 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001980 * <ahead> ::= integer ahead value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001981 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001982 * <behind> ::= integer behind value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001983 *
1984 * The end-of-line is defined by the -z flag.
1985 *
1986 * <eol> ::= NUL when -z,
1987 * LF when NOT -z.
1988 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00001989 * When an upstream is set and present, the 'branch.ab' line will
1990 * be printed with the ahead/behind counts for the branch and the
1991 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1992 * are different, '?' will be substituted for the actual count.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001993 */
1994static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1995{
1996 struct branch *branch;
1997 const char *base;
1998 const char *branch_name;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001999 int ab_info, nr_ahead, nr_behind;
2000 char eol = s->null_termination ? '\0' : '\n';
2001
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002002 fprintf(s->fp, "# branch.oid %s%c",
2003 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
2004 eol);
2005
2006 if (!s->branch)
2007 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2008 else {
2009 if (!strcmp(s->branch, "HEAD")) {
2010 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2011
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07002012 if (s->state.rebase_in_progress ||
2013 s->state.rebase_interactive_in_progress)
2014 branch_name = s->state.onto;
2015 else if (s->state.detached_from)
2016 branch_name = s->state.detached_from;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002017 else
2018 branch_name = "";
2019 } else {
2020 branch_name = NULL;
2021 skip_prefix(s->branch, "refs/heads/", &branch_name);
2022
2023 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2024 }
2025
2026 /* Lookup stats on the upstream tracking branch, if set. */
2027 branch = branch_get(branch_name);
2028 base = NULL;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002029 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
Damien Robertc646d092019-04-16 14:16:46 +02002030 &base, 0, s->ahead_behind_flags);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002031 if (base) {
2032 base = shorten_unambiguous_ref(base, 0);
2033 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2034 free((char *)base);
2035
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002036 if (ab_info > 0) {
2037 /* different */
2038 if (nr_ahead || nr_behind)
2039 fprintf(s->fp, "# branch.ab +%d -%d%c",
2040 nr_ahead, nr_behind, eol);
2041 else
2042 fprintf(s->fp, "# branch.ab +? -?%c",
2043 eol);
2044 } else if (!ab_info) {
2045 /* same */
2046 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2047 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002048 }
2049 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002050}
2051
2052/*
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002053 * Convert various submodule status values into a
2054 * fixed-length string of characters in the buffer provided.
2055 */
2056static void wt_porcelain_v2_submodule_state(
2057 struct wt_status_change_data *d,
2058 char sub[5])
2059{
2060 if (S_ISGITLINK(d->mode_head) ||
2061 S_ISGITLINK(d->mode_index) ||
2062 S_ISGITLINK(d->mode_worktree)) {
2063 sub[0] = 'S';
2064 sub[1] = d->new_submodule_commits ? 'C' : '.';
2065 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2066 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2067 } else {
2068 sub[0] = 'N';
2069 sub[1] = '.';
2070 sub[2] = '.';
2071 sub[3] = '.';
2072 }
2073 sub[4] = 0;
2074}
2075
2076/*
2077 * Fix-up changed entries before we print them.
2078 */
2079static void wt_porcelain_v2_fix_up_changed(
2080 struct string_list_item *it,
2081 struct wt_status *s)
2082{
2083 struct wt_status_change_data *d = it->util;
2084
2085 if (!d->index_status) {
2086 /*
2087 * This entry is unchanged in the index (relative to the head).
2088 * Therefore, the collect_updated_cb was never called for this
2089 * entry (during the head-vs-index scan) and so the head column
2090 * fields were never set.
2091 *
2092 * We must have data for the index column (from the
2093 * index-vs-worktree scan (otherwise, this entry should not be
2094 * in the list of changes)).
2095 *
2096 * Copy index column fields to the head column, so that our
2097 * output looks complete.
2098 */
2099 assert(d->mode_head == 0);
2100 d->mode_head = d->mode_index;
2101 oidcpy(&d->oid_head, &d->oid_index);
2102 }
2103
2104 if (!d->worktree_status) {
2105 /*
2106 * This entry is unchanged in the worktree (relative to the index).
2107 * Therefore, the collect_changed_cb was never called for this entry
2108 * (during the index-vs-worktree scan) and so the worktree column
2109 * fields were never set.
2110 *
2111 * We must have data for the index column (from the head-vs-index
2112 * scan).
2113 *
2114 * Copy the index column fields to the worktree column so that
2115 * our output looks complete.
2116 *
2117 * Note that we only have a mode field in the worktree column
2118 * because the scan code tries really hard to not have to compute it.
2119 */
2120 assert(d->mode_worktree == 0);
2121 d->mode_worktree = d->mode_index;
2122 }
2123}
2124
2125/*
2126 * Print porcelain v2 info for tracked entries with changes.
2127 */
2128static void wt_porcelain_v2_print_changed_entry(
2129 struct string_list_item *it,
2130 struct wt_status *s)
2131{
2132 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002133 struct strbuf buf = STRBUF_INIT;
2134 struct strbuf buf_from = STRBUF_INIT;
2135 const char *path = NULL;
2136 const char *path_from = NULL;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002137 char key[3];
2138 char submodule_token[5];
2139 char sep_char, eol_char;
2140
2141 wt_porcelain_v2_fix_up_changed(it, s);
2142 wt_porcelain_v2_submodule_state(d, submodule_token);
2143
2144 key[0] = d->index_status ? d->index_status : '.';
2145 key[1] = d->worktree_status ? d->worktree_status : '.';
2146 key[2] = 0;
2147
2148 if (s->null_termination) {
2149 /*
2150 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2151 * A single NUL character separates them.
2152 */
2153 sep_char = '\0';
2154 eol_char = '\0';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002155 path = it->string;
2156 path_from = d->rename_source;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002157 } else {
2158 /*
2159 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2160 * The source path is only present when necessary.
2161 * A single TAB separates them (because paths can contain spaces
2162 * which are not escaped and C-quoting does escape TAB characters).
2163 */
2164 sep_char = '\t';
2165 eol_char = '\n';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002166 path = quote_path(it->string, s->prefix, &buf);
2167 if (d->rename_source)
2168 path_from = quote_path(d->rename_source, s->prefix, &buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002169 }
2170
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002171 if (path_from)
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002172 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2173 key, submodule_token,
2174 d->mode_head, d->mode_index, d->mode_worktree,
2175 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002176 d->rename_status, d->rename_score,
2177 path, sep_char, path_from, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002178 else
2179 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2180 key, submodule_token,
2181 d->mode_head, d->mode_index, d->mode_worktree,
2182 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002183 path, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002184
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002185 strbuf_release(&buf);
2186 strbuf_release(&buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002187}
2188
2189/*
2190 * Print porcelain v2 status info for unmerged entries.
2191 */
2192static void wt_porcelain_v2_print_unmerged_entry(
2193 struct string_list_item *it,
2194 struct wt_status *s)
2195{
2196 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002197 struct index_state *istate = s->repo->index;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002198 const struct cache_entry *ce;
2199 struct strbuf buf_index = STRBUF_INIT;
2200 const char *path_index = NULL;
2201 int pos, stage, sum;
2202 struct {
2203 int mode;
2204 struct object_id oid;
2205 } stages[3];
2206 char *key;
2207 char submodule_token[5];
2208 char unmerged_prefix = 'u';
2209 char eol_char = s->null_termination ? '\0' : '\n';
2210
2211 wt_porcelain_v2_submodule_state(d, submodule_token);
2212
2213 switch (d->stagemask) {
2214 case 1: key = "DD"; break; /* both deleted */
2215 case 2: key = "AU"; break; /* added by us */
2216 case 3: key = "UD"; break; /* deleted by them */
2217 case 4: key = "UA"; break; /* added by them */
2218 case 5: key = "DU"; break; /* deleted by us */
2219 case 6: key = "AA"; break; /* both added */
2220 case 7: key = "UU"; break; /* both modified */
2221 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002222 BUG("unhandled unmerged status %x", d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002223 }
2224
2225 /*
2226 * Disregard d.aux.porcelain_v2 data that we accumulated
2227 * for the head and index columns during the scans and
2228 * replace with the actual stage data.
2229 *
2230 * Note that this is a last-one-wins for each the individual
2231 * stage [123] columns in the event of multiple cache entries
2232 * for same stage.
2233 */
2234 memset(stages, 0, sizeof(stages));
2235 sum = 0;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002236 pos = index_name_pos(istate, it->string, strlen(it->string));
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002237 assert(pos < 0);
2238 pos = -pos-1;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002239 while (pos < istate->cache_nr) {
2240 ce = istate->cache[pos++];
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002241 stage = ce_stage(ce);
2242 if (strcmp(ce->name, it->string) || !stage)
2243 break;
2244 stages[stage - 1].mode = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +01002245 oidcpy(&stages[stage - 1].oid, &ce->oid);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002246 sum |= (1 << (stage - 1));
2247 }
2248 if (sum != d->stagemask)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002249 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002250
2251 if (s->null_termination)
2252 path_index = it->string;
2253 else
2254 path_index = quote_path(it->string, s->prefix, &buf_index);
2255
2256 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2257 unmerged_prefix, key, submodule_token,
2258 stages[0].mode, /* stage 1 */
2259 stages[1].mode, /* stage 2 */
2260 stages[2].mode, /* stage 3 */
2261 d->mode_worktree,
2262 oid_to_hex(&stages[0].oid), /* stage 1 */
2263 oid_to_hex(&stages[1].oid), /* stage 2 */
2264 oid_to_hex(&stages[2].oid), /* stage 3 */
2265 path_index,
2266 eol_char);
2267
2268 strbuf_release(&buf_index);
2269}
2270
2271/*
2272 * Print porcelain V2 status info for untracked and ignored entries.
2273 */
2274static void wt_porcelain_v2_print_other(
2275 struct string_list_item *it,
2276 struct wt_status *s,
2277 char prefix)
2278{
2279 struct strbuf buf = STRBUF_INIT;
2280 const char *path;
2281 char eol_char;
2282
2283 if (s->null_termination) {
2284 path = it->string;
2285 eol_char = '\0';
2286 } else {
2287 path = quote_path(it->string, s->prefix, &buf);
2288 eol_char = '\n';
2289 }
2290
2291 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2292
2293 strbuf_release(&buf);
2294}
2295
2296/*
2297 * Print porcelain V2 status.
2298 *
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002299 * [<v2_branch>]
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002300 * [<v2_changed_items>]*
2301 * [<v2_unmerged_items>]*
2302 * [<v2_untracked_items>]*
2303 * [<v2_ignored_items>]*
2304 *
2305 */
2306static void wt_porcelain_v2_print(struct wt_status *s)
2307{
2308 struct wt_status_change_data *d;
2309 struct string_list_item *it;
2310 int i;
2311
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002312 if (s->show_branch)
2313 wt_porcelain_v2_print_tracking(s);
2314
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002315 for (i = 0; i < s->change.nr; i++) {
2316 it = &(s->change.items[i]);
2317 d = it->util;
2318 if (!d->stagemask)
2319 wt_porcelain_v2_print_changed_entry(it, s);
2320 }
2321
2322 for (i = 0; i < s->change.nr; i++) {
2323 it = &(s->change.items[i]);
2324 d = it->util;
2325 if (d->stagemask)
2326 wt_porcelain_v2_print_unmerged_entry(it, s);
2327 }
2328
2329 for (i = 0; i < s->untracked.nr; i++) {
2330 it = &(s->untracked.items[i]);
2331 wt_porcelain_v2_print_other(it, s, '?');
2332 }
2333
2334 for (i = 0; i < s->ignored.nr; i++) {
2335 it = &(s->ignored.items[i]);
2336 wt_porcelain_v2_print_other(it, s, '!');
2337 }
2338}
2339
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002340void wt_status_print(struct wt_status *s)
2341{
Jeff Hostetler942b2742019-02-22 14:25:03 -08002342 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2343 trace2_data_intmax("status", s->repo, "count/untracked",
2344 s->untracked.nr);
2345 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2346
2347 trace2_region_enter("status", "print", s->repo);
2348
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002349 switch (s->status_format) {
2350 case STATUS_FORMAT_SHORT:
2351 wt_shortstatus_print(s);
2352 break;
2353 case STATUS_FORMAT_PORCELAIN:
2354 wt_porcelain_print(s);
2355 break;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002356 case STATUS_FORMAT_PORCELAIN_V2:
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002357 wt_porcelain_v2_print(s);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002358 break;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002359 case STATUS_FORMAT_UNSPECIFIED:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002360 BUG("finalize_deferred_config() should have been called");
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002361 break;
2362 case STATUS_FORMAT_NONE:
2363 case STATUS_FORMAT_LONG:
2364 wt_longstatus_print(s);
2365 break;
2366 }
Jeff Hostetler942b2742019-02-22 14:25:03 -08002367
2368 trace2_region_leave("status", "print", s->repo);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002369}
Johannes Schindelinfd849862016-10-07 18:08:38 +02002370
2371/**
2372 * Returns 1 if there are unstaged changes, 0 otherwise.
2373 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002374int has_unstaged_changes(struct repository *r, int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002375{
2376 struct rev_info rev_info;
2377 int result;
2378
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002379 repo_init_revisions(r, &rev_info, NULL);
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002380 if (ignore_submodules) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002381 rev_info.diffopt.flags.ignore_submodules = 1;
Junio C Hamanob50d82b2017-11-15 12:14:30 +09002382 rev_info.diffopt.flags.override_submodule_config = 1;
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002383 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002384 rev_info.diffopt.flags.quick = 1;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002385 diff_setup_done(&rev_info.diffopt);
2386 result = run_diff_files(&rev_info, 0);
2387 return diff_result_code(&rev_info.diffopt, result);
2388}
2389
2390/**
2391 * Returns 1 if there are uncommitted changes, 0 otherwise.
2392 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002393int has_uncommitted_changes(struct repository *r,
2394 int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002395{
2396 struct rev_info rev_info;
2397 int result;
2398
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002399 if (is_index_unborn(r->index))
Johannes Schindelinfd849862016-10-07 18:08:38 +02002400 return 0;
2401
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002402 repo_init_revisions(r, &rev_info, NULL);
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002403 if (ignore_submodules)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002404 rev_info.diffopt.flags.ignore_submodules = 1;
2405 rev_info.diffopt.flags.quick = 1;
Jeff King3506dc92018-07-11 10:14:06 -04002406
Johannes Schindelinfd849862016-10-07 18:08:38 +02002407 add_head_to_pending(&rev_info);
Jeff King3506dc92018-07-11 10:14:06 -04002408 if (!rev_info.pending.nr) {
2409 /*
2410 * We have no head (or it's corrupt); use the empty tree,
2411 * which will complain if the index is non-empty.
2412 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002413 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
Jeff King3506dc92018-07-11 10:14:06 -04002414 add_pending_object(&rev_info, &tree->object, "");
2415 }
2416
Johannes Schindelinfd849862016-10-07 18:08:38 +02002417 diff_setup_done(&rev_info.diffopt);
2418 result = run_diff_index(&rev_info, 1);
2419 return diff_result_code(&rev_info.diffopt, result);
2420}
2421
2422/**
2423 * If the work tree has unstaged or uncommitted changes, dies with the
2424 * appropriate message.
2425 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002426int require_clean_work_tree(struct repository *r,
2427 const char *action,
2428 const char *hint,
2429 int ignore_submodules,
2430 int gently)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002431{
Martin Ågren837e34e2017-10-05 22:32:04 +02002432 struct lock_file lock_file = LOCK_INIT;
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002433 int err = 0, fd;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002434
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +07002435 fd = repo_hold_locked_index(r, &lock_file, 0);
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002436 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002437 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002438 repo_update_index_if_able(r, &lock_file);
Martin Ågren837e34e2017-10-05 22:32:04 +02002439 rollback_lock_file(&lock_file);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002440
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002441 if (has_unstaged_changes(r, ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002442 /* TRANSLATORS: the action is e.g. "pull with rebase" */
Johannes Schindelin4777e172016-10-07 18:09:04 +02002443 error(_("cannot %s: You have unstaged changes."), _(action));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002444 err = 1;
2445 }
2446
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002447 if (has_uncommitted_changes(r, ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002448 if (err)
Johannes Schindelin4777e172016-10-07 18:09:04 +02002449 error(_("additionally, your index contains uncommitted changes."));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002450 else
Johannes Schindelin4777e172016-10-07 18:09:04 +02002451 error(_("cannot %s: Your index contains uncommitted changes."),
Johannes Schindelinfd849862016-10-07 18:08:38 +02002452 _(action));
2453 err = 1;
2454 }
2455
2456 if (err) {
2457 if (hint)
2458 error("%s", hint);
2459 if (!gently)
2460 exit(128);
2461 }
2462
2463 return err;
2464}