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