blob: 9e8c08003b990c44ca17dc1e778bf572eb694786 [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00001#include "git-compat-util.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +00002#include "advice.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04003#include "wt-status.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04004#include "object.h"
5#include "dir.h"
6#include "commit.h"
7#include "diff.h"
Elijah Newren7ee24e12023-03-21 06:25:57 +00008#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00009#include "gettext.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000010#include "hash.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000011#include "hex.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070012#include "object-name.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000013#include "path.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -040014#include "revision.h"
15#include "diffcore.h"
Dmitry Potapova734d0b2008-03-07 05:30:58 +030016#include "quote.h"
Ping Yinac8d5af2008-04-12 23:05:32 +080017#include "run-command.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040018#include "strvec.h"
Junio C Hamanob6975ab2008-07-02 00:52:16 -070019#include "remote.h"
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020020#include "refs.h"
Jens Lehmann46a958b2010-06-25 16:56:47 +020021#include "submodule.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070022#include "column.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000023#include "read-cache.h"
Elijah Newrene38da482023-03-21 06:26:05 +000024#include "setup.h"
Lucien Kong2d1cceb2012-06-10 13:17:38 +020025#include "strbuf.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000026#include "trace.h"
27#include "trace2.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000028#include "tree.h"
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +070029#include "utf8.h"
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +070030#include "worktree.h"
Johannes Schindelinfd849862016-10-07 18:08:38 +020031#include "lockfile.h"
Phillip Wood4a724862019-04-16 11:18:42 +010032#include "sequencer.h"
Rudy Rigotecbc23e2022-11-30 00:52:16 +000033#include "fsmonitor-settings.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -040034
Jeff Hostetler0a535612019-06-18 13:21:27 -070035#define AB_DELAY_WARNING_IN_MS (2 * 1000)
Rudy Rigotecbc23e2022-11-30 00:52:16 +000036#define UF_DELAY_WARNING_IN_MS (2 * 1000)
Jeff Hostetler0a535612019-06-18 13:21:27 -070037
Nguyễn Thái Ngọc Duy983dc692014-02-17 19:15:30 +070038static const char cut_line[] =
Jens Lehmann1a72cfd2013-12-05 20:44:14 +010039"------------------------ >8 ------------------------\n";
40
Junio C Hamano23900a92009-08-09 23:08:40 -070041static char default_wt_status_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010042 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
43 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
44 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
45 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
46 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
Junio C Hamano4d4d5722009-08-05 00:04:51 -070047 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020048 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
49 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
Jeff King148135f2010-12-09 12:27:08 -050050 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
Jeff Kingc91f0d92006-09-08 04:05:34 -040051};
Junio C Hamano4d229652007-01-11 15:34:41 -080052
Junio C Hamanod249b092009-08-09 21:59:30 -070053static const char *color(int slot, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -040054{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070055 const char *c = "";
56 if (want_color(s->use_color))
57 c = s->color_palette[slot];
Jeff King148135f2010-12-09 12:27:08 -050058 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
59 c = s->color_palette[WT_STATUS_HEADER];
60 return c;
Jeff Kingc91f0d92006-09-08 04:05:34 -040061}
62
Jonathan Niederbecbdae2011-02-25 23:09:41 -060063static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
64 const char *fmt, va_list ap, const char *trail)
65{
66 struct strbuf sb = STRBUF_INIT;
67 struct strbuf linebuf = STRBUF_INIT;
68 const char *line, *eol;
69
70 strbuf_vaddf(&sb, fmt, ap);
71 if (!sb.len) {
Matthieu Moy2556b992013-09-06 19:43:07 +020072 if (s->display_comment_prefix) {
73 strbuf_addch(&sb, comment_line_char);
74 if (!trail)
75 strbuf_addch(&sb, ' ');
76 }
Jonathan Niederbecbdae2011-02-25 23:09:41 -060077 color_print_strbuf(s->fp, color, &sb);
78 if (trail)
79 fprintf(s->fp, "%s", trail);
80 strbuf_release(&sb);
81 return;
82 }
83 for (line = sb.buf; *line; line = eol + 1) {
84 eol = strchr(line, '\n');
85
86 strbuf_reset(&linebuf);
Matthieu Moy2556b992013-09-06 19:43:07 +020087 if (at_bol && s->display_comment_prefix) {
Junio C Hamanoeff80a92013-01-16 20:18:48 +010088 strbuf_addch(&linebuf, comment_line_char);
Jonathan Niederbecbdae2011-02-25 23:09:41 -060089 if (*line != '\n' && *line != '\t')
90 strbuf_addch(&linebuf, ' ');
91 }
92 if (eol)
93 strbuf_add(&linebuf, line, eol - line);
94 else
95 strbuf_addstr(&linebuf, line);
96 color_print_strbuf(s->fp, color, &linebuf);
97 if (eol)
98 fprintf(s->fp, "\n");
99 else
100 break;
101 at_bol = 1;
102 }
103 if (trail)
104 fprintf(s->fp, "%s", trail);
105 strbuf_release(&linebuf);
106 strbuf_release(&sb);
107}
108
109void status_printf_ln(struct wt_status *s, const char *color,
110 const char *fmt, ...)
111{
112 va_list ap;
113
114 va_start(ap, fmt);
115 status_vprintf(s, 1, color, fmt, ap, "\n");
116 va_end(ap);
117}
118
119void status_printf(struct wt_status *s, const char *color,
120 const char *fmt, ...)
121{
122 va_list ap;
123
124 va_start(ap, fmt);
125 status_vprintf(s, 1, color, fmt, ap, NULL);
126 va_end(ap);
127}
128
Junio C Hamano1e248452012-09-15 22:46:26 -0700129static void status_printf_more(struct wt_status *s, const char *color,
130 const char *fmt, ...)
Jonathan Niederbecbdae2011-02-25 23:09:41 -0600131{
132 va_list ap;
133
134 va_start(ap, fmt);
135 status_vprintf(s, 0, color, fmt, ap, NULL);
136 va_end(ap);
137}
138
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100139void wt_status_prepare(struct repository *r, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400140{
Junio C Hamanocc46a742007-02-09 16:22:42 -0800141 memset(s, 0, sizeof(*s));
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100142 s->repo = r;
Junio C Hamano23900a92009-08-09 23:08:40 -0700143 memcpy(s->color_palette, default_wt_status_colors,
144 sizeof(default_wt_status_colors));
Junio C Hamanod249b092009-08-09 21:59:30 -0700145 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
146 s->use_color = -1;
147 s->relative_paths = 1;
René Scharfeefbd4fd2017-10-01 09:29:03 +0200148 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400149 s->reference = "HEAD";
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400150 s->fp = stdout;
Kristian Høgsberg0f729f22007-09-17 20:06:43 -0400151 s->index_file = get_index_file();
Junio C Hamano50b7e702009-08-04 23:49:33 -0700152 s->change.strdup_strings = 1;
Junio C Hamano76378682009-08-10 00:36:33 -0700153 s->untracked.strdup_strings = 1;
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700154 s->ignored.strdup_strings = 1;
Junio C Hamano84b42022013-06-24 11:41:40 -0700155 s->show_branch = -1; /* unspecified */
Liam Beguinc1b5d012017-06-17 18:30:51 -0400156 s->show_stash = 0;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +0000157 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
Matthieu Moy2556b992013-09-06 19:43:07 +0200158 s->display_comment_prefix = 0;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000159 s->detect_rename = -1;
160 s->rename_score = -1;
161 s->rename_limit = -1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400162}
163
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400164static void wt_longstatus_print_unmerged_header(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700165{
Lucien Kong96b0ec12012-06-05 22:21:26 +0200166 int i;
167 int del_mod_conflict = 0;
168 int both_deleted = 0;
169 int not_deleted = 0;
Junio C Hamanod249b092009-08-09 21:59:30 -0700170 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800171
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000172 status_printf_ln(s, c, _("Unmerged paths:"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200173
174 for (i = 0; i < s->change.nr; i++) {
175 struct string_list_item *it = &(s->change.items[i]);
176 struct wt_status_change_data *d = it->util;
177
178 switch (d->stagemask) {
179 case 0:
180 break;
181 case 1:
182 both_deleted = 1;
183 break;
184 case 3:
185 case 5:
186 del_mod_conflict = 1;
187 break;
188 default:
189 not_deleted = 1;
190 break;
191 }
192 }
193
Matthieu Moy6a964f52013-09-12 12:50:05 +0200194 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400195 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500196 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800197 ;
Nguyễn Thái Ngọc Duy80f537f2019-04-25 16:45:58 +0700198 else if (!s->is_initial) {
199 if (!strcmp(s->reference, "HEAD"))
200 status_printf_ln(s, c,
201 _(" (use \"git restore --staged <file>...\" to unstage)"));
202 else
203 status_printf_ln(s, c,
204 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
205 s->reference);
206 } else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000207 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200208
209 if (!both_deleted) {
210 if (!del_mod_conflict)
211 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
212 else
213 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
214 } else if (!del_mod_conflict && !not_deleted) {
215 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
216 } else {
217 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
218 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700219}
220
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400221static void wt_longstatus_print_cached_header(struct wt_status *s)
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100222{
Junio C Hamanod249b092009-08-09 21:59:30 -0700223 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800224
Ævar Arnfjörð Bjarmason919a4ce2011-02-22 23:42:16 +0000225 status_printf_ln(s, c, _("Changes to be committed:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200226 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400227 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500228 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800229 ; /* NEEDSWORK: use "git reset --unresolve"??? */
Nguyễn Thái Ngọc Duy80f537f2019-04-25 16:45:58 +0700230 else if (!s->is_initial) {
231 if (!strcmp(s->reference, "HEAD"))
232 status_printf_ln(s, c
233 , _(" (use \"git restore --staged <file>...\" to unstage)"));
234 else
235 status_printf_ln(s, c,
236 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
237 s->reference);
238 } else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000239 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100240}
241
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400242static void wt_longstatus_print_dirty_header(struct wt_status *s,
243 int has_deleted,
244 int has_dirty_submodules)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400245{
Junio C Hamanod249b092009-08-09 21:59:30 -0700246 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800247
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000248 status_printf_ln(s, c, _("Changes not staged for commit:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200249 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400250 return;
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200251 if (!has_deleted)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000252 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200253 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000254 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
Nguyễn Thái Ngọc Duy80f537f2019-04-25 16:45:58 +0700255 status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100256 if (has_dirty_submodules)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000257 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200258}
259
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400260static void wt_longstatus_print_other_header(struct wt_status *s,
261 const char *what,
262 const char *how)
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200263{
Junio C Hamanod249b092009-08-09 21:59:30 -0700264 const char *c = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +0700265 status_printf_ln(s, c, "%s:", what);
Matthieu Moy6a964f52013-09-12 12:50:05 +0200266 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400267 return;
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000268 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400269}
270
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400271static void wt_longstatus_print_trailer(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400272{
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500273 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400274}
275
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800276static const char *wt_status_unmerged_status_string(int stagemask)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700277{
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800278 switch (stagemask) {
279 case 1:
280 return _("both deleted:");
281 case 2:
282 return _("added by us:");
283 case 3:
284 return _("deleted by them:");
285 case 4:
286 return _("added by them:");
287 case 5:
288 return _("deleted by us:");
289 case 6:
290 return _("both added:");
291 case 7:
292 return _("both modified:");
293 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200294 BUG("unhandled unmerged status %x", stagemask);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700295 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700296}
297
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700298static const char *wt_status_diff_status_string(int status)
299{
300 switch (status) {
301 case DIFF_STATUS_ADDED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700302 return _("new file:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700303 case DIFF_STATUS_COPIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700304 return _("copied:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700305 case DIFF_STATUS_DELETED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700306 return _("deleted:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700307 case DIFF_STATUS_MODIFIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700308 return _("modified:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700309 case DIFF_STATUS_RENAMED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700310 return _("renamed:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700311 case DIFF_STATUS_TYPE_CHANGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700312 return _("typechange:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700313 case DIFF_STATUS_UNKNOWN:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700314 return _("unknown:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700315 case DIFF_STATUS_UNMERGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700316 return _("unmerged:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700317 default:
318 return NULL;
319 }
320}
321
Jonathan Nieder335e8252013-12-19 11:43:19 -0800322static int maxwidth(const char *(*label)(int), int minval, int maxval)
323{
324 int result = 0, i;
325
326 for (i = minval; i <= maxval; i++) {
327 const char *s = label(i);
328 int len = s ? utf8_strwidth(s) : 0;
329 if (len > result)
330 result = len;
331 }
332 return result;
333}
334
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400335static void wt_longstatus_print_unmerged_data(struct wt_status *s,
336 struct string_list_item *it)
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800337{
338 const char *c = color(WT_STATUS_UNMERGED, s);
339 struct wt_status_change_data *d = it->util;
340 struct strbuf onebuf = STRBUF_INIT;
341 static char *padding;
342 static int label_width;
343 const char *one, *how;
344 int len;
345
346 if (!padding) {
347 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
348 label_width += strlen(" ");
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800349 padding = xmallocz(label_width);
350 memset(padding, ' ', label_width);
351 }
352
Junio C Hamano88910c92020-09-10 10:01:54 -0700353 one = quote_path(it->string, s->prefix, &onebuf, 0);
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800354 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
355
356 how = wt_status_unmerged_status_string(d->stagemask);
357 len = label_width - utf8_strwidth(how);
358 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
359 strbuf_release(&onebuf);
360}
361
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400362static void wt_longstatus_print_change_data(struct wt_status *s,
363 int change_type,
364 struct string_list_item *it)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400365{
Junio C Hamano50b7e702009-08-04 23:49:33 -0700366 struct wt_status_change_data *d = it->util;
Junio C Hamanod249b092009-08-09 21:59:30 -0700367 const char *c = color(change_type, s);
Jeff Kingb8527d52013-03-21 07:05:28 -0400368 int status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700369 char *one_name;
370 char *two_name;
Junio C Hamano3a946802006-11-08 13:20:46 -0800371 const char *one, *two;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500372 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100373 struct strbuf extra = STRBUF_INIT;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700374 static char *padding;
Junio C Hamanod52cb572014-03-12 13:51:22 -0700375 static int label_width;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700376 const char *what;
377 int len;
378
379 if (!padding) {
Jonathan Nieder335e8252013-12-19 11:43:19 -0800380 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
381 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
Junio C Hamanod52cb572014-03-12 13:51:22 -0700382 label_width += strlen(" ");
383 padding = xmallocz(label_width);
384 memset(padding, ' ', label_width);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700385 }
Junio C Hamano3a946802006-11-08 13:20:46 -0800386
Junio C Hamano50b7e702009-08-04 23:49:33 -0700387 one_name = two_name = it->string;
388 switch (change_type) {
389 case WT_STATUS_UPDATED:
390 status = d->index_status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700391 break;
392 case WT_STATUS_CHANGED:
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100393 if (d->new_submodule_commits || d->dirty_submodule) {
394 strbuf_addstr(&extra, " (");
395 if (d->new_submodule_commits)
René Scharfea22ae752016-09-15 20:31:00 +0200396 strbuf_addstr(&extra, _("new commits, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100397 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
René Scharfea22ae752016-09-15 20:31:00 +0200398 strbuf_addstr(&extra, _("modified content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100399 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
René Scharfea22ae752016-09-15 20:31:00 +0200400 strbuf_addstr(&extra, _("untracked content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100401 strbuf_setlen(&extra, extra.len - 2);
402 strbuf_addch(&extra, ')');
403 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700404 status = d->worktree_status;
405 break;
Jeff Kingb8527d52013-03-21 07:05:28 -0400406 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200407 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
Jeff Kingb8527d52013-03-21 07:05:28 -0400408 change_type);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700409 }
410
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700411 /*
412 * Only pick up the rename it's relevant. If the rename is for
413 * the changed section and we're printing the updated section,
414 * ignore it.
415 */
416 if (d->rename_status == status)
417 one_name = d->rename_source;
418
Junio C Hamano88910c92020-09-10 10:01:54 -0700419 one = quote_path(one_name, s->prefix, &onebuf, 0);
420 two = quote_path(two_name, s->prefix, &twobuf, 0);
Junio C Hamano3a946802006-11-08 13:20:46 -0800421
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600422 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700423 what = wt_status_diff_status_string(status);
424 if (!what)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200425 BUG("unhandled diff status %c", status);
Junio C Hamanod52cb572014-03-12 13:51:22 -0700426 len = label_width - utf8_strwidth(what);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700427 assert(len >= 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700428 if (one_name != two_name)
Junio C Hamanod52cb572014-03-12 13:51:22 -0700429 status_printf_more(s, c, "%s%.*s%s -> %s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700430 what, len, padding, one, two);
431 else
Junio C Hamanod52cb572014-03-12 13:51:22 -0700432 status_printf_more(s, c, "%s%.*s%s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700433 what, len, padding, one);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100434 if (extra.len) {
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600435 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100436 strbuf_release(&extra);
437 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600438 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
Johannes Schindelin367c9882007-11-11 17:35:41 +0000439 strbuf_release(&onebuf);
440 strbuf_release(&twobuf);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400441}
442
Nguyễn Thái Ngọc Duy98bc94e2017-12-27 17:18:36 +0700443static char short_submodule_status(struct wt_status_change_data *d)
444{
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700445 if (d->new_submodule_commits)
446 return 'M';
447 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
448 return 'm';
449 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
450 return '?';
451 return d->worktree_status;
452}
453
Junio C Hamano50b7e702009-08-04 23:49:33 -0700454static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
Jeff King61bdc7c2022-12-13 06:13:48 -0500455 struct diff_options *options UNUSED,
Junio C Hamano50b7e702009-08-04 23:49:33 -0700456 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400457{
458 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400459 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700460
461 if (!q->nr)
462 return;
463 s->workdir_dirty = 1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400464 for (i = 0; i < q->nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700465 struct diff_filepair *p;
466 struct string_list_item *it;
467 struct wt_status_change_data *d;
468
469 p = q->queue[i];
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700470 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700471 d = it->util;
472 if (!d) {
René Scharfeca56dad2021-03-13 17:17:22 +0100473 CALLOC_ARRAY(d, 1);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700474 it->util = d;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400475 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700476 if (!d->worktree_status)
477 d->worktree_status = p->status;
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700478 if (S_ISGITLINK(p->two->mode)) {
479 d->dirty_submodule = p->two->dirty_submodule;
Jeff King4a7e27e2018-08-28 17:22:40 -0400480 d->new_submodule_commits = !oideq(&p->one->oid,
481 &p->two->oid);
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700482 if (s->status_format == STATUS_FORMAT_SHORT)
483 d->worktree_status = short_submodule_status(d);
484 }
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400485
486 switch (p->status) {
487 case DIFF_STATUS_ADDED:
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700488 d->mode_worktree = p->two->mode;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400489 break;
490
491 case DIFF_STATUS_DELETED:
492 d->mode_index = p->one->mode;
493 oidcpy(&d->oid_index, &p->one->oid);
494 /* mode_worktree is zero for a delete. */
495 break;
496
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700497 case DIFF_STATUS_COPIED:
498 case DIFF_STATUS_RENAMED:
499 if (d->rename_status)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200500 BUG("multiple renames on the same target? how?");
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700501 d->rename_source = xstrdup(p->one->path);
502 d->rename_score = p->score * 100 / MAX_SCORE;
503 d->rename_status = p->status;
504 /* fallthru */
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400505 case DIFF_STATUS_MODIFIED:
506 case DIFF_STATUS_TYPE_CHANGED:
507 case DIFF_STATUS_UNMERGED:
508 d->mode_index = p->one->mode;
509 d->mode_worktree = p->two->mode;
510 oidcpy(&d->oid_index, &p->one->oid);
511 break;
512
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700513 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200514 BUG("unhandled diff-files status '%c'", p->status);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400515 break;
516 }
517
Jeff Kingc91f0d92006-09-08 04:05:34 -0400518 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400519}
520
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100521static int unmerged_mask(struct index_state *istate, const char *path)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700522{
523 int pos, mask;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700524 const struct cache_entry *ce;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700525
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100526 pos = index_name_pos(istate, path, strlen(path));
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700527 if (0 <= pos)
528 return 0;
529
530 mask = 0;
531 pos = -pos-1;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100532 while (pos < istate->cache_nr) {
533 ce = istate->cache[pos++];
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700534 if (strcmp(ce->name, path) || !ce_stage(ce))
535 break;
536 mask |= (1 << (ce_stage(ce) - 1));
537 }
538 return mask;
539}
540
Junio C Hamano50b7e702009-08-04 23:49:33 -0700541static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
Jeff King61bdc7c2022-12-13 06:13:48 -0500542 struct diff_options *options UNUSED,
Junio C Hamano50b7e702009-08-04 23:49:33 -0700543 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400544{
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100545 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400546 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700547
548 for (i = 0; i < q->nr; i++) {
549 struct diff_filepair *p;
550 struct string_list_item *it;
551 struct wt_status_change_data *d;
552
553 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100554 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700555 d = it->util;
556 if (!d) {
René Scharfeca56dad2021-03-13 17:17:22 +0100557 CALLOC_ARRAY(d, 1);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700558 it->util = d;
559 }
560 if (!d->index_status)
561 d->index_status = p->status;
562 switch (p->status) {
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400563 case DIFF_STATUS_ADDED:
564 /* Leave {mode,oid}_head zero for an add. */
565 d->mode_index = p->two->mode;
566 oidcpy(&d->oid_index, &p->two->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700567 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400568 break;
569 case DIFF_STATUS_DELETED:
570 d->mode_head = p->one->mode;
571 oidcpy(&d->oid_head, &p->one->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700572 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400573 /* Leave {mode,oid}_index zero for a delete. */
574 break;
575
Junio C Hamano50b7e702009-08-04 23:49:33 -0700576 case DIFF_STATUS_COPIED:
577 case DIFF_STATUS_RENAMED:
Nguyễn Thái Ngọc Duy176ea742017-12-27 17:18:39 +0700578 if (d->rename_status)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200579 BUG("multiple renames on the same target? how?");
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +0700580 d->rename_source = xstrdup(p->one->path);
581 d->rename_score = p->score * 100 / MAX_SCORE;
582 d->rename_status = p->status;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400583 /* fallthru */
584 case DIFF_STATUS_MODIFIED:
585 case DIFF_STATUS_TYPE_CHANGED:
586 d->mode_head = p->one->mode;
587 d->mode_index = p->two->mode;
588 oidcpy(&d->oid_head, &p->one->oid);
589 oidcpy(&d->oid_index, &p->two->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700590 s->committable = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700591 break;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700592 case DIFF_STATUS_UNMERGED:
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100593 d->stagemask = unmerged_mask(s->repo->index,
594 p->two->path);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400595 /*
596 * Don't bother setting {mode,oid}_{head,index} since the print
597 * code will output the stage values directly and not use the
598 * values in these fields.
599 */
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700600 break;
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700601
602 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +0200603 BUG("unhandled diff-index status '%c'", p->status);
Nguyễn Thái Ngọc Duyea56f972017-12-27 17:18:37 +0700604 break;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700605 }
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100606 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400607}
608
Junio C Hamano50b7e702009-08-04 23:49:33 -0700609static void wt_status_collect_changes_worktree(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400610{
611 struct rev_info rev;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700612
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100613 repo_init_revisions(s->repo, &rev, NULL);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700614 setup_revisions(0, NULL, &rev, NULL);
615 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700616 rev.diffopt.flags.dirty_submodules = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700617 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100618 if (!s->show_untracked_files)
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700619 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200620 if (s->ignore_submodule_arg) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700621 rev.diffopt.flags.override_submodule_config = 1;
Jens Lehmann46a958b2010-06-25 16:56:47 +0200622 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Sangeeta Jain8ef93122020-11-10 14:09:00 +0530623 } else if (!rev.diffopt.flags.ignore_submodule_set &&
624 s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
625 handle_ignore_submodules_arg(&rev.diffopt, "none");
Junio C Hamano50b7e702009-08-04 23:49:33 -0700626 rev.diffopt.format_callback = wt_status_collect_changed_cb;
627 rev.diffopt.format_callback_data = s;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000628 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
629 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
630 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 +0700631 copy_pathspec(&rev.prune_data, &s->pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700632 run_diff_files(&rev, 0);
Ævar Arnfjörð Bjarmasonf0cb6b82022-04-13 22:01:44 +0200633 release_revisions(&rev);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700634}
635
636static void wt_status_collect_changes_index(struct wt_status *s)
637{
638 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800639 struct setup_revision_opt opt;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700640
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100641 repo_init_revisions(s->repo, &rev, NULL);
Junio C Hamano32962c92010-03-08 22:58:09 -0800642 memset(&opt, 0, sizeof(opt));
brian m. carlsonf2e51192018-05-02 00:26:00 +0000643 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
Junio C Hamano32962c92010-03-08 22:58:09 -0800644 setup_revisions(0, NULL, &rev, &opt);
645
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700646 rev.diffopt.flags.override_submodule_config = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700647 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200648 if (s->ignore_submodule_arg) {
Jens Lehmann46a958b2010-06-25 16:56:47 +0200649 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Jens Lehmann1d2f3932014-04-05 18:59:03 +0200650 } else {
651 /*
652 * Unless the user did explicitly request a submodule ignore
653 * mode by passing a command line option we do not ignore any
654 * changed submodule SHA-1s when comparing index and HEAD, no
655 * matter what is configured. Otherwise the user won't be
Felipe Contreras0e20b222021-06-15 14:11:10 +0000656 * shown any submodules manually added (and which are
Jens Lehmann1d2f3932014-04-05 18:59:03 +0200657 * staged to be committed), which would be really confusing.
658 */
659 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200660 }
Jens Lehmann46a958b2010-06-25 16:56:47 +0200661
Jeff Kingc91f0d92006-09-08 04:05:34 -0400662 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700663 rev.diffopt.format_callback = wt_status_collect_updated_cb;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400664 rev.diffopt.format_callback_data = s;
Ben Pearte8b2dc22018-05-11 15:38:58 +0000665 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
666 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
667 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Victoria Dye2c521b02022-03-01 20:24:25 +0000668
669 /*
670 * The `recursive` option must be enabled to allow the diff to recurse
671 * into subdirectories of sparse directory index entries. If it is not
672 * enabled, a subdirectory containing file(s) with changes is reported
673 * as "modified", rather than the modified files themselves.
674 */
675 rev.diffopt.flags.recursive = 1;
676
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700677 copy_pathspec(&rev.prune_data, &s->pathspec);
Junio C Hamano976b97e2023-08-21 16:14:14 -0400678 run_diff_index(&rev, DIFF_INDEX_CACHED);
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +0200679 release_revisions(&rev);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400680}
681
Derrick Stoleefe0d5762021-07-14 13:12:38 +0000682static int add_file_to_list(const struct object_id *oid,
683 struct strbuf *base, const char *path,
684 unsigned int mode, void *context)
685{
686 struct string_list_item *it;
687 struct wt_status_change_data *d;
688 struct wt_status *s = context;
689 struct strbuf full_name = STRBUF_INIT;
690
691 if (S_ISDIR(mode))
692 return READ_TREE_RECURSIVE;
693
694 strbuf_add(&full_name, base->buf, base->len);
695 strbuf_addstr(&full_name, path);
696 it = string_list_insert(&s->change, full_name.buf);
697 d = it->util;
698 if (!d) {
699 CALLOC_ARRAY(d, 1);
700 it->util = d;
701 }
702
703 d->index_status = DIFF_STATUS_ADDED;
704 /* Leave {mode,oid}_head zero for adds. */
705 d->mode_index = mode;
706 oidcpy(&d->oid_index, oid);
707 s->committable = 1;
708 strbuf_release(&full_name);
709 return 0;
710}
711
Junio C Hamano50b7e702009-08-04 23:49:33 -0700712static void wt_status_collect_changes_initial(struct wt_status *s)
713{
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100714 struct index_state *istate = s->repo->index;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700715 int i;
716
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100717 for (i = 0; i < istate->cache_nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700718 struct string_list_item *it;
719 struct wt_status_change_data *d;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100720 const struct cache_entry *ce = istate->cache[i];
Junio C Hamano50b7e702009-08-04 23:49:33 -0700721
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100722 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700723 continue;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700724 if (ce_intent_to_add(ce))
725 continue;
Derrick Stoleefe0d5762021-07-14 13:12:38 +0000726 if (S_ISSPARSEDIR(ce->ce_mode)) {
727 /*
728 * This is a sparse directory entry, so we want to collect all
729 * of the added files within the tree. This requires recursively
730 * expanding the trees to find the elements that are new in this
731 * tree and marking them with DIFF_STATUS_ADDED.
732 */
733 struct strbuf base = STRBUF_INIT;
734 struct pathspec ps = { 0 };
735 struct tree *tree = lookup_tree(istate->repo, &ce->oid);
736
737 ps.recursive = 1;
738 ps.has_wildcard = 1;
739 ps.max_depth = -1;
740
741 strbuf_add(&base, ce->name, ce->ce_namelen);
Jeff King1ee7a5c2023-08-31 02:21:55 -0400742 read_tree_at(istate->repo, tree, &base, 0, &ps,
Derrick Stoleefe0d5762021-07-14 13:12:38 +0000743 add_file_to_list, s);
744 continue;
745 }
746
Julian Phillips78a395d2010-06-26 00:41:35 +0100747 it = string_list_insert(&s->change, ce->name);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700748 d = it->util;
749 if (!d) {
René Scharfeca56dad2021-03-13 17:17:22 +0100750 CALLOC_ARRAY(d, 1);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700751 it->util = d;
752 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700753 if (ce_stage(ce)) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700754 d->index_status = DIFF_STATUS_UNMERGED;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700755 d->stagemask |= (1 << (ce_stage(ce) - 1));
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400756 /*
757 * Don't bother setting {mode,oid}_{head,index} since the print
758 * code will output the stage values directly and not use the
759 * values in these fields.
760 */
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700761 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400762 } else {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700763 d->index_status = DIFF_STATUS_ADDED;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400764 /* Leave {mode,oid}_head zero for adds. */
765 d->mode_index = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +0100766 oidcpy(&d->oid_index, &ce->oid);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700767 s->committable = 1;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400768 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700769 }
770}
771
Junio C Hamano76378682009-08-10 00:36:33 -0700772static void wt_status_collect_untracked(struct wt_status *s)
773{
774 int i;
Ævar Arnfjörð Bjarmasonce93a4c2021-07-01 12:51:27 +0200775 struct dir_struct dir = DIR_INIT;
Karsten Blees132d41e2014-07-12 02:07:36 +0200776 uint64_t t_begin = getnanotime();
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100777 struct index_state *istate = s->repo->index;
Junio C Hamano76378682009-08-10 00:36:33 -0700778
779 if (!s->show_untracked_files)
780 return;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700781
Junio C Hamano76378682009-08-10 00:36:33 -0700782 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
783 dir.flags |=
784 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400785 if (s->show_ignored_mode) {
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200786 dir.flags |= DIR_SHOW_IGNORED_TOO;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400787
788 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
789 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
790 } else {
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100791 dir.untracked = istate->untracked;
Jameson Millereec0f7f2017-10-30 13:21:37 -0400792 }
793
Junio C Hamano76378682009-08-10 00:36:33 -0700794 setup_standard_excludes(&dir);
795
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +0100796 fill_directory(&dir, istate, &s->pathspec);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200797
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400798 for (i = 0; i < dir.nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700799 struct dir_entry *ent = dir.entries[i];
Elijah Newren95c11ec2020-04-01 04:17:45 +0000800 if (index_name_is_other(istate, ent->name, ent->len))
Brandon Caseyb8224232010-09-26 21:49:13 -0500801 string_list_insert(&s->untracked, ent->name);
Junio C Hamano76378682009-08-10 00:36:33 -0700802 }
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700803
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200804 for (i = 0; i < dir.ignored_nr; i++) {
805 struct dir_entry *ent = dir.ignored[i];
Elijah Newren95c11ec2020-04-01 04:17:45 +0000806 if (index_name_is_other(istate, ent->name, ent->len))
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200807 string_list_insert(&s->ignored, ent->name);
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700808 }
809
Elijah Newreneceba532020-08-18 22:58:26 +0000810 dir_clear(&dir);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700811
Ben Boeckeled9bff02021-08-23 12:44:00 +0200812 if (advice_enabled(ADVICE_STATUS_U_OPTION))
Karsten Blees132d41e2014-07-12 02:07:36 +0200813 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
Junio C Hamano76378682009-08-10 00:36:33 -0700814}
815
Stephen P. Smithc01d8f92018-09-05 17:53:26 -0700816static int has_unmerged(struct wt_status *s)
817{
818 int i;
819
820 for (i = 0; i < s->change.nr; i++) {
821 struct wt_status_change_data *d;
822 d = s->change.items[i].util;
823 if (d->stagemask)
824 return 1;
825 }
826 return 0;
827}
828
Junio C Hamano76378682009-08-10 00:36:33 -0700829void wt_status_collect(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700830{
Jeff Hostetler942b2742019-02-22 14:25:03 -0800831 trace2_region_enter("status", "worktrees", s->repo);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700832 wt_status_collect_changes_worktree(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800833 trace2_region_leave("status", "worktrees", s->repo);
834
835 if (s->is_initial) {
836 trace2_region_enter("status", "initial", s->repo);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700837 wt_status_collect_changes_initial(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800838 trace2_region_leave("status", "initial", s->repo);
839 } else {
840 trace2_region_enter("status", "index", s->repo);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700841 wt_status_collect_changes_index(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800842 trace2_region_leave("status", "index", s->repo);
843 }
844
845 trace2_region_enter("status", "untracked", s->repo);
Junio C Hamano76378682009-08-10 00:36:33 -0700846 wt_status_collect_untracked(s);
Jeff Hostetler942b2742019-02-22 14:25:03 -0800847 trace2_region_leave("status", "untracked", s->repo);
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700848
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +0100849 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700850 if (s->state.merge_in_progress && !has_unmerged(s))
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700851 s->committable = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700852}
853
Stephen P. Smith73ba5d72018-09-30 07:12:45 -0700854void wt_status_collect_free_buffers(struct wt_status *s)
855{
Martin Ågren962dd7e2020-09-27 15:15:43 +0200856 wt_status_state_free_buffers(&s->state);
857}
858
859void wt_status_state_free_buffers(struct wt_status_state *state)
860{
861 FREE_AND_NULL(state->branch);
862 FREE_AND_NULL(state->onto);
863 FREE_AND_NULL(state->detached_from);
Rubén Justo990adcc2023-09-09 22:12:47 +0200864 FREE_AND_NULL(state->bisecting_from);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700865}
866
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400867static void wt_longstatus_print_unmerged(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700868{
869 int shown_header = 0;
870 int i;
871
872 for (i = 0; i < s->change.nr; i++) {
873 struct wt_status_change_data *d;
874 struct string_list_item *it;
875 it = &(s->change.items[i]);
876 d = it->util;
877 if (!d->stagemask)
878 continue;
879 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400880 wt_longstatus_print_unmerged_header(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700881 shown_header = 1;
882 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400883 wt_longstatus_print_unmerged_data(s, it);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700884 }
885 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400886 wt_longstatus_print_trailer(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700887
888}
889
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400890static void wt_longstatus_print_updated(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700891{
892 int shown_header = 0;
893 int i;
894
895 for (i = 0; i < s->change.nr; i++) {
896 struct wt_status_change_data *d;
897 struct string_list_item *it;
898 it = &(s->change.items[i]);
899 d = it->util;
900 if (!d->index_status ||
901 d->index_status == DIFF_STATUS_UNMERGED)
902 continue;
903 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400904 wt_longstatus_print_cached_header(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700905 shown_header = 1;
906 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400907 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700908 }
909 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400910 wt_longstatus_print_trailer(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700911}
912
913/*
914 * -1 : has delete
915 * 0 : no change
916 * 1 : some change but no delete
917 */
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100918static int wt_status_check_worktree_changes(struct wt_status *s,
919 int *dirty_submodules)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700920{
921 int i;
922 int changes = 0;
923
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100924 *dirty_submodules = 0;
925
Junio C Hamano50b7e702009-08-04 23:49:33 -0700926 for (i = 0; i < s->change.nr; i++) {
927 struct wt_status_change_data *d;
928 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700929 if (!d->worktree_status ||
930 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700931 continue;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100932 if (!changes)
933 changes = 1;
934 if (d->dirty_submodule)
935 *dirty_submodules = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700936 if (d->worktree_status == DIFF_STATUS_DELETED)
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100937 changes = -1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700938 }
939 return changes;
940}
941
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400942static void wt_longstatus_print_changed(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400943{
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100944 int i, dirty_submodules;
945 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700946
947 if (!worktree_changes)
948 return;
949
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400950 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700951
952 for (i = 0; i < s->change.nr; i++) {
953 struct wt_status_change_data *d;
954 struct string_list_item *it;
955 it = &(s->change.items[i]);
956 d = it->util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700957 if (!d->worktree_status ||
958 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700959 continue;
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400960 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700961 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400962 wt_longstatus_print_trailer(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400963}
964
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200965static int stash_count_refs(struct object_id *ooid UNUSED,
966 struct object_id *noid UNUSED,
967 const char *email UNUSED,
968 timestamp_t timestamp UNUSED, int tz UNUSED,
969 const char *message UNUSED, void *cb_data)
Liam Beguinc1b5d012017-06-17 18:30:51 -0400970{
971 int *c = cb_data;
972 (*c)++;
973 return 0;
974}
975
Øystein Walle612942a2021-10-22 00:25:31 +0200976static int count_stash_entries(void)
977{
978 int n = 0;
979 for_each_reflog_ent("refs/stash", stash_count_refs, &n);
980 return n;
981}
982
Liam Beguinc1b5d012017-06-17 18:30:51 -0400983static void wt_longstatus_print_stash_summary(struct wt_status *s)
984{
Øystein Walle612942a2021-10-22 00:25:31 +0200985 int stash_count = count_stash_entries();
Liam Beguinc1b5d012017-06-17 18:30:51 -0400986
Liam Beguinc1b5d012017-06-17 18:30:51 -0400987 if (stash_count > 0)
988 status_printf_ln(s, GIT_COLOR_NORMAL,
989 Q_("Your stash currently has %d entry",
990 "Your stash currently has %d entries", stash_count),
991 stash_count);
992}
993
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400994static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
Ping Yinac8d5af2008-04-12 23:05:32 +0800995{
René Scharfed3180272014-08-19 21:09:35 +0200996 struct child_process sm_summary = CHILD_PROCESS_INIT;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200997 struct strbuf cmd_stdout = STRBUF_INIT;
998 struct strbuf summary = STRBUF_INIT;
999 char *summary_content;
Ping Yinac8d5af2008-04-12 23:05:32 +08001000
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001001 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
Ping Yinac8d5af2008-04-12 23:05:32 +08001002
Jeff Kingc972bf42020-07-28 16:25:12 -04001003 strvec_push(&sm_summary.args, "submodule");
1004 strvec_push(&sm_summary.args, "summary");
1005 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1006 strvec_push(&sm_summary.args, "--for-status");
1007 strvec_push(&sm_summary.args, "--summary-limit");
1008 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
Matthieu Moybb7e32e2013-09-06 19:43:05 +02001009 if (!uncommitted)
Jeff Kingc972bf42020-07-28 16:25:12 -04001010 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
Matthieu Moybb7e32e2013-09-06 19:43:05 +02001011
Ping Yinac8d5af2008-04-12 23:05:32 +08001012 sm_summary.git_cmd = 1;
1013 sm_summary.no_stdin = 1;
Matthieu Moy3ba74072013-09-06 19:43:06 +02001014
Jeff King5c950e92015-03-22 23:53:52 -04001015 capture_command(&sm_summary, &cmd_stdout, 1024);
Matthieu Moy3ba74072013-09-06 19:43:06 +02001016
1017 /* prepend header, only if there's an actual output */
Jeff Kingd56d9662015-03-22 06:00:32 -04001018 if (cmd_stdout.len) {
Matthieu Moy3ba74072013-09-06 19:43:06 +02001019 if (uncommitted)
1020 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1021 else
1022 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1023 strbuf_addstr(&summary, "\n\n");
1024 }
1025 strbuf_addbuf(&summary, &cmd_stdout);
1026 strbuf_release(&cmd_stdout);
1027
Matthieu Moy2556b992013-09-06 19:43:07 +02001028 if (s->display_comment_prefix) {
Jeff Kingd56d9662015-03-22 06:00:32 -04001029 size_t len;
Matthieu Moy2556b992013-09-06 19:43:07 +02001030 summary_content = strbuf_detach(&summary, &len);
Calvin Wan787cb8a2023-06-06 19:48:43 +00001031 strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
Matthieu Moy2556b992013-09-06 19:43:07 +02001032 free(summary_content);
1033 }
Matthieu Moy3ba74072013-09-06 19:43:06 +02001034
1035 fputs(summary.buf, s->fp);
1036 strbuf_release(&summary);
Ping Yinac8d5af2008-04-12 23:05:32 +08001037}
1038
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001039static void wt_longstatus_print_other(struct wt_status *s,
1040 struct string_list *l,
1041 const char *what,
1042 const char *how)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001043{
Jeff Kingc91f0d92006-09-08 04:05:34 -04001044 int i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001045 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001046 static struct string_list output = STRING_LIST_INIT_DUP;
1047 struct column_options copts;
Jeff Kingc91f0d92006-09-08 04:05:34 -04001048
Jeff King12829882011-06-02 01:54:49 -04001049 if (!l->nr)
Junio C Hamano76378682009-08-10 00:36:33 -07001050 return;
Jeff Kingc91f0d92006-09-08 04:05:34 -04001051
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001052 wt_longstatus_print_other_header(s, what, how);
Junio C Hamano1b908b62010-04-10 00:19:46 -07001053
1054 for (i = 0; i < l->nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -07001055 struct string_list_item *it;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001056 const char *path;
Junio C Hamano1b908b62010-04-10 00:19:46 -07001057 it = &(l->items[i]);
Junio C Hamano88910c92020-09-10 10:01:54 -07001058 path = quote_path(it->string, s->prefix, &buf, 0);
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001059 if (column_active(s->colopts)) {
1060 string_list_append(&output, path);
1061 continue;
1062 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001063 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1064 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001065 "%s\n", path);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001066 }
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001067
1068 strbuf_release(&buf);
1069 if (!column_active(s->colopts))
Matthieu Moy2f0f7f12013-09-06 19:43:09 +02001070 goto conclude;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001071
Matthieu Moy2556b992013-09-06 19:43:07 +02001072 strbuf_addf(&buf, "%s%s\t%s",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001073 color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001074 s->display_comment_prefix ? "#" : "",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001075 color(WT_STATUS_UNTRACKED, s));
1076 memset(&copts, 0, sizeof(copts));
1077 copts.padding = 1;
1078 copts.indent = buf.buf;
1079 if (want_color(s->use_color))
1080 copts.nl = GIT_COLOR_RESET "\n";
1081 print_columns(&output, s->colopts, &copts);
1082 string_list_clear(&output, 0);
Johannes Schindelin367c9882007-11-11 17:35:41 +00001083 strbuf_release(&buf);
Matthieu Moy2f0f7f12013-09-06 19:43:09 +02001084conclude:
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001085 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001086}
1087
Brian Malehornd76650b2017-05-15 23:06:49 -07001088size_t wt_status_locate_end(const char *s, size_t len)
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001089{
1090 const char *p;
1091 struct strbuf pattern = STRBUF_INIT;
1092
SZEDER Gáborfbfa0972015-06-09 02:28:34 +02001093 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
Brian Malehornd76650b2017-05-15 23:06:49 -07001094 if (starts_with(s, pattern.buf + 1))
1095 len = 0;
1096 else if ((p = strstr(s, pattern.buf)))
1097 len = p - s + 1;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001098 strbuf_release(&pattern);
Brian Malehornd76650b2017-05-15 23:06:49 -07001099 return len;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001100}
1101
Denton Liud540b702019-04-17 11:23:27 +01001102void wt_status_append_cut_line(struct strbuf *buf)
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001103{
Kaartic Sivaraam8c4b1a32017-09-13 13:05:38 +00001104 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
Denton Liud540b702019-04-17 11:23:27 +01001105
Calvin Wan787cb8a2023-06-06 19:48:43 +00001106 strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
1107 strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
Denton Liud540b702019-04-17 11:23:27 +01001108}
1109
1110void wt_status_add_cut_line(FILE *fp)
1111{
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001112 struct strbuf buf = STRBUF_INIT;
1113
Denton Liud540b702019-04-17 11:23:27 +01001114 wt_status_append_cut_line(&buf);
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001115 fputs(buf.buf, fp);
1116 strbuf_release(&buf);
1117}
1118
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001119static void wt_longstatus_print_verbose(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001120{
1121 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -08001122 struct setup_revision_opt opt;
Michael J Gruber40555002015-03-06 10:43:35 +01001123 int dirty_submodules;
1124 const char *c = color(WT_STATUS_HEADER, s);
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001125
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01001126 repo_init_revisions(s->repo, &rev, NULL);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001127 rev.diffopt.flags.allow_textconv = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +07001128 rev.diffopt.ita_invisible_in_index = 1;
Junio C Hamano32962c92010-03-08 22:58:09 -08001129
1130 memset(&opt, 0, sizeof(opt));
brian m. carlsonf2e51192018-05-02 00:26:00 +00001131 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
Junio C Hamano32962c92010-03-08 22:58:09 -08001132 setup_revisions(0, NULL, &rev, &opt);
1133
Jeff Kingc91f0d92006-09-08 04:05:34 -04001134 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001135 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1136 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1137 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Kristian Høgsberg4ba0cb22008-03-10 13:58:26 -04001138 rev.diffopt.file = s->fp;
1139 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 00:49:35 -04001140 /*
1141 * If we're not going to stdout, then we definitely don't
1142 * want color, since we are going to the commit message
1143 * file (and even the "auto" setting won't work, since it
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001144 * will have checked isatty on stdout). But we then do want
1145 * to insert the scissor line here to reliably remove the
1146 * diff before committing.
Jeff King4f672ad2008-10-26 00:49:35 -04001147 */
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001148 if (s->fp != stdout) {
Jeff Kingf1c96262011-08-17 22:03:12 -07001149 rev.diffopt.use_color = 0;
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001150 wt_status_add_cut_line(s->fp);
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001151 }
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001152 if (s->verbose > 1 && s->committable) {
Michael J Gruber40555002015-03-06 10:43:35 +01001153 /* print_updated() printed a header, so do we */
1154 if (s->fp != stdout)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001155 wt_longstatus_print_trailer(s);
Michael J Gruber40555002015-03-06 10:43:35 +01001156 status_printf_ln(s, c, _("Changes to be committed:"));
1157 rev.diffopt.a_prefix = "c/";
1158 rev.diffopt.b_prefix = "i/";
1159 } /* else use prefix as per user config */
Junio C Hamano976b97e2023-08-21 16:14:14 -04001160 run_diff_index(&rev, DIFF_INDEX_CACHED);
Michael J Gruber40555002015-03-06 10:43:35 +01001161 if (s->verbose > 1 &&
1162 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1163 status_printf_ln(s, c,
1164 "--------------------------------------------------");
1165 status_printf_ln(s, c, _("Changes not staged for commit:"));
1166 setup_work_tree();
1167 rev.diffopt.a_prefix = "i/";
1168 rev.diffopt.b_prefix = "w/";
1169 run_diff_files(&rev, 0);
1170 }
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001171 release_revisions(&rev);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001172}
1173
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001174static void wt_longstatus_print_tracking(struct wt_status *s)
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001175{
1176 struct strbuf sb = STRBUF_INIT;
René Scharfec72b49d2015-10-31 18:37:43 +01001177 const char *cp, *ep, *branch_name;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001178 struct branch *branch;
Matthieu Moy2556b992013-09-06 19:43:07 +02001179 char comment_line_string[3];
1180 int i;
Jeff Hostetler0a535612019-06-18 13:21:27 -07001181 uint64_t t_begin = 0;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001182
1183 assert(s->branch && !s->is_initial);
René Scharfec72b49d2015-10-31 18:37:43 +01001184 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001185 return;
René Scharfec72b49d2015-10-31 18:37:43 +01001186 branch = branch_get(branch_name);
Jeff Hostetler0a535612019-06-18 13:21:27 -07001187
1188 t_begin = getnanotime();
1189
Alex Henrieb6f3da52023-07-12 22:41:13 -06001190 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1191 !s->commit_template))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001192 return;
1193
Ben Boeckeled9bff02021-08-23 12:44:00 +02001194 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
Jeff Hostetler0a535612019-06-18 13:21:27 -07001195 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1196 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1197 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1198 strbuf_addf(&sb, _("\n"
1199 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1200 "You can use '--no-ahead-behind' to avoid this.\n"),
1201 t_delta_in_ms / 1000.0);
1202 }
1203 }
1204
Matthieu Moy2556b992013-09-06 19:43:07 +02001205 i = 0;
1206 if (s->display_comment_prefix) {
1207 comment_line_string[i++] = comment_line_char;
1208 comment_line_string[i++] = ' ';
1209 }
1210 comment_line_string[i] = '\0';
1211
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001212 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-09 21:59:30 -07001213 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001214 "%s%.*s", comment_line_string,
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001215 (int)(ep - cp), cp);
Matthieu Moy2556b992013-09-06 19:43:07 +02001216 if (s->display_comment_prefix)
1217 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1218 comment_line_char);
1219 else
Jeff King75177c82017-04-27 05:01:05 -04001220 fputs("\n", s->fp);
Rene Scharfeb6ec3072017-08-30 20:20:18 +02001221 strbuf_release(&sb);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001222}
1223
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001224static int uf_was_slow(struct wt_status *s)
1225{
1226 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1227 s->untracked_in_ms = 3250;
1228 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1229}
1230
Lucien Kong83c750a2012-06-05 22:21:24 +02001231static void show_merge_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001232 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001233{
1234 if (has_unmerged(s)) {
1235 status_printf_ln(s, color, _("You have unmerged paths."));
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001236 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001237 status_printf_ln(s, color,
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001238 _(" (fix conflicts and run \"git commit\")"));
1239 status_printf_ln(s, color,
1240 _(" (use \"git merge --abort\" to abort the merge)"));
1241 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001242 } else {
1243 status_printf_ln(s, color,
1244 _("All conflicts fixed but you are still merging."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001245 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001246 status_printf_ln(s, color,
1247 _(" (use \"git commit\" to conclude merge)"));
1248 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001249 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001250}
1251
1252static void show_am_in_progress(struct wt_status *s,
Lucien Kong83c750a2012-06-05 22:21:24 +02001253 const char *color)
1254{
徐沛文 (Aleen)9e7e41b2021-12-09 07:25:55 +00001255 int am_empty_patch;
1256
Lucien Kong83c750a2012-06-05 22:21:24 +02001257 status_printf_ln(s, color,
1258 _("You are in the middle of an am session."));
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001259 if (s->state.am_empty_patch)
Lucien Kong83c750a2012-06-05 22:21:24 +02001260 status_printf_ln(s, color,
1261 _("The current patch is empty."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001262 if (s->hints) {
徐沛文 (Aleen)9e7e41b2021-12-09 07:25:55 +00001263 am_empty_patch = s->state.am_empty_patch;
1264 if (!am_empty_patch)
Lucien Kong83c750a2012-06-05 22:21:24 +02001265 status_printf_ln(s, color,
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +03001266 _(" (fix conflicts and then run \"git am --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001267 status_printf_ln(s, color,
1268 _(" (use \"git am --skip\" to skip this patch)"));
徐沛文 (Aleen)9e7e41b2021-12-09 07:25:55 +00001269 if (am_empty_patch)
1270 status_printf_ln(s, color,
1271 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001272 status_printf_ln(s, color,
1273 _(" (use \"git am --abort\" to restore the original branch)"));
1274 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001275 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001276}
1277
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001278static char *read_line_from_git_path(const char *filename)
1279{
1280 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +07001281 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1282
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001283 if (!fp) {
1284 strbuf_release(&buf);
1285 return NULL;
1286 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001287 strbuf_getline_lf(&buf, fp);
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001288 if (!fclose(fp)) {
1289 return strbuf_detach(&buf, NULL);
1290 } else {
1291 strbuf_release(&buf);
1292 return NULL;
1293 }
1294}
1295
1296static int split_commit_in_progress(struct wt_status *s)
1297{
1298 int split_in_progress = 0;
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001299 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001300
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001301 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001302 !s->branch || strcmp(s->branch, "HEAD"))
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001303 return 0;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001304
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001305 head = read_line_from_git_path("HEAD");
1306 orig_head = read_line_from_git_path("ORIG_HEAD");
1307 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1308 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1309
1310 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1311 ; /* fall through, no split in progress */
1312 else if (!strcmp(rebase_amend, rebase_orig_head))
1313 split_in_progress = !!strcmp(head, rebase_amend);
1314 else if (strcmp(orig_head, rebase_orig_head))
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001315 split_in_progress = 1;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001316
1317 free(head);
1318 free(orig_head);
1319 free(rebase_amend);
1320 free(rebase_orig_head);
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001321
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001322 return split_in_progress;
1323}
1324
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001325/*
1326 * Turn
1327 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1328 * into
1329 * "pick d6a2f03 some message"
1330 *
1331 * The function assumes that the line does not contain useless spaces
1332 * before or after the command.
1333 */
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001334static void abbrev_oid_in_line(struct strbuf *line)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001335{
1336 struct strbuf **split;
1337 int i;
1338
1339 if (starts_with(line->buf, "exec ") ||
Johannes Schindelin225a7772019-05-10 13:23:14 -07001340 starts_with(line->buf, "x ") ||
1341 starts_with(line->buf, "label ") ||
1342 starts_with(line->buf, "l "))
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001343 return;
1344
1345 split = strbuf_split_max(line, ' ', 3);
1346 if (split[0] && split[1]) {
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001347 struct object_id oid;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001348
1349 /*
1350 * strbuf_split_max left a space. Trim it and re-add
1351 * it after abbreviation.
1352 */
1353 strbuf_trim(split[1]);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001354 if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001355 strbuf_reset(split[1]);
brian m. carlson30e677e2018-03-12 02:27:28 +00001356 strbuf_add_unique_abbrev(split[1], &oid,
René Scharfea94bb682016-10-08 17:38:47 +02001357 DEFAULT_ABBREV);
1358 strbuf_addch(split[1], ' ');
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001359 strbuf_reset(line);
1360 for (i = 0; split[i]; i++)
René Scharfe81099842016-07-19 20:36:29 +02001361 strbuf_addbuf(line, split[i]);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001362 }
1363 }
Stefan Beller6eb60782016-03-31 17:35:44 -07001364 strbuf_list_free(split);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001365}
1366
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001367static int read_rebase_todolist(const char *fname, struct string_list *lines)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001368{
1369 struct strbuf line = STRBUF_INIT;
1370 FILE *f = fopen(git_path("%s", fname), "r");
1371
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001372 if (!f) {
1373 if (errno == ENOENT)
1374 return -1;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001375 die_errno("Could not open file %s for reading",
1376 git_path("%s", fname));
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001377 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001378 while (!strbuf_getline_lf(&line, f)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001379 if (line.len && line.buf[0] == comment_line_char)
1380 continue;
1381 strbuf_trim(&line);
1382 if (!line.len)
1383 continue;
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001384 abbrev_oid_in_line(&line);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001385 string_list_append(lines, line.buf);
1386 }
Johannes Schindeline7b65e22017-05-04 15:55:52 +02001387 fclose(f);
Rene Scharfe6f495412017-08-30 20:20:17 +02001388 strbuf_release(&line);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001389 return 0;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001390}
1391
1392static void show_rebase_information(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001393 const char *color)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001394{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001395 if (s->state.rebase_interactive_in_progress) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001396 int i;
1397 int nr_lines_to_show = 2;
1398
1399 struct string_list have_done = STRING_LIST_INIT_DUP;
1400 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1401
1402 read_rebase_todolist("rebase-merge/done", &have_done);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001403 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1404 &yet_to_do))
1405 status_printf_ln(s, color,
1406 _("git-rebase-todo is missing."));
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001407 if (have_done.nr == 0)
1408 status_printf_ln(s, color, _("No commands done."));
1409 else {
1410 status_printf_ln(s, color,
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001411 Q_("Last command done (%"PRIuMAX" command done):",
1412 "Last commands done (%"PRIuMAX" commands done):",
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001413 have_done.nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001414 (uintmax_t)have_done.nr);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001415 for (i = (have_done.nr > nr_lines_to_show)
1416 ? have_done.nr - nr_lines_to_show : 0;
1417 i < have_done.nr;
1418 i++)
1419 status_printf_ln(s, color, " %s", have_done.items[i].string);
1420 if (have_done.nr > nr_lines_to_show && s->hints)
1421 status_printf_ln(s, color,
1422 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1423 }
1424
1425 if (yet_to_do.nr == 0)
1426 status_printf_ln(s, color,
1427 _("No commands remaining."));
1428 else {
1429 status_printf_ln(s, color,
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001430 Q_("Next command to do (%"PRIuMAX" remaining command):",
1431 "Next commands to do (%"PRIuMAX" remaining commands):",
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001432 yet_to_do.nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001433 (uintmax_t)yet_to_do.nr);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001434 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1435 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1436 if (s->hints)
1437 status_printf_ln(s, color,
1438 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1439 }
1440 string_list_clear(&yet_to_do, 0);
1441 string_list_clear(&have_done, 0);
1442 }
1443}
1444
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001445static void print_rebase_state(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001446 const char *color)
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001447{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001448 if (s->state.branch)
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001449 status_printf_ln(s, color,
1450 _("You are currently rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001451 s->state.branch,
1452 s->state.onto);
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001453 else
1454 status_printf_ln(s, color,
1455 _("You are currently rebasing."));
1456}
1457
Lucien Kong83c750a2012-06-05 22:21:24 +02001458static void show_rebase_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001459 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001460{
1461 struct stat st;
1462
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001463 show_rebase_information(s, color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001464 if (has_unmerged(s)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001465 print_rebase_state(s, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001466 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001467 status_printf_ln(s, color,
1468 _(" (fix conflicts and then run \"git rebase --continue\")"));
1469 status_printf_ln(s, color,
1470 _(" (use \"git rebase --skip\" to skip this patch)"));
1471 status_printf_ln(s, color,
1472 _(" (use \"git rebase --abort\" to check out the original branch)"));
1473 }
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001474 } else if (s->state.rebase_in_progress ||
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01001475 !stat(git_path_merge_msg(s->repo), &st)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001476 print_rebase_state(s, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001477 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001478 status_printf_ln(s, color,
1479 _(" (all conflicts fixed: run \"git rebase --continue\")"));
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001480 } else if (split_commit_in_progress(s)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001481 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001482 status_printf_ln(s, color,
1483 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001484 s->state.branch,
1485 s->state.onto);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001486 else
1487 status_printf_ln(s, color,
1488 _("You are currently splitting a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001489 if (s->hints)
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001490 status_printf_ln(s, color,
1491 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001492 } else {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001493 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001494 status_printf_ln(s, color,
1495 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001496 s->state.branch,
1497 s->state.onto);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001498 else
1499 status_printf_ln(s, color,
1500 _("You are currently editing a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001501 if (s->hints && !s->amend) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001502 status_printf_ln(s, color,
1503 _(" (use \"git commit --amend\" to amend the current commit)"));
1504 status_printf_ln(s, color,
1505 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1506 }
1507 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001508 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001509}
1510
1511static void show_cherry_pick_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001512 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001513{
Phillip Wood4a724862019-04-16 11:18:42 +01001514 if (is_null_oid(&s->state.cherry_pick_head_oid))
1515 status_printf_ln(s, color,
1516 _("Cherry-pick currently in progress."));
1517 else
1518 status_printf_ln(s, color,
1519 _("You are currently cherry-picking commit %s."),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001520 repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1521 DEFAULT_ABBREV));
Phillip Wood4a724862019-04-16 11:18:42 +01001522
Matthieu Moy6a964f52013-09-12 12:50:05 +02001523 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001524 if (has_unmerged(s))
1525 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001526 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
Phillip Wood4a724862019-04-16 11:18:42 +01001527 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1528 status_printf_ln(s, color,
1529 _(" (run \"git cherry-pick --continue\" to continue)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001530 else
1531 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001532 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1533 status_printf_ln(s, color,
Denton Liu86ae43d2019-08-27 00:45:41 -04001534 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1535 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001536 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001537 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001538 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001539}
1540
Matthieu Moydb4ef442013-04-02 16:20:21 +02001541static void show_revert_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001542 const char *color)
Matthieu Moydb4ef442013-04-02 16:20:21 +02001543{
Phillip Wood4a724862019-04-16 11:18:42 +01001544 if (is_null_oid(&s->state.revert_head_oid))
1545 status_printf_ln(s, color,
1546 _("Revert currently in progress."));
1547 else
1548 status_printf_ln(s, color,
1549 _("You are currently reverting commit %s."),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001550 repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1551 DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001552 if (s->hints) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001553 if (has_unmerged(s))
1554 status_printf_ln(s, color,
1555 _(" (fix conflicts and run \"git revert --continue\")"));
Phillip Wood4a724862019-04-16 11:18:42 +01001556 else if (is_null_oid(&s->state.revert_head_oid))
1557 status_printf_ln(s, color,
1558 _(" (run \"git revert --continue\" to continue)"));
Matthieu Moydb4ef442013-04-02 16:20:21 +02001559 else
1560 status_printf_ln(s, color,
1561 _(" (all conflicts fixed: run \"git revert --continue\")"));
1562 status_printf_ln(s, color,
Denton Liu86ae43d2019-08-27 00:45:41 -04001563 _(" (use \"git revert --skip\" to skip this patch)"));
1564 status_printf_ln(s, color,
Matthieu Moydb4ef442013-04-02 16:20:21 +02001565 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1566 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001567 wt_longstatus_print_trailer(s);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001568}
1569
Lucien Kong83c750a2012-06-05 22:21:24 +02001570static void show_bisect_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001571 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001572{
Rubén Justo990adcc2023-09-09 22:12:47 +02001573 if (s->state.bisecting_from)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001574 status_printf_ln(s, color,
Nguyễn Thái Ngọc Duy6deab242013-03-23 10:52:44 +07001575 _("You are currently bisecting, started from branch '%s'."),
Rubén Justo990adcc2023-09-09 22:12:47 +02001576 s->state.bisecting_from);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001577 else
1578 status_printf_ln(s, color,
1579 _("You are currently bisecting."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001580 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001581 status_printf_ln(s, color,
1582 _(" (use \"git bisect reset\" to get back to the original branch)"));
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001583 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001584}
1585
Elijah Newren051df3c2020-06-18 20:49:57 +00001586static void show_sparse_checkout_in_use(struct wt_status *s,
1587 const char *color)
1588{
1589 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1590 return;
1591
Derrick Stoleebf48e5a2021-07-14 13:12:36 +00001592 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1593 status_printf_ln(s, color, _("You are in a sparse checkout."));
1594 else
1595 status_printf_ln(s, color,
1596 _("You are in a sparse checkout with %d%% of tracked files present."),
1597 s->state.sparse_checkout_percentage);
Elijah Newren051df3c2020-06-18 20:49:57 +00001598 wt_longstatus_print_trailer(s);
1599}
1600
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001601/*
1602 * Extract branch information from rebase/bisect
1603 */
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001604static char *get_branch(const struct worktree *wt, const char *path)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001605{
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001606 struct strbuf sb = STRBUF_INIT;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001607 struct object_id oid;
René Scharfec72b49d2015-10-31 18:37:43 +01001608 const char *branch_name;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001609
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001610 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 +07001611 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001612
Jeff King66ec9042015-01-28 12:57:35 -05001613 while (sb.len && sb.buf[sb.len - 1] == '\n')
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001614 strbuf_setlen(&sb, sb.len - 1);
1615 if (!sb.len)
1616 goto got_nothing;
René Scharfec72b49d2015-10-31 18:37:43 +01001617 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1618 strbuf_remove(&sb, 0, branch_name - sb.buf);
Christian Couder59556542013-11-30 21:55:40 +01001619 else if (starts_with(sb.buf, "refs/"))
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001620 ;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001621 else if (!get_oid_hex(sb.buf, &oid)) {
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001622 strbuf_reset(&sb);
brian m. carlson30e677e2018-03-12 02:27:28 +00001623 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001624 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1625 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001626 else /* bisect */
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001627 ;
1628 return strbuf_detach(&sb, NULL);
1629
1630got_nothing:
1631 strbuf_release(&sb);
1632 return NULL;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001633}
1634
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001635struct grab_1st_switch_cbdata {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001636 struct strbuf buf;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001637 struct object_id noid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001638};
1639
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001640static int grab_1st_switch(struct object_id *ooid UNUSED,
Jeff Kingc006e9f2022-08-19 06:08:35 -04001641 struct object_id *noid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001642 const char *email UNUSED,
1643 timestamp_t timestamp UNUSED, int tz UNUSED,
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001644 const char *message, void *cb_data)
Lucien Kong83c750a2012-06-05 22:21:24 +02001645{
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001646 struct grab_1st_switch_cbdata *cb = cb_data;
1647 const char *target = NULL, *end;
1648
René Scharfec72b49d2015-10-31 18:37:43 +01001649 if (!skip_prefix(message, "checkout: moving from ", &message))
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001650 return 0;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001651 target = strstr(message, " to ");
1652 if (!target)
1653 return 0;
1654 target += strlen(" to ");
1655 strbuf_reset(&cb->buf);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001656 oidcpy(&cb->noid, noid);
René Scharfe904de442015-11-25 15:10:18 +01001657 end = strchrnul(target, '\n');
1658 strbuf_add(&cb->buf, target, end - target);
1659 if (!strcmp(cb->buf.buf, "HEAD")) {
Matthieu Moy0eb85482015-09-27 17:13:42 +02001660 /* HEAD is relative. Resolve it to the right reflog entry. */
René Scharfe904de442015-11-25 15:10:18 +01001661 strbuf_reset(&cb->buf);
brian m. carlson30e677e2018-03-12 02:27:28 +00001662 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
Matthieu Moy0eb85482015-09-27 17:13:42 +02001663 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001664 return 1;
1665}
1666
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001667static void wt_status_get_detached_from(struct repository *r,
1668 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001669{
1670 struct grab_1st_switch_cbdata cb;
1671 struct commit *commit;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001672 struct object_id oid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001673 char *ref = NULL;
1674
1675 strbuf_init(&cb.buf, 0);
1676 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1677 strbuf_release(&cb.buf);
1678 return;
1679 }
1680
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001681 if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
Ævar Arnfjörð Bjarmason12cb1c12023-03-28 15:58:54 +02001682 1) == 1 &&
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001683 /* oid is a commit? match without further lookup */
Jeff King4a7e27e2018-08-28 17:22:40 -04001684 (oideq(&cb.noid, &oid) ||
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001685 /* perhaps oid is a tag, try to dereference to a commit */
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001686 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001687 oideq(&cb.noid, &commit->object.oid)))) {
René Scharfec72b49d2015-10-31 18:37:43 +01001688 const char *from = ref;
1689 if (!skip_prefix(from, "refs/tags/", &from))
1690 skip_prefix(from, "refs/remotes/", &from);
1691 state->detached_from = xstrdup(from);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001692 } else
1693 state->detached_from =
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001694 xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
brian m. carlson40f55552018-03-12 02:27:29 +00001695 oidcpy(&state->detached_oid, &cb.noid);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001696 state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001697 oideq(&oid, &state->detached_oid);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001698
1699 free(ref);
1700 strbuf_release(&cb.buf);
1701}
1702
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001703int wt_status_check_rebase(const struct worktree *wt,
1704 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001705{
Lucien Kong83c750a2012-06-05 22:21:24 +02001706 struct stat st;
1707
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001708 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1709 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001710 state->am_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001711 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 +07001712 state->am_empty_patch = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001713 } else {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001714 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001715 state->branch = get_branch(wt, "rebase-apply/head-name");
1716 state->onto = get_branch(wt, "rebase-apply/onto");
Lucien Kong83c750a2012-06-05 22:21:24 +02001717 }
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001718 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1719 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001720 state->rebase_interactive_in_progress = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001721 else
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001722 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001723 state->branch = get_branch(wt, "rebase-merge/head-name");
1724 state->onto = get_branch(wt, "rebase-merge/onto");
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001725 } else
1726 return 0;
1727 return 1;
1728}
1729
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001730int wt_status_check_bisect(const struct worktree *wt,
1731 struct wt_status_state *state)
1732{
1733 struct stat st;
1734
1735 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1736 state->bisect_in_progress = 1;
Rubén Justo990adcc2023-09-09 22:12:47 +02001737 state->bisecting_from = get_branch(wt, "BISECT_START");
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001738 return 1;
1739 }
1740 return 0;
1741}
1742
Elijah Newren051df3c2020-06-18 20:49:57 +00001743static void wt_status_check_sparse_checkout(struct repository *r,
1744 struct wt_status_state *state)
1745{
1746 int skip_worktree = 0;
1747 int i;
1748
1749 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1750 /*
1751 * Don't compute percentage of checked out files if we
1752 * aren't in a sparse checkout or would get division by 0.
1753 */
1754 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1755 return;
1756 }
1757
Derrick Stoleebf48e5a2021-07-14 13:12:36 +00001758 if (r->index->sparse_index) {
1759 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1760 return;
1761 }
1762
Elijah Newren051df3c2020-06-18 20:49:57 +00001763 for (i = 0; i < r->index->cache_nr; i++) {
1764 struct cache_entry *ce = r->index->cache[i];
1765 if (ce_skip_worktree(ce))
1766 skip_worktree++;
1767 }
1768
1769 state->sparse_checkout_percentage =
1770 100 - (100 * skip_worktree)/r->index->cache_nr;
1771}
1772
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001773void wt_status_get_state(struct repository *r,
1774 struct wt_status_state *state,
Lucien Kong83c750a2012-06-05 22:21:24 +02001775 int get_detached_from)
1776{
1777 struct stat st;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001778 struct object_id oid;
Phillip Wood4a724862019-04-16 11:18:42 +01001779 enum replay_action action;
Lucien Kong83c750a2012-06-05 22:21:24 +02001780
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001781 if (!stat(git_path_merge_head(r), &st)) {
Johannes Schindelin982288e2018-11-12 15:26:02 -08001782 wt_status_check_rebase(NULL, state);
Lucien Kong83c750a2012-06-05 22:21:24 +02001783 state->merge_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001784 } else if (wt_status_check_rebase(NULL, state)) {
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001785 ; /* all set */
Han-Wen Nienhuysc8e41592020-08-21 16:59:35 +00001786 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001787 !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001788 state->cherry_pick_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001789 oidcpy(&state->cherry_pick_head_oid, &oid);
Lucien Kong83c750a2012-06-05 22:21:24 +02001790 }
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001791 wt_status_check_bisect(NULL, state);
Han-Wen Nienhuysb8825ef2020-08-21 16:59:37 +00001792 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001793 !repo_get_oid(r, "REVERT_HEAD", &oid)) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001794 state->revert_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001795 oidcpy(&state->revert_head_oid, &oid);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001796 }
Phillip Wood4a724862019-04-16 11:18:42 +01001797 if (!sequencer_get_last_command(r, &action)) {
Jacob Kellera096a882023-06-27 15:41:50 -07001798 if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
Phillip Wood4a724862019-04-16 11:18:42 +01001799 state->cherry_pick_in_progress = 1;
brian m. carlson14228442021-04-26 01:02:56 +00001800 oidcpy(&state->cherry_pick_head_oid, null_oid());
Jacob Kellera096a882023-06-27 15:41:50 -07001801 } else if (action == REPLAY_REVERT && !state->revert_in_progress) {
Phillip Wood4a724862019-04-16 11:18:42 +01001802 state->revert_in_progress = 1;
brian m. carlson14228442021-04-26 01:02:56 +00001803 oidcpy(&state->revert_head_oid, null_oid());
Phillip Wood4a724862019-04-16 11:18:42 +01001804 }
1805 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001806 if (get_detached_from)
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001807 wt_status_get_detached_from(r, state);
Elijah Newren051df3c2020-06-18 20:49:57 +00001808 wt_status_check_sparse_checkout(r, state);
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001809}
1810
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001811static void wt_longstatus_print_state(struct wt_status *s)
Lucien Kong83c750a2012-06-05 22:21:24 +02001812{
1813 const char *state_color = color(WT_STATUS_HEADER, s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001814 struct wt_status_state *state = &s->state;
1815
Johannes Schindelin982288e2018-11-12 15:26:02 -08001816 if (state->merge_in_progress) {
1817 if (state->rebase_interactive_in_progress) {
1818 show_rebase_information(s, state_color);
1819 fputs("\n", s->fp);
1820 }
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001821 show_merge_in_progress(s, state_color);
Johannes Schindelin982288e2018-11-12 15:26:02 -08001822 } else if (state->am_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001823 show_am_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001824 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001825 show_rebase_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001826 else if (state->cherry_pick_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001827 show_cherry_pick_in_progress(s, state_color);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001828 else if (state->revert_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001829 show_revert_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001830 if (state->bisect_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001831 show_bisect_in_progress(s, state_color);
Elijah Newren051df3c2020-06-18 20:49:57 +00001832
1833 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1834 show_sparse_checkout_in_use(s, state_color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001835}
1836
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001837static void wt_longstatus_print(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001838{
Aleksi Aalto1d282322010-11-18 01:40:05 +02001839 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1840 const char *branch_status_color = color(WT_STATUS_HEADER, s);
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001841 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
Jürgen Rühle98bf8a42007-01-02 20:26:23 +01001842
Junio C Hamanobda324c2007-01-03 01:09:34 -08001843 if (s->branch) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001844 const char *on_what = _("On branch ");
Junio C Hamanobda324c2007-01-03 01:09:34 -08001845 const char *branch_name = s->branch;
René Scharfec72b49d2015-10-31 18:37:43 +01001846 if (!strcmp(branch_name, "HEAD")) {
Aleksi Aalto1d282322010-11-18 01:40:05 +02001847 branch_status_color = color(WT_STATUS_NOBRANCH, s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001848 if (s->state.rebase_in_progress ||
1849 s->state.rebase_interactive_in_progress) {
1850 if (s->state.rebase_interactive_in_progress)
Guillaume Pagèsdf25e942015-06-30 15:01:13 +02001851 on_what = _("interactive rebase in progress; onto ");
1852 else
1853 on_what = _("rebase in progress; onto ");
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001854 branch_name = s->state.onto;
1855 } else if (s->state.detached_from) {
1856 branch_name = s->state.detached_from;
1857 if (s->state.detached_at)
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01001858 on_what = _("HEAD detached at ");
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001859 else
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01001860 on_what = _("HEAD detached from ");
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001861 } else {
1862 branch_name = "";
1863 on_what = _("Not currently on any branch.");
1864 }
René Scharfec72b49d2015-10-31 18:37:43 +01001865 } else
1866 skip_prefix(branch_name, "refs/heads/", &branch_name);
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001867 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001868 status_printf_more(s, branch_status_color, "%s", on_what);
1869 status_printf_more(s, branch_color, "%s\n", branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001870 if (!s->is_initial)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001871 wt_longstatus_print_tracking(s);
Junio C Hamanobda324c2007-01-03 01:09:34 -08001872 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001873
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001874 wt_longstatus_print_state(s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001875
Jeff Kingc91f0d92006-09-08 04:05:34 -04001876 if (s->is_initial) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001877 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301878 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1879 s->commit_template
1880 ? _("Initial commit")
1881 : _("No commits yet"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001882 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001883 }
1884
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001885 wt_longstatus_print_updated(s);
1886 wt_longstatus_print_unmerged(s);
1887 wt_longstatus_print_changed(s);
Jens Lehmann46a958b2010-06-25 16:56:47 +02001888 if (s->submodule_summary &&
1889 (!s->ignore_submodule_arg ||
1890 strcmp(s->ignore_submodule_arg, "all"))) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001891 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1892 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
Jens Lehmannf17a5d32010-01-17 20:42:31 +01001893 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001894 if (s->show_untracked_files) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001895 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
Jameson Millereec0f7f2017-10-30 13:21:37 -04001896 if (s->show_ignored_mode)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001897 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001898 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001899 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001900 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1901 status_printf_ln(s, GIT_COLOR_NORMAL,
1902 _("It took %.2f seconds to enumerate untracked files,\n"
1903 "but the results were cached, and subsequent runs may be faster."),
1904 s->untracked_in_ms / 1000.0);
1905 } else {
1906 status_printf_ln(s, GIT_COLOR_NORMAL,
1907 _("It took %.2f seconds to enumerate untracked files."),
1908 s->untracked_in_ms / 1000.0);
1909 }
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001910 status_printf_ln(s, GIT_COLOR_NORMAL,
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001911 _("See 'git help status' for information on how to improve this."));
1912 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001913 }
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001914 } else if (s->committable)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001915 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
Matthieu Moy6a964f52013-09-12 12:50:05 +02001916 s->hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001917 ? _(" (use -u option to show untracked files)") : "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001918
Jeff King1324fb62008-11-12 03:23:37 -05001919 if (s->verbose)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001920 wt_longstatus_print_verbose(s);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001921 if (!s->committable) {
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001922 if (s->amend)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001923 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
Junio C Hamano37d07f82007-12-12 19:09:16 -08001924 else if (s->nowarn)
1925 ; /* nothing */
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001926 else if (s->workdir_dirty) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001927 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001928 fprintf(s->fp, _("no changes added to commit "
1929 "(use \"git add\" and/or "
1930 "\"git commit -a\")\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001931 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001932 fprintf(s->fp, _("no changes added to "
1933 "commit\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001934 } else if (s->untracked.nr) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001935 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001936 fprintf(s->fp, _("nothing added to commit but "
1937 "untracked files present (use "
1938 "\"git add\" to track)\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001939 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001940 fprintf(s->fp, _("nothing added to commit but "
1941 "untracked files present\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001942 } else if (s->is_initial) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001943 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001944 fprintf(s->fp, _("nothing to commit (create/"
1945 "copy files and use \"git "
1946 "add\" to track)\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001947 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001948 fprintf(s->fp, _("nothing to commit\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001949 } else if (!s->show_untracked_files) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001950 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001951 fprintf(s->fp, _("nothing to commit (use -u to "
1952 "show untracked files)\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001953 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001954 fprintf(s->fp, _("nothing to commit\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001955 } else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001956 fprintf(s->fp, _("nothing to commit, working tree "
1957 "clean\n"));
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001958 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001959 if(s->show_stash)
1960 wt_longstatus_print_stash_summary(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001961}
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001962
Jeff King3207a3a2012-05-07 15:44:44 -04001963static void wt_shortstatus_unmerged(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001964 struct wt_status *s)
1965{
1966 struct wt_status_change_data *d = it->util;
1967 const char *how = "??";
1968
1969 switch (d->stagemask) {
1970 case 1: how = "DD"; break; /* both deleted */
1971 case 2: how = "AU"; break; /* added by us */
1972 case 3: how = "UD"; break; /* deleted by them */
1973 case 4: how = "UA"; break; /* added by them */
1974 case 5: how = "DU"; break; /* deleted by us */
1975 case 6: how = "AA"; break; /* both added */
1976 case 7: how = "UU"; break; /* both modified */
1977 }
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001978 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
Jeff King3207a3a2012-05-07 15:44:44 -04001979 if (s->null_termination) {
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001980 fprintf(s->fp, " %s%c", it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001981 } else {
1982 struct strbuf onebuf = STRBUF_INIT;
1983 const char *one;
Junio C Hamanoa361dd32020-09-10 10:01:57 -07001984 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001985 fprintf(s->fp, " %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001986 strbuf_release(&onebuf);
1987 }
1988}
1989
Jeff King3207a3a2012-05-07 15:44:44 -04001990static void wt_shortstatus_status(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001991 struct wt_status *s)
1992{
1993 struct wt_status_change_data *d = it->util;
1994
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001995 if (d->index_status)
1996 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1997 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001998 fputc(' ', s->fp);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001999 if (d->worktree_status)
2000 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2001 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002002 fputc(' ', s->fp);
2003 fputc(' ', s->fp);
Jeff King3207a3a2012-05-07 15:44:44 -04002004 if (s->null_termination) {
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002005 fprintf(s->fp, "%s%c", it->string, 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002006 if (d->rename_source)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002007 fprintf(s->fp, "%s%c", d->rename_source, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002008 } else {
2009 struct strbuf onebuf = STRBUF_INIT;
2010 const char *one;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002011
2012 if (d->rename_source) {
Junio C Hamanof3fc4a12020-09-10 10:01:55 -07002013 one = quote_path(d->rename_source, s->prefix, &onebuf,
2014 QUOTE_PATH_QUOTE_SP);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002015 fprintf(s->fp, "%s -> ", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002016 strbuf_release(&onebuf);
2017 }
Junio C Hamanof3fc4a12020-09-10 10:01:55 -07002018 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002019 fprintf(s->fp, "%s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002020 strbuf_release(&onebuf);
2021 }
2022}
2023
Jeff King3207a3a2012-05-07 15:44:44 -04002024static void wt_shortstatus_other(struct string_list_item *it,
Junio C Hamano2381e392010-04-10 00:33:17 -07002025 struct wt_status *s, const char *sign)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002026{
Jeff King3207a3a2012-05-07 15:44:44 -04002027 if (s->null_termination) {
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002028 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002029 } else {
2030 struct strbuf onebuf = STRBUF_INIT;
2031 const char *one;
Junio C Hamanoa361dd32020-09-10 10:01:57 -07002032 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
Junio C Hamanoc1909e72010-05-01 22:05:14 -07002033 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002034 fprintf(s->fp, " %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002035 strbuf_release(&onebuf);
2036 }
2037}
2038
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002039static void wt_shortstatus_print_tracking(struct wt_status *s)
2040{
2041 struct branch *branch;
2042 const char *header_color = color(WT_STATUS_HEADER, s);
2043 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2044 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2045
2046 const char *base;
René Scharfe5e8d2722017-07-08 12:51:01 +02002047 char *short_base;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002048 const char *branch_name;
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002049 int num_ours, num_theirs, sti;
Jiang Xinf2e08732013-08-26 15:02:48 +08002050 int upstream_is_gone = 0;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002051
2052 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2053
2054 if (!s->branch)
2055 return;
2056 branch_name = s->branch;
2057
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01002058#define LABEL(string) (s->no_gettext ? (string) : _(string))
2059
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002060 if (s->is_initial)
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05302061 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
Jiang Xinf2e08732013-08-26 15:02:48 +08002062
René Scharfebaf0a3e2015-10-31 18:36:35 +01002063 if (!strcmp(s->branch, "HEAD")) {
2064 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01002065 LABEL(N_("HEAD (no branch)")));
René Scharfebaf0a3e2015-10-31 18:36:35 +01002066 goto conclude;
2067 }
2068
René Scharfec72b49d2015-10-31 18:37:43 +01002069 skip_prefix(branch_name, "refs/heads/", &branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01002070
René Scharfe8d8325f2015-10-31 18:37:12 +01002071 branch = branch_get(branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01002072
Jiang Xinf2e08732013-08-26 15:02:48 +08002073 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2074
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002075 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
Damien Robertc646d092019-04-16 14:16:46 +02002076 0, s->ahead_behind_flags);
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002077 if (sti < 0) {
René Scharfebcf8cc22015-10-31 18:36:01 +01002078 if (!base)
2079 goto conclude;
Jeff King979cb242015-05-21 20:49:11 -04002080
Jiang Xinf2e08732013-08-26 15:02:48 +08002081 upstream_is_gone = 1;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002082 }
2083
René Scharfe5e8d2722017-07-08 12:51:01 +02002084 short_base = shorten_unambiguous_ref(base, 0);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002085 color_fprintf(s->fp, header_color, "...");
René Scharfe5e8d2722017-07-08 12:51:01 +02002086 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2087 free(short_base);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002088
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002089 if (!upstream_is_gone && !sti)
René Scharfebcf8cc22015-10-31 18:36:01 +01002090 goto conclude;
Jiang Xinf2234592013-08-26 15:02:49 +08002091
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002092 color_fprintf(s->fp, header_color, " [");
Jiang Xinf2e08732013-08-26 15:02:48 +08002093 if (upstream_is_gone) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01002094 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002095 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2096 color_fprintf(s->fp, header_color, LABEL(N_("different")));
Jiang Xinf2e08732013-08-26 15:02:48 +08002097 } else if (!num_ours) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01002098 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002099 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2100 } else if (!num_theirs) {
Michael J Gruberdf227242016-03-14 16:30:33 +01002101 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002102 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2103 } else {
Michael J Gruberdf227242016-03-14 16:30:33 +01002104 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002105 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
Matthieu Moy7a76c282014-03-20 13:12:41 +01002106 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002107 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2108 }
2109
Jeff Kinga5985232012-05-07 17:02:18 -04002110 color_fprintf(s->fp, header_color, "]");
René Scharfebcf8cc22015-10-31 18:36:01 +01002111 conclude:
Jeff Kinga5985232012-05-07 17:02:18 -04002112 fputc(s->null_termination ? '\0' : '\n', s->fp);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002113}
2114
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002115static void wt_shortstatus_print(struct wt_status *s)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002116{
Stefan Bellerd4aae452017-03-16 14:36:19 -07002117 struct string_list_item *it;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002118
Jeff Kingd4a6bf12012-05-07 17:09:04 -04002119 if (s->show_branch)
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002120 wt_shortstatus_print_tracking(s);
2121
Stefan Bellerd4aae452017-03-16 14:36:19 -07002122 for_each_string_list_item(it, &s->change) {
2123 struct wt_status_change_data *d = it->util;
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002124
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002125 if (d->stagemask)
Jeff King3207a3a2012-05-07 15:44:44 -04002126 wt_shortstatus_unmerged(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002127 else
Jeff King3207a3a2012-05-07 15:44:44 -04002128 wt_shortstatus_status(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002129 }
Stefan Bellerd4aae452017-03-16 14:36:19 -07002130 for_each_string_list_item(it, &s->untracked)
Jeff King3207a3a2012-05-07 15:44:44 -04002131 wt_shortstatus_other(it, s, "??");
Junio C Hamano2381e392010-04-10 00:33:17 -07002132
Stefan Bellerd4aae452017-03-16 14:36:19 -07002133 for_each_string_list_item(it, &s->ignored)
Jeff King3207a3a2012-05-07 15:44:44 -04002134 wt_shortstatus_other(it, s, "!!");
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002135}
Jeff King4a7cc2f2009-12-07 00:17:15 -05002136
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002137static void wt_porcelain_print(struct wt_status *s)
Jeff King4a7cc2f2009-12-07 00:17:15 -05002138{
2139 s->use_color = 0;
Jeff King86617682009-12-07 00:26:25 -05002140 s->relative_paths = 0;
2141 s->prefix = NULL;
Matthieu Moy7a76c282014-03-20 13:12:41 +01002142 s->no_gettext = 1;
Jeff Kingd4a6bf12012-05-07 17:09:04 -04002143 wt_shortstatus_print(s);
Jeff King4a7cc2f2009-12-07 00:17:15 -05002144}
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002145
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002146/*
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002147 * Print branch information for porcelain v2 output. These lines
2148 * are printed when the '--branch' parameter is given.
2149 *
2150 * # branch.oid <commit><eol>
2151 * # branch.head <head><eol>
2152 * [# branch.upstream <upstream><eol>
2153 * [# branch.ab +<ahead> -<behind><eol>]]
2154 *
Elijah Newren6d12b532020-07-28 20:45:38 +00002155 * <commit> ::= the current commit hash or the literal
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002156 * "(initial)" to indicate an initialized repo
2157 * with no commits.
2158 *
2159 * <head> ::= <branch_name> the current branch name or
2160 * "(detached)" literal when detached head or
2161 * "(unknown)" when something is wrong.
2162 *
2163 * <upstream> ::= the upstream branch name, when set.
2164 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002165 * <ahead> ::= integer ahead value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002166 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002167 * <behind> ::= integer behind value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002168 *
2169 * The end-of-line is defined by the -z flag.
2170 *
2171 * <eol> ::= NUL when -z,
2172 * LF when NOT -z.
2173 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002174 * When an upstream is set and present, the 'branch.ab' line will
2175 * be printed with the ahead/behind counts for the branch and the
2176 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2177 * are different, '?' will be substituted for the actual count.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002178 */
2179static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2180{
2181 struct branch *branch;
2182 const char *base;
2183 const char *branch_name;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002184 int ab_info, nr_ahead, nr_behind;
2185 char eol = s->null_termination ? '\0' : '\n';
2186
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002187 fprintf(s->fp, "# branch.oid %s%c",
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00002188 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002189 eol);
2190
2191 if (!s->branch)
2192 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2193 else {
2194 if (!strcmp(s->branch, "HEAD")) {
2195 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2196
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07002197 if (s->state.rebase_in_progress ||
2198 s->state.rebase_interactive_in_progress)
2199 branch_name = s->state.onto;
2200 else if (s->state.detached_from)
2201 branch_name = s->state.detached_from;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002202 else
2203 branch_name = "";
2204 } else {
2205 branch_name = NULL;
2206 skip_prefix(s->branch, "refs/heads/", &branch_name);
2207
2208 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2209 }
2210
2211 /* Lookup stats on the upstream tracking branch, if set. */
2212 branch = branch_get(branch_name);
2213 base = NULL;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002214 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
Damien Robertc646d092019-04-16 14:16:46 +02002215 &base, 0, s->ahead_behind_flags);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002216 if (base) {
2217 base = shorten_unambiguous_ref(base, 0);
2218 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2219 free((char *)base);
2220
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002221 if (ab_info > 0) {
2222 /* different */
2223 if (nr_ahead || nr_behind)
2224 fprintf(s->fp, "# branch.ab +%d -%d%c",
2225 nr_ahead, nr_behind, eol);
2226 else
2227 fprintf(s->fp, "# branch.ab +? -?%c",
2228 eol);
2229 } else if (!ab_info) {
2230 /* same */
2231 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2232 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002233 }
2234 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002235}
2236
2237/*
Øystein Walle2e59e782021-10-22 00:25:32 +02002238 * Print the stash count in a porcelain-friendly format
2239 */
2240static void wt_porcelain_v2_print_stash(struct wt_status *s)
2241{
2242 int stash_count = count_stash_entries();
2243 char eol = s->null_termination ? '\0' : '\n';
2244
2245 if (stash_count > 0)
2246 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2247}
2248
2249/*
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002250 * Convert various submodule status values into a
2251 * fixed-length string of characters in the buffer provided.
2252 */
2253static void wt_porcelain_v2_submodule_state(
2254 struct wt_status_change_data *d,
2255 char sub[5])
2256{
2257 if (S_ISGITLINK(d->mode_head) ||
2258 S_ISGITLINK(d->mode_index) ||
2259 S_ISGITLINK(d->mode_worktree)) {
2260 sub[0] = 'S';
2261 sub[1] = d->new_submodule_commits ? 'C' : '.';
2262 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2263 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2264 } else {
2265 sub[0] = 'N';
2266 sub[1] = '.';
2267 sub[2] = '.';
2268 sub[3] = '.';
2269 }
2270 sub[4] = 0;
2271}
2272
2273/*
2274 * Fix-up changed entries before we print them.
2275 */
Jeff King13a17812019-05-09 17:30:19 -04002276static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002277{
2278 struct wt_status_change_data *d = it->util;
2279
2280 if (!d->index_status) {
2281 /*
2282 * This entry is unchanged in the index (relative to the head).
2283 * Therefore, the collect_updated_cb was never called for this
2284 * entry (during the head-vs-index scan) and so the head column
2285 * fields were never set.
2286 *
2287 * We must have data for the index column (from the
2288 * index-vs-worktree scan (otherwise, this entry should not be
2289 * in the list of changes)).
2290 *
2291 * Copy index column fields to the head column, so that our
2292 * output looks complete.
2293 */
2294 assert(d->mode_head == 0);
2295 d->mode_head = d->mode_index;
2296 oidcpy(&d->oid_head, &d->oid_index);
2297 }
2298
2299 if (!d->worktree_status) {
2300 /*
2301 * This entry is unchanged in the worktree (relative to the index).
2302 * Therefore, the collect_changed_cb was never called for this entry
2303 * (during the index-vs-worktree scan) and so the worktree column
2304 * fields were never set.
2305 *
2306 * We must have data for the index column (from the head-vs-index
2307 * scan).
2308 *
2309 * Copy the index column fields to the worktree column so that
2310 * our output looks complete.
2311 *
2312 * Note that we only have a mode field in the worktree column
2313 * because the scan code tries really hard to not have to compute it.
2314 */
2315 assert(d->mode_worktree == 0);
2316 d->mode_worktree = d->mode_index;
2317 }
2318}
2319
2320/*
2321 * Print porcelain v2 info for tracked entries with changes.
2322 */
2323static void wt_porcelain_v2_print_changed_entry(
2324 struct string_list_item *it,
2325 struct wt_status *s)
2326{
2327 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002328 struct strbuf buf = STRBUF_INIT;
2329 struct strbuf buf_from = STRBUF_INIT;
2330 const char *path = NULL;
2331 const char *path_from = NULL;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002332 char key[3];
2333 char submodule_token[5];
2334 char sep_char, eol_char;
2335
Jeff King13a17812019-05-09 17:30:19 -04002336 wt_porcelain_v2_fix_up_changed(it);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002337 wt_porcelain_v2_submodule_state(d, submodule_token);
2338
2339 key[0] = d->index_status ? d->index_status : '.';
2340 key[1] = d->worktree_status ? d->worktree_status : '.';
2341 key[2] = 0;
2342
2343 if (s->null_termination) {
2344 /*
2345 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2346 * A single NUL character separates them.
2347 */
2348 sep_char = '\0';
2349 eol_char = '\0';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002350 path = it->string;
2351 path_from = d->rename_source;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002352 } else {
2353 /*
2354 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2355 * The source path is only present when necessary.
2356 * A single TAB separates them (because paths can contain spaces
2357 * which are not escaped and C-quoting does escape TAB characters).
2358 */
2359 sep_char = '\t';
2360 eol_char = '\n';
Junio C Hamano88910c92020-09-10 10:01:54 -07002361 path = quote_path(it->string, s->prefix, &buf, 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002362 if (d->rename_source)
Junio C Hamano88910c92020-09-10 10:01:54 -07002363 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002364 }
2365
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002366 if (path_from)
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002367 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2368 key, submodule_token,
2369 d->mode_head, d->mode_index, d->mode_worktree,
2370 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002371 d->rename_status, d->rename_score,
2372 path, sep_char, path_from, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002373 else
2374 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2375 key, submodule_token,
2376 d->mode_head, d->mode_index, d->mode_worktree,
2377 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002378 path, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002379
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002380 strbuf_release(&buf);
2381 strbuf_release(&buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002382}
2383
2384/*
2385 * Print porcelain v2 status info for unmerged entries.
2386 */
2387static void wt_porcelain_v2_print_unmerged_entry(
2388 struct string_list_item *it,
2389 struct wt_status *s)
2390{
2391 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002392 struct index_state *istate = s->repo->index;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002393 const struct cache_entry *ce;
2394 struct strbuf buf_index = STRBUF_INIT;
2395 const char *path_index = NULL;
2396 int pos, stage, sum;
2397 struct {
2398 int mode;
2399 struct object_id oid;
2400 } stages[3];
2401 char *key;
2402 char submodule_token[5];
2403 char unmerged_prefix = 'u';
2404 char eol_char = s->null_termination ? '\0' : '\n';
2405
2406 wt_porcelain_v2_submodule_state(d, submodule_token);
2407
2408 switch (d->stagemask) {
2409 case 1: key = "DD"; break; /* both deleted */
2410 case 2: key = "AU"; break; /* added by us */
2411 case 3: key = "UD"; break; /* deleted by them */
2412 case 4: key = "UA"; break; /* added by them */
2413 case 5: key = "DU"; break; /* deleted by us */
2414 case 6: key = "AA"; break; /* both added */
2415 case 7: key = "UU"; break; /* both modified */
2416 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002417 BUG("unhandled unmerged status %x", d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002418 }
2419
2420 /*
2421 * Disregard d.aux.porcelain_v2 data that we accumulated
2422 * for the head and index columns during the scans and
2423 * replace with the actual stage data.
2424 *
2425 * Note that this is a last-one-wins for each the individual
2426 * stage [123] columns in the event of multiple cache entries
2427 * for same stage.
2428 */
2429 memset(stages, 0, sizeof(stages));
2430 sum = 0;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002431 pos = index_name_pos(istate, it->string, strlen(it->string));
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002432 assert(pos < 0);
2433 pos = -pos-1;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002434 while (pos < istate->cache_nr) {
2435 ce = istate->cache[pos++];
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002436 stage = ce_stage(ce);
2437 if (strcmp(ce->name, it->string) || !stage)
2438 break;
2439 stages[stage - 1].mode = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +01002440 oidcpy(&stages[stage - 1].oid, &ce->oid);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002441 sum |= (1 << (stage - 1));
2442 }
2443 if (sum != d->stagemask)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002444 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002445
2446 if (s->null_termination)
2447 path_index = it->string;
2448 else
Junio C Hamano88910c92020-09-10 10:01:54 -07002449 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002450
2451 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2452 unmerged_prefix, key, submodule_token,
2453 stages[0].mode, /* stage 1 */
2454 stages[1].mode, /* stage 2 */
2455 stages[2].mode, /* stage 3 */
2456 d->mode_worktree,
2457 oid_to_hex(&stages[0].oid), /* stage 1 */
2458 oid_to_hex(&stages[1].oid), /* stage 2 */
2459 oid_to_hex(&stages[2].oid), /* stage 3 */
2460 path_index,
2461 eol_char);
2462
2463 strbuf_release(&buf_index);
2464}
2465
2466/*
2467 * Print porcelain V2 status info for untracked and ignored entries.
2468 */
2469static void wt_porcelain_v2_print_other(
2470 struct string_list_item *it,
2471 struct wt_status *s,
2472 char prefix)
2473{
2474 struct strbuf buf = STRBUF_INIT;
2475 const char *path;
2476 char eol_char;
2477
2478 if (s->null_termination) {
2479 path = it->string;
2480 eol_char = '\0';
2481 } else {
Junio C Hamano88910c92020-09-10 10:01:54 -07002482 path = quote_path(it->string, s->prefix, &buf, 0);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002483 eol_char = '\n';
2484 }
2485
2486 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2487
2488 strbuf_release(&buf);
2489}
2490
2491/*
2492 * Print porcelain V2 status.
2493 *
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002494 * [<v2_branch>]
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002495 * [<v2_changed_items>]*
2496 * [<v2_unmerged_items>]*
2497 * [<v2_untracked_items>]*
2498 * [<v2_ignored_items>]*
2499 *
2500 */
2501static void wt_porcelain_v2_print(struct wt_status *s)
2502{
2503 struct wt_status_change_data *d;
2504 struct string_list_item *it;
2505 int i;
2506
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002507 if (s->show_branch)
2508 wt_porcelain_v2_print_tracking(s);
2509
Øystein Walle2e59e782021-10-22 00:25:32 +02002510 if (s->show_stash)
2511 wt_porcelain_v2_print_stash(s);
2512
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002513 for (i = 0; i < s->change.nr; i++) {
2514 it = &(s->change.items[i]);
2515 d = it->util;
2516 if (!d->stagemask)
2517 wt_porcelain_v2_print_changed_entry(it, s);
2518 }
2519
2520 for (i = 0; i < s->change.nr; i++) {
2521 it = &(s->change.items[i]);
2522 d = it->util;
2523 if (d->stagemask)
2524 wt_porcelain_v2_print_unmerged_entry(it, s);
2525 }
2526
2527 for (i = 0; i < s->untracked.nr; i++) {
2528 it = &(s->untracked.items[i]);
2529 wt_porcelain_v2_print_other(it, s, '?');
2530 }
2531
2532 for (i = 0; i < s->ignored.nr; i++) {
2533 it = &(s->ignored.items[i]);
2534 wt_porcelain_v2_print_other(it, s, '!');
2535 }
2536}
2537
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002538void wt_status_print(struct wt_status *s)
2539{
Jeff Hostetler942b2742019-02-22 14:25:03 -08002540 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2541 trace2_data_intmax("status", s->repo, "count/untracked",
2542 s->untracked.nr);
2543 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2544
2545 trace2_region_enter("status", "print", s->repo);
2546
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002547 switch (s->status_format) {
2548 case STATUS_FORMAT_SHORT:
2549 wt_shortstatus_print(s);
2550 break;
2551 case STATUS_FORMAT_PORCELAIN:
2552 wt_porcelain_print(s);
2553 break;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002554 case STATUS_FORMAT_PORCELAIN_V2:
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002555 wt_porcelain_v2_print(s);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002556 break;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002557 case STATUS_FORMAT_UNSPECIFIED:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002558 BUG("finalize_deferred_config() should have been called");
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002559 break;
2560 case STATUS_FORMAT_NONE:
2561 case STATUS_FORMAT_LONG:
2562 wt_longstatus_print(s);
2563 break;
2564 }
Jeff Hostetler942b2742019-02-22 14:25:03 -08002565
2566 trace2_region_leave("status", "print", s->repo);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002567}
Johannes Schindelinfd849862016-10-07 18:08:38 +02002568
2569/**
2570 * Returns 1 if there are unstaged changes, 0 otherwise.
2571 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002572int has_unstaged_changes(struct repository *r, int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002573{
2574 struct rev_info rev_info;
2575 int result;
2576
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002577 repo_init_revisions(r, &rev_info, NULL);
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002578 if (ignore_submodules) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002579 rev_info.diffopt.flags.ignore_submodules = 1;
Junio C Hamanob50d82b2017-11-15 12:14:30 +09002580 rev_info.diffopt.flags.override_submodule_config = 1;
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002581 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002582 rev_info.diffopt.flags.quick = 1;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002583 diff_setup_done(&rev_info.diffopt);
Jeff King25bd3ac2023-08-21 16:18:55 -04002584 run_diff_files(&rev_info, 0);
Jeff King5cc6b2d2023-08-21 16:20:46 -04002585 result = diff_result_code(&rev_info.diffopt);
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +02002586 release_revisions(&rev_info);
Ævar Arnfjörð Bjarmason54c8a7c2022-04-14 07:56:40 +02002587 return result;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002588}
2589
2590/**
2591 * Returns 1 if there are uncommitted changes, 0 otherwise.
2592 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002593int has_uncommitted_changes(struct repository *r,
2594 int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002595{
2596 struct rev_info rev_info;
2597 int result;
2598
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002599 if (is_index_unborn(r->index))
Johannes Schindelinfd849862016-10-07 18:08:38 +02002600 return 0;
2601
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002602 repo_init_revisions(r, &rev_info, NULL);
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002603 if (ignore_submodules)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002604 rev_info.diffopt.flags.ignore_submodules = 1;
2605 rev_info.diffopt.flags.quick = 1;
Jeff King3506dc92018-07-11 10:14:06 -04002606
Johannes Schindelinfd849862016-10-07 18:08:38 +02002607 add_head_to_pending(&rev_info);
Jeff King3506dc92018-07-11 10:14:06 -04002608 if (!rev_info.pending.nr) {
2609 /*
2610 * We have no head (or it's corrupt); use the empty tree,
2611 * which will complain if the index is non-empty.
2612 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002613 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
Jeff King3506dc92018-07-11 10:14:06 -04002614 add_pending_object(&rev_info, &tree->object, "");
2615 }
2616
Johannes Schindelinfd849862016-10-07 18:08:38 +02002617 diff_setup_done(&rev_info.diffopt);
Jeff King25bd3ac2023-08-21 16:18:55 -04002618 run_diff_index(&rev_info, DIFF_INDEX_CACHED);
Jeff King5cc6b2d2023-08-21 16:20:46 -04002619 result = diff_result_code(&rev_info.diffopt);
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +02002620 release_revisions(&rev_info);
Ævar Arnfjörð Bjarmason54c8a7c2022-04-14 07:56:40 +02002621 return result;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002622}
2623
2624/**
2625 * If the work tree has unstaged or uncommitted changes, dies with the
2626 * appropriate message.
2627 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002628int require_clean_work_tree(struct repository *r,
2629 const char *action,
2630 const char *hint,
2631 int ignore_submodules,
2632 int gently)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002633{
Martin Ågren837e34e2017-10-05 22:32:04 +02002634 struct lock_file lock_file = LOCK_INIT;
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002635 int err = 0, fd;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002636
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +07002637 fd = repo_hold_locked_index(r, &lock_file, 0);
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002638 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002639 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002640 repo_update_index_if_able(r, &lock_file);
Martin Ågren837e34e2017-10-05 22:32:04 +02002641 rollback_lock_file(&lock_file);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002642
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002643 if (has_unstaged_changes(r, ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002644 /* TRANSLATORS: the action is e.g. "pull with rebase" */
Johannes Schindelin4777e172016-10-07 18:09:04 +02002645 error(_("cannot %s: You have unstaged changes."), _(action));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002646 err = 1;
2647 }
2648
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002649 if (has_uncommitted_changes(r, ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002650 if (err)
Johannes Schindelin4777e172016-10-07 18:09:04 +02002651 error(_("additionally, your index contains uncommitted changes."));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002652 else
Johannes Schindelin4777e172016-10-07 18:09:04 +02002653 error(_("cannot %s: Your index contains uncommitted changes."),
Johannes Schindelinfd849862016-10-07 18:08:38 +02002654 _(action));
2655 err = 1;
2656 }
2657
2658 if (err) {
Oswald Buddenhagena9b59552023-08-24 17:00:46 +02002659 if (hint) {
2660 if (!*hint)
2661 BUG("empty hint passed to require_clean_work_tree();"
2662 " use NULL instead");
Johannes Schindelinfd849862016-10-07 18:08:38 +02002663 error("%s", hint);
Oswald Buddenhagena9b59552023-08-24 17:00:46 +02002664 }
Johannes Schindelinfd849862016-10-07 18:08:38 +02002665 if (!gently)
2666 exit(128);
2667 }
2668
2669 return err;
2670}