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