]>
Commit | Line | Data |
---|---|---|
85023577 | 1 | #include "cache.h" |
c91f0d92 | 2 | #include "wt-status.h" |
c91f0d92 JK |
3 | #include "object.h" |
4 | #include "dir.h" | |
5 | #include "commit.h" | |
6 | #include "diff.h" | |
7ee24e18 | 7 | #include "environment.h" |
f394e093 | 8 | #include "gettext.h" |
41771fa4 | 9 | #include "hex.h" |
c91f0d92 JK |
10 | #include "revision.h" |
11 | #include "diffcore.h" | |
a734d0b1 | 12 | #include "quote.h" |
ac8d5afc | 13 | #include "run-command.h" |
dbbcd44f | 14 | #include "strvec.h" |
b6975ab5 | 15 | #include "remote.h" |
05a59a08 | 16 | #include "refs.h" |
46a958b3 | 17 | #include "submodule.h" |
323d0530 | 18 | #include "column.h" |
e38da487 | 19 | #include "setup.h" |
2d1cceba | 20 | #include "strbuf.h" |
3651e45c | 21 | #include "utf8.h" |
81eff27b | 22 | #include "worktree.h" |
fd84986f | 23 | #include "lockfile.h" |
4a72486d | 24 | #include "sequencer.h" |
ecbc23e4 | 25 | #include "fsmonitor-settings.h" |
c91f0d92 | 26 | |
0a53561a | 27 | #define AB_DELAY_WARNING_IN_MS (2 * 1000) |
ecbc23e4 | 28 | #define UF_DELAY_WARNING_IN_MS (2 * 1000) |
0a53561a | 29 | |
983dc697 | 30 | static const char cut_line[] = |
1a72cfd7 JL |
31 | "------------------------ >8 ------------------------\n"; |
32 | ||
23900a96 | 33 | static char default_wt_status_colors[][COLOR_MAXLEN] = { |
dc6ebd4c AL |
34 | GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */ |
35 | GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */ | |
36 | GIT_COLOR_RED, /* WT_STATUS_CHANGED */ | |
37 | GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */ | |
38 | GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */ | |
4d4d5726 | 39 | GIT_COLOR_RED, /* WT_STATUS_UNMERGED */ |
05a59a08 DKF |
40 | GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */ |
41 | GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */ | |
148135fc | 42 | GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */ |
c91f0d92 | 43 | }; |
4d229653 | 44 | |
d249b098 | 45 | static const char *color(int slot, struct wt_status *s) |
c91f0d92 | 46 | { |
daa0c3d9 JK |
47 | const char *c = ""; |
48 | if (want_color(s->use_color)) | |
49 | c = s->color_palette[slot]; | |
148135fc JK |
50 | if (slot == WT_STATUS_ONBRANCH && color_is_nil(c)) |
51 | c = s->color_palette[WT_STATUS_HEADER]; | |
52 | return c; | |
c91f0d92 JK |
53 | } |
54 | ||
becbdae8 JN |
55 | static void status_vprintf(struct wt_status *s, int at_bol, const char *color, |
56 | const char *fmt, va_list ap, const char *trail) | |
57 | { | |
58 | struct strbuf sb = STRBUF_INIT; | |
59 | struct strbuf linebuf = STRBUF_INIT; | |
60 | const char *line, *eol; | |
61 | ||
62 | strbuf_vaddf(&sb, fmt, ap); | |
63 | if (!sb.len) { | |
2556b996 MM |
64 | if (s->display_comment_prefix) { |
65 | strbuf_addch(&sb, comment_line_char); | |
66 | if (!trail) | |
67 | strbuf_addch(&sb, ' '); | |
68 | } | |
becbdae8 JN |
69 | color_print_strbuf(s->fp, color, &sb); |
70 | if (trail) | |
71 | fprintf(s->fp, "%s", trail); | |
72 | strbuf_release(&sb); | |
73 | return; | |
74 | } | |
75 | for (line = sb.buf; *line; line = eol + 1) { | |
76 | eol = strchr(line, '\n'); | |
77 | ||
78 | strbuf_reset(&linebuf); | |
2556b996 | 79 | if (at_bol && s->display_comment_prefix) { |
eff80a9f | 80 | strbuf_addch(&linebuf, comment_line_char); |
becbdae8 JN |
81 | if (*line != '\n' && *line != '\t') |
82 | strbuf_addch(&linebuf, ' '); | |
83 | } | |
84 | if (eol) | |
85 | strbuf_add(&linebuf, line, eol - line); | |
86 | else | |
87 | strbuf_addstr(&linebuf, line); | |
88 | color_print_strbuf(s->fp, color, &linebuf); | |
89 | if (eol) | |
90 | fprintf(s->fp, "\n"); | |
91 | else | |
92 | break; | |
93 | at_bol = 1; | |
94 | } | |
95 | if (trail) | |
96 | fprintf(s->fp, "%s", trail); | |
97 | strbuf_release(&linebuf); | |
98 | strbuf_release(&sb); | |
99 | } | |
100 | ||
101 | void status_printf_ln(struct wt_status *s, const char *color, | |
102 | const char *fmt, ...) | |
103 | { | |
104 | va_list ap; | |
105 | ||
106 | va_start(ap, fmt); | |
107 | status_vprintf(s, 1, color, fmt, ap, "\n"); | |
108 | va_end(ap); | |
109 | } | |
110 | ||
111 | void status_printf(struct wt_status *s, const char *color, | |
112 | const char *fmt, ...) | |
113 | { | |
114 | va_list ap; | |
115 | ||
116 | va_start(ap, fmt); | |
117 | status_vprintf(s, 1, color, fmt, ap, NULL); | |
118 | va_end(ap); | |
119 | } | |
120 | ||
1e24845c JH |
121 | static void status_printf_more(struct wt_status *s, const char *color, |
122 | const char *fmt, ...) | |
becbdae8 JN |
123 | { |
124 | va_list ap; | |
125 | ||
126 | va_start(ap, fmt); | |
127 | status_vprintf(s, 0, color, fmt, ap, NULL); | |
128 | va_end(ap); | |
129 | } | |
130 | ||
5b02ca38 | 131 | void wt_status_prepare(struct repository *r, struct wt_status *s) |
c91f0d92 | 132 | { |
cc46a743 | 133 | memset(s, 0, sizeof(*s)); |
5b02ca38 | 134 | s->repo = r; |
23900a96 JH |
135 | memcpy(s->color_palette, default_wt_status_colors, |
136 | sizeof(default_wt_status_colors)); | |
d249b098 JH |
137 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; |
138 | s->use_color = -1; | |
139 | s->relative_paths = 1; | |
efbd4fdf | 140 | s->branch = resolve_refdup("HEAD", 0, NULL, NULL); |
c91f0d92 | 141 | s->reference = "HEAD"; |
f26a0012 | 142 | s->fp = stdout; |
0f729f21 | 143 | s->index_file = get_index_file(); |
50b7e70f | 144 | s->change.strdup_strings = 1; |
76378683 | 145 | s->untracked.strdup_strings = 1; |
6cb3f6b2 | 146 | s->ignored.strdup_strings = 1; |
84b4202d | 147 | s->show_branch = -1; /* unspecified */ |
c1b5d019 | 148 | s->show_stash = 0; |
fd9b544a | 149 | s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED; |
2556b996 | 150 | s->display_comment_prefix = 0; |
e8b2dc2c BP |
151 | s->detect_rename = -1; |
152 | s->rename_score = -1; | |
153 | s->rename_limit = -1; | |
c91f0d92 JK |
154 | } |
155 | ||
957a0fe2 | 156 | static void wt_longstatus_print_unmerged_header(struct wt_status *s) |
4d4d5726 | 157 | { |
96b0ec1a LK |
158 | int i; |
159 | int del_mod_conflict = 0; | |
160 | int both_deleted = 0; | |
161 | int not_deleted = 0; | |
d249b098 | 162 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 163 | |
355ec7a1 | 164 | status_printf_ln(s, c, _("Unmerged paths:")); |
96b0ec1a LK |
165 | |
166 | for (i = 0; i < s->change.nr; i++) { | |
167 | struct string_list_item *it = &(s->change.items[i]); | |
168 | struct wt_status_change_data *d = it->util; | |
169 | ||
170 | switch (d->stagemask) { | |
171 | case 0: | |
172 | break; | |
173 | case 1: | |
174 | both_deleted = 1; | |
175 | break; | |
176 | case 3: | |
177 | case 5: | |
178 | del_mod_conflict = 1; | |
179 | break; | |
180 | default: | |
181 | not_deleted = 1; | |
182 | break; | |
183 | } | |
184 | } | |
185 | ||
6a964f57 | 186 | if (!s->hints) |
edf563fb | 187 | return; |
37f7a857 | 188 | if (s->whence != FROM_COMMIT) |
3c588453 | 189 | ; |
80f537f7 NTND |
190 | else if (!s->is_initial) { |
191 | if (!strcmp(s->reference, "HEAD")) | |
192 | status_printf_ln(s, c, | |
193 | _(" (use \"git restore --staged <file>...\" to unstage)")); | |
194 | else | |
195 | status_printf_ln(s, c, | |
196 | _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"), | |
197 | s->reference); | |
198 | } else | |
355ec7a1 | 199 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); |
96b0ec1a LK |
200 | |
201 | if (!both_deleted) { | |
202 | if (!del_mod_conflict) | |
203 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)")); | |
204 | else | |
205 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); | |
206 | } else if (!del_mod_conflict && !not_deleted) { | |
207 | status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)")); | |
208 | } else { | |
209 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); | |
210 | } | |
4d4d5726 JH |
211 | } |
212 | ||
957a0fe2 | 213 | static void wt_longstatus_print_cached_header(struct wt_status *s) |
3c1eb9cb | 214 | { |
d249b098 | 215 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 216 | |
919a4ce0 | 217 | status_printf_ln(s, c, _("Changes to be committed:")); |
6a964f57 | 218 | if (!s->hints) |
edf563fb | 219 | return; |
37f7a857 | 220 | if (s->whence != FROM_COMMIT) |
3c588453 | 221 | ; /* NEEDSWORK: use "git reset --unresolve"??? */ |
80f537f7 NTND |
222 | else if (!s->is_initial) { |
223 | if (!strcmp(s->reference, "HEAD")) | |
224 | status_printf_ln(s, c | |
225 | , _(" (use \"git restore --staged <file>...\" to unstage)")); | |
226 | else | |
227 | status_printf_ln(s, c, | |
228 | _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"), | |
229 | s->reference); | |
230 | } else | |
355ec7a1 | 231 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); |
3c1eb9cb JR |
232 | } |
233 | ||
957a0fe2 JH |
234 | static void wt_longstatus_print_dirty_header(struct wt_status *s, |
235 | int has_deleted, | |
236 | int has_dirty_submodules) | |
c91f0d92 | 237 | { |
d249b098 | 238 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 239 | |
355ec7a1 | 240 | status_printf_ln(s, c, _("Changes not staged for commit:")); |
6a964f57 | 241 | if (!s->hints) |
edf563fb | 242 | return; |
bb914b14 | 243 | if (!has_deleted) |
355ec7a1 | 244 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)")); |
bb914b14 | 245 | else |
355ec7a1 | 246 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)")); |
80f537f7 | 247 | status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)")); |
9297f77e | 248 | if (has_dirty_submodules) |
355ec7a1 | 249 | status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)")); |
bb914b14 AM |
250 | } |
251 | ||
957a0fe2 JH |
252 | static void wt_longstatus_print_other_header(struct wt_status *s, |
253 | const char *what, | |
254 | const char *how) | |
bb914b14 | 255 | { |
d249b098 | 256 | const char *c = color(WT_STATUS_HEADER, s); |
50bd8b7e | 257 | status_printf_ln(s, c, "%s:", what); |
6a964f57 | 258 | if (!s->hints) |
edf563fb | 259 | return; |
355ec7a1 | 260 | status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how); |
c91f0d92 JK |
261 | } |
262 | ||
957a0fe2 | 263 | static void wt_longstatus_print_trailer(struct wt_status *s) |
c91f0d92 | 264 | { |
7d7d6802 | 265 | status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", ""); |
c91f0d92 JK |
266 | } |
267 | ||
8f17f5b2 | 268 | static const char *wt_status_unmerged_status_string(int stagemask) |
4d4d5726 | 269 | { |
8f17f5b2 JN |
270 | switch (stagemask) { |
271 | case 1: | |
272 | return _("both deleted:"); | |
273 | case 2: | |
274 | return _("added by us:"); | |
275 | case 3: | |
276 | return _("deleted by them:"); | |
277 | case 4: | |
278 | return _("added by them:"); | |
279 | case 5: | |
280 | return _("deleted by us:"); | |
281 | case 6: | |
282 | return _("both added:"); | |
283 | case 7: | |
284 | return _("both modified:"); | |
285 | default: | |
033abf97 | 286 | BUG("unhandled unmerged status %x", stagemask); |
4d4d5726 | 287 | } |
4d4d5726 JH |
288 | } |
289 | ||
3651e45c NTND |
290 | static const char *wt_status_diff_status_string(int status) |
291 | { | |
292 | switch (status) { | |
293 | case DIFF_STATUS_ADDED: | |
d52cb576 | 294 | return _("new file:"); |
3651e45c | 295 | case DIFF_STATUS_COPIED: |
d52cb576 | 296 | return _("copied:"); |
3651e45c | 297 | case DIFF_STATUS_DELETED: |
d52cb576 | 298 | return _("deleted:"); |
3651e45c | 299 | case DIFF_STATUS_MODIFIED: |
d52cb576 | 300 | return _("modified:"); |
3651e45c | 301 | case DIFF_STATUS_RENAMED: |
d52cb576 | 302 | return _("renamed:"); |
3651e45c | 303 | case DIFF_STATUS_TYPE_CHANGED: |
d52cb576 | 304 | return _("typechange:"); |
3651e45c | 305 | case DIFF_STATUS_UNKNOWN: |
d52cb576 | 306 | return _("unknown:"); |
3651e45c | 307 | case DIFF_STATUS_UNMERGED: |
d52cb576 | 308 | return _("unmerged:"); |
3651e45c NTND |
309 | default: |
310 | return NULL; | |
311 | } | |
312 | } | |
313 | ||
335e8250 JN |
314 | static int maxwidth(const char *(*label)(int), int minval, int maxval) |
315 | { | |
316 | int result = 0, i; | |
317 | ||
318 | for (i = minval; i <= maxval; i++) { | |
319 | const char *s = label(i); | |
320 | int len = s ? utf8_strwidth(s) : 0; | |
321 | if (len > result) | |
322 | result = len; | |
323 | } | |
324 | return result; | |
325 | } | |
326 | ||
957a0fe2 JH |
327 | static void wt_longstatus_print_unmerged_data(struct wt_status *s, |
328 | struct string_list_item *it) | |
8f17f5b2 JN |
329 | { |
330 | const char *c = color(WT_STATUS_UNMERGED, s); | |
331 | struct wt_status_change_data *d = it->util; | |
332 | struct strbuf onebuf = STRBUF_INIT; | |
333 | static char *padding; | |
334 | static int label_width; | |
335 | const char *one, *how; | |
336 | int len; | |
337 | ||
338 | if (!padding) { | |
339 | label_width = maxwidth(wt_status_unmerged_status_string, 1, 7); | |
340 | label_width += strlen(" "); | |
8f17f5b2 JN |
341 | padding = xmallocz(label_width); |
342 | memset(padding, ' ', label_width); | |
343 | } | |
344 | ||
88910c99 | 345 | one = quote_path(it->string, s->prefix, &onebuf, 0); |
8f17f5b2 JN |
346 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
347 | ||
348 | how = wt_status_unmerged_status_string(d->stagemask); | |
349 | len = label_width - utf8_strwidth(how); | |
350 | status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one); | |
351 | strbuf_release(&onebuf); | |
352 | } | |
353 | ||
957a0fe2 JH |
354 | static void wt_longstatus_print_change_data(struct wt_status *s, |
355 | int change_type, | |
356 | struct string_list_item *it) | |
c91f0d92 | 357 | { |
50b7e70f | 358 | struct wt_status_change_data *d = it->util; |
d249b098 | 359 | const char *c = color(change_type, s); |
b8527d5f | 360 | int status; |
50b7e70f JH |
361 | char *one_name; |
362 | char *two_name; | |
3a946802 | 363 | const char *one, *two; |
f285a2d7 | 364 | struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT; |
9297f77e | 365 | struct strbuf extra = STRBUF_INIT; |
3651e45c | 366 | static char *padding; |
d52cb576 | 367 | static int label_width; |
3651e45c NTND |
368 | const char *what; |
369 | int len; | |
370 | ||
371 | if (!padding) { | |
335e8250 JN |
372 | /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */ |
373 | label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z'); | |
d52cb576 JH |
374 | label_width += strlen(" "); |
375 | padding = xmallocz(label_width); | |
376 | memset(padding, ' ', label_width); | |
3651e45c | 377 | } |
3a946802 | 378 | |
50b7e70f JH |
379 | one_name = two_name = it->string; |
380 | switch (change_type) { | |
381 | case WT_STATUS_UPDATED: | |
382 | status = d->index_status; | |
50b7e70f JH |
383 | break; |
384 | case WT_STATUS_CHANGED: | |
9297f77e JL |
385 | if (d->new_submodule_commits || d->dirty_submodule) { |
386 | strbuf_addstr(&extra, " ("); | |
387 | if (d->new_submodule_commits) | |
a22ae753 | 388 | strbuf_addstr(&extra, _("new commits, ")); |
9297f77e | 389 | if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
a22ae753 | 390 | strbuf_addstr(&extra, _("modified content, ")); |
9297f77e | 391 | if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
a22ae753 | 392 | strbuf_addstr(&extra, _("untracked content, ")); |
9297f77e JL |
393 | strbuf_setlen(&extra, extra.len - 2); |
394 | strbuf_addch(&extra, ')'); | |
395 | } | |
50b7e70f JH |
396 | status = d->worktree_status; |
397 | break; | |
b8527d5f | 398 | default: |
033abf97 | 399 | BUG("unhandled change_type %d in wt_longstatus_print_change_data", |
b8527d5f | 400 | change_type); |
50b7e70f JH |
401 | } |
402 | ||
176ea747 NTND |
403 | /* |
404 | * Only pick up the rename it's relevant. If the rename is for | |
405 | * the changed section and we're printing the updated section, | |
406 | * ignore it. | |
407 | */ | |
408 | if (d->rename_status == status) | |
409 | one_name = d->rename_source; | |
410 | ||
88910c99 JH |
411 | one = quote_path(one_name, s->prefix, &onebuf, 0); |
412 | two = quote_path(two_name, s->prefix, &twobuf, 0); | |
3a946802 | 413 | |
b926c0d1 | 414 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
3651e45c NTND |
415 | what = wt_status_diff_status_string(status); |
416 | if (!what) | |
033abf97 | 417 | BUG("unhandled diff status %c", status); |
d52cb576 | 418 | len = label_width - utf8_strwidth(what); |
3651e45c | 419 | assert(len >= 0); |
5134ccde | 420 | if (one_name != two_name) |
d52cb576 | 421 | status_printf_more(s, c, "%s%.*s%s -> %s", |
3651e45c NTND |
422 | what, len, padding, one, two); |
423 | else | |
d52cb576 | 424 | status_printf_more(s, c, "%s%.*s%s", |
3651e45c | 425 | what, len, padding, one); |
9297f77e | 426 | if (extra.len) { |
b926c0d1 | 427 | status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf); |
9297f77e JL |
428 | strbuf_release(&extra); |
429 | } | |
b926c0d1 | 430 | status_printf_more(s, GIT_COLOR_NORMAL, "\n"); |
367c9886 JS |
431 | strbuf_release(&onebuf); |
432 | strbuf_release(&twobuf); | |
c91f0d92 JK |
433 | } |
434 | ||
98bc94ec NTND |
435 | static char short_submodule_status(struct wt_status_change_data *d) |
436 | { | |
dd6962dd SB |
437 | if (d->new_submodule_commits) |
438 | return 'M'; | |
439 | if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) | |
440 | return 'm'; | |
441 | if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) | |
442 | return '?'; | |
443 | return d->worktree_status; | |
444 | } | |
445 | ||
50b7e70f | 446 | static void wt_status_collect_changed_cb(struct diff_queue_struct *q, |
61bdc7c5 | 447 | struct diff_options *options UNUSED, |
50b7e70f | 448 | void *data) |
c91f0d92 JK |
449 | { |
450 | struct wt_status *s = data; | |
c91f0d92 | 451 | int i; |
50b7e70f JH |
452 | |
453 | if (!q->nr) | |
454 | return; | |
455 | s->workdir_dirty = 1; | |
c91f0d92 | 456 | for (i = 0; i < q->nr; i++) { |
50b7e70f JH |
457 | struct diff_filepair *p; |
458 | struct string_list_item *it; | |
459 | struct wt_status_change_data *d; | |
460 | ||
461 | p = q->queue[i]; | |
176ea747 | 462 | it = string_list_insert(&s->change, p->two->path); |
50b7e70f JH |
463 | d = it->util; |
464 | if (!d) { | |
ca56dadb | 465 | CALLOC_ARRAY(d, 1); |
50b7e70f | 466 | it->util = d; |
c91f0d92 | 467 | } |
50b7e70f JH |
468 | if (!d->worktree_status) |
469 | d->worktree_status = p->status; | |
dd6962dd SB |
470 | if (S_ISGITLINK(p->two->mode)) { |
471 | d->dirty_submodule = p->two->dirty_submodule; | |
4a7e27e9 JK |
472 | d->new_submodule_commits = !oideq(&p->one->oid, |
473 | &p->two->oid); | |
dd6962dd SB |
474 | if (s->status_format == STATUS_FORMAT_SHORT) |
475 | d->worktree_status = short_submodule_status(d); | |
476 | } | |
1ecdecce JH |
477 | |
478 | switch (p->status) { | |
479 | case DIFF_STATUS_ADDED: | |
425a28e0 | 480 | d->mode_worktree = p->two->mode; |
1ecdecce JH |
481 | break; |
482 | ||
483 | case DIFF_STATUS_DELETED: | |
484 | d->mode_index = p->one->mode; | |
485 | oidcpy(&d->oid_index, &p->one->oid); | |
486 | /* mode_worktree is zero for a delete. */ | |
487 | break; | |
488 | ||
176ea747 NTND |
489 | case DIFF_STATUS_COPIED: |
490 | case DIFF_STATUS_RENAMED: | |
491 | if (d->rename_status) | |
033abf97 | 492 | BUG("multiple renames on the same target? how?"); |
176ea747 NTND |
493 | d->rename_source = xstrdup(p->one->path); |
494 | d->rename_score = p->score * 100 / MAX_SCORE; | |
495 | d->rename_status = p->status; | |
496 | /* fallthru */ | |
1ecdecce JH |
497 | case DIFF_STATUS_MODIFIED: |
498 | case DIFF_STATUS_TYPE_CHANGED: | |
499 | case DIFF_STATUS_UNMERGED: | |
500 | d->mode_index = p->one->mode; | |
501 | d->mode_worktree = p->two->mode; | |
502 | oidcpy(&d->oid_index, &p->one->oid); | |
503 | break; | |
504 | ||
ea56f977 | 505 | default: |
033abf97 | 506 | BUG("unhandled diff-files status '%c'", p->status); |
1ecdecce JH |
507 | break; |
508 | } | |
509 | ||
c91f0d92 | 510 | } |
c91f0d92 JK |
511 | } |
512 | ||
5b02ca38 | 513 | static int unmerged_mask(struct index_state *istate, const char *path) |
4d4d5726 JH |
514 | { |
515 | int pos, mask; | |
9c5e6c80 | 516 | const struct cache_entry *ce; |
4d4d5726 | 517 | |
5b02ca38 | 518 | pos = index_name_pos(istate, path, strlen(path)); |
4d4d5726 JH |
519 | if (0 <= pos) |
520 | return 0; | |
521 | ||
522 | mask = 0; | |
523 | pos = -pos-1; | |
5b02ca38 NTND |
524 | while (pos < istate->cache_nr) { |
525 | ce = istate->cache[pos++]; | |
4d4d5726 JH |
526 | if (strcmp(ce->name, path) || !ce_stage(ce)) |
527 | break; | |
528 | mask |= (1 << (ce_stage(ce) - 1)); | |
529 | } | |
530 | return mask; | |
531 | } | |
532 | ||
50b7e70f | 533 | static void wt_status_collect_updated_cb(struct diff_queue_struct *q, |
61bdc7c5 | 534 | struct diff_options *options UNUSED, |
50b7e70f | 535 | void *data) |
c91f0d92 | 536 | { |
6e458bf6 | 537 | struct wt_status *s = data; |
c91f0d92 | 538 | int i; |
50b7e70f JH |
539 | |
540 | for (i = 0; i < q->nr; i++) { | |
541 | struct diff_filepair *p; | |
542 | struct string_list_item *it; | |
543 | struct wt_status_change_data *d; | |
544 | ||
545 | p = q->queue[i]; | |
78a395d3 | 546 | it = string_list_insert(&s->change, p->two->path); |
50b7e70f JH |
547 | d = it->util; |
548 | if (!d) { | |
ca56dadb | 549 | CALLOC_ARRAY(d, 1); |
50b7e70f JH |
550 | it->util = d; |
551 | } | |
552 | if (!d->index_status) | |
553 | d->index_status = p->status; | |
554 | switch (p->status) { | |
1ecdecce JH |
555 | case DIFF_STATUS_ADDED: |
556 | /* Leave {mode,oid}_head zero for an add. */ | |
557 | d->mode_index = p->two->mode; | |
558 | oidcpy(&d->oid_index, &p->two->oid); | |
f3bd35fa | 559 | s->committable = 1; |
1ecdecce JH |
560 | break; |
561 | case DIFF_STATUS_DELETED: | |
562 | d->mode_head = p->one->mode; | |
563 | oidcpy(&d->oid_head, &p->one->oid); | |
f3bd35fa | 564 | s->committable = 1; |
1ecdecce JH |
565 | /* Leave {mode,oid}_index zero for a delete. */ |
566 | break; | |
567 | ||
50b7e70f JH |
568 | case DIFF_STATUS_COPIED: |
569 | case DIFF_STATUS_RENAMED: | |
176ea747 | 570 | if (d->rename_status) |
033abf97 | 571 | BUG("multiple renames on the same target? how?"); |
5134ccde NTND |
572 | d->rename_source = xstrdup(p->one->path); |
573 | d->rename_score = p->score * 100 / MAX_SCORE; | |
574 | d->rename_status = p->status; | |
1ecdecce JH |
575 | /* fallthru */ |
576 | case DIFF_STATUS_MODIFIED: | |
577 | case DIFF_STATUS_TYPE_CHANGED: | |
578 | d->mode_head = p->one->mode; | |
579 | d->mode_index = p->two->mode; | |
580 | oidcpy(&d->oid_head, &p->one->oid); | |
581 | oidcpy(&d->oid_index, &p->two->oid); | |
f3bd35fa | 582 | s->committable = 1; |
50b7e70f | 583 | break; |
4d4d5726 | 584 | case DIFF_STATUS_UNMERGED: |
5b02ca38 NTND |
585 | d->stagemask = unmerged_mask(s->repo->index, |
586 | p->two->path); | |
1ecdecce JH |
587 | /* |
588 | * Don't bother setting {mode,oid}_{head,index} since the print | |
589 | * code will output the stage values directly and not use the | |
590 | * values in these fields. | |
591 | */ | |
4d4d5726 | 592 | break; |
ea56f977 NTND |
593 | |
594 | default: | |
033abf97 | 595 | BUG("unhandled diff-index status '%c'", p->status); |
ea56f977 | 596 | break; |
50b7e70f | 597 | } |
6e458bf6 | 598 | } |
c91f0d92 JK |
599 | } |
600 | ||
50b7e70f | 601 | static void wt_status_collect_changes_worktree(struct wt_status *s) |
c91f0d92 JK |
602 | { |
603 | struct rev_info rev; | |
50b7e70f | 604 | |
5b02ca38 | 605 | repo_init_revisions(s->repo, &rev, NULL); |
50b7e70f JH |
606 | setup_revisions(0, NULL, &rev, NULL); |
607 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
0d1e0e78 | 608 | rev.diffopt.flags.dirty_submodules = 1; |
425a28e0 | 609 | rev.diffopt.ita_invisible_in_index = 1; |
3bfc4504 | 610 | if (!s->show_untracked_files) |
0d1e0e78 | 611 | rev.diffopt.flags.ignore_untracked_in_submodules = 1; |
aee9c7d6 | 612 | if (s->ignore_submodule_arg) { |
0d1e0e78 | 613 | rev.diffopt.flags.override_submodule_config = 1; |
46a958b3 | 614 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); |
8ef93124 SJ |
615 | } else if (!rev.diffopt.flags.ignore_submodule_set && |
616 | s->show_untracked_files != SHOW_NO_UNTRACKED_FILES) | |
617 | handle_ignore_submodules_arg(&rev.diffopt, "none"); | |
50b7e70f JH |
618 | rev.diffopt.format_callback = wt_status_collect_changed_cb; |
619 | rev.diffopt.format_callback_data = s; | |
e8b2dc2c BP |
620 | rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename; |
621 | rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit; | |
622 | rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score; | |
15b55ae0 | 623 | copy_pathspec(&rev.prune_data, &s->pathspec); |
50b7e70f | 624 | run_diff_files(&rev, 0); |
f0cb6b80 | 625 | release_revisions(&rev); |
50b7e70f JH |
626 | } |
627 | ||
628 | static void wt_status_collect_changes_index(struct wt_status *s) | |
629 | { | |
630 | struct rev_info rev; | |
32962c9b | 631 | struct setup_revision_opt opt; |
50b7e70f | 632 | |
5b02ca38 | 633 | repo_init_revisions(s->repo, &rev, NULL); |
32962c9b | 634 | memset(&opt, 0, sizeof(opt)); |
f2e51195 | 635 | opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference; |
32962c9b JH |
636 | setup_revisions(0, NULL, &rev, &opt); |
637 | ||
0d1e0e78 | 638 | rev.diffopt.flags.override_submodule_config = 1; |
425a28e0 | 639 | rev.diffopt.ita_invisible_in_index = 1; |
aee9c7d6 | 640 | if (s->ignore_submodule_arg) { |
46a958b3 | 641 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); |
1d2f393a JL |
642 | } else { |
643 | /* | |
644 | * Unless the user did explicitly request a submodule ignore | |
645 | * mode by passing a command line option we do not ignore any | |
646 | * changed submodule SHA-1s when comparing index and HEAD, no | |
647 | * matter what is configured. Otherwise the user won't be | |
0e20b229 | 648 | * shown any submodules manually added (and which are |
1d2f393a JL |
649 | * staged to be committed), which would be really confusing. |
650 | */ | |
651 | handle_ignore_submodules_arg(&rev.diffopt, "dirty"); | |
aee9c7d6 | 652 | } |
46a958b3 | 653 | |
c91f0d92 | 654 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
50b7e70f | 655 | rev.diffopt.format_callback = wt_status_collect_updated_cb; |
c91f0d92 | 656 | rev.diffopt.format_callback_data = s; |
e8b2dc2c BP |
657 | rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename; |
658 | rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit; | |
659 | rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score; | |
2c521b0e VD |
660 | |
661 | /* | |
662 | * The `recursive` option must be enabled to allow the diff to recurse | |
663 | * into subdirectories of sparse directory index entries. If it is not | |
664 | * enabled, a subdirectory containing file(s) with changes is reported | |
665 | * as "modified", rather than the modified files themselves. | |
666 | */ | |
667 | rev.diffopt.flags.recursive = 1; | |
668 | ||
15b55ae0 | 669 | copy_pathspec(&rev.prune_data, &s->pathspec); |
c91f0d92 | 670 | run_diff_index(&rev, 1); |
1878b5ed | 671 | release_revisions(&rev); |
c91f0d92 JK |
672 | } |
673 | ||
fe0d5761 DS |
674 | static int add_file_to_list(const struct object_id *oid, |
675 | struct strbuf *base, const char *path, | |
676 | unsigned int mode, void *context) | |
677 | { | |
678 | struct string_list_item *it; | |
679 | struct wt_status_change_data *d; | |
680 | struct wt_status *s = context; | |
681 | struct strbuf full_name = STRBUF_INIT; | |
682 | ||
683 | if (S_ISDIR(mode)) | |
684 | return READ_TREE_RECURSIVE; | |
685 | ||
686 | strbuf_add(&full_name, base->buf, base->len); | |
687 | strbuf_addstr(&full_name, path); | |
688 | it = string_list_insert(&s->change, full_name.buf); | |
689 | d = it->util; | |
690 | if (!d) { | |
691 | CALLOC_ARRAY(d, 1); | |
692 | it->util = d; | |
693 | } | |
694 | ||
695 | d->index_status = DIFF_STATUS_ADDED; | |
696 | /* Leave {mode,oid}_head zero for adds. */ | |
697 | d->mode_index = mode; | |
698 | oidcpy(&d->oid_index, oid); | |
699 | s->committable = 1; | |
700 | strbuf_release(&full_name); | |
701 | return 0; | |
702 | } | |
703 | ||
50b7e70f JH |
704 | static void wt_status_collect_changes_initial(struct wt_status *s) |
705 | { | |
5b02ca38 | 706 | struct index_state *istate = s->repo->index; |
50b7e70f JH |
707 | int i; |
708 | ||
5b02ca38 | 709 | for (i = 0; i < istate->cache_nr; i++) { |
50b7e70f JH |
710 | struct string_list_item *it; |
711 | struct wt_status_change_data *d; | |
5b02ca38 | 712 | const struct cache_entry *ce = istate->cache[i]; |
50b7e70f | 713 | |
5b02ca38 | 714 | if (!ce_path_match(istate, ce, &s->pathspec, NULL)) |
76e2f7ce | 715 | continue; |
425a28e0 NTND |
716 | if (ce_intent_to_add(ce)) |
717 | continue; | |
fe0d5761 DS |
718 | if (S_ISSPARSEDIR(ce->ce_mode)) { |
719 | /* | |
720 | * This is a sparse directory entry, so we want to collect all | |
721 | * of the added files within the tree. This requires recursively | |
722 | * expanding the trees to find the elements that are new in this | |
723 | * tree and marking them with DIFF_STATUS_ADDED. | |
724 | */ | |
725 | struct strbuf base = STRBUF_INIT; | |
726 | struct pathspec ps = { 0 }; | |
727 | struct tree *tree = lookup_tree(istate->repo, &ce->oid); | |
728 | ||
729 | ps.recursive = 1; | |
730 | ps.has_wildcard = 1; | |
731 | ps.max_depth = -1; | |
732 | ||
733 | strbuf_add(&base, ce->name, ce->ce_namelen); | |
734 | read_tree_at(istate->repo, tree, &base, &ps, | |
735 | add_file_to_list, s); | |
736 | continue; | |
737 | } | |
738 | ||
78a395d3 | 739 | it = string_list_insert(&s->change, ce->name); |
50b7e70f JH |
740 | d = it->util; |
741 | if (!d) { | |
ca56dadb | 742 | CALLOC_ARRAY(d, 1); |
50b7e70f JH |
743 | it->util = d; |
744 | } | |
4d4d5726 | 745 | if (ce_stage(ce)) { |
50b7e70f | 746 | d->index_status = DIFF_STATUS_UNMERGED; |
4d4d5726 | 747 | d->stagemask |= (1 << (ce_stage(ce) - 1)); |
1ecdecce JH |
748 | /* |
749 | * Don't bother setting {mode,oid}_{head,index} since the print | |
750 | * code will output the stage values directly and not use the | |
751 | * values in these fields. | |
752 | */ | |
f3bd35fa | 753 | s->committable = 1; |
1ecdecce | 754 | } else { |
50b7e70f | 755 | d->index_status = DIFF_STATUS_ADDED; |
1ecdecce JH |
756 | /* Leave {mode,oid}_head zero for adds. */ |
757 | d->mode_index = ce->ce_mode; | |
8694769f | 758 | oidcpy(&d->oid_index, &ce->oid); |
f3bd35fa | 759 | s->committable = 1; |
1ecdecce | 760 | } |
50b7e70f JH |
761 | } |
762 | } | |
763 | ||
76378683 JH |
764 | static void wt_status_collect_untracked(struct wt_status *s) |
765 | { | |
766 | int i; | |
ce93a4c6 | 767 | struct dir_struct dir = DIR_INIT; |
132d41e6 | 768 | uint64_t t_begin = getnanotime(); |
5b02ca38 | 769 | struct index_state *istate = s->repo->index; |
76378683 JH |
770 | |
771 | if (!s->show_untracked_files) | |
772 | return; | |
6a38ef2c | 773 | |
76378683 JH |
774 | if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES) |
775 | dir.flags |= | |
776 | DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; | |
eec0f7f2 | 777 | if (s->show_ignored_mode) { |
0aaf62b6 | 778 | dir.flags |= DIR_SHOW_IGNORED_TOO; |
eec0f7f2 JM |
779 | |
780 | if (s->show_ignored_mode == SHOW_MATCHING_IGNORED) | |
781 | dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING; | |
782 | } else { | |
5b02ca38 | 783 | dir.untracked = istate->untracked; |
eec0f7f2 JM |
784 | } |
785 | ||
76378683 JH |
786 | setup_standard_excludes(&dir); |
787 | ||
5b02ca38 | 788 | fill_directory(&dir, istate, &s->pathspec); |
0aaf62b6 | 789 | |
eeefa7c9 | 790 | for (i = 0; i < dir.nr; i++) { |
76378683 | 791 | struct dir_entry *ent = dir.entries[i]; |
95c11ecc | 792 | if (index_name_is_other(istate, ent->name, ent->len)) |
b822423e | 793 | string_list_insert(&s->untracked, ent->name); |
76378683 | 794 | } |
f5b26b1d | 795 | |
0aaf62b6 KB |
796 | for (i = 0; i < dir.ignored_nr; i++) { |
797 | struct dir_entry *ent = dir.ignored[i]; | |
95c11ecc | 798 | if (index_name_is_other(istate, ent->name, ent->len)) |
0aaf62b6 | 799 | string_list_insert(&s->ignored, ent->name); |
6cb3f6b2 JH |
800 | } |
801 | ||
eceba532 | 802 | dir_clear(&dir); |
6a38ef2c | 803 | |
ed9bff08 | 804 | if (advice_enabled(ADVICE_STATUS_U_OPTION)) |
132d41e6 | 805 | s->untracked_in_ms = (getnanotime() - t_begin) / 1000000; |
76378683 JH |
806 | } |
807 | ||
c01d8f94 SS |
808 | static int has_unmerged(struct wt_status *s) |
809 | { | |
810 | int i; | |
811 | ||
812 | for (i = 0; i < s->change.nr; i++) { | |
813 | struct wt_status_change_data *d; | |
814 | d = s->change.items[i].util; | |
815 | if (d->stagemask) | |
816 | return 1; | |
817 | } | |
818 | return 0; | |
819 | } | |
820 | ||
76378683 | 821 | void wt_status_collect(struct wt_status *s) |
50b7e70f | 822 | { |
942b2740 | 823 | trace2_region_enter("status", "worktrees", s->repo); |
50b7e70f | 824 | wt_status_collect_changes_worktree(s); |
942b2740 JH |
825 | trace2_region_leave("status", "worktrees", s->repo); |
826 | ||
827 | if (s->is_initial) { | |
828 | trace2_region_enter("status", "initial", s->repo); | |
50b7e70f | 829 | wt_status_collect_changes_initial(s); |
942b2740 JH |
830 | trace2_region_leave("status", "initial", s->repo); |
831 | } else { | |
832 | trace2_region_enter("status", "index", s->repo); | |
50b7e70f | 833 | wt_status_collect_changes_index(s); |
942b2740 JH |
834 | trace2_region_leave("status", "index", s->repo); |
835 | } | |
836 | ||
837 | trace2_region_enter("status", "untracked", s->repo); | |
76378683 | 838 | wt_status_collect_untracked(s); |
942b2740 | 839 | trace2_region_leave("status", "untracked", s->repo); |
f3bd35fa | 840 | |
78845457 | 841 | wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD")); |
73ba5d78 | 842 | if (s->state.merge_in_progress && !has_unmerged(s)) |
f3bd35fa | 843 | s->committable = 1; |
50b7e70f JH |
844 | } |
845 | ||
73ba5d78 SS |
846 | void wt_status_collect_free_buffers(struct wt_status *s) |
847 | { | |
962dd7eb MÅ |
848 | wt_status_state_free_buffers(&s->state); |
849 | } | |
850 | ||
851 | void wt_status_state_free_buffers(struct wt_status_state *state) | |
852 | { | |
853 | FREE_AND_NULL(state->branch); | |
854 | FREE_AND_NULL(state->onto); | |
855 | FREE_AND_NULL(state->detached_from); | |
50b7e70f JH |
856 | } |
857 | ||
957a0fe2 | 858 | static void wt_longstatus_print_unmerged(struct wt_status *s) |
4d4d5726 JH |
859 | { |
860 | int shown_header = 0; | |
861 | int i; | |
862 | ||
863 | for (i = 0; i < s->change.nr; i++) { | |
864 | struct wt_status_change_data *d; | |
865 | struct string_list_item *it; | |
866 | it = &(s->change.items[i]); | |
867 | d = it->util; | |
868 | if (!d->stagemask) | |
869 | continue; | |
870 | if (!shown_header) { | |
957a0fe2 | 871 | wt_longstatus_print_unmerged_header(s); |
4d4d5726 JH |
872 | shown_header = 1; |
873 | } | |
957a0fe2 | 874 | wt_longstatus_print_unmerged_data(s, it); |
4d4d5726 JH |
875 | } |
876 | if (shown_header) | |
957a0fe2 | 877 | wt_longstatus_print_trailer(s); |
4d4d5726 JH |
878 | |
879 | } | |
880 | ||
957a0fe2 | 881 | static void wt_longstatus_print_updated(struct wt_status *s) |
50b7e70f JH |
882 | { |
883 | int shown_header = 0; | |
884 | int i; | |
885 | ||
886 | for (i = 0; i < s->change.nr; i++) { | |
887 | struct wt_status_change_data *d; | |
888 | struct string_list_item *it; | |
889 | it = &(s->change.items[i]); | |
890 | d = it->util; | |
891 | if (!d->index_status || | |
892 | d->index_status == DIFF_STATUS_UNMERGED) | |
893 | continue; | |
894 | if (!shown_header) { | |
957a0fe2 | 895 | wt_longstatus_print_cached_header(s); |
50b7e70f JH |
896 | shown_header = 1; |
897 | } | |
957a0fe2 | 898 | wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it); |
50b7e70f JH |
899 | } |
900 | if (shown_header) | |
957a0fe2 | 901 | wt_longstatus_print_trailer(s); |
50b7e70f JH |
902 | } |
903 | ||
904 | /* | |
905 | * -1 : has delete | |
906 | * 0 : no change | |
907 | * 1 : some change but no delete | |
908 | */ | |
9297f77e JL |
909 | static int wt_status_check_worktree_changes(struct wt_status *s, |
910 | int *dirty_submodules) | |
50b7e70f JH |
911 | { |
912 | int i; | |
913 | int changes = 0; | |
914 | ||
9297f77e JL |
915 | *dirty_submodules = 0; |
916 | ||
50b7e70f JH |
917 | for (i = 0; i < s->change.nr; i++) { |
918 | struct wt_status_change_data *d; | |
919 | d = s->change.items[i].util; | |
4d4d5726 JH |
920 | if (!d->worktree_status || |
921 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f | 922 | continue; |
9297f77e JL |
923 | if (!changes) |
924 | changes = 1; | |
925 | if (d->dirty_submodule) | |
926 | *dirty_submodules = 1; | |
50b7e70f | 927 | if (d->worktree_status == DIFF_STATUS_DELETED) |
9297f77e | 928 | changes = -1; |
50b7e70f JH |
929 | } |
930 | return changes; | |
931 | } | |
932 | ||
957a0fe2 | 933 | static void wt_longstatus_print_changed(struct wt_status *s) |
c91f0d92 | 934 | { |
9297f77e JL |
935 | int i, dirty_submodules; |
936 | int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules); | |
50b7e70f JH |
937 | |
938 | if (!worktree_changes) | |
939 | return; | |
940 | ||
957a0fe2 | 941 | wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules); |
50b7e70f JH |
942 | |
943 | for (i = 0; i < s->change.nr; i++) { | |
944 | struct wt_status_change_data *d; | |
945 | struct string_list_item *it; | |
946 | it = &(s->change.items[i]); | |
947 | d = it->util; | |
4d4d5726 JH |
948 | if (!d->worktree_status || |
949 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f | 950 | continue; |
957a0fe2 | 951 | wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it); |
50b7e70f | 952 | } |
957a0fe2 | 953 | wt_longstatus_print_trailer(s); |
c91f0d92 JK |
954 | } |
955 | ||
5cf88fd8 ÆAB |
956 | static int stash_count_refs(struct object_id *ooid UNUSED, |
957 | struct object_id *noid UNUSED, | |
958 | const char *email UNUSED, | |
959 | timestamp_t timestamp UNUSED, int tz UNUSED, | |
960 | const char *message UNUSED, void *cb_data) | |
c1b5d019 LB |
961 | { |
962 | int *c = cb_data; | |
963 | (*c)++; | |
964 | return 0; | |
965 | } | |
966 | ||
612942a1 ØW |
967 | static int count_stash_entries(void) |
968 | { | |
969 | int n = 0; | |
970 | for_each_reflog_ent("refs/stash", stash_count_refs, &n); | |
971 | return n; | |
972 | } | |
973 | ||
c1b5d019 LB |
974 | static void wt_longstatus_print_stash_summary(struct wt_status *s) |
975 | { | |
612942a1 | 976 | int stash_count = count_stash_entries(); |
c1b5d019 | 977 | |
c1b5d019 LB |
978 | if (stash_count > 0) |
979 | status_printf_ln(s, GIT_COLOR_NORMAL, | |
980 | Q_("Your stash currently has %d entry", | |
981 | "Your stash currently has %d entries", stash_count), | |
982 | stash_count); | |
983 | } | |
984 | ||
957a0fe2 | 985 | static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted) |
ac8d5afc | 986 | { |
d3180279 | 987 | struct child_process sm_summary = CHILD_PROCESS_INIT; |
3ba7407b MM |
988 | struct strbuf cmd_stdout = STRBUF_INIT; |
989 | struct strbuf summary = STRBUF_INIT; | |
990 | char *summary_content; | |
ac8d5afc | 991 | |
29fda24d | 992 | strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file); |
ac8d5afc | 993 | |
c972bf4c JK |
994 | strvec_push(&sm_summary.args, "submodule"); |
995 | strvec_push(&sm_summary.args, "summary"); | |
996 | strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached"); | |
997 | strvec_push(&sm_summary.args, "--for-status"); | |
998 | strvec_push(&sm_summary.args, "--summary-limit"); | |
999 | strvec_pushf(&sm_summary.args, "%d", s->submodule_summary); | |
bb7e32e3 | 1000 | if (!uncommitted) |
c972bf4c | 1001 | strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD"); |
bb7e32e3 | 1002 | |
ac8d5afc PY |
1003 | sm_summary.git_cmd = 1; |
1004 | sm_summary.no_stdin = 1; | |
3ba7407b | 1005 | |
5c950e9b | 1006 | capture_command(&sm_summary, &cmd_stdout, 1024); |
3ba7407b MM |
1007 | |
1008 | /* prepend header, only if there's an actual output */ | |
d56d966b | 1009 | if (cmd_stdout.len) { |
3ba7407b MM |
1010 | if (uncommitted) |
1011 | strbuf_addstr(&summary, _("Submodules changed but not updated:")); | |
1012 | else | |
1013 | strbuf_addstr(&summary, _("Submodule changes to be committed:")); | |
1014 | strbuf_addstr(&summary, "\n\n"); | |
1015 | } | |
1016 | strbuf_addbuf(&summary, &cmd_stdout); | |
1017 | strbuf_release(&cmd_stdout); | |
1018 | ||
2556b996 | 1019 | if (s->display_comment_prefix) { |
d56d966b | 1020 | size_t len; |
2556b996 MM |
1021 | summary_content = strbuf_detach(&summary, &len); |
1022 | strbuf_add_commented_lines(&summary, summary_content, len); | |
1023 | free(summary_content); | |
1024 | } | |
3ba7407b MM |
1025 | |
1026 | fputs(summary.buf, s->fp); | |
1027 | strbuf_release(&summary); | |
ac8d5afc PY |
1028 | } |
1029 | ||
957a0fe2 JH |
1030 | static void wt_longstatus_print_other(struct wt_status *s, |
1031 | struct string_list *l, | |
1032 | const char *what, | |
1033 | const char *how) | |
c91f0d92 | 1034 | { |
c91f0d92 | 1035 | int i; |
f285a2d7 | 1036 | struct strbuf buf = STRBUF_INIT; |
323d0530 NTND |
1037 | static struct string_list output = STRING_LIST_INIT_DUP; |
1038 | struct column_options copts; | |
c91f0d92 | 1039 | |
1282988b | 1040 | if (!l->nr) |
76378683 | 1041 | return; |
c91f0d92 | 1042 | |
957a0fe2 | 1043 | wt_longstatus_print_other_header(s, what, how); |
1b908b6f JH |
1044 | |
1045 | for (i = 0; i < l->nr; i++) { | |
76378683 | 1046 | struct string_list_item *it; |
323d0530 | 1047 | const char *path; |
1b908b6f | 1048 | it = &(l->items[i]); |
88910c99 | 1049 | path = quote_path(it->string, s->prefix, &buf, 0); |
323d0530 NTND |
1050 | if (column_active(s->colopts)) { |
1051 | string_list_append(&output, path); | |
1052 | continue; | |
1053 | } | |
b926c0d1 JN |
1054 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
1055 | status_printf_more(s, color(WT_STATUS_UNTRACKED, s), | |
323d0530 | 1056 | "%s\n", path); |
c91f0d92 | 1057 | } |
323d0530 NTND |
1058 | |
1059 | strbuf_release(&buf); | |
1060 | if (!column_active(s->colopts)) | |
2f0f7f1c | 1061 | goto conclude; |
323d0530 | 1062 | |
2556b996 | 1063 | strbuf_addf(&buf, "%s%s\t%s", |
323d0530 | 1064 | color(WT_STATUS_HEADER, s), |
2556b996 | 1065 | s->display_comment_prefix ? "#" : "", |
323d0530 NTND |
1066 | color(WT_STATUS_UNTRACKED, s)); |
1067 | memset(&copts, 0, sizeof(copts)); | |
1068 | copts.padding = 1; | |
1069 | copts.indent = buf.buf; | |
1070 | if (want_color(s->use_color)) | |
1071 | copts.nl = GIT_COLOR_RESET "\n"; | |
1072 | print_columns(&output, s->colopts, &copts); | |
1073 | string_list_clear(&output, 0); | |
367c9886 | 1074 | strbuf_release(&buf); |
2f0f7f1c | 1075 | conclude: |
7d7d6802 | 1076 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); |
c91f0d92 JK |
1077 | } |
1078 | ||
d76650b8 | 1079 | size_t wt_status_locate_end(const char *s, size_t len) |
1a72cfd7 JL |
1080 | { |
1081 | const char *p; | |
1082 | struct strbuf pattern = STRBUF_INIT; | |
1083 | ||
fbfa0973 | 1084 | strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line); |
d76650b8 BM |
1085 | if (starts_with(s, pattern.buf + 1)) |
1086 | len = 0; | |
1087 | else if ((p = strstr(s, pattern.buf))) | |
1088 | len = p - s + 1; | |
1a72cfd7 | 1089 | strbuf_release(&pattern); |
d76650b8 | 1090 | return len; |
1a72cfd7 JL |
1091 | } |
1092 | ||
d540b70c | 1093 | void wt_status_append_cut_line(struct strbuf *buf) |
fcef9312 | 1094 | { |
8c4b1a35 | 1095 | const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored."); |
d540b70c DL |
1096 | |
1097 | strbuf_commented_addf(buf, "%s", cut_line); | |
1098 | strbuf_add_commented_lines(buf, explanation, strlen(explanation)); | |
1099 | } | |
1100 | ||
1101 | void wt_status_add_cut_line(FILE *fp) | |
1102 | { | |
fcef9312 NTND |
1103 | struct strbuf buf = STRBUF_INIT; |
1104 | ||
d540b70c | 1105 | wt_status_append_cut_line(&buf); |
fcef9312 NTND |
1106 | fputs(buf.buf, fp); |
1107 | strbuf_release(&buf); | |
1108 | } | |
1109 | ||
957a0fe2 | 1110 | static void wt_longstatus_print_verbose(struct wt_status *s) |
c91f0d92 JK |
1111 | { |
1112 | struct rev_info rev; | |
32962c9b | 1113 | struct setup_revision_opt opt; |
40555000 MG |
1114 | int dirty_submodules; |
1115 | const char *c = color(WT_STATUS_HEADER, s); | |
99a12694 | 1116 | |
5b02ca38 | 1117 | repo_init_revisions(s->repo, &rev, NULL); |
0d1e0e78 | 1118 | rev.diffopt.flags.allow_textconv = 1; |
425a28e0 | 1119 | rev.diffopt.ita_invisible_in_index = 1; |
32962c9b JH |
1120 | |
1121 | memset(&opt, 0, sizeof(opt)); | |
f2e51195 | 1122 | opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference; |
32962c9b JH |
1123 | setup_revisions(0, NULL, &rev, &opt); |
1124 | ||
c91f0d92 | 1125 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
e8b2dc2c BP |
1126 | rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename; |
1127 | rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit; | |
1128 | rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score; | |
4ba0cb27 KH |
1129 | rev.diffopt.file = s->fp; |
1130 | rev.diffopt.close_file = 0; | |
4f672ad6 JK |
1131 | /* |
1132 | * If we're not going to stdout, then we definitely don't | |
1133 | * want color, since we are going to the commit message | |
1134 | * file (and even the "auto" setting won't work, since it | |
1a72cfd7 JL |
1135 | * will have checked isatty on stdout). But we then do want |
1136 | * to insert the scissor line here to reliably remove the | |
1137 | * diff before committing. | |
4f672ad6 | 1138 | */ |
1a72cfd7 | 1139 | if (s->fp != stdout) { |
f1c96261 | 1140 | rev.diffopt.use_color = 0; |
fcef9312 | 1141 | wt_status_add_cut_line(s->fp); |
1a72cfd7 | 1142 | } |
6fa90194 | 1143 | if (s->verbose > 1 && s->committable) { |
40555000 MG |
1144 | /* print_updated() printed a header, so do we */ |
1145 | if (s->fp != stdout) | |
957a0fe2 | 1146 | wt_longstatus_print_trailer(s); |
40555000 MG |
1147 | status_printf_ln(s, c, _("Changes to be committed:")); |
1148 | rev.diffopt.a_prefix = "c/"; | |
1149 | rev.diffopt.b_prefix = "i/"; | |
1150 | } /* else use prefix as per user config */ | |
c91f0d92 | 1151 | run_diff_index(&rev, 1); |
40555000 MG |
1152 | if (s->verbose > 1 && |
1153 | wt_status_check_worktree_changes(s, &dirty_submodules)) { | |
1154 | status_printf_ln(s, c, | |
1155 | "--------------------------------------------------"); | |
1156 | status_printf_ln(s, c, _("Changes not staged for commit:")); | |
1157 | setup_work_tree(); | |
1158 | rev.diffopt.a_prefix = "i/"; | |
1159 | rev.diffopt.b_prefix = "w/"; | |
1160 | run_diff_files(&rev, 0); | |
1161 | } | |
2108fe4a | 1162 | release_revisions(&rev); |
c91f0d92 JK |
1163 | } |
1164 | ||
957a0fe2 | 1165 | static void wt_longstatus_print_tracking(struct wt_status *s) |
b6975ab5 JH |
1166 | { |
1167 | struct strbuf sb = STRBUF_INIT; | |
c72b49df | 1168 | const char *cp, *ep, *branch_name; |
b6975ab5 | 1169 | struct branch *branch; |
2556b996 MM |
1170 | char comment_line_string[3]; |
1171 | int i; | |
0a53561a | 1172 | uint64_t t_begin = 0; |
b6975ab5 JH |
1173 | |
1174 | assert(s->branch && !s->is_initial); | |
c72b49df | 1175 | if (!skip_prefix(s->branch, "refs/heads/", &branch_name)) |
b6975ab5 | 1176 | return; |
c72b49df | 1177 | branch = branch_get(branch_name); |
0a53561a JH |
1178 | |
1179 | t_begin = getnanotime(); | |
1180 | ||
f39a757d | 1181 | if (!format_tracking_info(branch, &sb, s->ahead_behind_flags)) |
b6975ab5 JH |
1182 | return; |
1183 | ||
ed9bff08 | 1184 | if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) && |
0a53561a JH |
1185 | s->ahead_behind_flags == AHEAD_BEHIND_FULL) { |
1186 | uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000; | |
1187 | if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) { | |
1188 | strbuf_addf(&sb, _("\n" | |
1189 | "It took %.2f seconds to compute the branch ahead/behind values.\n" | |
1190 | "You can use '--no-ahead-behind' to avoid this.\n"), | |
1191 | t_delta_in_ms / 1000.0); | |
1192 | } | |
1193 | } | |
1194 | ||
2556b996 MM |
1195 | i = 0; |
1196 | if (s->display_comment_prefix) { | |
1197 | comment_line_string[i++] = comment_line_char; | |
1198 | comment_line_string[i++] = ' '; | |
1199 | } | |
1200 | comment_line_string[i] = '\0'; | |
1201 | ||
b6975ab5 | 1202 | for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) |
d249b098 | 1203 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), |
2556b996 | 1204 | "%s%.*s", comment_line_string, |
eff80a9f | 1205 | (int)(ep - cp), cp); |
2556b996 MM |
1206 | if (s->display_comment_prefix) |
1207 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c", | |
1208 | comment_line_char); | |
1209 | else | |
75177c85 | 1210 | fputs("\n", s->fp); |
b6ec3071 | 1211 | strbuf_release(&sb); |
b6975ab5 JH |
1212 | } |
1213 | ||
ecbc23e4 RR |
1214 | static int uf_was_slow(struct wt_status *s) |
1215 | { | |
1216 | if (getenv("GIT_TEST_UF_DELAY_WARNING")) | |
1217 | s->untracked_in_ms = 3250; | |
1218 | return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms; | |
1219 | } | |
1220 | ||
83c750ac | 1221 | static void show_merge_in_progress(struct wt_status *s, |
73ba5d78 | 1222 | const char *color) |
83c750ac LK |
1223 | { |
1224 | if (has_unmerged(s)) { | |
1225 | status_printf_ln(s, color, _("You have unmerged paths.")); | |
b0a61ab2 | 1226 | if (s->hints) { |
83c750ac | 1227 | status_printf_ln(s, color, |
b0a61ab2 | 1228 | _(" (fix conflicts and run \"git commit\")")); |
83c750ac | 1229 | status_printf_ln(s, color, |
b0a61ab2 MM |
1230 | _(" (use \"git merge --abort\" to abort the merge)")); |
1231 | } | |
83c750ac LK |
1232 | } else { |
1233 | status_printf_ln(s, color, | |
1234 | _("All conflicts fixed but you are still merging.")); | |
6a964f57 | 1235 | if (s->hints) |
83c750ac LK |
1236 | status_printf_ln(s, color, |
1237 | _(" (use \"git commit\" to conclude merge)")); | |
1238 | } | |
957a0fe2 | 1239 | wt_longstatus_print_trailer(s); |
83c750ac LK |
1240 | } |
1241 | ||
1242 | static void show_am_in_progress(struct wt_status *s, | |
83c750ac LK |
1243 | const char *color) |
1244 | { | |
9e7e41bf A |
1245 | int am_empty_patch; |
1246 | ||
83c750ac LK |
1247 | status_printf_ln(s, color, |
1248 | _("You are in the middle of an am session.")); | |
73ba5d78 | 1249 | if (s->state.am_empty_patch) |
83c750ac LK |
1250 | status_printf_ln(s, color, |
1251 | _("The current patch is empty.")); | |
6a964f57 | 1252 | if (s->hints) { |
9e7e41bf A |
1253 | am_empty_patch = s->state.am_empty_patch; |
1254 | if (!am_empty_patch) | |
83c750ac | 1255 | status_printf_ln(s, color, |
8ceb6fbd | 1256 | _(" (fix conflicts and then run \"git am --continue\")")); |
83c750ac LK |
1257 | status_printf_ln(s, color, |
1258 | _(" (use \"git am --skip\" to skip this patch)")); | |
9e7e41bf A |
1259 | if (am_empty_patch) |
1260 | status_printf_ln(s, color, | |
1261 | _(" (use \"git am --allow-empty\" to record this patch as an empty commit)")); | |
83c750ac LK |
1262 | status_printf_ln(s, color, |
1263 | _(" (use \"git am --abort\" to restore the original branch)")); | |
1264 | } | |
957a0fe2 | 1265 | wt_longstatus_print_trailer(s); |
83c750ac LK |
1266 | } |
1267 | ||
2d1cceba LK |
1268 | static char *read_line_from_git_path(const char *filename) |
1269 | { | |
1270 | struct strbuf buf = STRBUF_INIT; | |
e9d983f1 NTND |
1271 | FILE *fp = fopen_or_warn(git_path("%s", filename), "r"); |
1272 | ||
2d1cceba LK |
1273 | if (!fp) { |
1274 | strbuf_release(&buf); | |
1275 | return NULL; | |
1276 | } | |
8f309aeb | 1277 | strbuf_getline_lf(&buf, fp); |
2d1cceba LK |
1278 | if (!fclose(fp)) { |
1279 | return strbuf_detach(&buf, NULL); | |
1280 | } else { | |
1281 | strbuf_release(&buf); | |
1282 | return NULL; | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | static int split_commit_in_progress(struct wt_status *s) | |
1287 | { | |
1288 | int split_in_progress = 0; | |
41fc6b33 | 1289 | char *head, *orig_head, *rebase_amend, *rebase_orig_head; |
2d1cceba | 1290 | |
41fc6b33 | 1291 | if ((!s->amend && !s->nowarn && !s->workdir_dirty) || |
2d1cceba | 1292 | !s->branch || strcmp(s->branch, "HEAD")) |
41fc6b33 | 1293 | return 0; |
2d1cceba | 1294 | |
41fc6b33 JS |
1295 | head = read_line_from_git_path("HEAD"); |
1296 | orig_head = read_line_from_git_path("ORIG_HEAD"); | |
1297 | rebase_amend = read_line_from_git_path("rebase-merge/amend"); | |
1298 | rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head"); | |
2d1cceba | 1299 | |
41fc6b33 JS |
1300 | if (!head || !orig_head || !rebase_amend || !rebase_orig_head) |
1301 | ; /* fall through, no split in progress */ | |
1302 | else if (!strcmp(rebase_amend, rebase_orig_head)) | |
1303 | split_in_progress = !!strcmp(head, rebase_amend); | |
1304 | else if (strcmp(orig_head, rebase_orig_head)) | |
1305 | split_in_progress = 1; | |
2d1cceba LK |
1306 | |
1307 | free(head); | |
1308 | free(orig_head); | |
1309 | free(rebase_amend); | |
1310 | free(rebase_orig_head); | |
41fc6b33 | 1311 | |
2d1cceba LK |
1312 | return split_in_progress; |
1313 | } | |
1314 | ||
84e6fb9d GP |
1315 | /* |
1316 | * Turn | |
1317 | * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message" | |
1318 | * into | |
1319 | * "pick d6a2f03 some message" | |
1320 | * | |
1321 | * The function assumes that the line does not contain useless spaces | |
1322 | * before or after the command. | |
1323 | */ | |
b1f1ade8 | 1324 | static void abbrev_oid_in_line(struct strbuf *line) |
84e6fb9d GP |
1325 | { |
1326 | struct strbuf **split; | |
1327 | int i; | |
1328 | ||
1329 | if (starts_with(line->buf, "exec ") || | |
225a777a JS |
1330 | starts_with(line->buf, "x ") || |
1331 | starts_with(line->buf, "label ") || | |
1332 | starts_with(line->buf, "l ")) | |
84e6fb9d GP |
1333 | return; |
1334 | ||
1335 | split = strbuf_split_max(line, ' ', 3); | |
1336 | if (split[0] && split[1]) { | |
e86ab2c1 | 1337 | struct object_id oid; |
84e6fb9d GP |
1338 | |
1339 | /* | |
1340 | * strbuf_split_max left a space. Trim it and re-add | |
1341 | * it after abbreviation. | |
1342 | */ | |
1343 | strbuf_trim(split[1]); | |
e86ab2c1 | 1344 | if (!get_oid(split[1]->buf, &oid)) { |
84e6fb9d | 1345 | strbuf_reset(split[1]); |
30e677e0 | 1346 | strbuf_add_unique_abbrev(split[1], &oid, |
a94bb683 RS |
1347 | DEFAULT_ABBREV); |
1348 | strbuf_addch(split[1], ' '); | |
84e6fb9d GP |
1349 | strbuf_reset(line); |
1350 | for (i = 0; split[i]; i++) | |
8109984d | 1351 | strbuf_addbuf(line, split[i]); |
84e6fb9d GP |
1352 | } |
1353 | } | |
6eb6078b | 1354 | strbuf_list_free(split); |
84e6fb9d GP |
1355 | } |
1356 | ||
df9ded49 | 1357 | static int read_rebase_todolist(const char *fname, struct string_list *lines) |
84e6fb9d GP |
1358 | { |
1359 | struct strbuf line = STRBUF_INIT; | |
1360 | FILE *f = fopen(git_path("%s", fname), "r"); | |
1361 | ||
df9ded49 JS |
1362 | if (!f) { |
1363 | if (errno == ENOENT) | |
1364 | return -1; | |
84e6fb9d GP |
1365 | die_errno("Could not open file %s for reading", |
1366 | git_path("%s", fname)); | |
df9ded49 | 1367 | } |
8f309aeb | 1368 | while (!strbuf_getline_lf(&line, f)) { |
84e6fb9d GP |
1369 | if (line.len && line.buf[0] == comment_line_char) |
1370 | continue; | |
1371 | strbuf_trim(&line); | |
1372 | if (!line.len) | |
1373 | continue; | |
b1f1ade8 | 1374 | abbrev_oid_in_line(&line); |
84e6fb9d GP |
1375 | string_list_append(lines, line.buf); |
1376 | } | |
e7b65e20 | 1377 | fclose(f); |
6f49541d | 1378 | strbuf_release(&line); |
df9ded49 | 1379 | return 0; |
84e6fb9d GP |
1380 | } |
1381 | ||
1382 | static void show_rebase_information(struct wt_status *s, | |
73ba5d78 | 1383 | const char *color) |
84e6fb9d | 1384 | { |
73ba5d78 | 1385 | if (s->state.rebase_interactive_in_progress) { |
84e6fb9d GP |
1386 | int i; |
1387 | int nr_lines_to_show = 2; | |
1388 | ||
1389 | struct string_list have_done = STRING_LIST_INIT_DUP; | |
1390 | struct string_list yet_to_do = STRING_LIST_INIT_DUP; | |
1391 | ||
1392 | read_rebase_todolist("rebase-merge/done", &have_done); | |
df9ded49 JS |
1393 | if (read_rebase_todolist("rebase-merge/git-rebase-todo", |
1394 | &yet_to_do)) | |
1395 | status_printf_ln(s, color, | |
1396 | _("git-rebase-todo is missing.")); | |
84e6fb9d GP |
1397 | if (have_done.nr == 0) |
1398 | status_printf_ln(s, color, _("No commands done.")); | |
1399 | else { | |
1400 | status_printf_ln(s, color, | |
99d60545 ÆAB |
1401 | Q_("Last command done (%"PRIuMAX" command done):", |
1402 | "Last commands done (%"PRIuMAX" commands done):", | |
84e6fb9d | 1403 | have_done.nr), |
99d60545 | 1404 | (uintmax_t)have_done.nr); |
84e6fb9d GP |
1405 | for (i = (have_done.nr > nr_lines_to_show) |
1406 | ? have_done.nr - nr_lines_to_show : 0; | |
1407 | i < have_done.nr; | |
1408 | i++) | |
1409 | status_printf_ln(s, color, " %s", have_done.items[i].string); | |
1410 | if (have_done.nr > nr_lines_to_show && s->hints) | |
1411 | status_printf_ln(s, color, | |
1412 | _(" (see more in file %s)"), git_path("rebase-merge/done")); | |
1413 | } | |
1414 | ||
1415 | if (yet_to_do.nr == 0) | |
1416 | status_printf_ln(s, color, | |
1417 | _("No commands remaining.")); | |
1418 | else { | |
1419 | status_printf_ln(s, color, | |
99d60545 ÆAB |
1420 | Q_("Next command to do (%"PRIuMAX" remaining command):", |
1421 | "Next commands to do (%"PRIuMAX" remaining commands):", | |
84e6fb9d | 1422 | yet_to_do.nr), |
99d60545 | 1423 | (uintmax_t)yet_to_do.nr); |
84e6fb9d GP |
1424 | for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++) |
1425 | status_printf_ln(s, color, " %s", yet_to_do.items[i].string); | |
1426 | if (s->hints) | |
1427 | status_printf_ln(s, color, | |
1428 | _(" (use \"git rebase --edit-todo\" to view and edit)")); | |
1429 | } | |
1430 | string_list_clear(&yet_to_do, 0); | |
1431 | string_list_clear(&have_done, 0); | |
1432 | } | |
1433 | } | |
1434 | ||
05eb5635 | 1435 | static void print_rebase_state(struct wt_status *s, |
73ba5d78 | 1436 | const char *color) |
05eb5635 | 1437 | { |
73ba5d78 | 1438 | if (s->state.branch) |
05eb5635 GP |
1439 | status_printf_ln(s, color, |
1440 | _("You are currently rebasing branch '%s' on '%s'."), | |
73ba5d78 SS |
1441 | s->state.branch, |
1442 | s->state.onto); | |
05eb5635 GP |
1443 | else |
1444 | status_printf_ln(s, color, | |
1445 | _("You are currently rebasing.")); | |
1446 | } | |
1447 | ||
83c750ac | 1448 | static void show_rebase_in_progress(struct wt_status *s, |
73ba5d78 | 1449 | const char *color) |
83c750ac LK |
1450 | { |
1451 | struct stat st; | |
1452 | ||
73ba5d78 | 1453 | show_rebase_information(s, color); |
83c750ac | 1454 | if (has_unmerged(s)) { |
73ba5d78 | 1455 | print_rebase_state(s, color); |
6a964f57 | 1456 | if (s->hints) { |
83c750ac LK |
1457 | status_printf_ln(s, color, |
1458 | _(" (fix conflicts and then run \"git rebase --continue\")")); | |
1459 | status_printf_ln(s, color, | |
1460 | _(" (use \"git rebase --skip\" to skip this patch)")); | |
1461 | status_printf_ln(s, color, | |
1462 | _(" (use \"git rebase --abort\" to check out the original branch)")); | |
1463 | } | |
73ba5d78 | 1464 | } else if (s->state.rebase_in_progress || |
5b02ca38 | 1465 | !stat(git_path_merge_msg(s->repo), &st)) { |
73ba5d78 | 1466 | print_rebase_state(s, color); |
6a964f57 | 1467 | if (s->hints) |
83c750ac LK |
1468 | status_printf_ln(s, color, |
1469 | _(" (all conflicts fixed: run \"git rebase --continue\")")); | |
2d1cceba | 1470 | } else if (split_commit_in_progress(s)) { |
73ba5d78 | 1471 | if (s->state.branch) |
0722c805 NTND |
1472 | status_printf_ln(s, color, |
1473 | _("You are currently splitting a commit while rebasing branch '%s' on '%s'."), | |
73ba5d78 SS |
1474 | s->state.branch, |
1475 | s->state.onto); | |
0722c805 NTND |
1476 | else |
1477 | status_printf_ln(s, color, | |
1478 | _("You are currently splitting a commit during a rebase.")); | |
6a964f57 | 1479 | if (s->hints) |
2d1cceba LK |
1480 | status_printf_ln(s, color, |
1481 | _(" (Once your working directory is clean, run \"git rebase --continue\")")); | |
83c750ac | 1482 | } else { |
73ba5d78 | 1483 | if (s->state.branch) |
0722c805 NTND |
1484 | status_printf_ln(s, color, |
1485 | _("You are currently editing a commit while rebasing branch '%s' on '%s'."), | |
73ba5d78 SS |
1486 | s->state.branch, |
1487 | s->state.onto); | |
0722c805 NTND |
1488 | else |
1489 | status_printf_ln(s, color, | |
1490 | _("You are currently editing a commit during a rebase.")); | |
6a964f57 | 1491 | if (s->hints && !s->amend) { |
83c750ac LK |
1492 | status_printf_ln(s, color, |
1493 | _(" (use \"git commit --amend\" to amend the current commit)")); | |
1494 | status_printf_ln(s, color, | |
1495 | _(" (use \"git rebase --continue\" once you are satisfied with your changes)")); | |
1496 | } | |
1497 | } | |
957a0fe2 | 1498 | wt_longstatus_print_trailer(s); |
83c750ac LK |
1499 | } |
1500 | ||
1501 | static void show_cherry_pick_in_progress(struct wt_status *s, | |
73ba5d78 | 1502 | const char *color) |
83c750ac | 1503 | { |
4a72486d PW |
1504 | if (is_null_oid(&s->state.cherry_pick_head_oid)) |
1505 | status_printf_ln(s, color, | |
1506 | _("Cherry-pick currently in progress.")); | |
1507 | else | |
1508 | status_printf_ln(s, color, | |
1509 | _("You are currently cherry-picking commit %s."), | |
1510 | find_unique_abbrev(&s->state.cherry_pick_head_oid, | |
1511 | DEFAULT_ABBREV)); | |
1512 | ||
6a964f57 | 1513 | if (s->hints) { |
83c750ac LK |
1514 | if (has_unmerged(s)) |
1515 | status_printf_ln(s, color, | |
b95e66f5 | 1516 | _(" (fix conflicts and run \"git cherry-pick --continue\")")); |
4a72486d PW |
1517 | else if (is_null_oid(&s->state.cherry_pick_head_oid)) |
1518 | status_printf_ln(s, color, | |
1519 | _(" (run \"git cherry-pick --continue\" to continue)")); | |
83c750ac LK |
1520 | else |
1521 | status_printf_ln(s, color, | |
b95e66f5 | 1522 | _(" (all conflicts fixed: run \"git cherry-pick --continue\")")); |
86ae43da DL |
1523 | status_printf_ln(s, color, |
1524 | _(" (use \"git cherry-pick --skip\" to skip this patch)")); | |
b95e66f5 RT |
1525 | status_printf_ln(s, color, |
1526 | _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)")); | |
83c750ac | 1527 | } |
957a0fe2 | 1528 | wt_longstatus_print_trailer(s); |
83c750ac LK |
1529 | } |
1530 | ||
db4ef449 | 1531 | static void show_revert_in_progress(struct wt_status *s, |
73ba5d78 | 1532 | const char *color) |
db4ef449 | 1533 | { |
4a72486d PW |
1534 | if (is_null_oid(&s->state.revert_head_oid)) |
1535 | status_printf_ln(s, color, | |
1536 | _("Revert currently in progress.")); | |
1537 | else | |
1538 | status_printf_ln(s, color, | |
1539 | _("You are currently reverting commit %s."), | |
1540 | find_unique_abbrev(&s->state.revert_head_oid, | |
1541 | DEFAULT_ABBREV)); | |
6a964f57 | 1542 | if (s->hints) { |
db4ef449 MM |
1543 | if (has_unmerged(s)) |
1544 | status_printf_ln(s, color, | |
1545 | _(" (fix conflicts and run \"git revert --continue\")")); | |
4a72486d PW |
1546 | else if (is_null_oid(&s->state.revert_head_oid)) |
1547 | status_printf_ln(s, color, | |
1548 | _(" (run \"git revert --continue\" to continue)")); | |
db4ef449 MM |
1549 | else |
1550 | status_printf_ln(s, color, | |
1551 | _(" (all conflicts fixed: run \"git revert --continue\")")); | |
86ae43da DL |
1552 | status_printf_ln(s, color, |
1553 | _(" (use \"git revert --skip\" to skip this patch)")); | |
db4ef449 MM |
1554 | status_printf_ln(s, color, |
1555 | _(" (use \"git revert --abort\" to cancel the revert operation)")); | |
1556 | } | |
957a0fe2 | 1557 | wt_longstatus_print_trailer(s); |
db4ef449 MM |
1558 | } |
1559 | ||
83c750ac | 1560 | static void show_bisect_in_progress(struct wt_status *s, |
73ba5d78 | 1561 | const char *color) |
83c750ac | 1562 | { |
73ba5d78 | 1563 | if (s->state.branch) |
0722c805 | 1564 | status_printf_ln(s, color, |
6deab24d | 1565 | _("You are currently bisecting, started from branch '%s'."), |
73ba5d78 | 1566 | s->state.branch); |
0722c805 NTND |
1567 | else |
1568 | status_printf_ln(s, color, | |
1569 | _("You are currently bisecting.")); | |
6a964f57 | 1570 | if (s->hints) |
83c750ac LK |
1571 | status_printf_ln(s, color, |
1572 | _(" (use \"git bisect reset\" to get back to the original branch)")); | |
957a0fe2 | 1573 | wt_longstatus_print_trailer(s); |
83c750ac LK |
1574 | } |
1575 | ||
051df3cf EN |
1576 | static void show_sparse_checkout_in_use(struct wt_status *s, |
1577 | const char *color) | |
1578 | { | |
1579 | if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED) | |
1580 | return; | |
1581 | ||
bf48e5ac DS |
1582 | if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX) |
1583 | status_printf_ln(s, color, _("You are in a sparse checkout.")); | |
1584 | else | |
1585 | status_printf_ln(s, color, | |
1586 | _("You are in a sparse checkout with %d%% of tracked files present."), | |
1587 | s->state.sparse_checkout_percentage); | |
051df3cf EN |
1588 | wt_longstatus_print_trailer(s); |
1589 | } | |
1590 | ||
0722c805 NTND |
1591 | /* |
1592 | * Extract branch information from rebase/bisect | |
1593 | */ | |
81eff27b | 1594 | static char *get_branch(const struct worktree *wt, const char *path) |
0722c805 | 1595 | { |
8b87cfd0 | 1596 | struct strbuf sb = STRBUF_INIT; |
e86ab2c1 | 1597 | struct object_id oid; |
c72b49df | 1598 | const char *branch_name; |
0722c805 | 1599 | |
81eff27b | 1600 | if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0) |
8b87cfd0 | 1601 | goto got_nothing; |
0722c805 | 1602 | |
66ec904b | 1603 | while (sb.len && sb.buf[sb.len - 1] == '\n') |
8b87cfd0 NTND |
1604 | strbuf_setlen(&sb, sb.len - 1); |
1605 | if (!sb.len) | |
1606 | goto got_nothing; | |
c72b49df RS |
1607 | if (skip_prefix(sb.buf, "refs/heads/", &branch_name)) |
1608 | strbuf_remove(&sb, 0, branch_name - sb.buf); | |
59556548 | 1609 | else if (starts_with(sb.buf, "refs/")) |
8b87cfd0 | 1610 | ; |
e86ab2c1 | 1611 | else if (!get_oid_hex(sb.buf, &oid)) { |
8b87cfd0 | 1612 | strbuf_reset(&sb); |
30e677e0 | 1613 | strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV); |
8b87cfd0 NTND |
1614 | } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */ |
1615 | goto got_nothing; | |
0722c805 | 1616 | else /* bisect */ |
8b87cfd0 NTND |
1617 | ; |
1618 | return strbuf_detach(&sb, NULL); | |
1619 | ||
1620 | got_nothing: | |
1621 | strbuf_release(&sb); | |
1622 | return NULL; | |
0722c805 NTND |
1623 | } |
1624 | ||
b397ea48 | 1625 | struct grab_1st_switch_cbdata { |
b397ea48 | 1626 | struct strbuf buf; |
e86ab2c1 | 1627 | struct object_id noid; |
b397ea48 NTND |
1628 | }; |
1629 | ||
5cf88fd8 | 1630 | static int grab_1st_switch(struct object_id *ooid UNUSED, |
c006e9fa | 1631 | struct object_id *noid, |
5cf88fd8 ÆAB |
1632 | const char *email UNUSED, |
1633 | timestamp_t timestamp UNUSED, int tz UNUSED, | |
b397ea48 | 1634 | const char *message, void *cb_data) |
83c750ac | 1635 | { |
b397ea48 NTND |
1636 | struct grab_1st_switch_cbdata *cb = cb_data; |
1637 | const char *target = NULL, *end; | |
83c750ac | 1638 | |
c72b49df | 1639 | if (!skip_prefix(message, "checkout: moving from ", &message)) |
b397ea48 | 1640 | return 0; |
b397ea48 NTND |
1641 | target = strstr(message, " to "); |
1642 | if (!target) | |
1643 | return 0; | |
1644 | target += strlen(" to "); | |
1645 | strbuf_reset(&cb->buf); | |
e86ab2c1 | 1646 | oidcpy(&cb->noid, noid); |
904de44c RS |
1647 | end = strchrnul(target, '\n'); |
1648 | strbuf_add(&cb->buf, target, end - target); | |
1649 | if (!strcmp(cb->buf.buf, "HEAD")) { | |
0eb8548f | 1650 | /* HEAD is relative. Resolve it to the right reflog entry. */ |
904de44c | 1651 | strbuf_reset(&cb->buf); |
30e677e0 | 1652 | strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV); |
0eb8548f | 1653 | } |
b397ea48 NTND |
1654 | return 1; |
1655 | } | |
1656 | ||
78845457 NTND |
1657 | static void wt_status_get_detached_from(struct repository *r, |
1658 | struct wt_status_state *state) | |
b397ea48 NTND |
1659 | { |
1660 | struct grab_1st_switch_cbdata cb; | |
1661 | struct commit *commit; | |
e86ab2c1 | 1662 | struct object_id oid; |
b397ea48 NTND |
1663 | char *ref = NULL; |
1664 | ||
1665 | strbuf_init(&cb.buf, 0); | |
1666 | if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) { | |
1667 | strbuf_release(&cb.buf); | |
1668 | return; | |
1669 | } | |
1670 | ||
f24c30e0 | 1671 | if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref, 1) == 1 && |
b1f1ade8 | 1672 | /* oid is a commit? match without further lookup */ |
4a7e27e9 | 1673 | (oideq(&cb.noid, &oid) || |
b1f1ade8 | 1674 | /* perhaps oid is a tag, try to dereference to a commit */ |
78845457 | 1675 | ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL && |
4a7e27e9 | 1676 | oideq(&cb.noid, &commit->object.oid)))) { |
c72b49df RS |
1677 | const char *from = ref; |
1678 | if (!skip_prefix(from, "refs/tags/", &from)) | |
1679 | skip_prefix(from, "refs/remotes/", &from); | |
1680 | state->detached_from = xstrdup(from); | |
b397ea48 NTND |
1681 | } else |
1682 | state->detached_from = | |
aab9583f | 1683 | xstrdup(find_unique_abbrev(&cb.noid, DEFAULT_ABBREV)); |
40f5555c | 1684 | oidcpy(&state->detached_oid, &cb.noid); |
e86ab2c1 | 1685 | state->detached_at = !get_oid("HEAD", &oid) && |
4a7e27e9 | 1686 | oideq(&oid, &state->detached_oid); |
b397ea48 NTND |
1687 | |
1688 | free(ref); | |
1689 | strbuf_release(&cb.buf); | |
1690 | } | |
1691 | ||
81eff27b NTND |
1692 | int wt_status_check_rebase(const struct worktree *wt, |
1693 | struct wt_status_state *state) | |
83c750ac | 1694 | { |
83c750ac | 1695 | struct stat st; |
83c750ac | 1696 | |
81eff27b NTND |
1697 | if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) { |
1698 | if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) { | |
b9691db4 | 1699 | state->am_in_progress = 1; |
81eff27b | 1700 | if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size) |
b9691db4 | 1701 | state->am_empty_patch = 1; |
83c750ac | 1702 | } else { |
b9691db4 | 1703 | state->rebase_in_progress = 1; |
81eff27b NTND |
1704 | state->branch = get_branch(wt, "rebase-apply/head-name"); |
1705 | state->onto = get_branch(wt, "rebase-apply/onto"); | |
83c750ac | 1706 | } |
81eff27b NTND |
1707 | } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) { |
1708 | if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st)) | |
b9691db4 | 1709 | state->rebase_interactive_in_progress = 1; |
83c750ac | 1710 | else |
b9691db4 | 1711 | state->rebase_in_progress = 1; |
81eff27b NTND |
1712 | state->branch = get_branch(wt, "rebase-merge/head-name"); |
1713 | state->onto = get_branch(wt, "rebase-merge/onto"); | |
bcd522a1 NTND |
1714 | } else |
1715 | return 0; | |
1716 | return 1; | |
1717 | } | |
1718 | ||
f5d067a2 NTND |
1719 | int wt_status_check_bisect(const struct worktree *wt, |
1720 | struct wt_status_state *state) | |
1721 | { | |
1722 | struct stat st; | |
1723 | ||
1724 | if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) { | |
1725 | state->bisect_in_progress = 1; | |
1726 | state->branch = get_branch(wt, "BISECT_START"); | |
1727 | return 1; | |
1728 | } | |
1729 | return 0; | |
1730 | } | |
1731 | ||
051df3cf EN |
1732 | static void wt_status_check_sparse_checkout(struct repository *r, |
1733 | struct wt_status_state *state) | |
1734 | { | |
1735 | int skip_worktree = 0; | |
1736 | int i; | |
1737 | ||
1738 | if (!core_apply_sparse_checkout || r->index->cache_nr == 0) { | |
1739 | /* | |
1740 | * Don't compute percentage of checked out files if we | |
1741 | * aren't in a sparse checkout or would get division by 0. | |
1742 | */ | |
1743 | state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED; | |
1744 | return; | |
1745 | } | |
1746 | ||
bf48e5ac DS |
1747 | if (r->index->sparse_index) { |
1748 | state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX; | |
1749 | return; | |
1750 | } | |
1751 | ||
051df3cf EN |
1752 | for (i = 0; i < r->index->cache_nr; i++) { |
1753 | struct cache_entry *ce = r->index->cache[i]; | |
1754 | if (ce_skip_worktree(ce)) | |
1755 | skip_worktree++; | |
1756 | } | |
1757 | ||
1758 | state->sparse_checkout_percentage = | |
1759 | 100 - (100 * skip_worktree)/r->index->cache_nr; | |
1760 | } | |
1761 | ||
78845457 NTND |
1762 | void wt_status_get_state(struct repository *r, |
1763 | struct wt_status_state *state, | |
bcd522a1 NTND |
1764 | int get_detached_from) |
1765 | { | |
1766 | struct stat st; | |
e86ab2c1 | 1767 | struct object_id oid; |
4a72486d | 1768 | enum replay_action action; |
bcd522a1 | 1769 | |
78845457 | 1770 | if (!stat(git_path_merge_head(r), &st)) { |
982288e9 | 1771 | wt_status_check_rebase(NULL, state); |
bcd522a1 | 1772 | state->merge_in_progress = 1; |
81eff27b | 1773 | } else if (wt_status_check_rebase(NULL, state)) { |
bcd522a1 | 1774 | ; /* all set */ |
c8e4159e HWN |
1775 | } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && |
1776 | !get_oid("CHERRY_PICK_HEAD", &oid)) { | |
b9691db4 | 1777 | state->cherry_pick_in_progress = 1; |
40f5555c | 1778 | oidcpy(&state->cherry_pick_head_oid, &oid); |
83c750ac | 1779 | } |
f5d067a2 | 1780 | wt_status_check_bisect(NULL, state); |
b8825ef2 | 1781 | if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") && |
e86ab2c1 | 1782 | !get_oid("REVERT_HEAD", &oid)) { |
db4ef449 | 1783 | state->revert_in_progress = 1; |
40f5555c | 1784 | oidcpy(&state->revert_head_oid, &oid); |
db4ef449 | 1785 | } |
4a72486d PW |
1786 | if (!sequencer_get_last_command(r, &action)) { |
1787 | if (action == REPLAY_PICK) { | |
1788 | state->cherry_pick_in_progress = 1; | |
14228447 | 1789 | oidcpy(&state->cherry_pick_head_oid, null_oid()); |
4a72486d PW |
1790 | } else { |
1791 | state->revert_in_progress = 1; | |
14228447 | 1792 | oidcpy(&state->revert_head_oid, null_oid()); |
4a72486d PW |
1793 | } |
1794 | } | |
b397ea48 | 1795 | if (get_detached_from) |
78845457 | 1796 | wt_status_get_detached_from(r, state); |
051df3cf | 1797 | wt_status_check_sparse_checkout(r, state); |
b9691db4 NTND |
1798 | } |
1799 | ||
73ba5d78 | 1800 | static void wt_longstatus_print_state(struct wt_status *s) |
b9691db4 NTND |
1801 | { |
1802 | const char *state_color = color(WT_STATUS_HEADER, s); | |
73ba5d78 SS |
1803 | struct wt_status_state *state = &s->state; |
1804 | ||
982288e9 JS |
1805 | if (state->merge_in_progress) { |
1806 | if (state->rebase_interactive_in_progress) { | |
1807 | show_rebase_information(s, state_color); | |
1808 | fputs("\n", s->fp); | |
1809 | } | |
73ba5d78 | 1810 | show_merge_in_progress(s, state_color); |
982288e9 | 1811 | } else if (state->am_in_progress) |
73ba5d78 | 1812 | show_am_in_progress(s, state_color); |
3b691ccc | 1813 | else if (state->rebase_in_progress || state->rebase_interactive_in_progress) |
73ba5d78 | 1814 | show_rebase_in_progress(s, state_color); |
3b691ccc | 1815 | else if (state->cherry_pick_in_progress) |
73ba5d78 | 1816 | show_cherry_pick_in_progress(s, state_color); |
db4ef449 | 1817 | else if (state->revert_in_progress) |
73ba5d78 | 1818 | show_revert_in_progress(s, state_color); |
3b691ccc | 1819 | if (state->bisect_in_progress) |
73ba5d78 | 1820 | show_bisect_in_progress(s, state_color); |
051df3cf EN |
1821 | |
1822 | if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED) | |
1823 | show_sparse_checkout_in_use(s, state_color); | |
83c750ac LK |
1824 | } |
1825 | ||
be7e795e | 1826 | static void wt_longstatus_print(struct wt_status *s) |
c91f0d92 | 1827 | { |
1d282327 AA |
1828 | const char *branch_color = color(WT_STATUS_ONBRANCH, s); |
1829 | const char *branch_status_color = color(WT_STATUS_HEADER, s); | |
ecbc23e4 | 1830 | enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo); |
98bf8a47 | 1831 | |
bda324cf | 1832 | if (s->branch) { |
355ec7a1 | 1833 | const char *on_what = _("On branch "); |
bda324cf | 1834 | const char *branch_name = s->branch; |
c72b49df | 1835 | if (!strcmp(branch_name, "HEAD")) { |
1d282327 | 1836 | branch_status_color = color(WT_STATUS_NOBRANCH, s); |
73ba5d78 SS |
1837 | if (s->state.rebase_in_progress || |
1838 | s->state.rebase_interactive_in_progress) { | |
1839 | if (s->state.rebase_interactive_in_progress) | |
df25e947 GP |
1840 | on_what = _("interactive rebase in progress; onto "); |
1841 | else | |
1842 | on_what = _("rebase in progress; onto "); | |
73ba5d78 SS |
1843 | branch_name = s->state.onto; |
1844 | } else if (s->state.detached_from) { | |
1845 | branch_name = s->state.detached_from; | |
1846 | if (s->state.detached_at) | |
2708ce62 | 1847 | on_what = _("HEAD detached at "); |
b397ea48 | 1848 | else |
2708ce62 | 1849 | on_what = _("HEAD detached from "); |
b397ea48 NTND |
1850 | } else { |
1851 | branch_name = ""; | |
1852 | on_what = _("Not currently on any branch."); | |
1853 | } | |
c72b49df RS |
1854 | } else |
1855 | skip_prefix(branch_name, "refs/heads/", &branch_name); | |
7d7d6802 | 1856 | status_printf(s, color(WT_STATUS_HEADER, s), "%s", ""); |
b926c0d1 JN |
1857 | status_printf_more(s, branch_status_color, "%s", on_what); |
1858 | status_printf_more(s, branch_color, "%s\n", branch_name); | |
b6975ab5 | 1859 | if (!s->is_initial) |
957a0fe2 | 1860 | wt_longstatus_print_tracking(s); |
bda324cf | 1861 | } |
c91f0d92 | 1862 | |
73ba5d78 | 1863 | wt_longstatus_print_state(s); |
3b691ccc | 1864 | |
c91f0d92 | 1865 | if (s->is_initial) { |
7d7d6802 | 1866 | status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", ""); |
4ddb1354 KS |
1867 | status_printf_ln(s, color(WT_STATUS_HEADER, s), |
1868 | s->commit_template | |
1869 | ? _("Initial commit") | |
1870 | : _("No commits yet")); | |
7d7d6802 | 1871 | status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", ""); |
c91f0d92 JK |
1872 | } |
1873 | ||
957a0fe2 JH |
1874 | wt_longstatus_print_updated(s); |
1875 | wt_longstatus_print_unmerged(s); | |
1876 | wt_longstatus_print_changed(s); | |
46a958b3 JL |
1877 | if (s->submodule_summary && |
1878 | (!s->ignore_submodule_arg || | |
1879 | strcmp(s->ignore_submodule_arg, "all"))) { | |
957a0fe2 JH |
1880 | wt_longstatus_print_submodule_summary(s, 0); /* staged */ |
1881 | wt_longstatus_print_submodule_summary(s, 1); /* unstaged */ | |
f17a5d34 | 1882 | } |
2381e39e | 1883 | if (s->show_untracked_files) { |
957a0fe2 | 1884 | wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add"); |
eec0f7f2 | 1885 | if (s->show_ignored_mode) |
957a0fe2 | 1886 | wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f"); |
ecbc23e4 | 1887 | if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) { |
7d7d6802 | 1888 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); |
ecbc23e4 RR |
1889 | if (fsm_mode > FSMONITOR_MODE_DISABLED) { |
1890 | status_printf_ln(s, GIT_COLOR_NORMAL, | |
1891 | _("It took %.2f seconds to enumerate untracked files,\n" | |
1892 | "but the results were cached, and subsequent runs may be faster."), | |
1893 | s->untracked_in_ms / 1000.0); | |
1894 | } else { | |
1895 | status_printf_ln(s, GIT_COLOR_NORMAL, | |
1896 | _("It took %.2f seconds to enumerate untracked files."), | |
1897 | s->untracked_in_ms / 1000.0); | |
1898 | } | |
6a38ef2c | 1899 | status_printf_ln(s, GIT_COLOR_NORMAL, |
ecbc23e4 RR |
1900 | _("See 'git help status' for information on how to improve this.")); |
1901 | status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); | |
6a38ef2c | 1902 | } |
6fa90194 | 1903 | } else if (s->committable) |
355ec7a1 | 1904 | status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"), |
6a964f57 | 1905 | s->hints |
355ec7a1 | 1906 | ? _(" (use -u option to show untracked files)") : ""); |
c91f0d92 | 1907 | |
1324fb6f | 1908 | if (s->verbose) |
957a0fe2 | 1909 | wt_longstatus_print_verbose(s); |
6fa90194 | 1910 | if (!s->committable) { |
6e458bf6 | 1911 | if (s->amend) |
355ec7a1 | 1912 | status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes")); |
37d07f8f JH |
1913 | else if (s->nowarn) |
1914 | ; /* nothing */ | |
50bd8b7e | 1915 | else if (s->workdir_dirty) { |
6a964f57 | 1916 | if (s->hints) |
8f7e3de0 MÅ |
1917 | fprintf(s->fp, _("no changes added to commit " |
1918 | "(use \"git add\" and/or " | |
1919 | "\"git commit -a\")\n")); | |
50bd8b7e | 1920 | else |
8f7e3de0 MÅ |
1921 | fprintf(s->fp, _("no changes added to " |
1922 | "commit\n")); | |
50bd8b7e | 1923 | } else if (s->untracked.nr) { |
6a964f57 | 1924 | if (s->hints) |
8f7e3de0 MÅ |
1925 | fprintf(s->fp, _("nothing added to commit but " |
1926 | "untracked files present (use " | |
1927 | "\"git add\" to track)\n")); | |
50bd8b7e | 1928 | else |
8f7e3de0 MÅ |
1929 | fprintf(s->fp, _("nothing added to commit but " |
1930 | "untracked files present\n")); | |
50bd8b7e | 1931 | } else if (s->is_initial) { |
6a964f57 | 1932 | if (s->hints) |
8f7e3de0 MÅ |
1933 | fprintf(s->fp, _("nothing to commit (create/" |
1934 | "copy files and use \"git " | |
1935 | "add\" to track)\n")); | |
50bd8b7e | 1936 | else |
8f7e3de0 | 1937 | fprintf(s->fp, _("nothing to commit\n")); |
50bd8b7e | 1938 | } else if (!s->show_untracked_files) { |
6a964f57 | 1939 | if (s->hints) |
8f7e3de0 MÅ |
1940 | fprintf(s->fp, _("nothing to commit (use -u to " |
1941 | "show untracked files)\n")); | |
50bd8b7e | 1942 | else |
8f7e3de0 | 1943 | fprintf(s->fp, _("nothing to commit\n")); |
50bd8b7e | 1944 | } else |
8f7e3de0 MÅ |
1945 | fprintf(s->fp, _("nothing to commit, working tree " |
1946 | "clean\n")); | |
6e458bf6 | 1947 | } |
c1b5d019 LB |
1948 | if(s->show_stash) |
1949 | wt_longstatus_print_stash_summary(s); | |
c91f0d92 | 1950 | } |
84dbe7b8 | 1951 | |
3207a3a2 | 1952 | static void wt_shortstatus_unmerged(struct string_list_item *it, |
84dbe7b8 MG |
1953 | struct wt_status *s) |
1954 | { | |
1955 | struct wt_status_change_data *d = it->util; | |
1956 | const char *how = "??"; | |
1957 | ||
1958 | switch (d->stagemask) { | |
1959 | case 1: how = "DD"; break; /* both deleted */ | |
1960 | case 2: how = "AU"; break; /* added by us */ | |
1961 | case 3: how = "UD"; break; /* deleted by them */ | |
1962 | case 4: how = "UA"; break; /* added by them */ | |
1963 | case 5: how = "DU"; break; /* deleted by us */ | |
1964 | case 6: how = "AA"; break; /* both added */ | |
1965 | case 7: how = "UU"; break; /* both modified */ | |
1966 | } | |
3fe2a894 | 1967 | color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how); |
3207a3a2 | 1968 | if (s->null_termination) { |
8f7e3de0 | 1969 | fprintf(s->fp, " %s%c", it->string, 0); |
84dbe7b8 MG |
1970 | } else { |
1971 | struct strbuf onebuf = STRBUF_INIT; | |
1972 | const char *one; | |
a361dd3f | 1973 | one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); |
8f7e3de0 | 1974 | fprintf(s->fp, " %s\n", one); |
84dbe7b8 MG |
1975 | strbuf_release(&onebuf); |
1976 | } | |
1977 | } | |
1978 | ||
3207a3a2 | 1979 | static void wt_shortstatus_status(struct string_list_item *it, |
84dbe7b8 MG |
1980 | struct wt_status *s) |
1981 | { | |
1982 | struct wt_status_change_data *d = it->util; | |
1983 | ||
3fe2a894 MG |
1984 | if (d->index_status) |
1985 | color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status); | |
1986 | else | |
8f7e3de0 | 1987 | fputc(' ', s->fp); |
3fe2a894 MG |
1988 | if (d->worktree_status) |
1989 | color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status); | |
1990 | else | |
8f7e3de0 MÅ |
1991 | fputc(' ', s->fp); |
1992 | fputc(' ', s->fp); | |
3207a3a2 | 1993 | if (s->null_termination) { |
8f7e3de0 | 1994 | fprintf(s->fp, "%s%c", it->string, 0); |
5134ccde | 1995 | if (d->rename_source) |
8f7e3de0 | 1996 | fprintf(s->fp, "%s%c", d->rename_source, 0); |
84dbe7b8 MG |
1997 | } else { |
1998 | struct strbuf onebuf = STRBUF_INIT; | |
1999 | const char *one; | |
5134ccde NTND |
2000 | |
2001 | if (d->rename_source) { | |
f3fc4a1b JH |
2002 | one = quote_path(d->rename_source, s->prefix, &onebuf, |
2003 | QUOTE_PATH_QUOTE_SP); | |
8f7e3de0 | 2004 | fprintf(s->fp, "%s -> ", one); |
84dbe7b8 MG |
2005 | strbuf_release(&onebuf); |
2006 | } | |
f3fc4a1b | 2007 | one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); |
8f7e3de0 | 2008 | fprintf(s->fp, "%s\n", one); |
84dbe7b8 MG |
2009 | strbuf_release(&onebuf); |
2010 | } | |
2011 | } | |
2012 | ||
3207a3a2 | 2013 | static void wt_shortstatus_other(struct string_list_item *it, |
2381e39e | 2014 | struct wt_status *s, const char *sign) |
84dbe7b8 | 2015 | { |
3207a3a2 | 2016 | if (s->null_termination) { |
8f7e3de0 | 2017 | fprintf(s->fp, "%s %s%c", sign, it->string, 0); |
84dbe7b8 MG |
2018 | } else { |
2019 | struct strbuf onebuf = STRBUF_INIT; | |
2020 | const char *one; | |
a361dd3f | 2021 | one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); |
c1909e72 | 2022 | color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign); |
8f7e3de0 | 2023 | fprintf(s->fp, " %s\n", one); |
84dbe7b8 MG |
2024 | strbuf_release(&onebuf); |
2025 | } | |
2026 | } | |
2027 | ||
05a59a08 DKF |
2028 | static void wt_shortstatus_print_tracking(struct wt_status *s) |
2029 | { | |
2030 | struct branch *branch; | |
2031 | const char *header_color = color(WT_STATUS_HEADER, s); | |
2032 | const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s); | |
2033 | const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s); | |
2034 | ||
2035 | const char *base; | |
5e8d2729 | 2036 | char *short_base; |
05a59a08 | 2037 | const char *branch_name; |
3ca1897c | 2038 | int num_ours, num_theirs, sti; |
f2e08739 | 2039 | int upstream_is_gone = 0; |
05a59a08 DKF |
2040 | |
2041 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## "); | |
2042 | ||
2043 | if (!s->branch) | |
2044 | return; | |
2045 | branch_name = s->branch; | |
2046 | ||
b9e2bc56 MG |
2047 | #define LABEL(string) (s->no_gettext ? (string) : _(string)) |
2048 | ||
05a59a08 | 2049 | if (s->is_initial) |
4ddb1354 | 2050 | color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on "))); |
f2e08739 | 2051 | |
baf0a3e4 RS |
2052 | if (!strcmp(s->branch, "HEAD")) { |
2053 | color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s", | |
b9e2bc56 | 2054 | LABEL(N_("HEAD (no branch)"))); |
baf0a3e4 RS |
2055 | goto conclude; |
2056 | } | |
2057 | ||
c72b49df | 2058 | skip_prefix(branch_name, "refs/heads/", &branch_name); |
05a59a08 | 2059 | |
8d8325f8 | 2060 | branch = branch_get(branch_name); |
f2e08739 JX |
2061 | |
2062 | color_fprintf(s->fp, branch_color_local, "%s", branch_name); | |
2063 | ||
3ca1897c | 2064 | sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base, |
c646d093 | 2065 | 0, s->ahead_behind_flags); |
3ca1897c | 2066 | if (sti < 0) { |
bcf8cc25 RS |
2067 | if (!base) |
2068 | goto conclude; | |
979cb245 | 2069 | |
f2e08739 | 2070 | upstream_is_gone = 1; |
05a59a08 DKF |
2071 | } |
2072 | ||
5e8d2729 | 2073 | short_base = shorten_unambiguous_ref(base, 0); |
05a59a08 | 2074 | color_fprintf(s->fp, header_color, "..."); |
5e8d2729 RS |
2075 | color_fprintf(s->fp, branch_color_remote, "%s", short_base); |
2076 | free(short_base); | |
05a59a08 | 2077 | |
3ca1897c | 2078 | if (!upstream_is_gone && !sti) |
bcf8cc25 | 2079 | goto conclude; |
f223459b | 2080 | |
05a59a08 | 2081 | color_fprintf(s->fp, header_color, " ["); |
f2e08739 | 2082 | if (upstream_is_gone) { |
7a76c28f | 2083 | color_fprintf(s->fp, header_color, LABEL(N_("gone"))); |
3ca1897c JH |
2084 | } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) { |
2085 | color_fprintf(s->fp, header_color, LABEL(N_("different"))); | |
f2e08739 | 2086 | } else if (!num_ours) { |
7a76c28f | 2087 | color_fprintf(s->fp, header_color, LABEL(N_("behind "))); |
05a59a08 DKF |
2088 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); |
2089 | } else if (!num_theirs) { | |
df227241 | 2090 | color_fprintf(s->fp, header_color, LABEL(N_("ahead "))); |
05a59a08 DKF |
2091 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); |
2092 | } else { | |
df227241 | 2093 | color_fprintf(s->fp, header_color, LABEL(N_("ahead "))); |
05a59a08 | 2094 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); |
7a76c28f | 2095 | color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind "))); |
05a59a08 DKF |
2096 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); |
2097 | } | |
2098 | ||
a5985237 | 2099 | color_fprintf(s->fp, header_color, "]"); |
bcf8cc25 | 2100 | conclude: |
a5985237 | 2101 | fputc(s->null_termination ? '\0' : '\n', s->fp); |
05a59a08 DKF |
2102 | } |
2103 | ||
be7e795e | 2104 | static void wt_shortstatus_print(struct wt_status *s) |
84dbe7b8 | 2105 | { |
d4aae459 | 2106 | struct string_list_item *it; |
05a59a08 | 2107 | |
d4a6bf1f | 2108 | if (s->show_branch) |
05a59a08 DKF |
2109 | wt_shortstatus_print_tracking(s); |
2110 | ||
d4aae459 SB |
2111 | for_each_string_list_item(it, &s->change) { |
2112 | struct wt_status_change_data *d = it->util; | |
84dbe7b8 | 2113 | |
84dbe7b8 | 2114 | if (d->stagemask) |
3207a3a2 | 2115 | wt_shortstatus_unmerged(it, s); |
84dbe7b8 | 2116 | else |
3207a3a2 | 2117 | wt_shortstatus_status(it, s); |
84dbe7b8 | 2118 | } |
d4aae459 | 2119 | for_each_string_list_item(it, &s->untracked) |
3207a3a2 | 2120 | wt_shortstatus_other(it, s, "??"); |
2381e39e | 2121 | |
d4aae459 | 2122 | for_each_string_list_item(it, &s->ignored) |
3207a3a2 | 2123 | wt_shortstatus_other(it, s, "!!"); |
84dbe7b8 | 2124 | } |
4a7cc2fd | 2125 | |
be7e795e | 2126 | static void wt_porcelain_print(struct wt_status *s) |
4a7cc2fd JK |
2127 | { |
2128 | s->use_color = 0; | |
8661768f JK |
2129 | s->relative_paths = 0; |
2130 | s->prefix = NULL; | |
7a76c28f | 2131 | s->no_gettext = 1; |
d4a6bf1f | 2132 | wt_shortstatus_print(s); |
4a7cc2fd | 2133 | } |
be7e795e | 2134 | |
d9fc746c JH |
2135 | /* |
2136 | * Print branch information for porcelain v2 output. These lines | |
2137 | * are printed when the '--branch' parameter is given. | |
2138 | * | |
2139 | * # branch.oid <commit><eol> | |
2140 | * # branch.head <head><eol> | |
2141 | * [# branch.upstream <upstream><eol> | |
2142 | * [# branch.ab +<ahead> -<behind><eol>]] | |
2143 | * | |
6d12b533 | 2144 | * <commit> ::= the current commit hash or the literal |
d9fc746c JH |
2145 | * "(initial)" to indicate an initialized repo |
2146 | * with no commits. | |
2147 | * | |
2148 | * <head> ::= <branch_name> the current branch name or | |
2149 | * "(detached)" literal when detached head or | |
2150 | * "(unknown)" when something is wrong. | |
2151 | * | |
2152 | * <upstream> ::= the upstream branch name, when set. | |
2153 | * | |
fd9b544a | 2154 | * <ahead> ::= integer ahead value or '?'. |
d9fc746c | 2155 | * |
fd9b544a | 2156 | * <behind> ::= integer behind value or '?'. |
d9fc746c JH |
2157 | * |
2158 | * The end-of-line is defined by the -z flag. | |
2159 | * | |
2160 | * <eol> ::= NUL when -z, | |
2161 | * LF when NOT -z. | |
2162 | * | |
fd9b544a JH |
2163 | * When an upstream is set and present, the 'branch.ab' line will |
2164 | * be printed with the ahead/behind counts for the branch and the | |
2165 | * upstream. When AHEAD_BEHIND_QUICK is requested and the branches | |
2166 | * are different, '?' will be substituted for the actual count. | |
d9fc746c JH |
2167 | */ |
2168 | static void wt_porcelain_v2_print_tracking(struct wt_status *s) | |
2169 | { | |
2170 | struct branch *branch; | |
2171 | const char *base; | |
2172 | const char *branch_name; | |
d9fc746c JH |
2173 | int ab_info, nr_ahead, nr_behind; |
2174 | char eol = s->null_termination ? '\0' : '\n'; | |
2175 | ||
d9fc746c | 2176 | fprintf(s->fp, "# branch.oid %s%c", |
e0cb7cdb | 2177 | (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)), |
d9fc746c JH |
2178 | eol); |
2179 | ||
2180 | if (!s->branch) | |
2181 | fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol); | |
2182 | else { | |
2183 | if (!strcmp(s->branch, "HEAD")) { | |
2184 | fprintf(s->fp, "# branch.head %s%c", "(detached)", eol); | |
2185 | ||
73ba5d78 SS |
2186 | if (s->state.rebase_in_progress || |
2187 | s->state.rebase_interactive_in_progress) | |
2188 | branch_name = s->state.onto; | |
2189 | else if (s->state.detached_from) | |
2190 | branch_name = s->state.detached_from; | |
d9fc746c JH |
2191 | else |
2192 | branch_name = ""; | |
2193 | } else { | |
2194 | branch_name = NULL; | |
2195 | skip_prefix(s->branch, "refs/heads/", &branch_name); | |
2196 | ||
2197 | fprintf(s->fp, "# branch.head %s%c", branch_name, eol); | |
2198 | } | |
2199 | ||
2200 | /* Lookup stats on the upstream tracking branch, if set. */ | |
2201 | branch = branch_get(branch_name); | |
2202 | base = NULL; | |
fd9b544a | 2203 | ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind, |
c646d093 | 2204 | &base, 0, s->ahead_behind_flags); |
d9fc746c JH |
2205 | if (base) { |
2206 | base = shorten_unambiguous_ref(base, 0); | |
2207 | fprintf(s->fp, "# branch.upstream %s%c", base, eol); | |
2208 | free((char *)base); | |
2209 | ||
fd9b544a JH |
2210 | if (ab_info > 0) { |
2211 | /* different */ | |
2212 | if (nr_ahead || nr_behind) | |
2213 | fprintf(s->fp, "# branch.ab +%d -%d%c", | |
2214 | nr_ahead, nr_behind, eol); | |
2215 | else | |
2216 | fprintf(s->fp, "# branch.ab +? -?%c", | |
2217 | eol); | |
2218 | } else if (!ab_info) { | |
2219 | /* same */ | |
2220 | fprintf(s->fp, "# branch.ab +0 -0%c", eol); | |
2221 | } | |
d9fc746c JH |
2222 | } |
2223 | } | |
d9fc746c JH |
2224 | } |
2225 | ||
2e59e780 ØW |
2226 | /* |
2227 | * Print the stash count in a porcelain-friendly format | |
2228 | */ | |
2229 | static void wt_porcelain_v2_print_stash(struct wt_status *s) | |
2230 | { | |
2231 | int stash_count = count_stash_entries(); | |
2232 | char eol = s->null_termination ? '\0' : '\n'; | |
2233 | ||
2234 | if (stash_count > 0) | |
2235 | fprintf(s->fp, "# stash %d%c", stash_count, eol); | |
2236 | } | |
2237 | ||
24959bad JH |
2238 | /* |
2239 | * Convert various submodule status values into a | |
2240 | * fixed-length string of characters in the buffer provided. | |
2241 | */ | |
2242 | static void wt_porcelain_v2_submodule_state( | |
2243 | struct wt_status_change_data *d, | |
2244 | char sub[5]) | |
2245 | { | |
2246 | if (S_ISGITLINK(d->mode_head) || | |
2247 | S_ISGITLINK(d->mode_index) || | |
2248 | S_ISGITLINK(d->mode_worktree)) { | |
2249 | sub[0] = 'S'; | |
2250 | sub[1] = d->new_submodule_commits ? 'C' : '.'; | |
2251 | sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.'; | |
2252 | sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.'; | |
2253 | } else { | |
2254 | sub[0] = 'N'; | |
2255 | sub[1] = '.'; | |
2256 | sub[2] = '.'; | |
2257 | sub[3] = '.'; | |
2258 | } | |
2259 | sub[4] = 0; | |
2260 | } | |
2261 | ||
2262 | /* | |
2263 | * Fix-up changed entries before we print them. | |
2264 | */ | |
13a17812 | 2265 | static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it) |
24959bad JH |
2266 | { |
2267 | struct wt_status_change_data *d = it->util; | |
2268 | ||
2269 | if (!d->index_status) { | |
2270 | /* | |
2271 | * This entry is unchanged in the index (relative to the head). | |
2272 | * Therefore, the collect_updated_cb was never called for this | |
2273 | * entry (during the head-vs-index scan) and so the head column | |
2274 | * fields were never set. | |
2275 | * | |
2276 | * We must have data for the index column (from the | |
2277 | * index-vs-worktree scan (otherwise, this entry should not be | |
2278 | * in the list of changes)). | |
2279 | * | |
2280 | * Copy index column fields to the head column, so that our | |
2281 | * output looks complete. | |
2282 | */ | |
2283 | assert(d->mode_head == 0); | |
2284 | d->mode_head = d->mode_index; | |
2285 | oidcpy(&d->oid_head, &d->oid_index); | |
2286 | } | |
2287 | ||
2288 | if (!d->worktree_status) { | |
2289 | /* | |
2290 | * This entry is unchanged in the worktree (relative to the index). | |
2291 | * Therefore, the collect_changed_cb was never called for this entry | |
2292 | * (during the index-vs-worktree scan) and so the worktree column | |
2293 | * fields were never set. | |
2294 | * | |
2295 | * We must have data for the index column (from the head-vs-index | |
2296 | * scan). | |
2297 | * | |
2298 | * Copy the index column fields to the worktree column so that | |
2299 | * our output looks complete. | |
2300 | * | |
2301 | * Note that we only have a mode field in the worktree column | |
2302 | * because the scan code tries really hard to not have to compute it. | |
2303 | */ | |
2304 | assert(d->mode_worktree == 0); | |
2305 | d->mode_worktree = d->mode_index; | |
2306 | } | |
2307 | } | |
2308 | ||
2309 | /* | |
2310 | * Print porcelain v2 info for tracked entries with changes. | |
2311 | */ | |
2312 | static void wt_porcelain_v2_print_changed_entry( | |
2313 | struct string_list_item *it, | |
2314 | struct wt_status *s) | |
2315 | { | |
2316 | struct wt_status_change_data *d = it->util; | |
5134ccde NTND |
2317 | struct strbuf buf = STRBUF_INIT; |
2318 | struct strbuf buf_from = STRBUF_INIT; | |
2319 | const char *path = NULL; | |
2320 | const char *path_from = NULL; | |
24959bad JH |
2321 | char key[3]; |
2322 | char submodule_token[5]; | |
2323 | char sep_char, eol_char; | |
2324 | ||
13a17812 | 2325 | wt_porcelain_v2_fix_up_changed(it); |
24959bad JH |
2326 | wt_porcelain_v2_submodule_state(d, submodule_token); |
2327 | ||
2328 | key[0] = d->index_status ? d->index_status : '.'; | |
2329 | key[1] = d->worktree_status ? d->worktree_status : '.'; | |
2330 | key[2] = 0; | |
2331 | ||
2332 | if (s->null_termination) { | |
2333 | /* | |
2334 | * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first. | |
2335 | * A single NUL character separates them. | |
2336 | */ | |
2337 | sep_char = '\0'; | |
2338 | eol_char = '\0'; | |
5134ccde NTND |
2339 | path = it->string; |
2340 | path_from = d->rename_source; | |
24959bad JH |
2341 | } else { |
2342 | /* | |
2343 | * Path(s) are C-quoted if necessary. Current path is ALWAYS first. | |
2344 | * The source path is only present when necessary. | |
2345 | * A single TAB separates them (because paths can contain spaces | |
2346 | * which are not escaped and C-quoting does escape TAB characters). | |
2347 | */ | |
2348 | sep_char = '\t'; | |
2349 | eol_char = '\n'; | |
88910c99 | 2350 | path = quote_path(it->string, s->prefix, &buf, 0); |
5134ccde | 2351 | if (d->rename_source) |
88910c99 | 2352 | path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0); |
24959bad JH |
2353 | } |
2354 | ||
5134ccde | 2355 | if (path_from) |
24959bad JH |
2356 | fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c", |
2357 | key, submodule_token, | |
2358 | d->mode_head, d->mode_index, d->mode_worktree, | |
2359 | oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index), | |
5134ccde NTND |
2360 | d->rename_status, d->rename_score, |
2361 | path, sep_char, path_from, eol_char); | |
24959bad JH |
2362 | else |
2363 | fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c", | |
2364 | key, submodule_token, | |
2365 | d->mode_head, d->mode_index, d->mode_worktree, | |
2366 | oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index), | |
5134ccde | 2367 | path, eol_char); |
24959bad | 2368 | |
5134ccde NTND |
2369 | strbuf_release(&buf); |
2370 | strbuf_release(&buf_from); | |
24959bad JH |
2371 | } |
2372 | ||
2373 | /* | |
2374 | * Print porcelain v2 status info for unmerged entries. | |
2375 | */ | |
2376 | static void wt_porcelain_v2_print_unmerged_entry( | |
2377 | struct string_list_item *it, | |
2378 | struct wt_status *s) | |
2379 | { | |
2380 | struct wt_status_change_data *d = it->util; | |
5b02ca38 | 2381 | struct index_state *istate = s->repo->index; |
24959bad JH |
2382 | const struct cache_entry *ce; |
2383 | struct strbuf buf_index = STRBUF_INIT; | |
2384 | const char *path_index = NULL; | |
2385 | int pos, stage, sum; | |
2386 | struct { | |
2387 | int mode; | |
2388 | struct object_id oid; | |
2389 | } stages[3]; | |
2390 | char *key; | |
2391 | char submodule_token[5]; | |
2392 | char unmerged_prefix = 'u'; | |
2393 | char eol_char = s->null_termination ? '\0' : '\n'; | |
2394 | ||
2395 | wt_porcelain_v2_submodule_state(d, submodule_token); | |
2396 | ||
2397 | switch (d->stagemask) { | |
2398 | case 1: key = "DD"; break; /* both deleted */ | |
2399 | case 2: key = "AU"; break; /* added by us */ | |
2400 | case 3: key = "UD"; break; /* deleted by them */ | |
2401 | case 4: key = "UA"; break; /* added by them */ | |
2402 | case 5: key = "DU"; break; /* deleted by us */ | |
2403 | case 6: key = "AA"; break; /* both added */ | |
2404 | case 7: key = "UU"; break; /* both modified */ | |
2405 | default: | |
033abf97 | 2406 | BUG("unhandled unmerged status %x", d->stagemask); |
24959bad JH |
2407 | } |
2408 | ||
2409 | /* | |
2410 | * Disregard d.aux.porcelain_v2 data that we accumulated | |
2411 | * for the head and index columns during the scans and | |
2412 | * replace with the actual stage data. | |
2413 | * | |
2414 | * Note that this is a last-one-wins for each the individual | |
2415 | * stage [123] columns in the event of multiple cache entries | |
2416 | * for same stage. | |
2417 | */ | |
2418 | memset(stages, 0, sizeof(stages)); | |
2419 | sum = 0; | |
5b02ca38 | 2420 | pos = index_name_pos(istate, it->string, strlen(it->string)); |
24959bad JH |
2421 | assert(pos < 0); |
2422 | pos = -pos-1; | |
5b02ca38 NTND |
2423 | while (pos < istate->cache_nr) { |
2424 | ce = istate->cache[pos++]; | |
24959bad JH |
2425 | stage = ce_stage(ce); |
2426 | if (strcmp(ce->name, it->string) || !stage) | |
2427 | break; | |
2428 | stages[stage - 1].mode = ce->ce_mode; | |
8694769f | 2429 | oidcpy(&stages[stage - 1].oid, &ce->oid); |
24959bad JH |
2430 | sum |= (1 << (stage - 1)); |
2431 | } | |
2432 | if (sum != d->stagemask) | |
033abf97 | 2433 | BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask); |
24959bad JH |
2434 | |
2435 | if (s->null_termination) | |
2436 | path_index = it->string; | |
2437 | else | |
88910c99 | 2438 | path_index = quote_path(it->string, s->prefix, &buf_index, 0); |
24959bad JH |
2439 | |
2440 | fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c", | |
2441 | unmerged_prefix, key, submodule_token, | |
2442 | stages[0].mode, /* stage 1 */ | |
2443 | stages[1].mode, /* stage 2 */ | |
2444 | stages[2].mode, /* stage 3 */ | |
2445 | d->mode_worktree, | |
2446 | oid_to_hex(&stages[0].oid), /* stage 1 */ | |
2447 | oid_to_hex(&stages[1].oid), /* stage 2 */ | |
2448 | oid_to_hex(&stages[2].oid), /* stage 3 */ | |
2449 | path_index, | |
2450 | eol_char); | |
2451 | ||
2452 | strbuf_release(&buf_index); | |
2453 | } | |
2454 | ||
2455 | /* | |
2456 | * Print porcelain V2 status info for untracked and ignored entries. | |
2457 | */ | |
2458 | static void wt_porcelain_v2_print_other( | |
2459 | struct string_list_item *it, | |
2460 | struct wt_status *s, | |
2461 | char prefix) | |
2462 | { | |
2463 | struct strbuf buf = STRBUF_INIT; | |
2464 | const char *path; | |
2465 | char eol_char; | |
2466 | ||
2467 | if (s->null_termination) { | |
2468 | path = it->string; | |
2469 | eol_char = '\0'; | |
2470 | } else { | |
88910c99 | 2471 | path = quote_path(it->string, s->prefix, &buf, 0); |
24959bad JH |
2472 | eol_char = '\n'; |
2473 | } | |
2474 | ||
2475 | fprintf(s->fp, "%c %s%c", prefix, path, eol_char); | |
2476 | ||
2477 | strbuf_release(&buf); | |
2478 | } | |
2479 | ||
2480 | /* | |
2481 | * Print porcelain V2 status. | |
2482 | * | |
d9fc746c | 2483 | * [<v2_branch>] |
24959bad JH |
2484 | * [<v2_changed_items>]* |
2485 | * [<v2_unmerged_items>]* | |
2486 | * [<v2_untracked_items>]* | |
2487 | * [<v2_ignored_items>]* | |
2488 | * | |
2489 | */ | |
2490 | static void wt_porcelain_v2_print(struct wt_status *s) | |
2491 | { | |
2492 | struct wt_status_change_data *d; | |
2493 | struct string_list_item *it; | |
2494 | int i; | |
2495 | ||
d9fc746c JH |
2496 | if (s->show_branch) |
2497 | wt_porcelain_v2_print_tracking(s); | |
2498 | ||
2e59e780 ØW |
2499 | if (s->show_stash) |
2500 | wt_porcelain_v2_print_stash(s); | |
2501 | ||
24959bad JH |
2502 | for (i = 0; i < s->change.nr; i++) { |
2503 | it = &(s->change.items[i]); | |
2504 | d = it->util; | |
2505 | if (!d->stagemask) | |
2506 | wt_porcelain_v2_print_changed_entry(it, s); | |
2507 | } | |
2508 | ||
2509 | for (i = 0; i < s->change.nr; i++) { | |
2510 | it = &(s->change.items[i]); | |
2511 | d = it->util; | |
2512 | if (d->stagemask) | |
2513 | wt_porcelain_v2_print_unmerged_entry(it, s); | |
2514 | } | |
2515 | ||
2516 | for (i = 0; i < s->untracked.nr; i++) { | |
2517 | it = &(s->untracked.items[i]); | |
2518 | wt_porcelain_v2_print_other(it, s, '?'); | |
2519 | } | |
2520 | ||
2521 | for (i = 0; i < s->ignored.nr; i++) { | |
2522 | it = &(s->ignored.items[i]); | |
2523 | wt_porcelain_v2_print_other(it, s, '!'); | |
2524 | } | |
2525 | } | |
2526 | ||
be7e795e JH |
2527 | void wt_status_print(struct wt_status *s) |
2528 | { | |
942b2740 JH |
2529 | trace2_data_intmax("status", s->repo, "count/changed", s->change.nr); |
2530 | trace2_data_intmax("status", s->repo, "count/untracked", | |
2531 | s->untracked.nr); | |
2532 | trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr); | |
2533 | ||
2534 | trace2_region_enter("status", "print", s->repo); | |
2535 | ||
be7e795e JH |
2536 | switch (s->status_format) { |
2537 | case STATUS_FORMAT_SHORT: | |
2538 | wt_shortstatus_print(s); | |
2539 | break; | |
2540 | case STATUS_FORMAT_PORCELAIN: | |
2541 | wt_porcelain_print(s); | |
2542 | break; | |
1ecdecce | 2543 | case STATUS_FORMAT_PORCELAIN_V2: |
24959bad | 2544 | wt_porcelain_v2_print(s); |
1ecdecce | 2545 | break; |
be7e795e | 2546 | case STATUS_FORMAT_UNSPECIFIED: |
033abf97 | 2547 | BUG("finalize_deferred_config() should have been called"); |
be7e795e JH |
2548 | break; |
2549 | case STATUS_FORMAT_NONE: | |
2550 | case STATUS_FORMAT_LONG: | |
2551 | wt_longstatus_print(s); | |
2552 | break; | |
2553 | } | |
942b2740 JH |
2554 | |
2555 | trace2_region_leave("status", "print", s->repo); | |
be7e795e | 2556 | } |
fd84986f JS |
2557 | |
2558 | /** | |
2559 | * Returns 1 if there are unstaged changes, 0 otherwise. | |
2560 | */ | |
5b02ca38 | 2561 | int has_unstaged_changes(struct repository *r, int ignore_submodules) |
fd84986f JS |
2562 | { |
2563 | struct rev_info rev_info; | |
2564 | int result; | |
2565 | ||
5b02ca38 | 2566 | repo_init_revisions(r, &rev_info, NULL); |
c6d8ccf3 | 2567 | if (ignore_submodules) { |
0d1e0e78 | 2568 | rev_info.diffopt.flags.ignore_submodules = 1; |
b50d82b0 | 2569 | rev_info.diffopt.flags.override_submodule_config = 1; |
c6d8ccf3 | 2570 | } |
0d1e0e78 | 2571 | rev_info.diffopt.flags.quick = 1; |
fd84986f JS |
2572 | diff_setup_done(&rev_info.diffopt); |
2573 | result = run_diff_files(&rev_info, 0); | |
54c8a7c3 | 2574 | result = diff_result_code(&rev_info.diffopt, result); |
1878b5ed | 2575 | release_revisions(&rev_info); |
54c8a7c3 | 2576 | return result; |
fd84986f JS |
2577 | } |
2578 | ||
2579 | /** | |
2580 | * Returns 1 if there are uncommitted changes, 0 otherwise. | |
2581 | */ | |
5b02ca38 NTND |
2582 | int has_uncommitted_changes(struct repository *r, |
2583 | int ignore_submodules) | |
fd84986f JS |
2584 | { |
2585 | struct rev_info rev_info; | |
2586 | int result; | |
2587 | ||
5b02ca38 | 2588 | if (is_index_unborn(r->index)) |
fd84986f JS |
2589 | return 0; |
2590 | ||
5b02ca38 | 2591 | repo_init_revisions(r, &rev_info, NULL); |
d8cc92ab | 2592 | if (ignore_submodules) |
0d1e0e78 BW |
2593 | rev_info.diffopt.flags.ignore_submodules = 1; |
2594 | rev_info.diffopt.flags.quick = 1; | |
3506dc94 | 2595 | |
fd84986f | 2596 | add_head_to_pending(&rev_info); |
3506dc94 JK |
2597 | if (!rev_info.pending.nr) { |
2598 | /* | |
2599 | * We have no head (or it's corrupt); use the empty tree, | |
2600 | * which will complain if the index is non-empty. | |
2601 | */ | |
5b02ca38 | 2602 | struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree); |
3506dc94 JK |
2603 | add_pending_object(&rev_info, &tree->object, ""); |
2604 | } | |
2605 | ||
fd84986f JS |
2606 | diff_setup_done(&rev_info.diffopt); |
2607 | result = run_diff_index(&rev_info, 1); | |
54c8a7c3 | 2608 | result = diff_result_code(&rev_info.diffopt, result); |
1878b5ed | 2609 | release_revisions(&rev_info); |
54c8a7c3 | 2610 | return result; |
fd84986f JS |
2611 | } |
2612 | ||
2613 | /** | |
2614 | * If the work tree has unstaged or uncommitted changes, dies with the | |
2615 | * appropriate message. | |
2616 | */ | |
5b02ca38 NTND |
2617 | int require_clean_work_tree(struct repository *r, |
2618 | const char *action, | |
2619 | const char *hint, | |
2620 | int ignore_submodules, | |
2621 | int gently) | |
fd84986f | 2622 | { |
837e34eb | 2623 | struct lock_file lock_file = LOCK_INIT; |
89d38fb2 | 2624 | int err = 0, fd; |
fd84986f | 2625 | |
3a95f31d | 2626 | fd = repo_hold_locked_index(r, &lock_file, 0); |
5b02ca38 | 2627 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
89d38fb2 | 2628 | if (0 <= fd) |
1b0d968b | 2629 | repo_update_index_if_able(r, &lock_file); |
837e34eb | 2630 | rollback_lock_file(&lock_file); |
fd84986f | 2631 | |
5b02ca38 | 2632 | if (has_unstaged_changes(r, ignore_submodules)) { |
fd84986f | 2633 | /* TRANSLATORS: the action is e.g. "pull with rebase" */ |
4777e175 | 2634 | error(_("cannot %s: You have unstaged changes."), _(action)); |
fd84986f JS |
2635 | err = 1; |
2636 | } | |
2637 | ||
5b02ca38 | 2638 | if (has_uncommitted_changes(r, ignore_submodules)) { |
fd84986f | 2639 | if (err) |
4777e175 | 2640 | error(_("additionally, your index contains uncommitted changes.")); |
fd84986f | 2641 | else |
4777e175 | 2642 | error(_("cannot %s: Your index contains uncommitted changes."), |
fd84986f JS |
2643 | _(action)); |
2644 | err = 1; | |
2645 | } | |
2646 | ||
2647 | if (err) { | |
2648 | if (hint) | |
2649 | error("%s", hint); | |
2650 | if (!gently) | |
2651 | exit(128); | |
2652 | } | |
2653 | ||
2654 | return err; | |
2655 | } |