blob: 77c27c51134d2feff93befeee20f4523a37d524a [file] [log] [blame]
Junio C Hamano85023572006-12-19 14:34:12 -08001#include "cache.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04002#include "wt-status.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04003#include "object.h"
4#include "dir.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
8#include "diffcore.h"
Dmitry Potapova734d0b2008-03-07 05:30:58 +03009#include "quote.h"
Ping Yinac8d5af2008-04-12 23:05:32 +080010#include "run-command.h"
Matthieu Moybb7e32e2013-09-06 19:43:05 +020011#include "argv-array.h"
Junio C Hamanob6975ab2008-07-02 00:52:16 -070012#include "remote.h"
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020013#include "refs.h"
Jens Lehmann46a958b2010-06-25 16:56:47 +020014#include "submodule.h"
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +070015#include "column.h"
Lucien Kong2d1cceb2012-06-10 13:17:38 +020016#include "strbuf.h"
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +070017#include "utf8.h"
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +070018#include "worktree.h"
Johannes Schindelinfd849862016-10-07 18:08:38 +020019#include "lockfile.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -040020
Nguyễn Thái Ngọc Duy983dc692014-02-17 19:15:30 +070021static const char cut_line[] =
Jens Lehmann1a72cfd2013-12-05 20:44:14 +010022"------------------------ >8 ------------------------\n";
23
Junio C Hamano23900a92009-08-09 23:08:40 -070024static char default_wt_status_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010025 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
26 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
27 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
28 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
29 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
Junio C Hamano4d4d5722009-08-05 00:04:51 -070030 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +020031 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
32 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
Jeff King148135f2010-12-09 12:27:08 -050033 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
Jeff Kingc91f0d92006-09-08 04:05:34 -040034};
Junio C Hamano4d229652007-01-11 15:34:41 -080035
Junio C Hamanod249b092009-08-09 21:59:30 -070036static const char *color(int slot, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -040037{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070038 const char *c = "";
39 if (want_color(s->use_color))
40 c = s->color_palette[slot];
Jeff King148135f2010-12-09 12:27:08 -050041 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
42 c = s->color_palette[WT_STATUS_HEADER];
43 return c;
Jeff Kingc91f0d92006-09-08 04:05:34 -040044}
45
Jonathan Niederbecbdae2011-02-25 23:09:41 -060046static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
47 const char *fmt, va_list ap, const char *trail)
48{
49 struct strbuf sb = STRBUF_INIT;
50 struct strbuf linebuf = STRBUF_INIT;
51 const char *line, *eol;
52
53 strbuf_vaddf(&sb, fmt, ap);
54 if (!sb.len) {
Matthieu Moy2556b992013-09-06 19:43:07 +020055 if (s->display_comment_prefix) {
56 strbuf_addch(&sb, comment_line_char);
57 if (!trail)
58 strbuf_addch(&sb, ' ');
59 }
Jonathan Niederbecbdae2011-02-25 23:09:41 -060060 color_print_strbuf(s->fp, color, &sb);
61 if (trail)
62 fprintf(s->fp, "%s", trail);
63 strbuf_release(&sb);
64 return;
65 }
66 for (line = sb.buf; *line; line = eol + 1) {
67 eol = strchr(line, '\n');
68
69 strbuf_reset(&linebuf);
Matthieu Moy2556b992013-09-06 19:43:07 +020070 if (at_bol && s->display_comment_prefix) {
Junio C Hamanoeff80a92013-01-16 20:18:48 +010071 strbuf_addch(&linebuf, comment_line_char);
Jonathan Niederbecbdae2011-02-25 23:09:41 -060072 if (*line != '\n' && *line != '\t')
73 strbuf_addch(&linebuf, ' ');
74 }
75 if (eol)
76 strbuf_add(&linebuf, line, eol - line);
77 else
78 strbuf_addstr(&linebuf, line);
79 color_print_strbuf(s->fp, color, &linebuf);
80 if (eol)
81 fprintf(s->fp, "\n");
82 else
83 break;
84 at_bol = 1;
85 }
86 if (trail)
87 fprintf(s->fp, "%s", trail);
88 strbuf_release(&linebuf);
89 strbuf_release(&sb);
90}
91
92void status_printf_ln(struct wt_status *s, const char *color,
93 const char *fmt, ...)
94{
95 va_list ap;
96
97 va_start(ap, fmt);
98 status_vprintf(s, 1, color, fmt, ap, "\n");
99 va_end(ap);
100}
101
102void status_printf(struct wt_status *s, const char *color,
103 const char *fmt, ...)
104{
105 va_list ap;
106
107 va_start(ap, fmt);
108 status_vprintf(s, 1, color, fmt, ap, NULL);
109 va_end(ap);
110}
111
Junio C Hamano1e248452012-09-15 22:46:26 -0700112static void status_printf_more(struct wt_status *s, const char *color,
113 const char *fmt, ...)
Jonathan Niederbecbdae2011-02-25 23:09:41 -0600114{
115 va_list ap;
116
117 va_start(ap, fmt);
118 status_vprintf(s, 0, color, fmt, ap, NULL);
119 va_end(ap);
120}
121
Jeff Kingc91f0d92006-09-08 04:05:34 -0400122void wt_status_prepare(struct wt_status *s)
123{
brian m. carlson29283252017-02-21 23:47:29 +0000124 struct object_id oid;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400125
Junio C Hamanocc46a742007-02-09 16:22:42 -0800126 memset(s, 0, sizeof(*s));
Junio C Hamano23900a92009-08-09 23:08:40 -0700127 memcpy(s->color_palette, default_wt_status_colors,
128 sizeof(default_wt_status_colors));
Junio C Hamanod249b092009-08-09 21:59:30 -0700129 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
130 s->use_color = -1;
131 s->relative_paths = 1;
brian m. carlson29283252017-02-21 23:47:29 +0000132 s->branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400133 s->reference = "HEAD";
Kristian Høgsbergf26a0012007-09-17 20:06:42 -0400134 s->fp = stdout;
Kristian Høgsberg0f729f22007-09-17 20:06:43 -0400135 s->index_file = get_index_file();
Junio C Hamano50b7e702009-08-04 23:49:33 -0700136 s->change.strdup_strings = 1;
Junio C Hamano76378682009-08-10 00:36:33 -0700137 s->untracked.strdup_strings = 1;
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700138 s->ignored.strdup_strings = 1;
Junio C Hamano84b42022013-06-24 11:41:40 -0700139 s->show_branch = -1; /* unspecified */
Liam Beguinc1b5d012017-06-17 18:30:51 -0400140 s->show_stash = 0;
Matthieu Moy2556b992013-09-06 19:43:07 +0200141 s->display_comment_prefix = 0;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400142}
143
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400144static void wt_longstatus_print_unmerged_header(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700145{
Lucien Kong96b0ec12012-06-05 22:21:26 +0200146 int i;
147 int del_mod_conflict = 0;
148 int both_deleted = 0;
149 int not_deleted = 0;
Junio C Hamanod249b092009-08-09 21:59:30 -0700150 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800151
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000152 status_printf_ln(s, c, _("Unmerged paths:"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200153
154 for (i = 0; i < s->change.nr; i++) {
155 struct string_list_item *it = &(s->change.items[i]);
156 struct wt_status_change_data *d = it->util;
157
158 switch (d->stagemask) {
159 case 0:
160 break;
161 case 1:
162 both_deleted = 1;
163 break;
164 case 3:
165 case 5:
166 del_mod_conflict = 1;
167 break;
168 default:
169 not_deleted = 1;
170 break;
171 }
172 }
173
Matthieu Moy6a964f52013-09-12 12:50:05 +0200174 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400175 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500176 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800177 ;
178 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000179 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700180 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000181 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Lucien Kong96b0ec12012-06-05 22:21:26 +0200182
183 if (!both_deleted) {
184 if (!del_mod_conflict)
185 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
186 else
187 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
188 } else if (!del_mod_conflict && !not_deleted) {
189 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
190 } else {
191 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
192 }
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500193 status_printf_ln(s, c, "%s", "");
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700194}
195
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400196static void wt_longstatus_print_cached_header(struct wt_status *s)
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100197{
Junio C Hamanod249b092009-08-09 21:59:30 -0700198 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800199
Ævar Arnfjörð Bjarmason919a4ce2011-02-22 23:42:16 +0000200 status_printf_ln(s, c, _("Changes to be committed:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200201 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400202 return;
Jay Soffian37f7a852011-02-19 23:12:29 -0500203 if (s->whence != FROM_COMMIT)
Junio C Hamano3c588452009-12-11 23:53:41 -0800204 ; /* NEEDSWORK: use "git reset --unresolve"??? */
205 else if (!s->is_initial)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000206 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
Junio C Hamano3c588452009-12-11 23:53:41 -0800207 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000208 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500209 status_printf_ln(s, c, "%s", "");
Jürgen Rühle3c1eb9c2007-01-02 20:26:21 +0100210}
211
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400212static void wt_longstatus_print_dirty_header(struct wt_status *s,
213 int has_deleted,
214 int has_dirty_submodules)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400215{
Junio C Hamanod249b092009-08-09 21:59:30 -0700216 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano3c588452009-12-11 23:53:41 -0800217
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000218 status_printf_ln(s, c, _("Changes not staged for commit:"));
Matthieu Moy6a964f52013-09-12 12:50:05 +0200219 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400220 return;
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200221 if (!has_deleted)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000222 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200223 else
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000224 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
225 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100226 if (has_dirty_submodules)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000227 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500228 status_printf_ln(s, c, "%s", "");
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200229}
230
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400231static void wt_longstatus_print_other_header(struct wt_status *s,
232 const char *what,
233 const char *how)
Anders Melchiorsenbb914b12008-09-08 00:05:02 +0200234{
Junio C Hamanod249b092009-08-09 21:59:30 -0700235 const char *c = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +0700236 status_printf_ln(s, c, "%s:", what);
Matthieu Moy6a964f52013-09-12 12:50:05 +0200237 if (!s->hints)
Jeff Kingedf563f2009-09-09 07:43:03 -0400238 return;
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +0000239 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500240 status_printf_ln(s, c, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400241}
242
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400243static void wt_longstatus_print_trailer(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400244{
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500245 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400246}
247
Dmitry Potapova734d0b2008-03-07 05:30:58 +0300248#define quote_path quote_path_relative
Junio C Hamano3a946802006-11-08 13:20:46 -0800249
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800250static const char *wt_status_unmerged_status_string(int stagemask)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700251{
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800252 switch (stagemask) {
253 case 1:
254 return _("both deleted:");
255 case 2:
256 return _("added by us:");
257 case 3:
258 return _("deleted by them:");
259 case 4:
260 return _("added by them:");
261 case 5:
262 return _("deleted by us:");
263 case 6:
264 return _("both added:");
265 case 7:
266 return _("both modified:");
267 default:
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200268 die("BUG: unhandled unmerged status %x", stagemask);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700269 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700270}
271
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700272static const char *wt_status_diff_status_string(int status)
273{
274 switch (status) {
275 case DIFF_STATUS_ADDED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700276 return _("new file:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700277 case DIFF_STATUS_COPIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700278 return _("copied:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700279 case DIFF_STATUS_DELETED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700280 return _("deleted:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700281 case DIFF_STATUS_MODIFIED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700282 return _("modified:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700283 case DIFF_STATUS_RENAMED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700284 return _("renamed:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700285 case DIFF_STATUS_TYPE_CHANGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700286 return _("typechange:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700287 case DIFF_STATUS_UNKNOWN:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700288 return _("unknown:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700289 case DIFF_STATUS_UNMERGED:
Junio C Hamanod52cb572014-03-12 13:51:22 -0700290 return _("unmerged:");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700291 default:
292 return NULL;
293 }
294}
295
Jonathan Nieder335e8252013-12-19 11:43:19 -0800296static int maxwidth(const char *(*label)(int), int minval, int maxval)
297{
298 int result = 0, i;
299
300 for (i = minval; i <= maxval; i++) {
301 const char *s = label(i);
302 int len = s ? utf8_strwidth(s) : 0;
303 if (len > result)
304 result = len;
305 }
306 return result;
307}
308
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400309static void wt_longstatus_print_unmerged_data(struct wt_status *s,
310 struct string_list_item *it)
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800311{
312 const char *c = color(WT_STATUS_UNMERGED, s);
313 struct wt_status_change_data *d = it->util;
314 struct strbuf onebuf = STRBUF_INIT;
315 static char *padding;
316 static int label_width;
317 const char *one, *how;
318 int len;
319
320 if (!padding) {
321 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
322 label_width += strlen(" ");
Jonathan Nieder8f17f5b2013-12-19 11:43:19 -0800323 padding = xmallocz(label_width);
324 memset(padding, ' ', label_width);
325 }
326
327 one = quote_path(it->string, s->prefix, &onebuf);
328 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
329
330 how = wt_status_unmerged_status_string(d->stagemask);
331 len = label_width - utf8_strwidth(how);
332 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
333 strbuf_release(&onebuf);
334}
335
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400336static void wt_longstatus_print_change_data(struct wt_status *s,
337 int change_type,
338 struct string_list_item *it)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400339{
Junio C Hamano50b7e702009-08-04 23:49:33 -0700340 struct wt_status_change_data *d = it->util;
Junio C Hamanod249b092009-08-09 21:59:30 -0700341 const char *c = color(change_type, s);
Jeff Kingb8527d52013-03-21 07:05:28 -0400342 int status;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700343 char *one_name;
344 char *two_name;
Junio C Hamano3a946802006-11-08 13:20:46 -0800345 const char *one, *two;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500346 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100347 struct strbuf extra = STRBUF_INIT;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700348 static char *padding;
Junio C Hamanod52cb572014-03-12 13:51:22 -0700349 static int label_width;
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700350 const char *what;
351 int len;
352
353 if (!padding) {
Jonathan Nieder335e8252013-12-19 11:43:19 -0800354 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
355 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
Junio C Hamanod52cb572014-03-12 13:51:22 -0700356 label_width += strlen(" ");
357 padding = xmallocz(label_width);
358 memset(padding, ' ', label_width);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700359 }
Junio C Hamano3a946802006-11-08 13:20:46 -0800360
Junio C Hamano50b7e702009-08-04 23:49:33 -0700361 one_name = two_name = it->string;
362 switch (change_type) {
363 case WT_STATUS_UPDATED:
364 status = d->index_status;
365 if (d->head_path)
366 one_name = d->head_path;
367 break;
368 case WT_STATUS_CHANGED:
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100369 if (d->new_submodule_commits || d->dirty_submodule) {
370 strbuf_addstr(&extra, " (");
371 if (d->new_submodule_commits)
René Scharfea22ae752016-09-15 20:31:00 +0200372 strbuf_addstr(&extra, _("new commits, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100373 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
René Scharfea22ae752016-09-15 20:31:00 +0200374 strbuf_addstr(&extra, _("modified content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100375 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
René Scharfea22ae752016-09-15 20:31:00 +0200376 strbuf_addstr(&extra, _("untracked content, "));
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100377 strbuf_setlen(&extra, extra.len - 2);
378 strbuf_addch(&extra, ')');
379 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700380 status = d->worktree_status;
381 break;
Jeff Kingb8527d52013-03-21 07:05:28 -0400382 default:
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400383 die("BUG: unhandled change_type %d in wt_longstatus_print_change_data",
Jeff Kingb8527d52013-03-21 07:05:28 -0400384 change_type);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700385 }
386
Jiang Xin39598f92013-06-25 23:53:45 +0800387 one = quote_path(one_name, s->prefix, &onebuf);
388 two = quote_path(two_name, s->prefix, &twobuf);
Junio C Hamano3a946802006-11-08 13:20:46 -0800389
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600390 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700391 what = wt_status_diff_status_string(status);
392 if (!what)
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200393 die("BUG: unhandled diff status %c", status);
Junio C Hamanod52cb572014-03-12 13:51:22 -0700394 len = label_width - utf8_strwidth(what);
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700395 assert(len >= 0);
396 if (status == DIFF_STATUS_COPIED || status == DIFF_STATUS_RENAMED)
Junio C Hamanod52cb572014-03-12 13:51:22 -0700397 status_printf_more(s, c, "%s%.*s%s -> %s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700398 what, len, padding, one, two);
399 else
Junio C Hamanod52cb572014-03-12 13:51:22 -0700400 status_printf_more(s, c, "%s%.*s%s",
Nguyễn Thái Ngọc Duy3651e452013-11-05 09:07:29 +0700401 what, len, padding, one);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100402 if (extra.len) {
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600403 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100404 strbuf_release(&extra);
405 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600406 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
Johannes Schindelin367c9882007-11-11 17:35:41 +0000407 strbuf_release(&onebuf);
408 strbuf_release(&twobuf);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400409}
410
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700411static char short_submodule_status(struct wt_status_change_data *d) {
412 if (d->new_submodule_commits)
413 return 'M';
414 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
415 return 'm';
416 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
417 return '?';
418 return d->worktree_status;
419}
420
Junio C Hamano50b7e702009-08-04 23:49:33 -0700421static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
422 struct diff_options *options,
423 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400424{
425 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400426 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700427
428 if (!q->nr)
429 return;
430 s->workdir_dirty = 1;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400431 for (i = 0; i < q->nr; i++) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700432 struct diff_filepair *p;
433 struct string_list_item *it;
434 struct wt_status_change_data *d;
435
436 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100437 it = string_list_insert(&s->change, p->one->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700438 d = it->util;
439 if (!d) {
440 d = xcalloc(1, sizeof(*d));
441 it->util = d;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400442 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700443 if (!d->worktree_status)
444 d->worktree_status = p->status;
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700445 if (S_ISGITLINK(p->two->mode)) {
446 d->dirty_submodule = p->two->dirty_submodule;
brian m. carlsona0d12c42016-06-24 23:09:23 +0000447 d->new_submodule_commits = !!oidcmp(&p->one->oid,
448 &p->two->oid);
Stefan Bellerdd6962d2017-03-29 15:26:15 -0700449 if (s->status_format == STATUS_FORMAT_SHORT)
450 d->worktree_status = short_submodule_status(d);
451 }
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400452
453 switch (p->status) {
454 case DIFF_STATUS_ADDED:
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700455 d->mode_worktree = p->two->mode;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400456 break;
457
458 case DIFF_STATUS_DELETED:
459 d->mode_index = p->one->mode;
460 oidcpy(&d->oid_index, &p->one->oid);
461 /* mode_worktree is zero for a delete. */
462 break;
463
464 case DIFF_STATUS_MODIFIED:
465 case DIFF_STATUS_TYPE_CHANGED:
466 case DIFF_STATUS_UNMERGED:
467 d->mode_index = p->one->mode;
468 d->mode_worktree = p->two->mode;
469 oidcpy(&d->oid_index, &p->one->oid);
470 break;
471
472 case DIFF_STATUS_UNKNOWN:
473 die("BUG: worktree status unknown???");
474 break;
475 }
476
Jeff Kingc91f0d92006-09-08 04:05:34 -0400477 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400478}
479
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700480static int unmerged_mask(const char *path)
481{
482 int pos, mask;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700483 const struct cache_entry *ce;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700484
485 pos = cache_name_pos(path, strlen(path));
486 if (0 <= pos)
487 return 0;
488
489 mask = 0;
490 pos = -pos-1;
491 while (pos < active_nr) {
492 ce = active_cache[pos++];
493 if (strcmp(ce->name, path) || !ce_stage(ce))
494 break;
495 mask |= (1 << (ce_stage(ce) - 1));
496 }
497 return mask;
498}
499
Junio C Hamano50b7e702009-08-04 23:49:33 -0700500static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
501 struct diff_options *options,
502 void *data)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400503{
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100504 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400505 int i;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700506
507 for (i = 0; i < q->nr; i++) {
508 struct diff_filepair *p;
509 struct string_list_item *it;
510 struct wt_status_change_data *d;
511
512 p = q->queue[i];
Julian Phillips78a395d2010-06-26 00:41:35 +0100513 it = string_list_insert(&s->change, p->two->path);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700514 d = it->util;
515 if (!d) {
516 d = xcalloc(1, sizeof(*d));
517 it->util = d;
518 }
519 if (!d->index_status)
520 d->index_status = p->status;
521 switch (p->status) {
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400522 case DIFF_STATUS_ADDED:
523 /* Leave {mode,oid}_head zero for an add. */
524 d->mode_index = p->two->mode;
525 oidcpy(&d->oid_index, &p->two->oid);
526 break;
527 case DIFF_STATUS_DELETED:
528 d->mode_head = p->one->mode;
529 oidcpy(&d->oid_head, &p->one->oid);
530 /* Leave {mode,oid}_index zero for a delete. */
531 break;
532
Junio C Hamano50b7e702009-08-04 23:49:33 -0700533 case DIFF_STATUS_COPIED:
534 case DIFF_STATUS_RENAMED:
535 d->head_path = xstrdup(p->one->path);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400536 d->score = p->score * 100 / MAX_SCORE;
537 /* fallthru */
538 case DIFF_STATUS_MODIFIED:
539 case DIFF_STATUS_TYPE_CHANGED:
540 d->mode_head = p->one->mode;
541 d->mode_index = p->two->mode;
542 oidcpy(&d->oid_head, &p->one->oid);
543 oidcpy(&d->oid_index, &p->two->oid);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700544 break;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700545 case DIFF_STATUS_UNMERGED:
546 d->stagemask = unmerged_mask(p->two->path);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400547 /*
548 * Don't bother setting {mode,oid}_{head,index} since the print
549 * code will output the stage values directly and not use the
550 * values in these fields.
551 */
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700552 break;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700553 }
Jürgen Rühle6e458bf2007-01-02 20:26:22 +0100554 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400555}
556
Junio C Hamano50b7e702009-08-04 23:49:33 -0700557static void wt_status_collect_changes_worktree(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400558{
559 struct rev_info rev;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700560
561 init_revisions(&rev, NULL);
562 setup_revisions(0, NULL, &rev, NULL);
563 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100564 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700565 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100566 if (!s->show_untracked_files)
567 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200568 if (s->ignore_submodule_arg) {
569 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
Jens Lehmann46a958b2010-06-25 16:56:47 +0200570 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Nguyễn Thái Ngọc Duyc4c42f22011-10-24 15:24:51 +1100571 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700572 rev.diffopt.format_callback = wt_status_collect_changed_cb;
573 rev.diffopt.format_callback_data = s;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700574 copy_pathspec(&rev.prune_data, &s->pathspec);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700575 run_diff_files(&rev, 0);
576}
577
578static void wt_status_collect_changes_index(struct wt_status *s)
579{
580 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800581 struct setup_revision_opt opt;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700582
Jeff Kingc91f0d92006-09-08 04:05:34 -0400583 init_revisions(&rev, NULL);
Junio C Hamano32962c92010-03-08 22:58:09 -0800584 memset(&opt, 0, sizeof(opt));
585 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
586 setup_revisions(0, NULL, &rev, &opt);
587
Jens Lehmann1d2f3932014-04-05 18:59:03 +0200588 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700589 rev.diffopt.ita_invisible_in_index = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200590 if (s->ignore_submodule_arg) {
Jens Lehmann46a958b2010-06-25 16:56:47 +0200591 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
Jens Lehmann1d2f3932014-04-05 18:59:03 +0200592 } else {
593 /*
594 * Unless the user did explicitly request a submodule ignore
595 * mode by passing a command line option we do not ignore any
596 * changed submodule SHA-1s when comparing index and HEAD, no
597 * matter what is configured. Otherwise the user won't be
598 * shown any submodules she manually added (and which are
599 * staged to be committed), which would be really confusing.
600 */
601 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200602 }
Jens Lehmann46a958b2010-06-25 16:56:47 +0200603
Jeff Kingc91f0d92006-09-08 04:05:34 -0400604 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700605 rev.diffopt.format_callback = wt_status_collect_updated_cb;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400606 rev.diffopt.format_callback_data = s;
607 rev.diffopt.detect_rename = 1;
Jeff King50705912008-04-30 13:24:43 -0400608 rev.diffopt.rename_limit = 200;
Jeff Kingf714fb82007-12-02 22:58:37 -0800609 rev.diffopt.break_opt = 0;
Nguyễn Thái Ngọc Duy15b55ae2013-07-14 15:35:39 +0700610 copy_pathspec(&rev.prune_data, &s->pathspec);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400611 run_diff_index(&rev, 1);
612}
613
Junio C Hamano50b7e702009-08-04 23:49:33 -0700614static void wt_status_collect_changes_initial(struct wt_status *s)
615{
616 int i;
617
618 for (i = 0; i < active_nr; i++) {
619 struct string_list_item *it;
620 struct wt_status_change_data *d;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700621 const struct cache_entry *ce = active_cache[i];
Junio C Hamano50b7e702009-08-04 23:49:33 -0700622
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +0700623 if (!ce_path_match(ce, &s->pathspec, NULL))
Junio C Hamano76e2f7c2009-08-07 23:31:57 -0700624 continue;
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700625 if (ce_intent_to_add(ce))
626 continue;
Julian Phillips78a395d2010-06-26 00:41:35 +0100627 it = string_list_insert(&s->change, ce->name);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700628 d = it->util;
629 if (!d) {
630 d = xcalloc(1, sizeof(*d));
631 it->util = d;
632 }
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700633 if (ce_stage(ce)) {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700634 d->index_status = DIFF_STATUS_UNMERGED;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700635 d->stagemask |= (1 << (ce_stage(ce) - 1));
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400636 /*
637 * Don't bother setting {mode,oid}_{head,index} since the print
638 * code will output the stage values directly and not use the
639 * values in these fields.
640 */
641 } else {
Junio C Hamano50b7e702009-08-04 23:49:33 -0700642 d->index_status = DIFF_STATUS_ADDED;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400643 /* Leave {mode,oid}_head zero for adds. */
644 d->mode_index = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +0100645 oidcpy(&d->oid_index, &ce->oid);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -0400646 }
Junio C Hamano50b7e702009-08-04 23:49:33 -0700647 }
648}
649
Junio C Hamano76378682009-08-10 00:36:33 -0700650static void wt_status_collect_untracked(struct wt_status *s)
651{
652 int i;
653 struct dir_struct dir;
Karsten Blees132d41e2014-07-12 02:07:36 +0200654 uint64_t t_begin = getnanotime();
Junio C Hamano76378682009-08-10 00:36:33 -0700655
656 if (!s->show_untracked_files)
657 return;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700658
Junio C Hamano76378682009-08-10 00:36:33 -0700659 memset(&dir, 0, sizeof(dir));
660 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
661 dir.flags |=
662 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200663 if (s->show_ignored_files)
664 dir.flags |= DIR_SHOW_IGNORED_TOO;
Nguyễn Thái Ngọc Duy226c0512015-03-08 17:12:41 +0700665 else
666 dir.untracked = the_index.untracked;
Junio C Hamano76378682009-08-10 00:36:33 -0700667 setup_standard_excludes(&dir);
668
Brandon Williams0d32c182017-05-05 12:53:34 -0700669 fill_directory(&dir, &the_index, &s->pathspec);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200670
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400671 for (i = 0; i < dir.nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700672 struct dir_entry *ent = dir.entries[i];
Brandon Caseyb8224232010-09-26 21:49:13 -0500673 if (cache_name_is_other(ent->name, ent->len) &&
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700674 dir_path_match(ent, &s->pathspec, 0, NULL))
Brandon Caseyb8224232010-09-26 21:49:13 -0500675 string_list_insert(&s->untracked, ent->name);
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700676 free(ent);
Junio C Hamano76378682009-08-10 00:36:33 -0700677 }
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700678
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200679 for (i = 0; i < dir.ignored_nr; i++) {
680 struct dir_entry *ent = dir.ignored[i];
681 if (cache_name_is_other(ent->name, ent->len) &&
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700682 dir_path_match(ent, &s->pathspec, 0, NULL))
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200683 string_list_insert(&s->ignored, ent->name);
684 free(ent);
Junio C Hamano6cb3f6b2010-04-10 00:11:53 -0700685 }
686
Junio C Hamanof5b26b12010-04-09 23:58:27 -0700687 free(dir.entries);
Karsten Blees0aaf62b2013-04-15 21:15:03 +0200688 free(dir.ignored);
689 clear_directory(&dir);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +0700690
Karsten Blees132d41e2014-07-12 02:07:36 +0200691 if (advice_status_u_option)
692 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
Junio C Hamano76378682009-08-10 00:36:33 -0700693}
694
695void wt_status_collect(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700696{
697 wt_status_collect_changes_worktree(s);
698
699 if (s->is_initial)
700 wt_status_collect_changes_initial(s);
701 else
702 wt_status_collect_changes_index(s);
Junio C Hamano76378682009-08-10 00:36:33 -0700703 wt_status_collect_untracked(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700704}
705
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400706static void wt_longstatus_print_unmerged(struct wt_status *s)
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700707{
708 int shown_header = 0;
709 int i;
710
711 for (i = 0; i < s->change.nr; i++) {
712 struct wt_status_change_data *d;
713 struct string_list_item *it;
714 it = &(s->change.items[i]);
715 d = it->util;
716 if (!d->stagemask)
717 continue;
718 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400719 wt_longstatus_print_unmerged_header(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700720 shown_header = 1;
721 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400722 wt_longstatus_print_unmerged_data(s, it);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700723 }
724 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400725 wt_longstatus_print_trailer(s);
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700726
727}
728
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400729static void wt_longstatus_print_updated(struct wt_status *s)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700730{
731 int shown_header = 0;
732 int i;
733
734 for (i = 0; i < s->change.nr; i++) {
735 struct wt_status_change_data *d;
736 struct string_list_item *it;
737 it = &(s->change.items[i]);
738 d = it->util;
739 if (!d->index_status ||
740 d->index_status == DIFF_STATUS_UNMERGED)
741 continue;
742 if (!shown_header) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400743 wt_longstatus_print_cached_header(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700744 s->commitable = 1;
745 shown_header = 1;
746 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400747 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700748 }
749 if (shown_header)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400750 wt_longstatus_print_trailer(s);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700751}
752
753/*
754 * -1 : has delete
755 * 0 : no change
756 * 1 : some change but no delete
757 */
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100758static int wt_status_check_worktree_changes(struct wt_status *s,
759 int *dirty_submodules)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700760{
761 int i;
762 int changes = 0;
763
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100764 *dirty_submodules = 0;
765
Junio C Hamano50b7e702009-08-04 23:49:33 -0700766 for (i = 0; i < s->change.nr; i++) {
767 struct wt_status_change_data *d;
768 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700769 if (!d->worktree_status ||
770 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700771 continue;
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100772 if (!changes)
773 changes = 1;
774 if (d->dirty_submodule)
775 *dirty_submodules = 1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700776 if (d->worktree_status == DIFF_STATUS_DELETED)
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100777 changes = -1;
Junio C Hamano50b7e702009-08-04 23:49:33 -0700778 }
779 return changes;
780}
781
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400782static void wt_longstatus_print_changed(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400783{
Jens Lehmann9297f77e62010-03-08 13:53:19 +0100784 int i, dirty_submodules;
785 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700786
787 if (!worktree_changes)
788 return;
789
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400790 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700791
792 for (i = 0; i < s->change.nr; i++) {
793 struct wt_status_change_data *d;
794 struct string_list_item *it;
795 it = &(s->change.items[i]);
796 d = it->util;
Junio C Hamano4d4d5722009-08-05 00:04:51 -0700797 if (!d->worktree_status ||
798 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-04 23:49:33 -0700799 continue;
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400800 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
Junio C Hamano50b7e702009-08-04 23:49:33 -0700801 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400802 wt_longstatus_print_trailer(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400803}
804
Liam Beguinc1b5d012017-06-17 18:30:51 -0400805static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
806 const char *email, timestamp_t timestamp, int tz,
807 const char *message, void *cb_data)
808{
809 int *c = cb_data;
810 (*c)++;
811 return 0;
812}
813
814static void wt_longstatus_print_stash_summary(struct wt_status *s)
815{
816 int stash_count = 0;
817
818 for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
819 if (stash_count > 0)
820 status_printf_ln(s, GIT_COLOR_NORMAL,
821 Q_("Your stash currently has %d entry",
822 "Your stash currently has %d entries", stash_count),
823 stash_count);
824}
825
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400826static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
Ping Yinac8d5af2008-04-12 23:05:32 +0800827{
René Scharfed3180272014-08-19 21:09:35 +0200828 struct child_process sm_summary = CHILD_PROCESS_INIT;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200829 struct strbuf cmd_stdout = STRBUF_INIT;
830 struct strbuf summary = STRBUF_INIT;
831 char *summary_content;
Ping Yinac8d5af2008-04-12 23:05:32 +0800832
René Scharfea9154592014-10-19 13:14:20 +0200833 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
834 s->index_file);
Ping Yinac8d5af2008-04-12 23:05:32 +0800835
René Scharfea2bae2d2014-11-09 14:49:54 +0100836 argv_array_push(&sm_summary.args, "submodule");
837 argv_array_push(&sm_summary.args, "summary");
838 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
839 argv_array_push(&sm_summary.args, "--for-status");
840 argv_array_push(&sm_summary.args, "--summary-limit");
841 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200842 if (!uncommitted)
René Scharfea2bae2d2014-11-09 14:49:54 +0100843 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
Matthieu Moybb7e32e2013-09-06 19:43:05 +0200844
Ping Yinac8d5af2008-04-12 23:05:32 +0800845 sm_summary.git_cmd = 1;
846 sm_summary.no_stdin = 1;
Matthieu Moy3ba74072013-09-06 19:43:06 +0200847
Jeff King5c950e92015-03-22 23:53:52 -0400848 capture_command(&sm_summary, &cmd_stdout, 1024);
Matthieu Moy3ba74072013-09-06 19:43:06 +0200849
850 /* prepend header, only if there's an actual output */
Jeff Kingd56d9662015-03-22 06:00:32 -0400851 if (cmd_stdout.len) {
Matthieu Moy3ba74072013-09-06 19:43:06 +0200852 if (uncommitted)
853 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
854 else
855 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
856 strbuf_addstr(&summary, "\n\n");
857 }
858 strbuf_addbuf(&summary, &cmd_stdout);
859 strbuf_release(&cmd_stdout);
860
Matthieu Moy2556b992013-09-06 19:43:07 +0200861 if (s->display_comment_prefix) {
Jeff Kingd56d9662015-03-22 06:00:32 -0400862 size_t len;
Matthieu Moy2556b992013-09-06 19:43:07 +0200863 summary_content = strbuf_detach(&summary, &len);
864 strbuf_add_commented_lines(&summary, summary_content, len);
865 free(summary_content);
866 }
Matthieu Moy3ba74072013-09-06 19:43:06 +0200867
868 fputs(summary.buf, s->fp);
869 strbuf_release(&summary);
Ping Yinac8d5af2008-04-12 23:05:32 +0800870}
871
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400872static void wt_longstatus_print_other(struct wt_status *s,
873 struct string_list *l,
874 const char *what,
875 const char *how)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400876{
Jeff Kingc91f0d92006-09-08 04:05:34 -0400877 int i;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500878 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700879 static struct string_list output = STRING_LIST_INIT_DUP;
880 struct column_options copts;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400881
Jeff King12829882011-06-02 01:54:49 -0400882 if (!l->nr)
Junio C Hamano76378682009-08-10 00:36:33 -0700883 return;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400884
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400885 wt_longstatus_print_other_header(s, what, how);
Junio C Hamano1b908b62010-04-10 00:19:46 -0700886
887 for (i = 0; i < l->nr; i++) {
Junio C Hamano76378682009-08-10 00:36:33 -0700888 struct string_list_item *it;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700889 const char *path;
Junio C Hamano1b908b62010-04-10 00:19:46 -0700890 it = &(l->items[i]);
Jiang Xin39598f92013-06-25 23:53:45 +0800891 path = quote_path(it->string, s->prefix, &buf);
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700892 if (column_active(s->colopts)) {
893 string_list_append(&output, path);
894 continue;
895 }
Jonathan Niederb926c0d2011-02-25 23:11:37 -0600896 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
897 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700898 "%s\n", path);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400899 }
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700900
901 strbuf_release(&buf);
902 if (!column_active(s->colopts))
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200903 goto conclude;
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700904
Matthieu Moy2556b992013-09-06 19:43:07 +0200905 strbuf_addf(&buf, "%s%s\t%s",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700906 color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +0200907 s->display_comment_prefix ? "#" : "",
Nguyễn Thái Ngọc Duy323d0532012-04-13 17:54:39 +0700908 color(WT_STATUS_UNTRACKED, s));
909 memset(&copts, 0, sizeof(copts));
910 copts.padding = 1;
911 copts.indent = buf.buf;
912 if (want_color(s->use_color))
913 copts.nl = GIT_COLOR_RESET "\n";
914 print_columns(&output, s->colopts, &copts);
915 string_list_clear(&output, 0);
Johannes Schindelin367c9882007-11-11 17:35:41 +0000916 strbuf_release(&buf);
Matthieu Moy2f0f7f12013-09-06 19:43:09 +0200917conclude:
Felipe Contreras7d7d6802014-05-04 01:12:55 -0500918 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -0400919}
920
Brian Malehornd76650b2017-05-15 23:06:49 -0700921size_t wt_status_locate_end(const char *s, size_t len)
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100922{
923 const char *p;
924 struct strbuf pattern = STRBUF_INIT;
925
SZEDER Gáborfbfa0972015-06-09 02:28:34 +0200926 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
Brian Malehornd76650b2017-05-15 23:06:49 -0700927 if (starts_with(s, pattern.buf + 1))
928 len = 0;
929 else if ((p = strstr(s, pattern.buf)))
930 len = p - s + 1;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100931 strbuf_release(&pattern);
Brian Malehornd76650b2017-05-15 23:06:49 -0700932 return len;
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100933}
934
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +0700935void wt_status_add_cut_line(FILE *fp)
936{
937 const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
938 struct strbuf buf = STRBUF_INIT;
939
940 fprintf(fp, "%c %s", comment_line_char, cut_line);
941 strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
942 fputs(buf.buf, fp);
943 strbuf_release(&buf);
944}
945
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400946static void wt_longstatus_print_verbose(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400947{
948 struct rev_info rev;
Junio C Hamano32962c92010-03-08 22:58:09 -0800949 struct setup_revision_opt opt;
Michael J Gruber40555002015-03-06 10:43:35 +0100950 int dirty_submodules;
951 const char *c = color(WT_STATUS_HEADER, s);
Kristian Høgsberg99a12692007-11-21 21:54:49 -0500952
Jeff Kingc91f0d92006-09-08 04:05:34 -0400953 init_revisions(&rev, NULL);
Jeff King5ec11af2008-12-07 21:54:17 -0500954 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
Nguyễn Thái Ngọc Duy425a28e2016-10-24 17:42:19 +0700955 rev.diffopt.ita_invisible_in_index = 1;
Junio C Hamano32962c92010-03-08 22:58:09 -0800956
957 memset(&opt, 0, sizeof(opt));
958 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
959 setup_revisions(0, NULL, &rev, &opt);
960
Jeff Kingc91f0d92006-09-08 04:05:34 -0400961 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
962 rev.diffopt.detect_rename = 1;
Kristian Høgsberg4ba0cb22008-03-10 13:58:26 -0400963 rev.diffopt.file = s->fp;
964 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 00:49:35 -0400965 /*
966 * If we're not going to stdout, then we definitely don't
967 * want color, since we are going to the commit message
968 * file (and even the "auto" setting won't work, since it
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100969 * will have checked isatty on stdout). But we then do want
970 * to insert the scissor line here to reliably remove the
971 * diff before committing.
Jeff King4f672ad2008-10-26 00:49:35 -0400972 */
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100973 if (s->fp != stdout) {
Jeff Kingf1c96262011-08-17 22:03:12 -0700974 rev.diffopt.use_color = 0;
Nguyễn Thái Ngọc Duyfcef9312014-02-17 19:15:31 +0700975 wt_status_add_cut_line(s->fp);
Jens Lehmann1a72cfd2013-12-05 20:44:14 +0100976 }
Michael J Gruber40555002015-03-06 10:43:35 +0100977 if (s->verbose > 1 && s->commitable) {
978 /* print_updated() printed a header, so do we */
979 if (s->fp != stdout)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400980 wt_longstatus_print_trailer(s);
Michael J Gruber40555002015-03-06 10:43:35 +0100981 status_printf_ln(s, c, _("Changes to be committed:"));
982 rev.diffopt.a_prefix = "c/";
983 rev.diffopt.b_prefix = "i/";
984 } /* else use prefix as per user config */
Jeff Kingc91f0d92006-09-08 04:05:34 -0400985 run_diff_index(&rev, 1);
Michael J Gruber40555002015-03-06 10:43:35 +0100986 if (s->verbose > 1 &&
987 wt_status_check_worktree_changes(s, &dirty_submodules)) {
988 status_printf_ln(s, c,
989 "--------------------------------------------------");
990 status_printf_ln(s, c, _("Changes not staged for commit:"));
991 setup_work_tree();
992 rev.diffopt.a_prefix = "i/";
993 rev.diffopt.b_prefix = "w/";
994 run_diff_files(&rev, 0);
995 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400996}
997
Jeff Hostetler957a0fe2016-08-05 18:00:26 -0400998static void wt_longstatus_print_tracking(struct wt_status *s)
Junio C Hamanob6975ab2008-07-02 00:52:16 -0700999{
1000 struct strbuf sb = STRBUF_INIT;
René Scharfec72b49d2015-10-31 18:37:43 +01001001 const char *cp, *ep, *branch_name;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001002 struct branch *branch;
Matthieu Moy2556b992013-09-06 19:43:07 +02001003 char comment_line_string[3];
1004 int i;
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001005
1006 assert(s->branch && !s->is_initial);
René Scharfec72b49d2015-10-31 18:37:43 +01001007 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001008 return;
René Scharfec72b49d2015-10-31 18:37:43 +01001009 branch = branch_get(branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001010 if (!format_tracking_info(branch, &sb))
1011 return;
1012
Matthieu Moy2556b992013-09-06 19:43:07 +02001013 i = 0;
1014 if (s->display_comment_prefix) {
1015 comment_line_string[i++] = comment_line_char;
1016 comment_line_string[i++] = ' ';
1017 }
1018 comment_line_string[i] = '\0';
1019
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001020 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-09 21:59:30 -07001021 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Matthieu Moy2556b992013-09-06 19:43:07 +02001022 "%s%.*s", comment_line_string,
Junio C Hamanoeff80a92013-01-16 20:18:48 +01001023 (int)(ep - cp), cp);
Matthieu Moy2556b992013-09-06 19:43:07 +02001024 if (s->display_comment_prefix)
1025 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1026 comment_line_char);
1027 else
Jeff King75177c82017-04-27 05:01:05 -04001028 fputs("\n", s->fp);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001029}
1030
Lucien Kong83c750a2012-06-05 22:21:24 +02001031static int has_unmerged(struct wt_status *s)
1032{
1033 int i;
1034
1035 for (i = 0; i < s->change.nr; i++) {
1036 struct wt_status_change_data *d;
1037 d = s->change.items[i].util;
1038 if (d->stagemask)
1039 return 1;
1040 }
1041 return 0;
1042}
1043
1044static void show_merge_in_progress(struct wt_status *s,
1045 struct wt_status_state *state,
1046 const char *color)
1047{
1048 if (has_unmerged(s)) {
1049 status_printf_ln(s, color, _("You have unmerged paths."));
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001050 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001051 status_printf_ln(s, color,
Matthieu Moyb0a61ab2016-07-21 14:58:37 +02001052 _(" (fix conflicts and run \"git commit\")"));
1053 status_printf_ln(s, color,
1054 _(" (use \"git merge --abort\" to abort the merge)"));
1055 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001056 } else {
Stephen P. Smith8dc874b2016-02-15 19:38:25 -07001057 s-> commitable = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001058 status_printf_ln(s, color,
1059 _("All conflicts fixed but you are still merging."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001060 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001061 status_printf_ln(s, color,
1062 _(" (use \"git commit\" to conclude merge)"));
1063 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001064 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001065}
1066
1067static void show_am_in_progress(struct wt_status *s,
1068 struct wt_status_state *state,
1069 const char *color)
1070{
1071 status_printf_ln(s, color,
1072 _("You are in the middle of an am session."));
1073 if (state->am_empty_patch)
1074 status_printf_ln(s, color,
1075 _("The current patch is empty."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001076 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001077 if (!state->am_empty_patch)
1078 status_printf_ln(s, color,
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +03001079 _(" (fix conflicts and then run \"git am --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001080 status_printf_ln(s, color,
1081 _(" (use \"git am --skip\" to skip this patch)"));
1082 status_printf_ln(s, color,
1083 _(" (use \"git am --abort\" to restore the original branch)"));
1084 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001085 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001086}
1087
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001088static char *read_line_from_git_path(const char *filename)
1089{
1090 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +07001091 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1092
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001093 if (!fp) {
1094 strbuf_release(&buf);
1095 return NULL;
1096 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001097 strbuf_getline_lf(&buf, fp);
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001098 if (!fclose(fp)) {
1099 return strbuf_detach(&buf, NULL);
1100 } else {
1101 strbuf_release(&buf);
1102 return NULL;
1103 }
1104}
1105
1106static int split_commit_in_progress(struct wt_status *s)
1107{
1108 int split_in_progress = 0;
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001109 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001110
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001111 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001112 !s->branch || strcmp(s->branch, "HEAD"))
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001113 return 0;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001114
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001115 head = read_line_from_git_path("HEAD");
1116 orig_head = read_line_from_git_path("ORIG_HEAD");
1117 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1118 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1119
1120 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1121 ; /* fall through, no split in progress */
1122 else if (!strcmp(rebase_amend, rebase_orig_head))
1123 split_in_progress = !!strcmp(head, rebase_amend);
1124 else if (strcmp(orig_head, rebase_orig_head))
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001125 split_in_progress = 1;
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001126
1127 free(head);
1128 free(orig_head);
1129 free(rebase_amend);
1130 free(rebase_orig_head);
Johannes Schindelin41fc6b32017-05-04 15:56:44 +02001131
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001132 return split_in_progress;
1133}
1134
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001135/*
1136 * Turn
1137 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1138 * into
1139 * "pick d6a2f03 some message"
1140 *
1141 * The function assumes that the line does not contain useless spaces
1142 * before or after the command.
1143 */
1144static void abbrev_sha1_in_line(struct strbuf *line)
1145{
1146 struct strbuf **split;
1147 int i;
1148
1149 if (starts_with(line->buf, "exec ") ||
1150 starts_with(line->buf, "x "))
1151 return;
1152
1153 split = strbuf_split_max(line, ' ', 3);
1154 if (split[0] && split[1]) {
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001155 struct object_id oid;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001156
1157 /*
1158 * strbuf_split_max left a space. Trim it and re-add
1159 * it after abbreviation.
1160 */
1161 strbuf_trim(split[1]);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001162 if (!get_oid(split[1]->buf, &oid)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001163 strbuf_reset(split[1]);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001164 strbuf_add_unique_abbrev(split[1], oid.hash,
René Scharfea94bb682016-10-08 17:38:47 +02001165 DEFAULT_ABBREV);
1166 strbuf_addch(split[1], ' ');
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001167 strbuf_reset(line);
1168 for (i = 0; split[i]; i++)
René Scharfe81099842016-07-19 20:36:29 +02001169 strbuf_addbuf(line, split[i]);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001170 }
1171 }
Stefan Beller6eb60782016-03-31 17:35:44 -07001172 strbuf_list_free(split);
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001173}
1174
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001175static int read_rebase_todolist(const char *fname, struct string_list *lines)
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001176{
1177 struct strbuf line = STRBUF_INIT;
1178 FILE *f = fopen(git_path("%s", fname), "r");
1179
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001180 if (!f) {
1181 if (errno == ENOENT)
1182 return -1;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001183 die_errno("Could not open file %s for reading",
1184 git_path("%s", fname));
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001185 }
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001186 while (!strbuf_getline_lf(&line, f)) {
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001187 if (line.len && line.buf[0] == comment_line_char)
1188 continue;
1189 strbuf_trim(&line);
1190 if (!line.len)
1191 continue;
1192 abbrev_sha1_in_line(&line);
1193 string_list_append(lines, line.buf);
1194 }
Johannes Schindeline7b65e22017-05-04 15:55:52 +02001195 fclose(f);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001196 return 0;
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001197}
1198
1199static void show_rebase_information(struct wt_status *s,
1200 struct wt_status_state *state,
1201 const char *color)
1202{
1203 if (state->rebase_interactive_in_progress) {
1204 int i;
1205 int nr_lines_to_show = 2;
1206
1207 struct string_list have_done = STRING_LIST_INIT_DUP;
1208 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1209
1210 read_rebase_todolist("rebase-merge/done", &have_done);
Johannes Schindelindf9ded42017-01-26 17:08:41 +01001211 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1212 &yet_to_do))
1213 status_printf_ln(s, color,
1214 _("git-rebase-todo is missing."));
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001215 if (have_done.nr == 0)
1216 status_printf_ln(s, color, _("No commands done."));
1217 else {
1218 status_printf_ln(s, color,
1219 Q_("Last command done (%d command done):",
1220 "Last commands done (%d commands done):",
1221 have_done.nr),
1222 have_done.nr);
1223 for (i = (have_done.nr > nr_lines_to_show)
1224 ? have_done.nr - nr_lines_to_show : 0;
1225 i < have_done.nr;
1226 i++)
1227 status_printf_ln(s, color, " %s", have_done.items[i].string);
1228 if (have_done.nr > nr_lines_to_show && s->hints)
1229 status_printf_ln(s, color,
1230 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1231 }
1232
1233 if (yet_to_do.nr == 0)
1234 status_printf_ln(s, color,
1235 _("No commands remaining."));
1236 else {
1237 status_printf_ln(s, color,
1238 Q_("Next command to do (%d remaining command):",
1239 "Next commands to do (%d remaining commands):",
1240 yet_to_do.nr),
1241 yet_to_do.nr);
1242 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1243 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1244 if (s->hints)
1245 status_printf_ln(s, color,
1246 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1247 }
1248 string_list_clear(&yet_to_do, 0);
1249 string_list_clear(&have_done, 0);
1250 }
1251}
1252
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001253static void print_rebase_state(struct wt_status *s,
1254 struct wt_status_state *state,
1255 const char *color)
1256{
1257 if (state->branch)
1258 status_printf_ln(s, color,
1259 _("You are currently rebasing branch '%s' on '%s'."),
1260 state->branch,
1261 state->onto);
1262 else
1263 status_printf_ln(s, color,
1264 _("You are currently rebasing."));
1265}
1266
Lucien Kong83c750a2012-06-05 22:21:24 +02001267static void show_rebase_in_progress(struct wt_status *s,
1268 struct wt_status_state *state,
1269 const char *color)
1270{
1271 struct stat st;
1272
Guillaume Pagès84e6fb92015-07-06 22:56:03 +02001273 show_rebase_information(s, state, color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001274 if (has_unmerged(s)) {
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001275 print_rebase_state(s, state, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001276 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001277 status_printf_ln(s, color,
1278 _(" (fix conflicts and then run \"git rebase --continue\")"));
1279 status_printf_ln(s, color,
1280 _(" (use \"git rebase --skip\" to skip this patch)"));
1281 status_printf_ln(s, color,
1282 _(" (use \"git rebase --abort\" to check out the original branch)"));
1283 }
Jeff Kingf9327292015-08-10 05:38:57 -04001284 } else if (state->rebase_in_progress || !stat(git_path_merge_msg(), &st)) {
Guillaume Pagès05eb5632015-06-30 15:01:12 +02001285 print_rebase_state(s, state, color);
Matthieu Moy6a964f52013-09-12 12:50:05 +02001286 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001287 status_printf_ln(s, color,
1288 _(" (all conflicts fixed: run \"git rebase --continue\")"));
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001289 } else if (split_commit_in_progress(s)) {
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001290 if (state->branch)
1291 status_printf_ln(s, color,
1292 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1293 state->branch,
1294 state->onto);
1295 else
1296 status_printf_ln(s, color,
1297 _("You are currently splitting a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001298 if (s->hints)
Lucien Kong2d1cceb2012-06-10 13:17:38 +02001299 status_printf_ln(s, color,
1300 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001301 } else {
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001302 if (state->branch)
1303 status_printf_ln(s, color,
1304 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1305 state->branch,
1306 state->onto);
1307 else
1308 status_printf_ln(s, color,
1309 _("You are currently editing a commit during a rebase."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001310 if (s->hints && !s->amend) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001311 status_printf_ln(s, color,
1312 _(" (use \"git commit --amend\" to amend the current commit)"));
1313 status_printf_ln(s, color,
1314 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1315 }
1316 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001317 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001318}
1319
1320static void show_cherry_pick_in_progress(struct wt_status *s,
1321 struct wt_status_state *state,
1322 const char *color)
1323{
Ralf Thielowbffd8092013-10-11 17:58:37 +02001324 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1325 find_unique_abbrev(state->cherry_pick_head_sha1, DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001326 if (s->hints) {
Lucien Kong83c750a2012-06-05 22:21:24 +02001327 if (has_unmerged(s))
1328 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001329 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001330 else
1331 status_printf_ln(s, color,
Ralf Thielowb95e66f2013-06-17 06:28:26 +02001332 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1333 status_printf_ln(s, color,
1334 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
Lucien Kong83c750a2012-06-05 22:21:24 +02001335 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001336 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001337}
1338
Matthieu Moydb4ef442013-04-02 16:20:21 +02001339static void show_revert_in_progress(struct wt_status *s,
1340 struct wt_status_state *state,
1341 const char *color)
1342{
Matthieu Moy87e139c2013-04-02 16:20:22 +02001343 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1344 find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001345 if (s->hints) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001346 if (has_unmerged(s))
1347 status_printf_ln(s, color,
1348 _(" (fix conflicts and run \"git revert --continue\")"));
1349 else
1350 status_printf_ln(s, color,
1351 _(" (all conflicts fixed: run \"git revert --continue\")"));
1352 status_printf_ln(s, color,
1353 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1354 }
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001355 wt_longstatus_print_trailer(s);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001356}
1357
Lucien Kong83c750a2012-06-05 22:21:24 +02001358static void show_bisect_in_progress(struct wt_status *s,
1359 struct wt_status_state *state,
1360 const char *color)
1361{
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001362 if (state->branch)
1363 status_printf_ln(s, color,
Nguyễn Thái Ngọc Duy6deab242013-03-23 10:52:44 +07001364 _("You are currently bisecting, started from branch '%s'."),
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001365 state->branch);
1366 else
1367 status_printf_ln(s, color,
1368 _("You are currently bisecting."));
Matthieu Moy6a964f52013-09-12 12:50:05 +02001369 if (s->hints)
Lucien Kong83c750a2012-06-05 22:21:24 +02001370 status_printf_ln(s, color,
1371 _(" (use \"git bisect reset\" to get back to the original branch)"));
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001372 wt_longstatus_print_trailer(s);
Lucien Kong83c750a2012-06-05 22:21:24 +02001373}
1374
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001375/*
1376 * Extract branch information from rebase/bisect
1377 */
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001378static char *get_branch(const struct worktree *wt, const char *path)
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001379{
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001380 struct strbuf sb = STRBUF_INIT;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001381 struct object_id oid;
René Scharfec72b49d2015-10-31 18:37:43 +01001382 const char *branch_name;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001383
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001384 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 +07001385 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001386
Jeff King66ec9042015-01-28 12:57:35 -05001387 while (sb.len && sb.buf[sb.len - 1] == '\n')
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001388 strbuf_setlen(&sb, sb.len - 1);
1389 if (!sb.len)
1390 goto got_nothing;
René Scharfec72b49d2015-10-31 18:37:43 +01001391 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1392 strbuf_remove(&sb, 0, branch_name - sb.buf);
Christian Couder59556542013-11-30 21:55:40 +01001393 else if (starts_with(sb.buf, "refs/"))
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001394 ;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001395 else if (!get_oid_hex(sb.buf, &oid)) {
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001396 strbuf_reset(&sb);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001397 strbuf_add_unique_abbrev(&sb, oid.hash, DEFAULT_ABBREV);
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001398 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1399 goto got_nothing;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001400 else /* bisect */
Nguyễn Thái Ngọc Duy8b87cfd2013-03-16 09:12:36 +07001401 ;
1402 return strbuf_detach(&sb, NULL);
1403
1404got_nothing:
1405 strbuf_release(&sb);
1406 return NULL;
Nguyễn Thái Ngọc Duy0722c802013-02-03 12:53:27 +07001407}
1408
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001409struct grab_1st_switch_cbdata {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001410 struct strbuf buf;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001411 struct object_id noid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001412};
1413
brian m. carlson9461d272017-02-21 23:47:32 +00001414static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +02001415 const char *email, timestamp_t timestamp, int tz,
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001416 const char *message, void *cb_data)
Lucien Kong83c750a2012-06-05 22:21:24 +02001417{
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001418 struct grab_1st_switch_cbdata *cb = cb_data;
1419 const char *target = NULL, *end;
1420
René Scharfec72b49d2015-10-31 18:37:43 +01001421 if (!skip_prefix(message, "checkout: moving from ", &message))
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001422 return 0;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001423 target = strstr(message, " to ");
1424 if (!target)
1425 return 0;
1426 target += strlen(" to ");
1427 strbuf_reset(&cb->buf);
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001428 oidcpy(&cb->noid, noid);
René Scharfe904de442015-11-25 15:10:18 +01001429 end = strchrnul(target, '\n');
1430 strbuf_add(&cb->buf, target, end - target);
1431 if (!strcmp(cb->buf.buf, "HEAD")) {
Matthieu Moy0eb85482015-09-27 17:13:42 +02001432 /* HEAD is relative. Resolve it to the right reflog entry. */
René Scharfe904de442015-11-25 15:10:18 +01001433 strbuf_reset(&cb->buf);
brian m. carlson9461d272017-02-21 23:47:32 +00001434 strbuf_add_unique_abbrev(&cb->buf, noid->hash, DEFAULT_ABBREV);
Matthieu Moy0eb85482015-09-27 17:13:42 +02001435 }
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001436 return 1;
1437}
1438
1439static void wt_status_get_detached_from(struct wt_status_state *state)
1440{
1441 struct grab_1st_switch_cbdata cb;
1442 struct commit *commit;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001443 struct object_id oid;
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001444 char *ref = NULL;
1445
1446 strbuf_init(&cb.buf, 0);
1447 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1448 strbuf_release(&cb.buf);
1449 return;
1450 }
1451
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001452 if (dwim_ref(cb.buf.buf, cb.buf.len, oid.hash, &ref) == 1 &&
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001453 /* sha1 is a commit? match without further lookup */
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001454 (!oidcmp(&cb.noid, &oid) ||
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001455 /* perhaps sha1 is a tag, try to dereference to a commit */
brian m. carlsonbc832662017-05-06 22:10:10 +00001456 ((commit = lookup_commit_reference_gently(&oid, 1)) != NULL &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001457 !oidcmp(&cb.noid, &commit->object.oid)))) {
René Scharfec72b49d2015-10-31 18:37:43 +01001458 const char *from = ref;
1459 if (!skip_prefix(from, "refs/tags/", &from))
1460 skip_prefix(from, "refs/remotes/", &from);
1461 state->detached_from = xstrdup(from);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001462 } else
1463 state->detached_from =
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001464 xstrdup(find_unique_abbrev(cb.noid.hash, DEFAULT_ABBREV));
1465 hashcpy(state->detached_sha1, cb.noid.hash);
1466 state->detached_at = !get_oid("HEAD", &oid) &&
1467 !hashcmp(oid.hash, state->detached_sha1);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001468
1469 free(ref);
1470 strbuf_release(&cb.buf);
1471}
1472
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001473int wt_status_check_rebase(const struct worktree *wt,
1474 struct wt_status_state *state)
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001475{
Lucien Kong83c750a2012-06-05 22:21:24 +02001476 struct stat st;
1477
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001478 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1479 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001480 state->am_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001481 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 +07001482 state->am_empty_patch = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001483 } else {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001484 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001485 state->branch = get_branch(wt, "rebase-apply/head-name");
1486 state->onto = get_branch(wt, "rebase-apply/onto");
Lucien Kong83c750a2012-06-05 22:21:24 +02001487 }
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001488 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1489 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001490 state->rebase_interactive_in_progress = 1;
Lucien Kong83c750a2012-06-05 22:21:24 +02001491 else
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001492 state->rebase_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001493 state->branch = get_branch(wt, "rebase-merge/head-name");
1494 state->onto = get_branch(wt, "rebase-merge/onto");
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001495 } else
1496 return 0;
1497 return 1;
1498}
1499
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001500int wt_status_check_bisect(const struct worktree *wt,
1501 struct wt_status_state *state)
1502{
1503 struct stat st;
1504
1505 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1506 state->bisect_in_progress = 1;
1507 state->branch = get_branch(wt, "BISECT_START");
1508 return 1;
1509 }
1510 return 0;
1511}
1512
Lucien Kong83c750a2012-06-05 22:21:24 +02001513void wt_status_get_state(struct wt_status_state *state,
1514 int get_detached_from)
1515{
1516 struct stat st;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001517 struct object_id oid;
Lucien Kong83c750a2012-06-05 22:21:24 +02001518
1519 if (!stat(git_path_merge_head(), &st)) {
1520 state->merge_in_progress = 1;
Nguyễn Thái Ngọc Duy81eff272016-04-22 20:01:31 +07001521 } else if (wt_status_check_rebase(NULL, state)) {
Nguyễn Thái Ngọc Duybcd522a2016-04-22 20:01:30 +07001522 ; /* all set */
Jeff Kingf9327292015-08-10 05:38:57 -04001523 } else if (!stat(git_path_cherry_pick_head(), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001524 !get_oid("CHERRY_PICK_HEAD", &oid)) {
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001525 state->cherry_pick_in_progress = 1;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001526 hashcpy(state->cherry_pick_head_sha1, oid.hash);
Lucien Kong83c750a2012-06-05 22:21:24 +02001527 }
Nguyễn Thái Ngọc Duyf5d067a2016-04-22 20:01:34 +07001528 wt_status_check_bisect(NULL, state);
Jeff Kingf9327292015-08-10 05:38:57 -04001529 if (!stat(git_path_revert_head(), &st) &&
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001530 !get_oid("REVERT_HEAD", &oid)) {
Matthieu Moydb4ef442013-04-02 16:20:21 +02001531 state->revert_in_progress = 1;
brian m. carlsone86ab2c2017-02-21 23:47:37 +00001532 hashcpy(state->revert_head_sha1, oid.hash);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001533 }
Lucien Kong83c750a2012-06-05 22:21:24 +02001534
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001535 if (get_detached_from)
1536 wt_status_get_detached_from(state);
Nguyễn Thái Ngọc Duyb9691db2013-03-13 18:42:50 +07001537}
1538
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001539static void wt_longstatus_print_state(struct wt_status *s,
1540 struct wt_status_state *state)
Lucien Kong83c750a2012-06-05 22:21:24 +02001541{
1542 const char *state_color = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001543 if (state->merge_in_progress)
1544 show_merge_in_progress(s, state, state_color);
1545 else if (state->am_in_progress)
1546 show_am_in_progress(s, state, state_color);
1547 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1548 show_rebase_in_progress(s, state, state_color);
1549 else if (state->cherry_pick_in_progress)
1550 show_cherry_pick_in_progress(s, state, state_color);
Matthieu Moydb4ef442013-04-02 16:20:21 +02001551 else if (state->revert_in_progress)
1552 show_revert_in_progress(s, state, state_color);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001553 if (state->bisect_in_progress)
1554 show_bisect_in_progress(s, state, state_color);
Lucien Kong83c750a2012-06-05 22:21:24 +02001555}
1556
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001557static void wt_longstatus_print(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 04:05:34 -04001558{
Aleksi Aalto1d282322010-11-18 01:40:05 +02001559 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1560 const char *branch_status_color = color(WT_STATUS_HEADER, s);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001561 struct wt_status_state state;
1562
1563 memset(&state, 0, sizeof(state));
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001564 wt_status_get_state(&state,
1565 s->branch && !strcmp(s->branch, "HEAD"));
Jürgen Rühle98bf8a42007-01-02 20:26:23 +01001566
Junio C Hamanobda324c2007-01-03 01:09:34 -08001567 if (s->branch) {
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001568 const char *on_what = _("On branch ");
Junio C Hamanobda324c2007-01-03 01:09:34 -08001569 const char *branch_name = s->branch;
René Scharfec72b49d2015-10-31 18:37:43 +01001570 if (!strcmp(branch_name, "HEAD")) {
Aleksi Aalto1d282322010-11-18 01:40:05 +02001571 branch_status_color = color(WT_STATUS_NOBRANCH, s);
Ramkumar Ramachandraec506312013-06-16 14:15:15 +05301572 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
Guillaume Pagèsdf25e942015-06-30 15:01:13 +02001573 if (state.rebase_interactive_in_progress)
1574 on_what = _("interactive rebase in progress; onto ");
1575 else
1576 on_what = _("rebase in progress; onto ");
Ramkumar Ramachandraec506312013-06-16 14:15:15 +05301577 branch_name = state.onto;
1578 } else if (state.detached_from) {
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001579 branch_name = state.detached_from;
Michael J Gruber970399e2015-03-06 16:04:06 +01001580 if (state.detached_at)
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001581 on_what = _("HEAD detached at ");
1582 else
1583 on_what = _("HEAD detached from ");
1584 } else {
1585 branch_name = "";
1586 on_what = _("Not currently on any branch.");
1587 }
René Scharfec72b49d2015-10-31 18:37:43 +01001588 } else
1589 skip_prefix(branch_name, "refs/heads/", &branch_name);
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001590 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
Jonathan Niederb926c0d2011-02-25 23:11:37 -06001591 status_printf_more(s, branch_status_color, "%s", on_what);
1592 status_printf_more(s, branch_color, "%s\n", branch_name);
Junio C Hamanob6975ab2008-07-02 00:52:16 -07001593 if (!s->is_initial)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001594 wt_longstatus_print_tracking(s);
Junio C Hamanobda324c2007-01-03 01:09:34 -08001595 }
Jeff Kingc91f0d92006-09-08 04:05:34 -04001596
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001597 wt_longstatus_print_state(s, &state);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001598 free(state.branch);
1599 free(state.onto);
Nguyễn Thái Ngọc Duyb397ea42013-03-13 18:42:52 +07001600 free(state.detached_from);
Nguyễn Thái Ngọc Duy3b691cc2013-03-13 18:42:51 +07001601
Jeff Kingc91f0d92006-09-08 04:05:34 -04001602 if (s->is_initial) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001603 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301604 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1605 s->commit_template
1606 ? _("Initial commit")
1607 : _("No commits yet"));
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001608 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001609 }
1610
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001611 wt_longstatus_print_updated(s);
1612 wt_longstatus_print_unmerged(s);
1613 wt_longstatus_print_changed(s);
Jens Lehmann46a958b2010-06-25 16:56:47 +02001614 if (s->submodule_summary &&
1615 (!s->ignore_submodule_arg ||
1616 strcmp(s->ignore_submodule_arg, "all"))) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001617 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1618 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
Jens Lehmannf17a5d32010-01-17 20:42:31 +01001619 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001620 if (s->show_untracked_files) {
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001621 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
Junio C Hamano2381e392010-04-10 00:33:17 -07001622 if (s->show_ignored_files)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001623 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001624 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
Felipe Contreras7d7d6802014-05-04 01:12:55 -05001625 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001626 status_printf_ln(s, GIT_COLOR_NORMAL,
Jiang Xin62901172013-04-12 11:53:01 +08001627 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1628 "may speed it up, but you have to be careful not to forget to add\n"
1629 "new files yourself (see 'git help status')."),
1630 s->untracked_in_ms / 1000.0);
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +07001631 }
Junio C Hamano2381e392010-04-10 00:33:17 -07001632 } else if (s->commitable)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001633 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
Matthieu Moy6a964f52013-09-12 12:50:05 +02001634 s->hints
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001635 ? _(" (use -u option to show untracked files)") : "");
Jeff Kingc91f0d92006-09-08 04:05:34 -04001636
Jeff King1324fb62008-11-12 03:23:37 -05001637 if (s->verbose)
Jeff Hostetler957a0fe2016-08-05 18:00:26 -04001638 wt_longstatus_print_verbose(s);
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001639 if (!s->commitable) {
1640 if (s->amend)
Ævar Arnfjörð Bjarmason355ec7a2011-02-22 23:42:13 +00001641 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
Junio C Hamano37d07f82007-12-12 19:09:16 -08001642 else if (s->nowarn)
1643 ; /* nothing */
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001644 else if (s->workdir_dirty) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001645 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001646 printf(_("no changes added to commit "
1647 "(use \"git add\" and/or \"git commit -a\")\n"));
1648 else
1649 printf(_("no changes added to commit\n"));
1650 } else if (s->untracked.nr) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001651 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001652 printf(_("nothing added to commit but untracked files "
1653 "present (use \"git add\" to track)\n"));
1654 else
1655 printf(_("nothing added to commit but untracked files present\n"));
1656 } else if (s->is_initial) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001657 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001658 printf(_("nothing to commit (create/copy files "
1659 "and use \"git add\" to track)\n"));
1660 else
1661 printf(_("nothing to commit\n"));
1662 } else if (!s->show_untracked_files) {
Matthieu Moy6a964f52013-09-12 12:50:05 +02001663 if (s->hints)
Nguyễn Thái Ngọc Duy50bd8b72012-09-06 22:16:50 +07001664 printf(_("nothing to commit (use -u to show untracked files)\n"));
1665 else
1666 printf(_("nothing to commit\n"));
1667 } else
Lars Vogel2a0e6cd2016-06-09 20:19:30 +02001668 printf(_("nothing to commit, working tree clean\n"));
Jürgen Rühle6e458bf2007-01-02 20:26:22 +01001669 }
Liam Beguinc1b5d012017-06-17 18:30:51 -04001670 if(s->show_stash)
1671 wt_longstatus_print_stash_summary(s);
Jeff Kingc91f0d92006-09-08 04:05:34 -04001672}
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001673
Jeff King3207a3a2012-05-07 15:44:44 -04001674static void wt_shortstatus_unmerged(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001675 struct wt_status *s)
1676{
1677 struct wt_status_change_data *d = it->util;
1678 const char *how = "??";
1679
1680 switch (d->stagemask) {
1681 case 1: how = "DD"; break; /* both deleted */
1682 case 2: how = "AU"; break; /* added by us */
1683 case 3: how = "UD"; break; /* deleted by them */
1684 case 4: how = "UA"; break; /* added by them */
1685 case 5: how = "DU"; break; /* deleted by us */
1686 case 6: how = "AA"; break; /* both added */
1687 case 7: how = "UU"; break; /* both modified */
1688 }
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001689 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
Jeff King3207a3a2012-05-07 15:44:44 -04001690 if (s->null_termination) {
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001691 fprintf(stdout, " %s%c", it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001692 } else {
1693 struct strbuf onebuf = STRBUF_INIT;
1694 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001695 one = quote_path(it->string, s->prefix, &onebuf);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001696 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001697 strbuf_release(&onebuf);
1698 }
1699}
1700
Jeff King3207a3a2012-05-07 15:44:44 -04001701static void wt_shortstatus_status(struct string_list_item *it,
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001702 struct wt_status *s)
1703{
1704 struct wt_status_change_data *d = it->util;
1705
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001706 if (d->index_status)
1707 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1708 else
1709 putchar(' ');
1710 if (d->worktree_status)
1711 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1712 else
1713 putchar(' ');
1714 putchar(' ');
Jeff King3207a3a2012-05-07 15:44:44 -04001715 if (s->null_termination) {
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001716 fprintf(stdout, "%s%c", it->string, 0);
1717 if (d->head_path)
1718 fprintf(stdout, "%s%c", d->head_path, 0);
1719 } else {
1720 struct strbuf onebuf = STRBUF_INIT;
1721 const char *one;
1722 if (d->head_path) {
Jiang Xin39598f92013-06-25 23:53:45 +08001723 one = quote_path(d->head_path, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001724 if (*one != '"' && strchr(one, ' ') != NULL) {
1725 putchar('"');
1726 strbuf_addch(&onebuf, '"');
1727 one = onebuf.buf;
1728 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001729 printf("%s -> ", one);
1730 strbuf_release(&onebuf);
1731 }
Jiang Xin39598f92013-06-25 23:53:45 +08001732 one = quote_path(it->string, s->prefix, &onebuf);
Kevin Ballarddbfdc622010-11-08 18:44:38 -08001733 if (*one != '"' && strchr(one, ' ') != NULL) {
1734 putchar('"');
1735 strbuf_addch(&onebuf, '"');
1736 one = onebuf.buf;
1737 }
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001738 printf("%s\n", one);
1739 strbuf_release(&onebuf);
1740 }
1741}
1742
Jeff King3207a3a2012-05-07 15:44:44 -04001743static void wt_shortstatus_other(struct string_list_item *it,
Junio C Hamano2381e392010-04-10 00:33:17 -07001744 struct wt_status *s, const char *sign)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001745{
Jeff King3207a3a2012-05-07 15:44:44 -04001746 if (s->null_termination) {
Junio C Hamano2381e392010-04-10 00:33:17 -07001747 fprintf(stdout, "%s %s%c", sign, it->string, 0);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001748 } else {
1749 struct strbuf onebuf = STRBUF_INIT;
1750 const char *one;
Jiang Xin39598f92013-06-25 23:53:45 +08001751 one = quote_path(it->string, s->prefix, &onebuf);
Junio C Hamanoc1909e72010-05-01 22:05:14 -07001752 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
Michael J Gruber3fe2a892009-12-05 16:04:38 +01001753 printf(" %s\n", one);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001754 strbuf_release(&onebuf);
1755 }
1756}
1757
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001758static void wt_shortstatus_print_tracking(struct wt_status *s)
1759{
1760 struct branch *branch;
1761 const char *header_color = color(WT_STATUS_HEADER, s);
1762 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1763 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1764
1765 const char *base;
René Scharfe5e8d2722017-07-08 12:51:01 +02001766 char *short_base;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001767 const char *branch_name;
1768 int num_ours, num_theirs;
Jiang Xinf2e08732013-08-26 15:02:48 +08001769 int upstream_is_gone = 0;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001770
1771 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1772
1773 if (!s->branch)
1774 return;
1775 branch_name = s->branch;
1776
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001777#define LABEL(string) (s->no_gettext ? (string) : _(string))
1778
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001779 if (s->is_initial)
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +05301780 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001781
René Scharfebaf0a3e2015-10-31 18:36:35 +01001782 if (!strcmp(s->branch, "HEAD")) {
1783 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
Michael J Gruberb9e2bc52017-03-14 17:02:02 +01001784 LABEL(N_("HEAD (no branch)")));
René Scharfebaf0a3e2015-10-31 18:36:35 +01001785 goto conclude;
1786 }
1787
René Scharfec72b49d2015-10-31 18:37:43 +01001788 skip_prefix(branch_name, "refs/heads/", &branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001789
René Scharfe8d8325f2015-10-31 18:37:12 +01001790 branch = branch_get(branch_name);
René Scharfebaf0a3e2015-10-31 18:36:35 +01001791
Jiang Xinf2e08732013-08-26 15:02:48 +08001792 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1793
Jeff King979cb242015-05-21 20:49:11 -04001794 if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) {
René Scharfebcf8cc22015-10-31 18:36:01 +01001795 if (!base)
1796 goto conclude;
Jeff King979cb242015-05-21 20:49:11 -04001797
Jiang Xinf2e08732013-08-26 15:02:48 +08001798 upstream_is_gone = 1;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001799 }
1800
René Scharfe5e8d2722017-07-08 12:51:01 +02001801 short_base = shorten_unambiguous_ref(base, 0);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001802 color_fprintf(s->fp, header_color, "...");
René Scharfe5e8d2722017-07-08 12:51:01 +02001803 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
1804 free(short_base);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001805
René Scharfebcf8cc22015-10-31 18:36:01 +01001806 if (!upstream_is_gone && !num_ours && !num_theirs)
1807 goto conclude;
Jiang Xinf2234592013-08-26 15:02:49 +08001808
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001809 color_fprintf(s->fp, header_color, " [");
Jiang Xinf2e08732013-08-26 15:02:48 +08001810 if (upstream_is_gone) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001811 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
Jiang Xinf2e08732013-08-26 15:02:48 +08001812 } else if (!num_ours) {
Matthieu Moy7a76c282014-03-20 13:12:41 +01001813 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001814 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1815 } else if (!num_theirs) {
Michael J Gruberdf227242016-03-14 16:30:33 +01001816 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001817 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1818 } else {
Michael J Gruberdf227242016-03-14 16:30:33 +01001819 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001820 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
Matthieu Moy7a76c282014-03-20 13:12:41 +01001821 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001822 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1823 }
1824
Jeff Kinga5985232012-05-07 17:02:18 -04001825 color_fprintf(s->fp, header_color, "]");
René Scharfebcf8cc22015-10-31 18:36:01 +01001826 conclude:
Jeff Kinga5985232012-05-07 17:02:18 -04001827 fputc(s->null_termination ? '\0' : '\n', s->fp);
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001828}
1829
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001830static void wt_shortstatus_print(struct wt_status *s)
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001831{
Stefan Bellerd4aae452017-03-16 14:36:19 -07001832 struct string_list_item *it;
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001833
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001834 if (s->show_branch)
Daniel Knittl-Frank05a59a02010-05-25 15:45:51 +02001835 wt_shortstatus_print_tracking(s);
1836
Stefan Bellerd4aae452017-03-16 14:36:19 -07001837 for_each_string_list_item(it, &s->change) {
1838 struct wt_status_change_data *d = it->util;
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001839
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001840 if (d->stagemask)
Jeff King3207a3a2012-05-07 15:44:44 -04001841 wt_shortstatus_unmerged(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001842 else
Jeff King3207a3a2012-05-07 15:44:44 -04001843 wt_shortstatus_status(it, s);
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001844 }
Stefan Bellerd4aae452017-03-16 14:36:19 -07001845 for_each_string_list_item(it, &s->untracked)
Jeff King3207a3a2012-05-07 15:44:44 -04001846 wt_shortstatus_other(it, s, "??");
Junio C Hamano2381e392010-04-10 00:33:17 -07001847
Stefan Bellerd4aae452017-03-16 14:36:19 -07001848 for_each_string_list_item(it, &s->ignored)
Jeff King3207a3a2012-05-07 15:44:44 -04001849 wt_shortstatus_other(it, s, "!!");
Michael J Gruber84dbe7b2009-12-05 16:04:37 +01001850}
Jeff King4a7cc2f2009-12-07 00:17:15 -05001851
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001852static void wt_porcelain_print(struct wt_status *s)
Jeff King4a7cc2f2009-12-07 00:17:15 -05001853{
1854 s->use_color = 0;
Jeff King86617682009-12-07 00:26:25 -05001855 s->relative_paths = 0;
1856 s->prefix = NULL;
Matthieu Moy7a76c282014-03-20 13:12:41 +01001857 s->no_gettext = 1;
Jeff Kingd4a6bf12012-05-07 17:09:04 -04001858 wt_shortstatus_print(s);
Jeff King4a7cc2f2009-12-07 00:17:15 -05001859}
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04001860
Jeff Hostetler24959ba2016-08-11 10:45:58 -04001861/*
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04001862 * Print branch information for porcelain v2 output. These lines
1863 * are printed when the '--branch' parameter is given.
1864 *
1865 * # branch.oid <commit><eol>
1866 * # branch.head <head><eol>
1867 * [# branch.upstream <upstream><eol>
1868 * [# branch.ab +<ahead> -<behind><eol>]]
1869 *
1870 * <commit> ::= the current commit hash or the the literal
1871 * "(initial)" to indicate an initialized repo
1872 * with no commits.
1873 *
1874 * <head> ::= <branch_name> the current branch name or
1875 * "(detached)" literal when detached head or
1876 * "(unknown)" when something is wrong.
1877 *
1878 * <upstream> ::= the upstream branch name, when set.
1879 *
1880 * <ahead> ::= integer ahead value, when upstream set
1881 * and the commit is present (not gone).
1882 *
1883 * <behind> ::= integer behind value, when upstream set
1884 * and commit is present.
1885 *
1886 *
1887 * The end-of-line is defined by the -z flag.
1888 *
1889 * <eol> ::= NUL when -z,
1890 * LF when NOT -z.
1891 *
1892 */
1893static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1894{
1895 struct branch *branch;
1896 const char *base;
1897 const char *branch_name;
1898 struct wt_status_state state;
1899 int ab_info, nr_ahead, nr_behind;
1900 char eol = s->null_termination ? '\0' : '\n';
1901
1902 memset(&state, 0, sizeof(state));
1903 wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1904
1905 fprintf(s->fp, "# branch.oid %s%c",
1906 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1907 eol);
1908
1909 if (!s->branch)
1910 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1911 else {
1912 if (!strcmp(s->branch, "HEAD")) {
1913 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1914
1915 if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1916 branch_name = state.onto;
1917 else if (state.detached_from)
1918 branch_name = state.detached_from;
1919 else
1920 branch_name = "";
1921 } else {
1922 branch_name = NULL;
1923 skip_prefix(s->branch, "refs/heads/", &branch_name);
1924
1925 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1926 }
1927
1928 /* Lookup stats on the upstream tracking branch, if set. */
1929 branch = branch_get(branch_name);
1930 base = NULL;
1931 ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind, &base) == 0);
1932 if (base) {
1933 base = shorten_unambiguous_ref(base, 0);
1934 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1935 free((char *)base);
1936
1937 if (ab_info)
1938 fprintf(s->fp, "# branch.ab +%d -%d%c", nr_ahead, nr_behind, eol);
1939 }
1940 }
1941
1942 free(state.branch);
1943 free(state.onto);
1944 free(state.detached_from);
1945}
1946
1947/*
Jeff Hostetler24959ba2016-08-11 10:45:58 -04001948 * Convert various submodule status values into a
1949 * fixed-length string of characters in the buffer provided.
1950 */
1951static void wt_porcelain_v2_submodule_state(
1952 struct wt_status_change_data *d,
1953 char sub[5])
1954{
1955 if (S_ISGITLINK(d->mode_head) ||
1956 S_ISGITLINK(d->mode_index) ||
1957 S_ISGITLINK(d->mode_worktree)) {
1958 sub[0] = 'S';
1959 sub[1] = d->new_submodule_commits ? 'C' : '.';
1960 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
1961 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
1962 } else {
1963 sub[0] = 'N';
1964 sub[1] = '.';
1965 sub[2] = '.';
1966 sub[3] = '.';
1967 }
1968 sub[4] = 0;
1969}
1970
1971/*
1972 * Fix-up changed entries before we print them.
1973 */
1974static void wt_porcelain_v2_fix_up_changed(
1975 struct string_list_item *it,
1976 struct wt_status *s)
1977{
1978 struct wt_status_change_data *d = it->util;
1979
1980 if (!d->index_status) {
1981 /*
1982 * This entry is unchanged in the index (relative to the head).
1983 * Therefore, the collect_updated_cb was never called for this
1984 * entry (during the head-vs-index scan) and so the head column
1985 * fields were never set.
1986 *
1987 * We must have data for the index column (from the
1988 * index-vs-worktree scan (otherwise, this entry should not be
1989 * in the list of changes)).
1990 *
1991 * Copy index column fields to the head column, so that our
1992 * output looks complete.
1993 */
1994 assert(d->mode_head == 0);
1995 d->mode_head = d->mode_index;
1996 oidcpy(&d->oid_head, &d->oid_index);
1997 }
1998
1999 if (!d->worktree_status) {
2000 /*
2001 * This entry is unchanged in the worktree (relative to the index).
2002 * Therefore, the collect_changed_cb was never called for this entry
2003 * (during the index-vs-worktree scan) and so the worktree column
2004 * fields were never set.
2005 *
2006 * We must have data for the index column (from the head-vs-index
2007 * scan).
2008 *
2009 * Copy the index column fields to the worktree column so that
2010 * our output looks complete.
2011 *
2012 * Note that we only have a mode field in the worktree column
2013 * because the scan code tries really hard to not have to compute it.
2014 */
2015 assert(d->mode_worktree == 0);
2016 d->mode_worktree = d->mode_index;
2017 }
2018}
2019
2020/*
2021 * Print porcelain v2 info for tracked entries with changes.
2022 */
2023static void wt_porcelain_v2_print_changed_entry(
2024 struct string_list_item *it,
2025 struct wt_status *s)
2026{
2027 struct wt_status_change_data *d = it->util;
2028 struct strbuf buf_index = STRBUF_INIT;
2029 struct strbuf buf_head = STRBUF_INIT;
2030 const char *path_index = NULL;
2031 const char *path_head = NULL;
2032 char key[3];
2033 char submodule_token[5];
2034 char sep_char, eol_char;
2035
2036 wt_porcelain_v2_fix_up_changed(it, s);
2037 wt_porcelain_v2_submodule_state(d, submodule_token);
2038
2039 key[0] = d->index_status ? d->index_status : '.';
2040 key[1] = d->worktree_status ? d->worktree_status : '.';
2041 key[2] = 0;
2042
2043 if (s->null_termination) {
2044 /*
2045 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2046 * A single NUL character separates them.
2047 */
2048 sep_char = '\0';
2049 eol_char = '\0';
2050 path_index = it->string;
2051 path_head = d->head_path;
2052 } else {
2053 /*
2054 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2055 * The source path is only present when necessary.
2056 * A single TAB separates them (because paths can contain spaces
2057 * which are not escaped and C-quoting does escape TAB characters).
2058 */
2059 sep_char = '\t';
2060 eol_char = '\n';
2061 path_index = quote_path(it->string, s->prefix, &buf_index);
2062 if (d->head_path)
2063 path_head = quote_path(d->head_path, s->prefix, &buf_head);
2064 }
2065
2066 if (path_head)
2067 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2068 key, submodule_token,
2069 d->mode_head, d->mode_index, d->mode_worktree,
2070 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2071 key[0], d->score,
2072 path_index, sep_char, path_head, eol_char);
2073 else
2074 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2075 key, submodule_token,
2076 d->mode_head, d->mode_index, d->mode_worktree,
2077 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2078 path_index, eol_char);
2079
2080 strbuf_release(&buf_index);
2081 strbuf_release(&buf_head);
2082}
2083
2084/*
2085 * Print porcelain v2 status info for unmerged entries.
2086 */
2087static void wt_porcelain_v2_print_unmerged_entry(
2088 struct string_list_item *it,
2089 struct wt_status *s)
2090{
2091 struct wt_status_change_data *d = it->util;
2092 const struct cache_entry *ce;
2093 struct strbuf buf_index = STRBUF_INIT;
2094 const char *path_index = NULL;
2095 int pos, stage, sum;
2096 struct {
2097 int mode;
2098 struct object_id oid;
2099 } stages[3];
2100 char *key;
2101 char submodule_token[5];
2102 char unmerged_prefix = 'u';
2103 char eol_char = s->null_termination ? '\0' : '\n';
2104
2105 wt_porcelain_v2_submodule_state(d, submodule_token);
2106
2107 switch (d->stagemask) {
2108 case 1: key = "DD"; break; /* both deleted */
2109 case 2: key = "AU"; break; /* added by us */
2110 case 3: key = "UD"; break; /* deleted by them */
2111 case 4: key = "UA"; break; /* added by them */
2112 case 5: key = "DU"; break; /* deleted by us */
2113 case 6: key = "AA"; break; /* both added */
2114 case 7: key = "UU"; break; /* both modified */
2115 default:
2116 die("BUG: unhandled unmerged status %x", d->stagemask);
2117 }
2118
2119 /*
2120 * Disregard d.aux.porcelain_v2 data that we accumulated
2121 * for the head and index columns during the scans and
2122 * replace with the actual stage data.
2123 *
2124 * Note that this is a last-one-wins for each the individual
2125 * stage [123] columns in the event of multiple cache entries
2126 * for same stage.
2127 */
2128 memset(stages, 0, sizeof(stages));
2129 sum = 0;
2130 pos = cache_name_pos(it->string, strlen(it->string));
2131 assert(pos < 0);
2132 pos = -pos-1;
2133 while (pos < active_nr) {
2134 ce = active_cache[pos++];
2135 stage = ce_stage(ce);
2136 if (strcmp(ce->name, it->string) || !stage)
2137 break;
2138 stages[stage - 1].mode = ce->ce_mode;
René Scharfe86947692017-01-28 23:03:06 +01002139 oidcpy(&stages[stage - 1].oid, &ce->oid);
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002140 sum |= (1 << (stage - 1));
2141 }
2142 if (sum != d->stagemask)
2143 die("BUG: observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2144
2145 if (s->null_termination)
2146 path_index = it->string;
2147 else
2148 path_index = quote_path(it->string, s->prefix, &buf_index);
2149
2150 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2151 unmerged_prefix, key, submodule_token,
2152 stages[0].mode, /* stage 1 */
2153 stages[1].mode, /* stage 2 */
2154 stages[2].mode, /* stage 3 */
2155 d->mode_worktree,
2156 oid_to_hex(&stages[0].oid), /* stage 1 */
2157 oid_to_hex(&stages[1].oid), /* stage 2 */
2158 oid_to_hex(&stages[2].oid), /* stage 3 */
2159 path_index,
2160 eol_char);
2161
2162 strbuf_release(&buf_index);
2163}
2164
2165/*
2166 * Print porcelain V2 status info for untracked and ignored entries.
2167 */
2168static void wt_porcelain_v2_print_other(
2169 struct string_list_item *it,
2170 struct wt_status *s,
2171 char prefix)
2172{
2173 struct strbuf buf = STRBUF_INIT;
2174 const char *path;
2175 char eol_char;
2176
2177 if (s->null_termination) {
2178 path = it->string;
2179 eol_char = '\0';
2180 } else {
2181 path = quote_path(it->string, s->prefix, &buf);
2182 eol_char = '\n';
2183 }
2184
2185 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2186
2187 strbuf_release(&buf);
2188}
2189
2190/*
2191 * Print porcelain V2 status.
2192 *
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002193 * [<v2_branch>]
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002194 * [<v2_changed_items>]*
2195 * [<v2_unmerged_items>]*
2196 * [<v2_untracked_items>]*
2197 * [<v2_ignored_items>]*
2198 *
2199 */
2200static void wt_porcelain_v2_print(struct wt_status *s)
2201{
2202 struct wt_status_change_data *d;
2203 struct string_list_item *it;
2204 int i;
2205
Jeff Hostetlerd9fc7462016-08-11 10:45:59 -04002206 if (s->show_branch)
2207 wt_porcelain_v2_print_tracking(s);
2208
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002209 for (i = 0; i < s->change.nr; i++) {
2210 it = &(s->change.items[i]);
2211 d = it->util;
2212 if (!d->stagemask)
2213 wt_porcelain_v2_print_changed_entry(it, s);
2214 }
2215
2216 for (i = 0; i < s->change.nr; i++) {
2217 it = &(s->change.items[i]);
2218 d = it->util;
2219 if (d->stagemask)
2220 wt_porcelain_v2_print_unmerged_entry(it, s);
2221 }
2222
2223 for (i = 0; i < s->untracked.nr; i++) {
2224 it = &(s->untracked.items[i]);
2225 wt_porcelain_v2_print_other(it, s, '?');
2226 }
2227
2228 for (i = 0; i < s->ignored.nr; i++) {
2229 it = &(s->ignored.items[i]);
2230 wt_porcelain_v2_print_other(it, s, '!');
2231 }
2232}
2233
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002234void wt_status_print(struct wt_status *s)
2235{
2236 switch (s->status_format) {
2237 case STATUS_FORMAT_SHORT:
2238 wt_shortstatus_print(s);
2239 break;
2240 case STATUS_FORMAT_PORCELAIN:
2241 wt_porcelain_print(s);
2242 break;
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002243 case STATUS_FORMAT_PORCELAIN_V2:
Jeff Hostetler24959ba2016-08-11 10:45:58 -04002244 wt_porcelain_v2_print(s);
Jeff Hostetler1ecdecc2016-08-11 10:45:57 -04002245 break;
Jeff Hostetlerbe7e7952016-08-05 18:00:27 -04002246 case STATUS_FORMAT_UNSPECIFIED:
2247 die("BUG: finalize_deferred_config() should have been called");
2248 break;
2249 case STATUS_FORMAT_NONE:
2250 case STATUS_FORMAT_LONG:
2251 wt_longstatus_print(s);
2252 break;
2253 }
2254}
Johannes Schindelinfd849862016-10-07 18:08:38 +02002255
2256/**
2257 * Returns 1 if there are unstaged changes, 0 otherwise.
2258 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002259int has_unstaged_changes(int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002260{
2261 struct rev_info rev_info;
2262 int result;
2263
2264 init_revisions(&rev_info, NULL);
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002265 if (ignore_submodules)
2266 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002267 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
2268 diff_setup_done(&rev_info.diffopt);
2269 result = run_diff_files(&rev_info, 0);
2270 return diff_result_code(&rev_info.diffopt, result);
2271}
2272
2273/**
2274 * Returns 1 if there are uncommitted changes, 0 otherwise.
2275 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002276int has_uncommitted_changes(int ignore_submodules)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002277{
2278 struct rev_info rev_info;
2279 int result;
2280
2281 if (is_cache_unborn())
2282 return 0;
2283
2284 init_revisions(&rev_info, NULL);
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002285 if (ignore_submodules)
2286 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002287 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
2288 add_head_to_pending(&rev_info);
2289 diff_setup_done(&rev_info.diffopt);
2290 result = run_diff_index(&rev_info, 1);
2291 return diff_result_code(&rev_info.diffopt, result);
2292}
2293
2294/**
2295 * If the work tree has unstaged or uncommitted changes, dies with the
2296 * appropriate message.
2297 */
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002298int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
Johannes Schindelinfd849862016-10-07 18:08:38 +02002299{
2300 struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002301 int err = 0, fd;
Johannes Schindelinfd849862016-10-07 18:08:38 +02002302
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002303 fd = hold_locked_index(lock_file, 0);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002304 refresh_cache(REFRESH_QUIET);
Junio C Hamano89d38fb2016-12-07 11:11:26 -08002305 if (0 <= fd)
2306 update_index_if_able(&the_index, lock_file);
Johannes Schindelinfd849862016-10-07 18:08:38 +02002307 rollback_lock_file(lock_file);
2308
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002309 if (has_unstaged_changes(ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002310 /* TRANSLATORS: the action is e.g. "pull with rebase" */
Johannes Schindelin4777e172016-10-07 18:09:04 +02002311 error(_("cannot %s: You have unstaged changes."), _(action));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002312 err = 1;
2313 }
2314
Johannes Schindelind8cc92a2016-10-07 18:09:00 +02002315 if (has_uncommitted_changes(ignore_submodules)) {
Johannes Schindelinfd849862016-10-07 18:08:38 +02002316 if (err)
Johannes Schindelin4777e172016-10-07 18:09:04 +02002317 error(_("additionally, your index contains uncommitted changes."));
Johannes Schindelinfd849862016-10-07 18:08:38 +02002318 else
Johannes Schindelin4777e172016-10-07 18:09:04 +02002319 error(_("cannot %s: Your index contains uncommitted changes."),
Johannes Schindelinfd849862016-10-07 18:08:38 +02002320 _(action));
2321 err = 1;
2322 }
2323
2324 if (err) {
2325 if (hint)
2326 error("%s", hint);
2327 if (!gently)
2328 exit(128);
2329 }
2330
2331 return err;
2332}