blob: d03dfab9e47d128b15295e499c8113346a593cf7 [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);
742 read_tree_at(istate->repo, tree, &base, &ps,
743 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);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700864}
865
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400866static void wt_longstatus_print_unmerged(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700867{
868 int shown_header = 0;
869 int i;
870
871 for (i = 0; i < s->change.nr; i++) {
872 struct wt_status_change_data *d;
873 struct string_list_item *it;
874 it = &(s->change.items[i]);
875 d = it->util;
876 if (!d->stagemask)
877 continue;
878 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400879 wt_longstatus_print_unmerged_header(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700880 shown_header = 1;
881 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400882 wt_longstatus_print_unmerged_data(s, it);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700883 }
884 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400885 wt_longstatus_print_trailer(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700886
887}
888
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400889static void wt_longstatus_print_updated(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700890{
891 int shown_header = 0;
892 int i;
893
894 for (i = 0; i < s->change.nr; i++) {
895 struct wt_status_change_data *d;
896 struct string_list_item *it;
897 it = &(s->change.items[i]);
898 d = it->util;
899 if (!d->index_status ||
900 d->index_status == DIFF_STATUS_UNMERGED)
901 continue;
902 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400903 wt_longstatus_print_cached_header(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700904 shown_header = 1;
905 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400906 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700907 }
908 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400909 wt_longstatus_print_trailer(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700910}
911
912/*
913 * -1 : has delete
914 * 0 : no change
915 * 1 : some change but no delete
916 */
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100917static int wt_status_check_worktree_changes(struct wt_status *s,
918 int *dirty_submodules)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700919{
920 int i;
921 int changes = 0;
922
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100923 *dirty_submodules = 0;
924
Junio C Hamano50b7e702009-08-04 23:49:33 -0700925 for (i = 0; i < s->change.nr; i++) {
926 struct wt_status_change_data *d;
927 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700928 if (!d->worktree_status ||
929 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700930 continue;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100931 if (!changes)
932 changes = 1;
933 if (d->dirty_submodule)
934 *dirty_submodules = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700935 if (d->worktree_status == DIFF_STATUS_DELETED)
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100936 changes = -1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700937 }
938 return changes;
939}
940
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400941static void wt_longstatus_print_changed(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400942{
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100943 int i, dirty_submodules;
944 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700945
946 if (!worktree_changes)
947 return;
948
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400949 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700950
951 for (i = 0; i < s->change.nr; i++) {
952 struct wt_status_change_data *d;
953 struct string_list_item *it;
954 it = &(s->change.items[i]);
955 d = it->util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700956 if (!d->worktree_status ||
957 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700958 continue;
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400959 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700960 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400961 wt_longstatus_print_trailer(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400962}
963
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200964static int stash_count_refs(struct object_id *ooid UNUSED,
965 struct object_id *noid UNUSED,
966 const char *email UNUSED,
967 timestamp_t timestamp UNUSED, int tz UNUSED,
968 const char *message UNUSED, void *cb_data)
Liam Beguinc1b5d012017-06-17 18:30:51 -0400969{
970 int *c = cb_data;
971 (*c)++;
972 return 0;
973}
974
Øystein Walle612942a2021-10-22 00:25:31 +0200975static int count_stash_entries(void)
976{
977 int n = 0;
978 for_each_reflog_ent("refs/stash", stash_count_refs, &n);
979 return n;
980}
981
Liam Beguinc1b5d012017-06-17 18:30:51 -0400982static void wt_longstatus_print_stash_summary(struct wt_status *s)
983{
Øystein Walle612942a2021-10-22 00:25:31 +0200984 int stash_count = count_stash_entries();
Liam Beguinc1b5d012017-06-17 18:30:51 -0400985
Liam Beguinc1b5d012017-06-17 18:30:51 -0400986 if (stash_count > 0)
987 status_printf_ln(s, GIT_COLOR_NORMAL,
988 Q_("Your stash currently has %d entry",
989 "Your stash currently has %d entries", stash_count),
990 stash_count);
991}
992
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400993static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
Ping Yinac8d5af2008-04-12 23:05:32 +0800994{
René Scharfed3180272014-08-19 21:09:35 +0200995 struct child_process sm_summary = CHILD_PROCESS_INIT;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200996 struct strbuf cmd_stdout = STRBUF_INIT;
997 struct strbuf summary = STRBUF_INIT;
998 char *summary_content;
Ping Yinac8d5af2008-04-12 23:05:32 +0800999
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001000 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
Ping Yinac8d5af2008-04-12 23:05:32 +08001001
Jeff Kingc972bf42020-07-28 16:25:12 -04001002 strvec_push(&sm_summary.args, "submodule");
1003 strvec_push(&sm_summary.args, "summary");
1004 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1005 strvec_push(&sm_summary.args, "--for-status");
1006 strvec_push(&sm_summary.args, "--summary-limit");
1007 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
Matthieu Moybb7e32e2013-09-06 19:43:05 +02001008 if (!uncommitted)
Jeff Kingc972bf42020-07-28 16:25:12 -04001009 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
Matthieu Moybb7e32e2013-09-06 19:43:05 +02001010
Ping Yinac8d5af2008-04-12 23:05:32 +08001011 sm_summary.git_cmd = 1;
1012 sm_summary.no_stdin = 1;
Matthieu Moy3ba74072013-09-06 19:43:06 +02001013
Jeff King5c950e92015-03-22 23:53:52 -04001014 capture_command(&sm_summary, &cmd_stdout, 1024);
Matthieu Moy3ba74072013-09-06 19:43:06 +02001015
1016 /* prepend header, only if there's an actual output */
Jeff Kingd56d9662015-03-22 06:00:32 -04001017 if (cmd_stdout.len) {
Matthieu Moy3ba74072013-09-06 19:43:06 +02001018 if (uncommitted)
1019 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1020 else
1021 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1022 strbuf_addstr(&summary, "\n\n");
1023 }
1024 strbuf_addbuf(&summary, &cmd_stdout);
1025 strbuf_release(&cmd_stdout);
1026
Matthieu Moy2556b992013-09-06 19:43:07 +02001027 if (s->display_comment_prefix) {
Jeff Kingd56d9662015-03-22 06:00:32 -04001028 size_t len;
Matthieu Moy2556b992013-09-06 19:43:07 +02001029 summary_content = strbuf_detach(&summary, &len);
Calvin Wan787cb8a2023-06-06 19:48:43 +00001030 strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
Matthieu Moy2556b992013-09-06 19:43:07 +02001031 free(summary_content);
1032 }
Matthieu Moy3ba74072013-09-06 19:43:06 +02001033
1034 fputs(summary.buf, s->fp);
1035 strbuf_release(&summary);
Ping Yinac8d5af2008-04-12 23:05:32 +08001036}
1037
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001038static void wt_longstatus_print_other(struct wt_status *s,
1039 struct string_list *l,
1040 const char *what,
1041 const char *how)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001042{
Jeff Kingc91f0d92006-09-08 04:05:34 -04001043 int i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001044 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001045 static struct string_list output = STRING_LIST_INIT_DUP;
1046 struct column_options copts;
Jeff Kingc91f0d92006-09-08 04:05:34 -04001047
Jeff King12829882011-06-02 01:54:49 -04001048 if (!l->nr)
Junio C Hamano76378682009-08-10 00:36:33 -07001049 return;
Jeff Kingc91f0d92006-09-08 04:05:34 -04001050
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001051 wt_longstatus_print_other_header(s, what, how);
Junio C Hamano1b908b62010-04-10 00:19:46 -07001052
1053 for (i = 0; i < l->nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -07001054 struct string_list_item *it;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001055 const char *path;
Junio C Hamano1b908b62010-04-10 00:19:46 -07001056 it = &(l->items[i]);
Junio C Hamano88910c92020-09-10 10:01:54 -07001057 path = quote_path(it->string, s->prefix, &buf, 0);
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001058 if (column_active(s->colopts)) {
1059 string_list_append(&output, path);
1060 continue;
1061 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001062 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1063 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001064 "%s\n", path);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001065 }
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001066
1067 strbuf_release(&buf);
1068 if (!column_active(s->colopts))
Matthieu Moy2f0f7f12013-09-06 19:43:09 +02001069 goto conclude;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001070
Matthieu Moy2556b992013-09-06 19:43:07 +02001071 strbuf_addf(&buf, "%s%s\t%s",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001072 color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001073 s->display_comment_prefix ? "#" : "",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +07001074 color(WT_STATUS_UNTRACKED, s));
1075 memset(&copts, 0, sizeof(copts));
1076 copts.padding = 1;
1077 copts.indent = buf.buf;
1078 if (want_color(s->use_color))
1079 copts.nl = GIT_COLOR_RESET "\n";
1080 print_columns(&output, s->colopts, &copts);
1081 string_list_clear(&output, 0);
Johannes Schindelin367c9882007-11-11 17:35:41 +00001082 strbuf_release(&buf);
Matthieu Moy2f0f7f12013-09-06 19:43:09 +02001083conclude:
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001084 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001085}
1086
Brian Malehornd76650b2017-05-15 23:06:49 -07001087size_t wt_status_locate_end(const char *s, size_t len)
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001088{
1089 const char *p;
1090 struct strbuf pattern = STRBUF_INIT;
1091
SZEDER Gáborfbfa0972015-06-09 02:28:34 +02001092 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
Brian Malehornd76650b2017-05-15 23:06:49 -07001093 if (starts_with(s, pattern.buf + 1))
1094 len = 0;
1095 else if ((p = strstr(s, pattern.buf)))
1096 len = p - s + 1;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001097 strbuf_release(&pattern);
Brian Malehornd76650b2017-05-15 23:06:49 -07001098 return len;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001099}
1100
Denton Liud540b702019-04-17 11:23:27 +01001101void wt_status_append_cut_line(struct strbuf *buf)
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001102{
Kaartic Sivaraam8c4b1a32017-09-13 13:05:38 +00001103 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
Denton Liud540b702019-04-17 11:23:27 +01001104
Calvin Wan787cb8a2023-06-06 19:48:43 +00001105 strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
1106 strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
Denton Liud540b702019-04-17 11:23:27 +01001107}
1108
1109void wt_status_add_cut_line(FILE *fp)
1110{
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001111 struct strbuf buf = STRBUF_INIT;
1112
Denton Liud540b702019-04-17 11:23:27 +01001113 wt_status_append_cut_line(&buf);
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001114 fputs(buf.buf, fp);
1115 strbuf_release(&buf);
1116}
1117
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001118static void wt_longstatus_print_verbose(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001119{
1120 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -08001121 struct setup_revision_opt opt;
Michael J Gruber40555002015-03-06 10:43:35 +01001122 int dirty_submodules;
1123 const char *c = color(WT_STATUS_HEADER, s);
Kristian Høgsberg99a12692007-11-21 21:54:49 -05001124
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01001125 repo_init_revisions(s->repo, &rev, NULL);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001126 rev.diffopt.flags.allow_textconv = 1;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +07001127 rev.diffopt.ita_invisible_in_index = 1;
Junio C Hamano32962c92010-03-08 22:58:09 -08001128
1129 memset(&opt, 0, sizeof(opt));
brian m. carlsonf2e51192018-05-02 00:26:00 +00001130 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
Junio C Hamano32962c92010-03-08 22:58:09 -08001131 setup_revisions(0, NULL, &rev, &opt);
1132
Jeff Kingc91f0d92006-09-08 04:05:34 -04001133 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
Ben Pearte8b2dc22018-05-11 15:38:58 +00001134 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1135 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1136 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
Kristian Høgsberg4ba0cb22008-03-10 13:58:26 -04001137 rev.diffopt.file = s->fp;
1138 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 00:49:35 -04001139 /*
1140 * If we're not going to stdout, then we definitely don't
1141 * want color, since we are going to the commit message
1142 * file (and even the "auto" setting won't work, since it
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001143 * will have checked isatty on stdout). But we then do want
1144 * to insert the scissor line here to reliably remove the
1145 * diff before committing.
Jeff King4f672ad2008-10-26 00:49:35 -04001146 */
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001147 if (s->fp != stdout) {
Jeff Kingf1c96262011-08-17 22:03:12 -07001148 rev.diffopt.use_color = 0;
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +07001149 wt_status_add_cut_line(s->fp);
Jens Lehmann1a72cfd2013-12-05 20:44:14 +01001150 }
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001151 if (s->verbose > 1 && s->committable) {
Michael J Gruber40555002015-03-06 10:43:35 +01001152 /* print_updated() printed a header, so do we */
1153 if (s->fp != stdout)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001154 wt_longstatus_print_trailer(s);
Michael J Gruber40555002015-03-06 10:43:35 +01001155 status_printf_ln(s, c, _("Changes to be committed:"));
1156 rev.diffopt.a_prefix = "c/";
1157 rev.diffopt.b_prefix = "i/";
1158 } /* else use prefix as per user config */
Junio C Hamano976b97e2023-08-21 16:14:14 -04001159 run_diff_index(&rev, DIFF_INDEX_CACHED);
Michael J Gruber40555002015-03-06 10:43:35 +01001160 if (s->verbose > 1 &&
1161 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1162 status_printf_ln(s, c,
1163 "--------------------------------------------------");
1164 status_printf_ln(s, c, _("Changes not staged for commit:"));
1165 setup_work_tree();
1166 rev.diffopt.a_prefix = "i/";
1167 rev.diffopt.b_prefix = "w/";
1168 run_diff_files(&rev, 0);
1169 }
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001170 release_revisions(&rev);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001171}
1172
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001173static void wt_longstatus_print_tracking(struct wt_status *s)
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001174{
1175 struct strbuf sb = STRBUF_INIT;
René Scharfec72b49d2015-10-31 18:37:43 +01001176 const char *cp, *ep, *branch_name;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001177 struct branch *branch;
Matthieu Moy2556b992013-09-06 19:43:07 +02001178 char comment_line_string[3];
1179 int i;
Jeff Hostetler0a535612019-06-18 13:21:27 -07001180 uint64_t t_begin = 0;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001181
1182 assert(s->branch && !s->is_initial);
René Scharfec72b49d2015-10-31 18:37:43 +01001183 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001184 return;
René Scharfec72b49d2015-10-31 18:37:43 +01001185 branch = branch_get(branch_name);
Jeff Hostetler0a535612019-06-18 13:21:27 -07001186
1187 t_begin = getnanotime();
1188
Alex Henrieb6f3da52023-07-12 22:41:13 -06001189 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1190 !s->commit_template))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001191 return;
1192
Ben Boeckeled9bff02021-08-23 12:44:00 +02001193 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
Jeff Hostetler0a535612019-06-18 13:21:27 -07001194 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1195 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1196 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1197 strbuf_addf(&sb, _("\n"
1198 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1199 "You can use '--no-ahead-behind' to avoid this.\n"),
1200 t_delta_in_ms / 1000.0);
1201 }
1202 }
1203
Matthieu Moy2556b992013-09-06 19:43:07 +02001204 i = 0;
1205 if (s->display_comment_prefix) {
1206 comment_line_string[i++] = comment_line_char;
1207 comment_line_string[i++] = ' ';
1208 }
1209 comment_line_string[i] = '\0';
1210
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001211 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-09 21:59:30 -07001212 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001213 "%s%.*s", comment_line_string,
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001214 (int)(ep - cp), cp);
Matthieu Moy2556b992013-09-06 19:43:07 +02001215 if (s->display_comment_prefix)
1216 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1217 comment_line_char);
1218 else
Jeff King75177c82017-04-27 05:01:05 -04001219 fputs("\n", s->fp);
Rene Scharfeb6ec3072017-08-30 20:20:18 +02001220 strbuf_release(&sb);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001221}
1222
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001223static int uf_was_slow(struct wt_status *s)
1224{
1225 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1226 s->untracked_in_ms = 3250;
1227 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1228}
1229
Lucien Kong83c750a2012-06-05 22:21:24 +02001230static void show_merge_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001231 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001232{
1233 if (has_unmerged(s)) {
1234 status_printf_ln(s, color, _("You have unmerged paths."));
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001235 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001236 status_printf_ln(s, color,
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001237 _(" (fix conflicts and run \"git commit\")"));
1238 status_printf_ln(s, color,
1239 _(" (use \"git merge --abort\" to abort the merge)"));
1240 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001241 } else {
1242 status_printf_ln(s, color,
1243 _("All conflicts fixed but you are still merging."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001244 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001245 status_printf_ln(s, color,
1246 _(" (use \"git commit\" to conclude merge)"));
1247 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001248 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001249}
1250
1251static void show_am_in_progress(struct wt_status *s,
Lucien Kong83c750a2012-06-05 22:21:24 +02001252 const char *color)
1253{
徐沛文 (Aleen)9e7e41b2021-12-09 07:25:55 +00001254 int am_empty_patch;
1255
Lucien Kong83c750a2012-06-05 22:21:24 +02001256 status_printf_ln(s, color,
1257 _("You are in the middle of an am session."));
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001258 if (s->state.am_empty_patch)
Lucien Kong83c750a2012-06-05 22:21:24 +02001259 status_printf_ln(s, color,
1260 _("The current patch is empty."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001261 if (s->hints) {
徐沛文 (Aleen)9e7e41b2021-12-09 07:25:55 +00001262 am_empty_patch = s->state.am_empty_patch;
1263 if (!am_empty_patch)
Lucien Kong83c750a2012-06-05 22:21:24 +02001264 status_printf_ln(s, color,
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +03001265 _(" (fix conflicts and then run \"git am --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001266 status_printf_ln(s, color,
1267 _(" (use \"git am --skip\" to skip this patch)"));
徐沛文 (Aleen)9e7e41b2021-12-09 07:25:55 +00001268 if (am_empty_patch)
1269 status_printf_ln(s, color,
1270 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001271 status_printf_ln(s, color,
1272 _(" (use \"git am --abort\" to restore the original branch)"));
1273 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001274 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001275}
1276
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001277static char *read_line_from_git_path(const char *filename)
1278{
1279 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +07001280 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1281
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001282 if (!fp) {
1283 strbuf_release(&buf);
1284 return NULL;
1285 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001286 strbuf_getline_lf(&buf, fp);
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001287 if (!fclose(fp)) {
1288 return strbuf_detach(&buf, NULL);
1289 } else {
1290 strbuf_release(&buf);
1291 return NULL;
1292 }
1293}
1294
1295static int split_commit_in_progress(struct wt_status *s)
1296{
1297 int split_in_progress = 0;
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001298 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001299
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001300 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001301 !s->branch || strcmp(s->branch, "HEAD"))
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001302 return 0;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001303
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001304 head = read_line_from_git_path("HEAD");
1305 orig_head = read_line_from_git_path("ORIG_HEAD");
1306 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1307 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1308
1309 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1310 ; /* fall through, no split in progress */
1311 else if (!strcmp(rebase_amend, rebase_orig_head))
1312 split_in_progress = !!strcmp(head, rebase_amend);
1313 else if (strcmp(orig_head, rebase_orig_head))
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001314 split_in_progress = 1;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001315
1316 free(head);
1317 free(orig_head);
1318 free(rebase_amend);
1319 free(rebase_orig_head);
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001320
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001321 return split_in_progress;
1322}
1323
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001324/*
1325 * Turn
1326 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1327 * into
1328 * "pick d6a2f03 some message"
1329 *
1330 * The function assumes that the line does not contain useless spaces
1331 * before or after the command.
1332 */
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001333static void abbrev_oid_in_line(struct strbuf *line)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001334{
1335 struct strbuf **split;
1336 int i;
1337
1338 if (starts_with(line->buf, "exec ") ||
Johannes Schindelin225a7772019-05-10 13:23:14 -07001339 starts_with(line->buf, "x ") ||
1340 starts_with(line->buf, "label ") ||
1341 starts_with(line->buf, "l "))
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001342 return;
1343
1344 split = strbuf_split_max(line, ' ', 3);
1345 if (split[0] && split[1]) {
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001346 struct object_id oid;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001347
1348 /*
1349 * strbuf_split_max left a space. Trim it and re-add
1350 * it after abbreviation.
1351 */
1352 strbuf_trim(split[1]);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001353 if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001354 strbuf_reset(split[1]);
brian m. carlson30e677e2018-03-12 02:27:28 +00001355 strbuf_add_unique_abbrev(split[1], &oid,
René Scharfea94bb682016-10-08 17:38:47 +02001356 DEFAULT_ABBREV);
1357 strbuf_addch(split[1], ' ');
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001358 strbuf_reset(line);
1359 for (i = 0; split[i]; i++)
René Scharfe81099842016-07-19 20:36:29 +02001360 strbuf_addbuf(line, split[i]);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001361 }
1362 }
Stefan Beller6eb60782016-03-31 17:35:44 -07001363 strbuf_list_free(split);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001364}
1365
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001366static int read_rebase_todolist(const char *fname, struct string_list *lines)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001367{
1368 struct strbuf line = STRBUF_INIT;
1369 FILE *f = fopen(git_path("%s", fname), "r");
1370
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001371 if (!f) {
1372 if (errno == ENOENT)
1373 return -1;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001374 die_errno("Could not open file %s for reading",
1375 git_path("%s", fname));
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001376 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001377 while (!strbuf_getline_lf(&line, f)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001378 if (line.len && line.buf[0] == comment_line_char)
1379 continue;
1380 strbuf_trim(&line);
1381 if (!line.len)
1382 continue;
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001383 abbrev_oid_in_line(&line);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001384 string_list_append(lines, line.buf);
1385 }
Johannes Schindeline7b65e22017-05-04 15:55:52 +02001386 fclose(f);
Rene Scharfe6f495412017-08-30 20:20:17 +02001387 strbuf_release(&line);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001388 return 0;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001389}
1390
1391static void show_rebase_information(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001392 const char *color)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001393{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001394 if (s->state.rebase_interactive_in_progress) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001395 int i;
1396 int nr_lines_to_show = 2;
1397
1398 struct string_list have_done = STRING_LIST_INIT_DUP;
1399 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1400
1401 read_rebase_todolist("rebase-merge/done", &have_done);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001402 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1403 &yet_to_do))
1404 status_printf_ln(s, color,
1405 _("git-rebase-todo is missing."));
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001406 if (have_done.nr == 0)
1407 status_printf_ln(s, color, _("No commands done."));
1408 else {
1409 status_printf_ln(s, color,
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001410 Q_("Last command done (%"PRIuMAX" command done):",
1411 "Last commands done (%"PRIuMAX" commands done):",
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001412 have_done.nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001413 (uintmax_t)have_done.nr);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001414 for (i = (have_done.nr > nr_lines_to_show)
1415 ? have_done.nr - nr_lines_to_show : 0;
1416 i < have_done.nr;
1417 i++)
1418 status_printf_ln(s, color, " %s", have_done.items[i].string);
1419 if (have_done.nr > nr_lines_to_show && s->hints)
1420 status_printf_ln(s, color,
1421 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1422 }
1423
1424 if (yet_to_do.nr == 0)
1425 status_printf_ln(s, color,
1426 _("No commands remaining."));
1427 else {
1428 status_printf_ln(s, color,
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001429 Q_("Next command to do (%"PRIuMAX" remaining command):",
1430 "Next commands to do (%"PRIuMAX" remaining commands):",
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001431 yet_to_do.nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +01001432 (uintmax_t)yet_to_do.nr);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001433 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1434 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1435 if (s->hints)
1436 status_printf_ln(s, color,
1437 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1438 }
1439 string_list_clear(&yet_to_do, 0);
1440 string_list_clear(&have_done, 0);
1441 }
1442}
1443
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001444static void print_rebase_state(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001445 const char *color)
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001446{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001447 if (s->state.branch)
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001448 status_printf_ln(s, color,
1449 _("You are currently rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001450 s->state.branch,
1451 s->state.onto);
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001452 else
1453 status_printf_ln(s, color,
1454 _("You are currently rebasing."));
1455}
1456
Lucien Kong83c750a2012-06-05 22:21:24 +02001457static void show_rebase_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001458 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001459{
1460 struct stat st;
1461
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001462 show_rebase_information(s, color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001463 if (has_unmerged(s)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001464 print_rebase_state(s, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001465 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001466 status_printf_ln(s, color,
1467 _(" (fix conflicts and then run \"git rebase --continue\")"));
1468 status_printf_ln(s, color,
1469 _(" (use \"git rebase --skip\" to skip this patch)"));
1470 status_printf_ln(s, color,
1471 _(" (use \"git rebase --abort\" to check out the original branch)"));
1472 }
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001473 } else if (s->state.rebase_in_progress ||
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01001474 !stat(git_path_merge_msg(s->repo), &st)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001475 print_rebase_state(s, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001476 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001477 status_printf_ln(s, color,
1478 _(" (all conflicts fixed: run \"git rebase --continue\")"));
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001479 } else if (split_commit_in_progress(s)) {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001480 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001481 status_printf_ln(s, color,
1482 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001483 s->state.branch,
1484 s->state.onto);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001485 else
1486 status_printf_ln(s, color,
1487 _("You are currently splitting a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001488 if (s->hints)
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001489 status_printf_ln(s, color,
1490 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001491 } else {
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001492 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001493 status_printf_ln(s, color,
1494 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001495 s->state.branch,
1496 s->state.onto);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001497 else
1498 status_printf_ln(s, color,
1499 _("You are currently editing a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001500 if (s->hints && !s->amend) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001501 status_printf_ln(s, color,
1502 _(" (use \"git commit --amend\" to amend the current commit)"));
1503 status_printf_ln(s, color,
1504 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1505 }
1506 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001507 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001508}
1509
1510static void show_cherry_pick_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001511 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001512{
Phillip Wood4a724862019-04-16 11:18:42 +01001513 if (is_null_oid(&s->state.cherry_pick_head_oid))
1514 status_printf_ln(s, color,
1515 _("Cherry-pick currently in progress."));
1516 else
1517 status_printf_ln(s, color,
1518 _("You are currently cherry-picking commit %s."),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001519 repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1520 DEFAULT_ABBREV));
Phillip Wood4a724862019-04-16 11:18:42 +01001521
Matthieu Moy6a964f52013-09-12 12:50:05 +02001522 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001523 if (has_unmerged(s))
1524 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001525 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
Phillip Wood4a724862019-04-16 11:18:42 +01001526 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1527 status_printf_ln(s, color,
1528 _(" (run \"git cherry-pick --continue\" to continue)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001529 else
1530 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001531 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1532 status_printf_ln(s, color,
Denton Liu86ae43d2019-08-27 00:45:41 -04001533 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1534 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001535 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001536 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001537 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001538}
1539
Matthieu Moydb4ef442013-04-02 16:20:21 +02001540static void show_revert_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001541 const char *color)
Matthieu Moydb4ef442013-04-02 16:20:21 +02001542{
Phillip Wood4a724862019-04-16 11:18:42 +01001543 if (is_null_oid(&s->state.revert_head_oid))
1544 status_printf_ln(s, color,
1545 _("Revert currently in progress."));
1546 else
1547 status_printf_ln(s, color,
1548 _("You are currently reverting commit %s."),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001549 repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1550 DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001551 if (s->hints) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001552 if (has_unmerged(s))
1553 status_printf_ln(s, color,
1554 _(" (fix conflicts and run \"git revert --continue\")"));
Phillip Wood4a724862019-04-16 11:18:42 +01001555 else if (is_null_oid(&s->state.revert_head_oid))
1556 status_printf_ln(s, color,
1557 _(" (run \"git revert --continue\" to continue)"));
Matthieu Moydb4ef442013-04-02 16:20:21 +02001558 else
1559 status_printf_ln(s, color,
1560 _(" (all conflicts fixed: run \"git revert --continue\")"));
1561 status_printf_ln(s, color,
Denton Liu86ae43d2019-08-27 00:45:41 -04001562 _(" (use \"git revert --skip\" to skip this patch)"));
1563 status_printf_ln(s, color,
Matthieu Moydb4ef442013-04-02 16:20:21 +02001564 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1565 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001566 wt_longstatus_print_trailer(s);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001567}
1568
Lucien Kong83c750a2012-06-05 22:21:24 +02001569static void show_bisect_in_progress(struct wt_status *s,
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001570 const char *color)
Lucien Kong83c750a2012-06-05 22:21:24 +02001571{
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001572 if (s->state.branch)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001573 status_printf_ln(s, color,
Nguyễn Thái Ngọc Duy6deab242013-03-23 10:52:44 +07001574 _("You are currently bisecting, started from branch '%s'."),
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001575 s->state.branch);
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001576 else
1577 status_printf_ln(s, color,
1578 _("You are currently bisecting."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001579 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001580 status_printf_ln(s, color,
1581 _(" (use \"git bisect reset\" to get back to the original branch)"));
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001582 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001583}
1584
Elijah Newren051df3c2020-06-18 20:49:57 +00001585static void show_sparse_checkout_in_use(struct wt_status *s,
1586 const char *color)
1587{
1588 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1589 return;
1590
Derrick Stoleebf48e5a2021-07-14 13:12:36 +00001591 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1592 status_printf_ln(s, color, _("You are in a sparse checkout."));
1593 else
1594 status_printf_ln(s, color,
1595 _("You are in a sparse checkout with %d%% of tracked files present."),
1596 s->state.sparse_checkout_percentage);
Elijah Newren051df3c2020-06-18 20:49:57 +00001597 wt_longstatus_print_trailer(s);
1598}
1599
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001600/*
1601 * Extract branch information from rebase/bisect
1602 */
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001603static char *get_branch(const struct worktree *wt, const char *path)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001604{
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001605 struct strbuf sb = STRBUF_INIT;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001606 struct object_id oid;
René Scharfec72b49d2015-10-31 18:37:43 +01001607 const char *branch_name;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001608
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001609 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 +07001610 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001611
Jeff King66ec9042015-01-28 12:57:35 -05001612 while (sb.len && sb.buf[sb.len - 1] == '\n')
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001613 strbuf_setlen(&sb, sb.len - 1);
1614 if (!sb.len)
1615 goto got_nothing;
René Scharfec72b49d2015-10-31 18:37:43 +01001616 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1617 strbuf_remove(&sb, 0, branch_name - sb.buf);
Christian Couder59556542013-11-30 21:55:40 +01001618 else if (starts_with(sb.buf, "refs/"))
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001619 ;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001620 else if (!get_oid_hex(sb.buf, &oid)) {
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001621 strbuf_reset(&sb);
brian m. carlson30e677e2018-03-12 02:27:28 +00001622 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001623 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1624 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001625 else /* bisect */
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001626 ;
1627 return strbuf_detach(&sb, NULL);
1628
1629got_nothing:
1630 strbuf_release(&sb);
1631 return NULL;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001632}
1633
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001634struct grab_1st_switch_cbdata {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001635 struct strbuf buf;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001636 struct object_id noid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001637};
1638
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001639static int grab_1st_switch(struct object_id *ooid UNUSED,
Jeff Kingc006e9f2022-08-19 06:08:35 -04001640 struct object_id *noid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001641 const char *email UNUSED,
1642 timestamp_t timestamp UNUSED, int tz UNUSED,
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001643 const char *message, void *cb_data)
Lucien Kong83c750a2012-06-05 22:21:24 +02001644{
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001645 struct grab_1st_switch_cbdata *cb = cb_data;
1646 const char *target = NULL, *end;
1647
René Scharfec72b49d2015-10-31 18:37:43 +01001648 if (!skip_prefix(message, "checkout: moving from ", &message))
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001649 return 0;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001650 target = strstr(message, " to ");
1651 if (!target)
1652 return 0;
1653 target += strlen(" to ");
1654 strbuf_reset(&cb->buf);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001655 oidcpy(&cb->noid, noid);
René Scharfe904de442015-11-25 15:10:18 +01001656 end = strchrnul(target, '\n');
1657 strbuf_add(&cb->buf, target, end - target);
1658 if (!strcmp(cb->buf.buf, "HEAD")) {
Matthieu Moy0eb85482015-09-27 17:13:42 +02001659 /* HEAD is relative. Resolve it to the right reflog entry. */
René Scharfe904de442015-11-25 15:10:18 +01001660 strbuf_reset(&cb->buf);
brian m. carlson30e677e2018-03-12 02:27:28 +00001661 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
Matthieu Moy0eb85482015-09-27 17:13:42 +02001662 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001663 return 1;
1664}
1665
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001666static void wt_status_get_detached_from(struct repository *r,
1667 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001668{
1669 struct grab_1st_switch_cbdata cb;
1670 struct commit *commit;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001671 struct object_id oid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001672 char *ref = NULL;
1673
1674 strbuf_init(&cb.buf, 0);
1675 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1676 strbuf_release(&cb.buf);
1677 return;
1678 }
1679
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001680 if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
Ævar Arnfjörð Bjarmason12cb1c12023-03-28 15:58:54 +02001681 1) == 1 &&
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001682 /* oid is a commit? match without further lookup */
Jeff King4a7e27e2018-08-28 17:22:40 -04001683 (oideq(&cb.noid, &oid) ||
Martin Ågrenb1f1ade2020-09-27 15:15:41 +02001684 /* perhaps oid is a tag, try to dereference to a commit */
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001685 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001686 oideq(&cb.noid, &commit->object.oid)))) {
René Scharfec72b49d2015-10-31 18:37:43 +01001687 const char *from = ref;
1688 if (!skip_prefix(from, "refs/tags/", &from))
1689 skip_prefix(from, "refs/remotes/", &from);
1690 state->detached_from = xstrdup(from);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001691 } else
1692 state->detached_from =
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001693 xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
brian m. carlson40f55552018-03-12 02:27:29 +00001694 oidcpy(&state->detached_oid, &cb.noid);
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001695 state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001696 oideq(&oid, &state->detached_oid);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001697
1698 free(ref);
1699 strbuf_release(&cb.buf);
1700}
1701
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001702int wt_status_check_rebase(const struct worktree *wt,
1703 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001704{
Lucien Kong83c750a2012-06-05 22:21:24 +02001705 struct stat st;
1706
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001707 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1708 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001709 state->am_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001710 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 +07001711 state->am_empty_patch = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001712 } else {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001713 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001714 state->branch = get_branch(wt, "rebase-apply/head-name");
1715 state->onto = get_branch(wt, "rebase-apply/onto");
Lucien Kong83c750a2012-06-05 22:21:24 +02001716 }
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001717 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1718 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001719 state->rebase_interactive_in_progress = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001720 else
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001721 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001722 state->branch = get_branch(wt, "rebase-merge/head-name");
1723 state->onto = get_branch(wt, "rebase-merge/onto");
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001724 } else
1725 return 0;
1726 return 1;
1727}
1728
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001729int wt_status_check_bisect(const struct worktree *wt,
1730 struct wt_status_state *state)
1731{
1732 struct stat st;
1733
1734 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1735 state->bisect_in_progress = 1;
1736 state->branch = get_branch(wt, "BISECT_START");
1737 return 1;
1738 }
1739 return 0;
1740}
1741
Elijah Newren051df3c2020-06-18 20:49:57 +00001742static void wt_status_check_sparse_checkout(struct repository *r,
1743 struct wt_status_state *state)
1744{
1745 int skip_worktree = 0;
1746 int i;
1747
1748 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1749 /*
1750 * Don't compute percentage of checked out files if we
1751 * aren't in a sparse checkout or would get division by 0.
1752 */
1753 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1754 return;
1755 }
1756
Derrick Stoleebf48e5a2021-07-14 13:12:36 +00001757 if (r->index->sparse_index) {
1758 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1759 return;
1760 }
1761
Elijah Newren051df3c2020-06-18 20:49:57 +00001762 for (i = 0; i < r->index->cache_nr; i++) {
1763 struct cache_entry *ce = r->index->cache[i];
1764 if (ce_skip_worktree(ce))
1765 skip_worktree++;
1766 }
1767
1768 state->sparse_checkout_percentage =
1769 100 - (100 * skip_worktree)/r->index->cache_nr;
1770}
1771
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001772void wt_status_get_state(struct repository *r,
1773 struct wt_status_state *state,
Lucien Kong83c750a2012-06-05 22:21:24 +02001774 int get_detached_from)
1775{
1776 struct stat st;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001777 struct object_id oid;
Phillip Wood4a724862019-04-16 11:18:42 +01001778 enum replay_action action;
Lucien Kong83c750a2012-06-05 22:21:24 +02001779
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001780 if (!stat(git_path_merge_head(r), &st)) {
Johannes Schindelin982288e2018-11-12 15:26:02 -08001781 wt_status_check_rebase(NULL, state);
Lucien Kong83c750a2012-06-05 22:21:24 +02001782 state->merge_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001783 } else if (wt_status_check_rebase(NULL, state)) {
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001784 ; /* all set */
Han-Wen Nienhuysc8e41592020-08-21 16:59:35 +00001785 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001786 !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001787 state->cherry_pick_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001788 oidcpy(&state->cherry_pick_head_oid, &oid);
Lucien Kong83c750a2012-06-05 22:21:24 +02001789 }
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001790 wt_status_check_bisect(NULL, state);
Han-Wen Nienhuysb8825ef2020-08-21 16:59:37 +00001791 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02001792 !repo_get_oid(r, "REVERT_HEAD", &oid)) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001793 state->revert_in_progress = 1;
brian m. carlson40f55552018-03-12 02:27:29 +00001794 oidcpy(&state->revert_head_oid, &oid);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001795 }
Phillip Wood4a724862019-04-16 11:18:42 +01001796 if (!sequencer_get_last_command(r, &action)) {
Jacob Kellera096a882023-06-27 15:41:50 -07001797 if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
Phillip Wood4a724862019-04-16 11:18:42 +01001798 state->cherry_pick_in_progress = 1;
brian m. carlson14228442021-04-26 01:02:56 +00001799 oidcpy(&state->cherry_pick_head_oid, null_oid());
Jacob Kellera096a882023-06-27 15:41:50 -07001800 } else if (action == REPLAY_REVERT && !state->revert_in_progress) {
Phillip Wood4a724862019-04-16 11:18:42 +01001801 state->revert_in_progress = 1;
brian m. carlson14228442021-04-26 01:02:56 +00001802 oidcpy(&state->revert_head_oid, null_oid());
Phillip Wood4a724862019-04-16 11:18:42 +01001803 }
1804 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001805 if (get_detached_from)
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01001806 wt_status_get_detached_from(r, state);
Elijah Newren051df3c2020-06-18 20:49:57 +00001807 wt_status_check_sparse_checkout(r, state);
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001808}
1809
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001810static void wt_longstatus_print_state(struct wt_status *s)
Lucien Kong83c750a2012-06-05 22:21:24 +02001811{
1812 const char *state_color = color(WT_STATUS_HEADER, s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001813 struct wt_status_state *state = &s->state;
1814
Johannes Schindelin982288e2018-11-12 15:26:02 -08001815 if (state->merge_in_progress) {
1816 if (state->rebase_interactive_in_progress) {
1817 show_rebase_information(s, state_color);
1818 fputs("\n", s->fp);
1819 }
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001820 show_merge_in_progress(s, state_color);
Johannes Schindelin982288e2018-11-12 15:26:02 -08001821 } else if (state->am_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001822 show_am_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001823 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001824 show_rebase_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001825 else if (state->cherry_pick_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001826 show_cherry_pick_in_progress(s, state_color);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001827 else if (state->revert_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001828 show_revert_in_progress(s, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001829 if (state->bisect_in_progress)
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001830 show_bisect_in_progress(s, state_color);
Elijah Newren051df3c2020-06-18 20:49:57 +00001831
1832 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1833 show_sparse_checkout_in_use(s, state_color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001834}
1835
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001836static void wt_longstatus_print(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001837{
Aleksi Aalto1d282322010-11-18 01:40:05 +02001838 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1839 const char *branch_status_color = color(WT_STATUS_HEADER, s);
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001840 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
Jürgen Rühle98bf8a42007-01-02 20:26:23 +01001841
Junio C Hamanobda324c2007-01-03 01:09:34 -08001842 if (s->branch) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001843 const char *on_what = _("On branch ");
Junio C Hamanobda324c2007-01-03 01:09:34 -08001844 const char *branch_name = s->branch;
René Scharfec72b49d2015-10-31 18:37:43 +01001845 if (!strcmp(branch_name, "HEAD")) {
Aleksi Aalto1d282322010-11-18 01:40:05 +02001846 branch_status_color = color(WT_STATUS_NOBRANCH, s);
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001847 if (s->state.rebase_in_progress ||
1848 s->state.rebase_interactive_in_progress) {
1849 if (s->state.rebase_interactive_in_progress)
Guillaume Pagèsdf25e942015-06-30 15:01:13 +02001850 on_what = _("interactive rebase in progress; onto ");
1851 else
1852 on_what = _("rebase in progress; onto ");
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001853 branch_name = s->state.onto;
1854 } else if (s->state.detached_from) {
1855 branch_name = s->state.detached_from;
1856 if (s->state.detached_at)
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01001857 on_what = _("HEAD detached at ");
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001858 else
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01001859 on_what = _("HEAD detached from ");
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001860 } else {
1861 branch_name = "";
1862 on_what = _("Not currently on any branch.");
1863 }
René Scharfec72b49d2015-10-31 18:37:43 +01001864 } else
1865 skip_prefix(branch_name, "refs/heads/", &branch_name);
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001866 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001867 status_printf_more(s, branch_status_color, "%s", on_what);
1868 status_printf_more(s, branch_color, "%s\n", branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001869 if (!s->is_initial)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001870 wt_longstatus_print_tracking(s);
Junio C Hamanobda324c2007-01-03 01:09:34 -08001871 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001872
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07001873 wt_longstatus_print_state(s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001874
Jeff Kingc91f0d92006-09-08 04:05:34 -04001875 if (s->is_initial) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001876 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301877 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1878 s->commit_template
1879 ? _("Initial commit")
1880 : _("No commits yet"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001881 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001882 }
1883
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001884 wt_longstatus_print_updated(s);
1885 wt_longstatus_print_unmerged(s);
1886 wt_longstatus_print_changed(s);
Jens Lehmann46a958b2010-06-25 16:56:47 +02001887 if (s->submodule_summary &&
1888 (!s->ignore_submodule_arg ||
1889 strcmp(s->ignore_submodule_arg, "all"))) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001890 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1891 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
Jens Lehmannf17a5d32010-01-17 20:42:31 +01001892 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001893 if (s->show_untracked_files) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001894 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
Jameson Millereec0f7f2017-10-30 13:21:37 -04001895 if (s->show_ignored_mode)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001896 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001897 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001898 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001899 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1900 status_printf_ln(s, GIT_COLOR_NORMAL,
1901 _("It took %.2f seconds to enumerate untracked files,\n"
1902 "but the results were cached, and subsequent runs may be faster."),
1903 s->untracked_in_ms / 1000.0);
1904 } else {
1905 status_printf_ln(s, GIT_COLOR_NORMAL,
1906 _("It took %.2f seconds to enumerate untracked files."),
1907 s->untracked_in_ms / 1000.0);
1908 }
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001909 status_printf_ln(s, GIT_COLOR_NORMAL,
Rudy Rigotecbc23e2022-11-30 00:52:16 +00001910 _("See 'git help status' for information on how to improve this."));
1911 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001912 }
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001913 } else if (s->committable)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001914 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
Matthieu Moy6a964f52013-09-12 12:50:05 +02001915 s->hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001916 ? _(" (use -u option to show untracked files)") : "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001917
Jeff King1324fb62008-11-12 03:23:37 -05001918 if (s->verbose)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001919 wt_longstatus_print_verbose(s);
Stephen P. Smith6fa90192018-09-05 17:53:27 -07001920 if (!s->committable) {
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001921 if (s->amend)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001922 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
Junio C Hamano37d07f82007-12-12 19:09:16 -08001923 else if (s->nowarn)
1924 ; /* nothing */
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001925 else if (s->workdir_dirty) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001926 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001927 fprintf(s->fp, _("no changes added to commit "
1928 "(use \"git add\" and/or "
1929 "\"git commit -a\")\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001930 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001931 fprintf(s->fp, _("no changes added to "
1932 "commit\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001933 } else if (s->untracked.nr) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001934 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001935 fprintf(s->fp, _("nothing added to commit but "
1936 "untracked files present (use "
1937 "\"git add\" to track)\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001938 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001939 fprintf(s->fp, _("nothing added to commit but "
1940 "untracked files present\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001941 } else if (s->is_initial) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001942 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001943 fprintf(s->fp, _("nothing to commit (create/"
1944 "copy files and use \"git "
1945 "add\" to track)\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001946 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001947 fprintf(s->fp, _("nothing to commit\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001948 } else if (!s->show_untracked_files) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001949 if (s->hints)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001950 fprintf(s->fp, _("nothing to commit (use -u to "
1951 "show untracked files)\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001952 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001953 fprintf(s->fp, _("nothing to commit\n"));
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001954 } else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001955 fprintf(s->fp, _("nothing to commit, working tree "
1956 "clean\n"));
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001957 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001958 if(s->show_stash)
1959 wt_longstatus_print_stash_summary(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001960}
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001961
Jeff King3207a3a2012-05-07 15:44:44 -04001962static void wt_shortstatus_unmerged(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001963 struct wt_status *s)
1964{
1965 struct wt_status_change_data *d = it->util;
1966 const char *how = "??";
1967
1968 switch (d->stagemask) {
1969 case 1: how = "DD"; break; /* both deleted */
1970 case 2: how = "AU"; break; /* added by us */
1971 case 3: how = "UD"; break; /* deleted by them */
1972 case 4: how = "UA"; break; /* added by them */
1973 case 5: how = "DU"; break; /* deleted by us */
1974 case 6: how = "AA"; break; /* both added */
1975 case 7: how = "UU"; break; /* both modified */
1976 }
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001977 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
Jeff King3207a3a2012-05-07 15:44:44 -04001978 if (s->null_termination) {
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001979 fprintf(s->fp, " %s%c", it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001980 } else {
1981 struct strbuf onebuf = STRBUF_INIT;
1982 const char *one;
Junio C Hamanoa361dd32020-09-10 10:01:57 -07001983 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001984 fprintf(s->fp, " %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001985 strbuf_release(&onebuf);
1986 }
1987}
1988
Jeff King3207a3a2012-05-07 15:44:44 -04001989static void wt_shortstatus_status(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001990 struct wt_status *s)
1991{
1992 struct wt_status_change_data *d = it->util;
1993
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001994 if (d->index_status)
1995 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1996 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02001997 fputc(' ', s->fp);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001998 if (d->worktree_status)
1999 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2000 else
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002001 fputc(' ', s->fp);
2002 fputc(' ', s->fp);
Jeff King3207a3a2012-05-07 15:44:44 -04002003 if (s->null_termination) {
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002004 fprintf(s->fp, "%s%c", it->string, 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002005 if (d->rename_source)
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002006 fprintf(s->fp, "%s%c", d->rename_source, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002007 } else {
2008 struct strbuf onebuf = STRBUF_INIT;
2009 const char *one;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002010
2011 if (d->rename_source) {
Junio C Hamanof3fc4a12020-09-10 10:01:55 -07002012 one = quote_path(d->rename_source, s->prefix, &onebuf,
2013 QUOTE_PATH_QUOTE_SP);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002014 fprintf(s->fp, "%s -> ", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002015 strbuf_release(&onebuf);
2016 }
Junio C Hamanof3fc4a12020-09-10 10:01:55 -07002017 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002018 fprintf(s->fp, "%s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002019 strbuf_release(&onebuf);
2020 }
2021}
2022
Jeff King3207a3a2012-05-07 15:44:44 -04002023static void wt_shortstatus_other(struct string_list_item *it,
Junio C Hamano2381e392010-04-10 00:33:17 -07002024 struct wt_status *s, const char *sign)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002025{
Jeff King3207a3a2012-05-07 15:44:44 -04002026 if (s->null_termination) {
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002027 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002028 } else {
2029 struct strbuf onebuf = STRBUF_INIT;
2030 const char *one;
Junio C Hamanoa361dd32020-09-10 10:01:57 -07002031 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
Junio C Hamanoc1909e72010-05-01 22:05:14 -07002032 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
Martin Ågren8f7e3de2020-09-27 15:15:42 +02002033 fprintf(s->fp, " %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002034 strbuf_release(&onebuf);
2035 }
2036}
2037
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002038static void wt_shortstatus_print_tracking(struct wt_status *s)
2039{
2040 struct branch *branch;
2041 const char *header_color = color(WT_STATUS_HEADER, s);
2042 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2043 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2044
2045 const char *base;
René Scharfe5e8d2722017-07-08 12:51:01 +02002046 char *short_base;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002047 const char *branch_name;
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002048 int num_ours, num_theirs, sti;
Jiang Xinf2e08732013-08-26 15:02:48 +08002049 int upstream_is_gone = 0;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002050
2051 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2052
2053 if (!s->branch)
2054 return;
2055 branch_name = s->branch;
2056
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01002057#define LABEL(string) (s->no_gettext ? (string) : _(string))
2058
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002059 if (s->is_initial)
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05302060 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
Jiang Xinf2e08732013-08-26 15:02:48 +08002061
René Scharfebaf0a3e2015-10-31 18:36:35 +01002062 if (!strcmp(s->branch, "HEAD")) {
2063 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01002064 LABEL(N_("HEAD (no branch)")));
René Scharfebaf0a3e2015-10-31 18:36:35 +01002065 goto conclude;
2066 }
2067
René Scharfec72b49d2015-10-31 18:37:43 +01002068 skip_prefix(branch_name, "refs/heads/", &branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01002069
René Scharfe8d8325f2015-10-31 18:37:12 +01002070 branch = branch_get(branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01002071
Jiang Xinf2e08732013-08-26 15:02:48 +08002072 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2073
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002074 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
Damien Robertc646d092019-04-16 14:16:46 +02002075 0, s->ahead_behind_flags);
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002076 if (sti < 0) {
René Scharfebcf8cc22015-10-31 18:36:01 +01002077 if (!base)
2078 goto conclude;
Jeff King979cb242015-05-21 20:49:11 -04002079
Jiang Xinf2e08732013-08-26 15:02:48 +08002080 upstream_is_gone = 1;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002081 }
2082
René Scharfe5e8d2722017-07-08 12:51:01 +02002083 short_base = shorten_unambiguous_ref(base, 0);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002084 color_fprintf(s->fp, header_color, "...");
René Scharfe5e8d2722017-07-08 12:51:01 +02002085 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2086 free(short_base);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002087
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002088 if (!upstream_is_gone && !sti)
René Scharfebcf8cc22015-10-31 18:36:01 +01002089 goto conclude;
Jiang Xinf2234592013-08-26 15:02:49 +08002090
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002091 color_fprintf(s->fp, header_color, " [");
Jiang Xinf2e08732013-08-26 15:02:48 +08002092 if (upstream_is_gone) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01002093 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
Jeff Hostetler3ca18972018-01-09 18:50:17 +00002094 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2095 color_fprintf(s->fp, header_color, LABEL(N_("different")));
Jiang Xinf2e08732013-08-26 15:02:48 +08002096 } else if (!num_ours) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01002097 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002098 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2099 } else if (!num_theirs) {
Michael J Gruberdf227242016-03-14 16:30:33 +01002100 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002101 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2102 } else {
Michael J Gruberdf227242016-03-14 16:30:33 +01002103 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002104 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
Matthieu Moy7a76c282014-03-20 13:12:41 +01002105 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002106 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2107 }
2108
Jeff Kinga5985232012-05-07 17:02:18 -04002109 color_fprintf(s->fp, header_color, "]");
René Scharfebcf8cc22015-10-31 18:36:01 +01002110 conclude:
Jeff Kinga5985232012-05-07 17:02:18 -04002111 fputc(s->null_termination ? '\0' : '\n', s->fp);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002112}
2113
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002114static void wt_shortstatus_print(struct wt_status *s)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002115{
Stefan Bellerd4aae452017-03-16 14:36:19 -07002116 struct string_list_item *it;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002117
Jeff Kingd4a6bf12012-05-07 17:09:04 -04002118 if (s->show_branch)
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02002119 wt_shortstatus_print_tracking(s);
2120
Stefan Bellerd4aae452017-03-16 14:36:19 -07002121 for_each_string_list_item(it, &s->change) {
2122 struct wt_status_change_data *d = it->util;
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002123
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002124 if (d->stagemask)
Jeff King3207a3a2012-05-07 15:44:44 -04002125 wt_shortstatus_unmerged(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002126 else
Jeff King3207a3a2012-05-07 15:44:44 -04002127 wt_shortstatus_status(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002128 }
Stefan Bellerd4aae452017-03-16 14:36:19 -07002129 for_each_string_list_item(it, &s->untracked)
Jeff King3207a3a2012-05-07 15:44:44 -04002130 wt_shortstatus_other(it, s, "??");
Junio C Hamano2381e392010-04-10 00:33:17 -07002131
Stefan Bellerd4aae452017-03-16 14:36:19 -07002132 for_each_string_list_item(it, &s->ignored)
Jeff King3207a3a2012-05-07 15:44:44 -04002133 wt_shortstatus_other(it, s, "!!");
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01002134}
Jeff King4a7cc2f2009-12-07 00:17:15 -05002135
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002136static void wt_porcelain_print(struct wt_status *s)
Jeff King4a7cc2f2009-12-07 00:17:15 -05002137{
2138 s->use_color = 0;
Jeff King86617682009-12-07 00:26:25 -05002139 s->relative_paths = 0;
2140 s->prefix = NULL;
Matthieu Moy7a76c282014-03-20 13:12:41 +01002141 s->no_gettext = 1;
Jeff Kingd4a6bf12012-05-07 17:09:04 -04002142 wt_shortstatus_print(s);
Jeff King4a7cc2f2009-12-07 00:17:15 -05002143}
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002144
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002145/*
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002146 * Print branch information for porcelain v2 output. These lines
2147 * are printed when the '--branch' parameter is given.
2148 *
2149 * # branch.oid <commit><eol>
2150 * # branch.head <head><eol>
2151 * [# branch.upstream <upstream><eol>
2152 * [# branch.ab +<ahead> -<behind><eol>]]
2153 *
Elijah Newren6d12b532020-07-28 20:45:38 +00002154 * <commit> ::= the current commit hash or the literal
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002155 * "(initial)" to indicate an initialized repo
2156 * with no commits.
2157 *
2158 * <head> ::= <branch_name> the current branch name or
2159 * "(detached)" literal when detached head or
2160 * "(unknown)" when something is wrong.
2161 *
2162 * <upstream> ::= the upstream branch name, when set.
2163 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002164 * <ahead> ::= integer ahead value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002165 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002166 * <behind> ::= integer behind value or '?'.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002167 *
2168 * The end-of-line is defined by the -z flag.
2169 *
2170 * <eol> ::= NUL when -z,
2171 * LF when NOT -z.
2172 *
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002173 * When an upstream is set and present, the 'branch.ab' line will
2174 * be printed with the ahead/behind counts for the branch and the
2175 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2176 * are different, '?' will be substituted for the actual count.
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002177 */
2178static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2179{
2180 struct branch *branch;
2181 const char *base;
2182 const char *branch_name;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002183 int ab_info, nr_ahead, nr_behind;
2184 char eol = s->null_termination ? '\0' : '\n';
2185
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002186 fprintf(s->fp, "# branch.oid %s%c",
brian m. carlsone0cb7cd2019-08-18 20:04:21 +00002187 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002188 eol);
2189
2190 if (!s->branch)
2191 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2192 else {
2193 if (!strcmp(s->branch, "HEAD")) {
2194 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2195
Stephen P. Smith73ba5d72018-09-30 07:12:45 -07002196 if (s->state.rebase_in_progress ||
2197 s->state.rebase_interactive_in_progress)
2198 branch_name = s->state.onto;
2199 else if (s->state.detached_from)
2200 branch_name = s->state.detached_from;
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002201 else
2202 branch_name = "";
2203 } else {
2204 branch_name = NULL;
2205 skip_prefix(s->branch, "refs/heads/", &branch_name);
2206
2207 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2208 }
2209
2210 /* Lookup stats on the upstream tracking branch, if set. */
2211 branch = branch_get(branch_name);
2212 base = NULL;
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002213 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
Damien Robertc646d092019-04-16 14:16:46 +02002214 &base, 0, s->ahead_behind_flags);
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002215 if (base) {
2216 base = shorten_unambiguous_ref(base, 0);
2217 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2218 free((char *)base);
2219
Jeff Hostetlerfd9b5442018-01-09 18:50:16 +00002220 if (ab_info > 0) {
2221 /* different */
2222 if (nr_ahead || nr_behind)
2223 fprintf(s->fp, "# branch.ab +%d -%d%c",
2224 nr_ahead, nr_behind, eol);
2225 else
2226 fprintf(s->fp, "# branch.ab +? -?%c",
2227 eol);
2228 } else if (!ab_info) {
2229 /* same */
2230 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2231 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002232 }
2233 }
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002234}
2235
2236/*
Øystein Walle2e59e782021-10-22 00:25:32 +02002237 * Print the stash count in a porcelain-friendly format
2238 */
2239static void wt_porcelain_v2_print_stash(struct wt_status *s)
2240{
2241 int stash_count = count_stash_entries();
2242 char eol = s->null_termination ? '\0' : '\n';
2243
2244 if (stash_count > 0)
2245 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2246}
2247
2248/*
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002249 * Convert various submodule status values into a
2250 * fixed-length string of characters in the buffer provided.
2251 */
2252static void wt_porcelain_v2_submodule_state(
2253 struct wt_status_change_data *d,
2254 char sub[5])
2255{
2256 if (S_ISGITLINK(d->mode_head) ||
2257 S_ISGITLINK(d->mode_index) ||
2258 S_ISGITLINK(d->mode_worktree)) {
2259 sub[0] = 'S';
2260 sub[1] = d->new_submodule_commits ? 'C' : '.';
2261 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2262 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2263 } else {
2264 sub[0] = 'N';
2265 sub[1] = '.';
2266 sub[2] = '.';
2267 sub[3] = '.';
2268 }
2269 sub[4] = 0;
2270}
2271
2272/*
2273 * Fix-up changed entries before we print them.
2274 */
Jeff King13a17812019-05-09 17:30:19 -04002275static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002276{
2277 struct wt_status_change_data *d = it->util;
2278
2279 if (!d->index_status) {
2280 /*
2281 * This entry is unchanged in the index (relative to the head).
2282 * Therefore, the collect_updated_cb was never called for this
2283 * entry (during the head-vs-index scan) and so the head column
2284 * fields were never set.
2285 *
2286 * We must have data for the index column (from the
2287 * index-vs-worktree scan (otherwise, this entry should not be
2288 * in the list of changes)).
2289 *
2290 * Copy index column fields to the head column, so that our
2291 * output looks complete.
2292 */
2293 assert(d->mode_head == 0);
2294 d->mode_head = d->mode_index;
2295 oidcpy(&d->oid_head, &d->oid_index);
2296 }
2297
2298 if (!d->worktree_status) {
2299 /*
2300 * This entry is unchanged in the worktree (relative to the index).
2301 * Therefore, the collect_changed_cb was never called for this entry
2302 * (during the index-vs-worktree scan) and so the worktree column
2303 * fields were never set.
2304 *
2305 * We must have data for the index column (from the head-vs-index
2306 * scan).
2307 *
2308 * Copy the index column fields to the worktree column so that
2309 * our output looks complete.
2310 *
2311 * Note that we only have a mode field in the worktree column
2312 * because the scan code tries really hard to not have to compute it.
2313 */
2314 assert(d->mode_worktree == 0);
2315 d->mode_worktree = d->mode_index;
2316 }
2317}
2318
2319/*
2320 * Print porcelain v2 info for tracked entries with changes.
2321 */
2322static void wt_porcelain_v2_print_changed_entry(
2323 struct string_list_item *it,
2324 struct wt_status *s)
2325{
2326 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002327 struct strbuf buf = STRBUF_INIT;
2328 struct strbuf buf_from = STRBUF_INIT;
2329 const char *path = NULL;
2330 const char *path_from = NULL;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002331 char key[3];
2332 char submodule_token[5];
2333 char sep_char, eol_char;
2334
Jeff King13a17812019-05-09 17:30:19 -04002335 wt_porcelain_v2_fix_up_changed(it);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002336 wt_porcelain_v2_submodule_state(d, submodule_token);
2337
2338 key[0] = d->index_status ? d->index_status : '.';
2339 key[1] = d->worktree_status ? d->worktree_status : '.';
2340 key[2] = 0;
2341
2342 if (s->null_termination) {
2343 /*
2344 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2345 * A single NUL character separates them.
2346 */
2347 sep_char = '\0';
2348 eol_char = '\0';
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002349 path = it->string;
2350 path_from = d->rename_source;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002351 } else {
2352 /*
2353 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2354 * The source path is only present when necessary.
2355 * A single TAB separates them (because paths can contain spaces
2356 * which are not escaped and C-quoting does escape TAB characters).
2357 */
2358 sep_char = '\t';
2359 eol_char = '\n';
Junio C Hamano88910c92020-09-10 10:01:54 -07002360 path = quote_path(it->string, s->prefix, &buf, 0);
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002361 if (d->rename_source)
Junio C Hamano88910c92020-09-10 10:01:54 -07002362 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002363 }
2364
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002365 if (path_from)
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002366 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2367 key, submodule_token,
2368 d->mode_head, d->mode_index, d->mode_worktree,
2369 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002370 d->rename_status, d->rename_score,
2371 path, sep_char, path_from, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002372 else
2373 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2374 key, submodule_token,
2375 d->mode_head, d->mode_index, d->mode_worktree,
2376 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002377 path, eol_char);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002378
Nguyễn Thái Ngọc Duy5134ccd2017-12-27 17:18:38 +07002379 strbuf_release(&buf);
2380 strbuf_release(&buf_from);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002381}
2382
2383/*
2384 * Print porcelain v2 status info for unmerged entries.
2385 */
2386static void wt_porcelain_v2_print_unmerged_entry(
2387 struct string_list_item *it,
2388 struct wt_status *s)
2389{
2390 struct wt_status_change_data *d = it->util;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002391 struct index_state *istate = s->repo->index;
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002392 const struct cache_entry *ce;
2393 struct strbuf buf_index = STRBUF_INIT;
2394 const char *path_index = NULL;
2395 int pos, stage, sum;
2396 struct {
2397 int mode;
2398 struct object_id oid;
2399 } stages[3];
2400 char *key;
2401 char submodule_token[5];
2402 char unmerged_prefix = 'u';
2403 char eol_char = s->null_termination ? '\0' : '\n';
2404
2405 wt_porcelain_v2_submodule_state(d, submodule_token);
2406
2407 switch (d->stagemask) {
2408 case 1: key = "DD"; break; /* both deleted */
2409 case 2: key = "AU"; break; /* added by us */
2410 case 3: key = "UD"; break; /* deleted by them */
2411 case 4: key = "UA"; break; /* added by them */
2412 case 5: key = "DU"; break; /* deleted by us */
2413 case 6: key = "AA"; break; /* both added */
2414 case 7: key = "UU"; break; /* both modified */
2415 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002416 BUG("unhandled unmerged status %x", d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002417 }
2418
2419 /*
2420 * Disregard d.aux.porcelain_v2 data that we accumulated
2421 * for the head and index columns during the scans and
2422 * replace with the actual stage data.
2423 *
2424 * Note that this is a last-one-wins for each the individual
2425 * stage [123] columns in the event of multiple cache entries
2426 * for same stage.
2427 */
2428 memset(stages, 0, sizeof(stages));
2429 sum = 0;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002430 pos = index_name_pos(istate, it->string, strlen(it->string));
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002431 assert(pos < 0);
2432 pos = -pos-1;
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002433 while (pos < istate->cache_nr) {
2434 ce = istate->cache[pos++];
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002435 stage = ce_stage(ce);
2436 if (strcmp(ce->name, it->string) || !stage)
2437 break;
2438 stages[stage - 1].mode = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +01002439 oidcpy(&stages[stage - 1].oid, &ce->oid);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002440 sum |= (1 << (stage - 1));
2441 }
2442 if (sum != d->stagemask)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002443 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002444
2445 if (s->null_termination)
2446 path_index = it->string;
2447 else
Junio C Hamano88910c92020-09-10 10:01:54 -07002448 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002449
2450 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2451 unmerged_prefix, key, submodule_token,
2452 stages[0].mode, /* stage 1 */
2453 stages[1].mode, /* stage 2 */
2454 stages[2].mode, /* stage 3 */
2455 d->mode_worktree,
2456 oid_to_hex(&stages[0].oid), /* stage 1 */
2457 oid_to_hex(&stages[1].oid), /* stage 2 */
2458 oid_to_hex(&stages[2].oid), /* stage 3 */
2459 path_index,
2460 eol_char);
2461
2462 strbuf_release(&buf_index);
2463}
2464
2465/*
2466 * Print porcelain V2 status info for untracked and ignored entries.
2467 */
2468static void wt_porcelain_v2_print_other(
2469 struct string_list_item *it,
2470 struct wt_status *s,
2471 char prefix)
2472{
2473 struct strbuf buf = STRBUF_INIT;
2474 const char *path;
2475 char eol_char;
2476
2477 if (s->null_termination) {
2478 path = it->string;
2479 eol_char = '\0';
2480 } else {
Junio C Hamano88910c92020-09-10 10:01:54 -07002481 path = quote_path(it->string, s->prefix, &buf, 0);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002482 eol_char = '\n';
2483 }
2484
2485 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2486
2487 strbuf_release(&buf);
2488}
2489
2490/*
2491 * Print porcelain V2 status.
2492 *
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002493 * [<v2_branch>]
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002494 * [<v2_changed_items>]*
2495 * [<v2_unmerged_items>]*
2496 * [<v2_untracked_items>]*
2497 * [<v2_ignored_items>]*
2498 *
2499 */
2500static void wt_porcelain_v2_print(struct wt_status *s)
2501{
2502 struct wt_status_change_data *d;
2503 struct string_list_item *it;
2504 int i;
2505
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002506 if (s->show_branch)
2507 wt_porcelain_v2_print_tracking(s);
2508
Øystein Walle2e59e782021-10-22 00:25:32 +02002509 if (s->show_stash)
2510 wt_porcelain_v2_print_stash(s);
2511
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002512 for (i = 0; i < s->change.nr; i++) {
2513 it = &(s->change.items[i]);
2514 d = it->util;
2515 if (!d->stagemask)
2516 wt_porcelain_v2_print_changed_entry(it, s);
2517 }
2518
2519 for (i = 0; i < s->change.nr; i++) {
2520 it = &(s->change.items[i]);
2521 d = it->util;
2522 if (d->stagemask)
2523 wt_porcelain_v2_print_unmerged_entry(it, s);
2524 }
2525
2526 for (i = 0; i < s->untracked.nr; i++) {
2527 it = &(s->untracked.items[i]);
2528 wt_porcelain_v2_print_other(it, s, '?');
2529 }
2530
2531 for (i = 0; i < s->ignored.nr; i++) {
2532 it = &(s->ignored.items[i]);
2533 wt_porcelain_v2_print_other(it, s, '!');
2534 }
2535}
2536
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002537void wt_status_print(struct wt_status *s)
2538{
Jeff Hostetler942b2742019-02-22 14:25:03 -08002539 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2540 trace2_data_intmax("status", s->repo, "count/untracked",
2541 s->untracked.nr);
2542 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2543
2544 trace2_region_enter("status", "print", s->repo);
2545
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002546 switch (s->status_format) {
2547 case STATUS_FORMAT_SHORT:
2548 wt_shortstatus_print(s);
2549 break;
2550 case STATUS_FORMAT_PORCELAIN:
2551 wt_porcelain_print(s);
2552 break;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002553 case STATUS_FORMAT_PORCELAIN_V2:
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002554 wt_porcelain_v2_print(s);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002555 break;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002556 case STATUS_FORMAT_UNSPECIFIED:
Johannes Schindelin033abf92018-05-02 11:38:39 +02002557 BUG("finalize_deferred_config() should have been called");
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002558 break;
2559 case STATUS_FORMAT_NONE:
2560 case STATUS_FORMAT_LONG:
2561 wt_longstatus_print(s);
2562 break;
2563 }
Jeff Hostetler942b2742019-02-22 14:25:03 -08002564
2565 trace2_region_leave("status", "print", s->repo);
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002566}
Johannes Schindelinfd849862016-10-07 18:08:38 +02002567
2568/**
2569 * Returns 1 if there are unstaged changes, 0 otherwise.
2570 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002571int has_unstaged_changes(struct repository *r, int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002572{
2573 struct rev_info rev_info;
2574 int result;
2575
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002576 repo_init_revisions(r, &rev_info, NULL);
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002577 if (ignore_submodules) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002578 rev_info.diffopt.flags.ignore_submodules = 1;
Junio C Hamanob50d82b2017-11-15 12:14:30 +09002579 rev_info.diffopt.flags.override_submodule_config = 1;
Brandon Williamsc6d8ccf2017-11-06 14:08:19 -08002580 }
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002581 rev_info.diffopt.flags.quick = 1;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002582 diff_setup_done(&rev_info.diffopt);
Jeff King25bd3ac2023-08-21 16:18:55 -04002583 run_diff_files(&rev_info, 0);
Jeff King5cc6b2d2023-08-21 16:20:46 -04002584 result = diff_result_code(&rev_info.diffopt);
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +02002585 release_revisions(&rev_info);
Ævar Arnfjörð Bjarmason54c8a7c2022-04-14 07:56:40 +02002586 return result;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002587}
2588
2589/**
2590 * Returns 1 if there are uncommitted changes, 0 otherwise.
2591 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002592int has_uncommitted_changes(struct repository *r,
2593 int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002594{
2595 struct rev_info rev_info;
2596 int result;
2597
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002598 if (is_index_unborn(r->index))
Johannes Schindelinfd849862016-10-07 18:08:38 +02002599 return 0;
2600
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002601 repo_init_revisions(r, &rev_info, NULL);
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002602 if (ignore_submodules)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002603 rev_info.diffopt.flags.ignore_submodules = 1;
2604 rev_info.diffopt.flags.quick = 1;
Jeff King3506dc92018-07-11 10:14:06 -04002605
Johannes Schindelinfd849862016-10-07 18:08:38 +02002606 add_head_to_pending(&rev_info);
Jeff King3506dc92018-07-11 10:14:06 -04002607 if (!rev_info.pending.nr) {
2608 /*
2609 * We have no head (or it's corrupt); use the empty tree,
2610 * which will complain if the index is non-empty.
2611 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002612 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
Jeff King3506dc92018-07-11 10:14:06 -04002613 add_pending_object(&rev_info, &tree->object, "");
2614 }
2615
Johannes Schindelinfd849862016-10-07 18:08:38 +02002616 diff_setup_done(&rev_info.diffopt);
Jeff King25bd3ac2023-08-21 16:18:55 -04002617 run_diff_index(&rev_info, DIFF_INDEX_CACHED);
Jeff King5cc6b2d2023-08-21 16:20:46 -04002618 result = diff_result_code(&rev_info.diffopt);
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +02002619 release_revisions(&rev_info);
Ævar Arnfjörð Bjarmason54c8a7c2022-04-14 07:56:40 +02002620 return result;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002621}
2622
2623/**
2624 * If the work tree has unstaged or uncommitted changes, dies with the
2625 * appropriate message.
2626 */
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002627int require_clean_work_tree(struct repository *r,
2628 const char *action,
2629 const char *hint,
2630 int ignore_submodules,
2631 int gently)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002632{
Martin Ågren837e34e2017-10-05 22:32:04 +02002633 struct lock_file lock_file = LOCK_INIT;
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002634 int err = 0, fd;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002635
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +07002636 fd = repo_hold_locked_index(r, &lock_file, 0);
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002637 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002638 if (0 <= fd)
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002639 repo_update_index_if_able(r, &lock_file);
Martin Ågren837e34e2017-10-05 22:32:04 +02002640 rollback_lock_file(&lock_file);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002641
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002642 if (has_unstaged_changes(r, ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002643 /* TRANSLATORS: the action is e.g. "pull with rebase" */
Johannes Schindelin4777e172016-10-07 18:09:04 +02002644 error(_("cannot %s: You have unstaged changes."), _(action));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002645 err = 1;
2646 }
2647
Nguyễn Thái Ngọc Duy5b02ca32018-11-10 06:48:49 +01002648 if (has_uncommitted_changes(r, ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002649 if (err)
Johannes Schindelin4777e172016-10-07 18:09:04 +02002650 error(_("additionally, your index contains uncommitted changes."));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002651 else
Johannes Schindelin4777e172016-10-07 18:09:04 +02002652 error(_("cannot %s: Your index contains uncommitted changes."),
Johannes Schindelinfd849862016-10-07 18:08:38 +02002653 _(action));
2654 err = 1;
2655 }
2656
2657 if (err) {
Oswald Buddenhagena9b59552023-08-24 17:00:46 +02002658 if (hint) {
2659 if (!*hint)
2660 BUG("empty hint passed to require_clean_work_tree();"
2661 " use NULL instead");
Johannes Schindelinfd849862016-10-07 18:08:38 +02002662 error("%s", hint);
Oswald Buddenhagena9b59552023-08-24 17:00:46 +02002663 }
Johannes Schindelinfd849862016-10-07 18:08:38 +02002664 if (!gently)
2665 exit(128);
2666 }
2667
2668 return err;
2669}