]>
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" |
b6975ab5 | 11 | #include "remote.h" |
05a59a08 | 12 | #include "refs.h" |
46a958b3 | 13 | #include "submodule.h" |
323d0530 | 14 | #include "column.h" |
2d1cceba | 15 | #include "strbuf.h" |
c91f0d92 | 16 | |
23900a96 | 17 | static char default_wt_status_colors[][COLOR_MAXLEN] = { |
dc6ebd4c AL |
18 | GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */ |
19 | GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */ | |
20 | GIT_COLOR_RED, /* WT_STATUS_CHANGED */ | |
21 | GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */ | |
22 | GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */ | |
4d4d5726 | 23 | GIT_COLOR_RED, /* WT_STATUS_UNMERGED */ |
05a59a08 DKF |
24 | GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */ |
25 | GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */ | |
148135fc | 26 | GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */ |
c91f0d92 | 27 | }; |
4d229653 | 28 | |
d249b098 | 29 | static const char *color(int slot, struct wt_status *s) |
c91f0d92 | 30 | { |
daa0c3d9 JK |
31 | const char *c = ""; |
32 | if (want_color(s->use_color)) | |
33 | c = s->color_palette[slot]; | |
148135fc JK |
34 | if (slot == WT_STATUS_ONBRANCH && color_is_nil(c)) |
35 | c = s->color_palette[WT_STATUS_HEADER]; | |
36 | return c; | |
c91f0d92 JK |
37 | } |
38 | ||
becbdae8 JN |
39 | static void status_vprintf(struct wt_status *s, int at_bol, const char *color, |
40 | const char *fmt, va_list ap, const char *trail) | |
41 | { | |
42 | struct strbuf sb = STRBUF_INIT; | |
43 | struct strbuf linebuf = STRBUF_INIT; | |
44 | const char *line, *eol; | |
45 | ||
46 | strbuf_vaddf(&sb, fmt, ap); | |
47 | if (!sb.len) { | |
48 | strbuf_addch(&sb, '#'); | |
49 | if (!trail) | |
50 | strbuf_addch(&sb, ' '); | |
51 | color_print_strbuf(s->fp, color, &sb); | |
52 | if (trail) | |
53 | fprintf(s->fp, "%s", trail); | |
54 | strbuf_release(&sb); | |
55 | return; | |
56 | } | |
57 | for (line = sb.buf; *line; line = eol + 1) { | |
58 | eol = strchr(line, '\n'); | |
59 | ||
60 | strbuf_reset(&linebuf); | |
61 | if (at_bol) { | |
62 | strbuf_addch(&linebuf, '#'); | |
63 | if (*line != '\n' && *line != '\t') | |
64 | strbuf_addch(&linebuf, ' '); | |
65 | } | |
66 | if (eol) | |
67 | strbuf_add(&linebuf, line, eol - line); | |
68 | else | |
69 | strbuf_addstr(&linebuf, line); | |
70 | color_print_strbuf(s->fp, color, &linebuf); | |
71 | if (eol) | |
72 | fprintf(s->fp, "\n"); | |
73 | else | |
74 | break; | |
75 | at_bol = 1; | |
76 | } | |
77 | if (trail) | |
78 | fprintf(s->fp, "%s", trail); | |
79 | strbuf_release(&linebuf); | |
80 | strbuf_release(&sb); | |
81 | } | |
82 | ||
83 | void status_printf_ln(struct wt_status *s, const char *color, | |
84 | const char *fmt, ...) | |
85 | { | |
86 | va_list ap; | |
87 | ||
88 | va_start(ap, fmt); | |
89 | status_vprintf(s, 1, color, fmt, ap, "\n"); | |
90 | va_end(ap); | |
91 | } | |
92 | ||
93 | void status_printf(struct wt_status *s, const char *color, | |
94 | const char *fmt, ...) | |
95 | { | |
96 | va_list ap; | |
97 | ||
98 | va_start(ap, fmt); | |
99 | status_vprintf(s, 1, color, fmt, ap, NULL); | |
100 | va_end(ap); | |
101 | } | |
102 | ||
1e24845c JH |
103 | static void status_printf_more(struct wt_status *s, const char *color, |
104 | const char *fmt, ...) | |
becbdae8 JN |
105 | { |
106 | va_list ap; | |
107 | ||
108 | va_start(ap, fmt); | |
109 | status_vprintf(s, 0, color, fmt, ap, NULL); | |
110 | va_end(ap); | |
111 | } | |
112 | ||
c91f0d92 JK |
113 | void wt_status_prepare(struct wt_status *s) |
114 | { | |
115 | unsigned char sha1[20]; | |
c91f0d92 | 116 | |
cc46a743 | 117 | memset(s, 0, sizeof(*s)); |
23900a96 JH |
118 | memcpy(s->color_palette, default_wt_status_colors, |
119 | sizeof(default_wt_status_colors)); | |
d249b098 JH |
120 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; |
121 | s->use_color = -1; | |
122 | s->relative_paths = 1; | |
96ec7b1e | 123 | s->branch = resolve_refdup("HEAD", sha1, 0, NULL); |
c91f0d92 | 124 | s->reference = "HEAD"; |
f26a0012 | 125 | s->fp = stdout; |
0f729f21 | 126 | s->index_file = get_index_file(); |
50b7e70f | 127 | s->change.strdup_strings = 1; |
76378683 | 128 | s->untracked.strdup_strings = 1; |
6cb3f6b2 | 129 | s->ignored.strdup_strings = 1; |
c91f0d92 JK |
130 | } |
131 | ||
4d4d5726 JH |
132 | static void wt_status_print_unmerged_header(struct wt_status *s) |
133 | { | |
96b0ec1a LK |
134 | int i; |
135 | int del_mod_conflict = 0; | |
136 | int both_deleted = 0; | |
137 | int not_deleted = 0; | |
d249b098 | 138 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 139 | |
355ec7a1 | 140 | status_printf_ln(s, c, _("Unmerged paths:")); |
96b0ec1a LK |
141 | |
142 | for (i = 0; i < s->change.nr; i++) { | |
143 | struct string_list_item *it = &(s->change.items[i]); | |
144 | struct wt_status_change_data *d = it->util; | |
145 | ||
146 | switch (d->stagemask) { | |
147 | case 0: | |
148 | break; | |
149 | case 1: | |
150 | both_deleted = 1; | |
151 | break; | |
152 | case 3: | |
153 | case 5: | |
154 | del_mod_conflict = 1; | |
155 | break; | |
156 | default: | |
157 | not_deleted = 1; | |
158 | break; | |
159 | } | |
160 | } | |
161 | ||
edf563fb JK |
162 | if (!advice_status_hints) |
163 | return; | |
37f7a857 | 164 | if (s->whence != FROM_COMMIT) |
3c588453 JH |
165 | ; |
166 | else if (!s->is_initial) | |
355ec7a1 | 167 | status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference); |
4d4d5726 | 168 | else |
355ec7a1 | 169 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); |
96b0ec1a LK |
170 | |
171 | if (!both_deleted) { | |
172 | if (!del_mod_conflict) | |
173 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)")); | |
174 | else | |
175 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); | |
176 | } else if (!del_mod_conflict && !not_deleted) { | |
177 | status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)")); | |
178 | } else { | |
179 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); | |
180 | } | |
b926c0d1 | 181 | status_printf_ln(s, c, ""); |
4d4d5726 JH |
182 | } |
183 | ||
f26a0012 | 184 | static void wt_status_print_cached_header(struct wt_status *s) |
3c1eb9cb | 185 | { |
d249b098 | 186 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 187 | |
919a4ce0 | 188 | status_printf_ln(s, c, _("Changes to be committed:")); |
edf563fb JK |
189 | if (!advice_status_hints) |
190 | return; | |
37f7a857 | 191 | if (s->whence != FROM_COMMIT) |
3c588453 JH |
192 | ; /* NEEDSWORK: use "git reset --unresolve"??? */ |
193 | else if (!s->is_initial) | |
355ec7a1 | 194 | status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference); |
3c588453 | 195 | else |
355ec7a1 | 196 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); |
b926c0d1 | 197 | status_printf_ln(s, c, ""); |
3c1eb9cb JR |
198 | } |
199 | ||
bb914b14 | 200 | static void wt_status_print_dirty_header(struct wt_status *s, |
9297f77e JL |
201 | int has_deleted, |
202 | int has_dirty_submodules) | |
c91f0d92 | 203 | { |
d249b098 | 204 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 205 | |
355ec7a1 | 206 | status_printf_ln(s, c, _("Changes not staged for commit:")); |
edf563fb JK |
207 | if (!advice_status_hints) |
208 | return; | |
bb914b14 | 209 | if (!has_deleted) |
355ec7a1 | 210 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)")); |
bb914b14 | 211 | else |
355ec7a1 ÆAB |
212 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)")); |
213 | status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)")); | |
9297f77e | 214 | if (has_dirty_submodules) |
355ec7a1 | 215 | status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)")); |
b926c0d1 | 216 | status_printf_ln(s, c, ""); |
bb914b14 AM |
217 | } |
218 | ||
1b908b6f JH |
219 | static void wt_status_print_other_header(struct wt_status *s, |
220 | const char *what, | |
221 | const char *how) | |
bb914b14 | 222 | { |
d249b098 | 223 | const char *c = color(WT_STATUS_HEADER, s); |
50bd8b7e | 224 | status_printf_ln(s, c, "%s:", what); |
edf563fb JK |
225 | if (!advice_status_hints) |
226 | return; | |
355ec7a1 | 227 | status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how); |
b926c0d1 | 228 | status_printf_ln(s, c, ""); |
c91f0d92 JK |
229 | } |
230 | ||
f26a0012 | 231 | static void wt_status_print_trailer(struct wt_status *s) |
c91f0d92 | 232 | { |
b926c0d1 | 233 | status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); |
c91f0d92 JK |
234 | } |
235 | ||
a734d0b1 | 236 | #define quote_path quote_path_relative |
3a946802 | 237 | |
4d4d5726 JH |
238 | static void wt_status_print_unmerged_data(struct wt_status *s, |
239 | struct string_list_item *it) | |
240 | { | |
d249b098 | 241 | const char *c = color(WT_STATUS_UNMERGED, s); |
4d4d5726 JH |
242 | struct wt_status_change_data *d = it->util; |
243 | struct strbuf onebuf = STRBUF_INIT; | |
355ec7a1 | 244 | const char *one, *how = _("bug"); |
4d4d5726 JH |
245 | |
246 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
b926c0d1 | 247 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
4d4d5726 | 248 | switch (d->stagemask) { |
355ec7a1 ÆAB |
249 | case 1: how = _("both deleted:"); break; |
250 | case 2: how = _("added by us:"); break; | |
251 | case 3: how = _("deleted by them:"); break; | |
252 | case 4: how = _("added by them:"); break; | |
253 | case 5: how = _("deleted by us:"); break; | |
254 | case 6: how = _("both added:"); break; | |
255 | case 7: how = _("both modified:"); break; | |
4d4d5726 | 256 | } |
b926c0d1 | 257 | status_printf_more(s, c, "%-20s%s\n", how, one); |
4d4d5726 JH |
258 | strbuf_release(&onebuf); |
259 | } | |
260 | ||
50b7e70f JH |
261 | static void wt_status_print_change_data(struct wt_status *s, |
262 | int change_type, | |
263 | struct string_list_item *it) | |
c91f0d92 | 264 | { |
50b7e70f | 265 | struct wt_status_change_data *d = it->util; |
d249b098 | 266 | const char *c = color(change_type, s); |
50b7e70f JH |
267 | int status = status; |
268 | char *one_name; | |
269 | char *two_name; | |
3a946802 | 270 | const char *one, *two; |
f285a2d7 | 271 | struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT; |
9297f77e | 272 | struct strbuf extra = STRBUF_INIT; |
3a946802 | 273 | |
50b7e70f JH |
274 | one_name = two_name = it->string; |
275 | switch (change_type) { | |
276 | case WT_STATUS_UPDATED: | |
277 | status = d->index_status; | |
278 | if (d->head_path) | |
279 | one_name = d->head_path; | |
280 | break; | |
281 | case WT_STATUS_CHANGED: | |
9297f77e JL |
282 | if (d->new_submodule_commits || d->dirty_submodule) { |
283 | strbuf_addstr(&extra, " ("); | |
284 | if (d->new_submodule_commits) | |
355ec7a1 | 285 | strbuf_addf(&extra, _("new commits, ")); |
9297f77e | 286 | if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
355ec7a1 | 287 | strbuf_addf(&extra, _("modified content, ")); |
9297f77e | 288 | if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
355ec7a1 | 289 | strbuf_addf(&extra, _("untracked content, ")); |
9297f77e JL |
290 | strbuf_setlen(&extra, extra.len - 2); |
291 | strbuf_addch(&extra, ')'); | |
292 | } | |
50b7e70f JH |
293 | status = d->worktree_status; |
294 | break; | |
295 | } | |
296 | ||
297 | one = quote_path(one_name, -1, &onebuf, s->prefix); | |
298 | two = quote_path(two_name, -1, &twobuf, s->prefix); | |
3a946802 | 299 | |
b926c0d1 | 300 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
50b7e70f | 301 | switch (status) { |
c91f0d92 | 302 | case DIFF_STATUS_ADDED: |
355ec7a1 | 303 | status_printf_more(s, c, _("new file: %s"), one); |
3a946802 | 304 | break; |
c91f0d92 | 305 | case DIFF_STATUS_COPIED: |
355ec7a1 | 306 | status_printf_more(s, c, _("copied: %s -> %s"), one, two); |
c91f0d92 JK |
307 | break; |
308 | case DIFF_STATUS_DELETED: | |
355ec7a1 | 309 | status_printf_more(s, c, _("deleted: %s"), one); |
3a946802 | 310 | break; |
c91f0d92 | 311 | case DIFF_STATUS_MODIFIED: |
355ec7a1 | 312 | status_printf_more(s, c, _("modified: %s"), one); |
3a946802 | 313 | break; |
c91f0d92 | 314 | case DIFF_STATUS_RENAMED: |
d2b044be | 315 | status_printf_more(s, c, _("renamed: %s -> %s"), one, two); |
c91f0d92 JK |
316 | break; |
317 | case DIFF_STATUS_TYPE_CHANGED: | |
355ec7a1 | 318 | status_printf_more(s, c, _("typechange: %s"), one); |
3a946802 | 319 | break; |
c91f0d92 | 320 | case DIFF_STATUS_UNKNOWN: |
355ec7a1 | 321 | status_printf_more(s, c, _("unknown: %s"), one); |
3a946802 | 322 | break; |
c91f0d92 | 323 | case DIFF_STATUS_UNMERGED: |
355ec7a1 | 324 | status_printf_more(s, c, _("unmerged: %s"), one); |
3a946802 | 325 | break; |
c91f0d92 | 326 | default: |
355ec7a1 | 327 | die(_("bug: unhandled diff status %c"), status); |
c91f0d92 | 328 | } |
9297f77e | 329 | if (extra.len) { |
b926c0d1 | 330 | status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf); |
9297f77e JL |
331 | strbuf_release(&extra); |
332 | } | |
b926c0d1 | 333 | status_printf_more(s, GIT_COLOR_NORMAL, "\n"); |
367c9886 JS |
334 | strbuf_release(&onebuf); |
335 | strbuf_release(&twobuf); | |
c91f0d92 JK |
336 | } |
337 | ||
50b7e70f JH |
338 | static void wt_status_collect_changed_cb(struct diff_queue_struct *q, |
339 | struct diff_options *options, | |
340 | void *data) | |
c91f0d92 JK |
341 | { |
342 | struct wt_status *s = data; | |
c91f0d92 | 343 | int i; |
50b7e70f JH |
344 | |
345 | if (!q->nr) | |
346 | return; | |
347 | s->workdir_dirty = 1; | |
c91f0d92 | 348 | for (i = 0; i < q->nr; i++) { |
50b7e70f JH |
349 | struct diff_filepair *p; |
350 | struct string_list_item *it; | |
351 | struct wt_status_change_data *d; | |
352 | ||
353 | p = q->queue[i]; | |
78a395d3 | 354 | it = string_list_insert(&s->change, p->one->path); |
50b7e70f JH |
355 | d = it->util; |
356 | if (!d) { | |
357 | d = xcalloc(1, sizeof(*d)); | |
358 | it->util = d; | |
c91f0d92 | 359 | } |
50b7e70f JH |
360 | if (!d->worktree_status) |
361 | d->worktree_status = p->status; | |
9297f77e JL |
362 | d->dirty_submodule = p->two->dirty_submodule; |
363 | if (S_ISGITLINK(p->two->mode)) | |
364 | d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1); | |
c91f0d92 | 365 | } |
c91f0d92 JK |
366 | } |
367 | ||
4d4d5726 JH |
368 | static int unmerged_mask(const char *path) |
369 | { | |
370 | int pos, mask; | |
371 | struct cache_entry *ce; | |
372 | ||
373 | pos = cache_name_pos(path, strlen(path)); | |
374 | if (0 <= pos) | |
375 | return 0; | |
376 | ||
377 | mask = 0; | |
378 | pos = -pos-1; | |
379 | while (pos < active_nr) { | |
380 | ce = active_cache[pos++]; | |
381 | if (strcmp(ce->name, path) || !ce_stage(ce)) | |
382 | break; | |
383 | mask |= (1 << (ce_stage(ce) - 1)); | |
384 | } | |
385 | return mask; | |
386 | } | |
387 | ||
50b7e70f JH |
388 | static void wt_status_collect_updated_cb(struct diff_queue_struct *q, |
389 | struct diff_options *options, | |
390 | void *data) | |
c91f0d92 | 391 | { |
6e458bf6 | 392 | struct wt_status *s = data; |
c91f0d92 | 393 | int i; |
50b7e70f JH |
394 | |
395 | for (i = 0; i < q->nr; i++) { | |
396 | struct diff_filepair *p; | |
397 | struct string_list_item *it; | |
398 | struct wt_status_change_data *d; | |
399 | ||
400 | p = q->queue[i]; | |
78a395d3 | 401 | it = string_list_insert(&s->change, p->two->path); |
50b7e70f JH |
402 | d = it->util; |
403 | if (!d) { | |
404 | d = xcalloc(1, sizeof(*d)); | |
405 | it->util = d; | |
406 | } | |
407 | if (!d->index_status) | |
408 | d->index_status = p->status; | |
409 | switch (p->status) { | |
410 | case DIFF_STATUS_COPIED: | |
411 | case DIFF_STATUS_RENAMED: | |
412 | d->head_path = xstrdup(p->one->path); | |
413 | break; | |
4d4d5726 JH |
414 | case DIFF_STATUS_UNMERGED: |
415 | d->stagemask = unmerged_mask(p->two->path); | |
416 | break; | |
50b7e70f | 417 | } |
6e458bf6 | 418 | } |
c91f0d92 JK |
419 | } |
420 | ||
50b7e70f | 421 | static void wt_status_collect_changes_worktree(struct wt_status *s) |
c91f0d92 JK |
422 | { |
423 | struct rev_info rev; | |
50b7e70f JH |
424 | |
425 | init_revisions(&rev, NULL); | |
426 | setup_revisions(0, NULL, &rev, NULL); | |
427 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
9297f77e | 428 | DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES); |
3bfc4504 JL |
429 | if (!s->show_untracked_files) |
430 | DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
aee9c7d6 JL |
431 | if (s->ignore_submodule_arg) { |
432 | DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG); | |
46a958b3 | 433 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); |
c4c42f2c | 434 | } |
50b7e70f JH |
435 | rev.diffopt.format_callback = wt_status_collect_changed_cb; |
436 | rev.diffopt.format_callback_data = s; | |
afe069d1 | 437 | init_pathspec(&rev.prune_data, s->pathspec); |
50b7e70f JH |
438 | run_diff_files(&rev, 0); |
439 | } | |
440 | ||
441 | static void wt_status_collect_changes_index(struct wt_status *s) | |
442 | { | |
443 | struct rev_info rev; | |
32962c9b | 444 | struct setup_revision_opt opt; |
50b7e70f | 445 | |
c91f0d92 | 446 | init_revisions(&rev, NULL); |
32962c9b JH |
447 | memset(&opt, 0, sizeof(opt)); |
448 | opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference; | |
449 | setup_revisions(0, NULL, &rev, &opt); | |
450 | ||
aee9c7d6 JL |
451 | if (s->ignore_submodule_arg) { |
452 | DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG); | |
46a958b3 | 453 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); |
aee9c7d6 | 454 | } |
46a958b3 | 455 | |
c91f0d92 | 456 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
50b7e70f | 457 | rev.diffopt.format_callback = wt_status_collect_updated_cb; |
c91f0d92 JK |
458 | rev.diffopt.format_callback_data = s; |
459 | rev.diffopt.detect_rename = 1; | |
50705915 | 460 | rev.diffopt.rename_limit = 200; |
f714fb84 | 461 | rev.diffopt.break_opt = 0; |
afe069d1 | 462 | init_pathspec(&rev.prune_data, s->pathspec); |
c91f0d92 JK |
463 | run_diff_index(&rev, 1); |
464 | } | |
465 | ||
50b7e70f JH |
466 | static void wt_status_collect_changes_initial(struct wt_status *s) |
467 | { | |
eb9cb55b | 468 | struct pathspec pathspec; |
50b7e70f JH |
469 | int i; |
470 | ||
eb9cb55b | 471 | init_pathspec(&pathspec, s->pathspec); |
50b7e70f JH |
472 | for (i = 0; i < active_nr; i++) { |
473 | struct string_list_item *it; | |
474 | struct wt_status_change_data *d; | |
475 | struct cache_entry *ce = active_cache[i]; | |
476 | ||
eb9cb55b | 477 | if (!ce_path_match(ce, &pathspec)) |
76e2f7ce | 478 | continue; |
78a395d3 | 479 | it = string_list_insert(&s->change, ce->name); |
50b7e70f JH |
480 | d = it->util; |
481 | if (!d) { | |
482 | d = xcalloc(1, sizeof(*d)); | |
483 | it->util = d; | |
484 | } | |
4d4d5726 | 485 | if (ce_stage(ce)) { |
50b7e70f | 486 | d->index_status = DIFF_STATUS_UNMERGED; |
4d4d5726 JH |
487 | d->stagemask |= (1 << (ce_stage(ce) - 1)); |
488 | } | |
50b7e70f JH |
489 | else |
490 | d->index_status = DIFF_STATUS_ADDED; | |
491 | } | |
eb9cb55b | 492 | free_pathspec(&pathspec); |
50b7e70f JH |
493 | } |
494 | ||
76378683 JH |
495 | static void wt_status_collect_untracked(struct wt_status *s) |
496 | { | |
497 | int i; | |
498 | struct dir_struct dir; | |
499 | ||
500 | if (!s->show_untracked_files) | |
501 | return; | |
502 | memset(&dir, 0, sizeof(dir)); | |
503 | if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES) | |
504 | dir.flags |= | |
505 | DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; | |
506 | setup_standard_excludes(&dir); | |
507 | ||
688cd6d2 | 508 | fill_directory(&dir, s->pathspec); |
eeefa7c9 | 509 | for (i = 0; i < dir.nr; i++) { |
76378683 | 510 | struct dir_entry *ent = dir.entries[i]; |
b822423e BC |
511 | if (cache_name_is_other(ent->name, ent->len) && |
512 | match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL)) | |
513 | string_list_insert(&s->untracked, ent->name); | |
f5b26b1d | 514 | free(ent); |
76378683 | 515 | } |
f5b26b1d | 516 | |
6cb3f6b2 JH |
517 | if (s->show_ignored_files) { |
518 | dir.nr = 0; | |
519 | dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES; | |
520 | fill_directory(&dir, s->pathspec); | |
521 | for (i = 0; i < dir.nr; i++) { | |
522 | struct dir_entry *ent = dir.entries[i]; | |
b822423e BC |
523 | if (cache_name_is_other(ent->name, ent->len) && |
524 | match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL)) | |
525 | string_list_insert(&s->ignored, ent->name); | |
6cb3f6b2 JH |
526 | free(ent); |
527 | } | |
528 | } | |
529 | ||
f5b26b1d | 530 | free(dir.entries); |
76378683 JH |
531 | } |
532 | ||
533 | void wt_status_collect(struct wt_status *s) | |
50b7e70f JH |
534 | { |
535 | wt_status_collect_changes_worktree(s); | |
536 | ||
537 | if (s->is_initial) | |
538 | wt_status_collect_changes_initial(s); | |
539 | else | |
540 | wt_status_collect_changes_index(s); | |
76378683 | 541 | wt_status_collect_untracked(s); |
50b7e70f JH |
542 | } |
543 | ||
4d4d5726 JH |
544 | static void wt_status_print_unmerged(struct wt_status *s) |
545 | { | |
546 | int shown_header = 0; | |
547 | int i; | |
548 | ||
549 | for (i = 0; i < s->change.nr; i++) { | |
550 | struct wt_status_change_data *d; | |
551 | struct string_list_item *it; | |
552 | it = &(s->change.items[i]); | |
553 | d = it->util; | |
554 | if (!d->stagemask) | |
555 | continue; | |
556 | if (!shown_header) { | |
557 | wt_status_print_unmerged_header(s); | |
558 | shown_header = 1; | |
559 | } | |
560 | wt_status_print_unmerged_data(s, it); | |
561 | } | |
562 | if (shown_header) | |
563 | wt_status_print_trailer(s); | |
564 | ||
565 | } | |
566 | ||
50b7e70f JH |
567 | static void wt_status_print_updated(struct wt_status *s) |
568 | { | |
569 | int shown_header = 0; | |
570 | int i; | |
571 | ||
572 | for (i = 0; i < s->change.nr; i++) { | |
573 | struct wt_status_change_data *d; | |
574 | struct string_list_item *it; | |
575 | it = &(s->change.items[i]); | |
576 | d = it->util; | |
577 | if (!d->index_status || | |
578 | d->index_status == DIFF_STATUS_UNMERGED) | |
579 | continue; | |
580 | if (!shown_header) { | |
581 | wt_status_print_cached_header(s); | |
582 | s->commitable = 1; | |
583 | shown_header = 1; | |
584 | } | |
585 | wt_status_print_change_data(s, WT_STATUS_UPDATED, it); | |
586 | } | |
587 | if (shown_header) | |
588 | wt_status_print_trailer(s); | |
589 | } | |
590 | ||
591 | /* | |
592 | * -1 : has delete | |
593 | * 0 : no change | |
594 | * 1 : some change but no delete | |
595 | */ | |
9297f77e JL |
596 | static int wt_status_check_worktree_changes(struct wt_status *s, |
597 | int *dirty_submodules) | |
50b7e70f JH |
598 | { |
599 | int i; | |
600 | int changes = 0; | |
601 | ||
9297f77e JL |
602 | *dirty_submodules = 0; |
603 | ||
50b7e70f JH |
604 | for (i = 0; i < s->change.nr; i++) { |
605 | struct wt_status_change_data *d; | |
606 | d = s->change.items[i].util; | |
4d4d5726 JH |
607 | if (!d->worktree_status || |
608 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f | 609 | continue; |
9297f77e JL |
610 | if (!changes) |
611 | changes = 1; | |
612 | if (d->dirty_submodule) | |
613 | *dirty_submodules = 1; | |
50b7e70f | 614 | if (d->worktree_status == DIFF_STATUS_DELETED) |
9297f77e | 615 | changes = -1; |
50b7e70f JH |
616 | } |
617 | return changes; | |
618 | } | |
619 | ||
c91f0d92 JK |
620 | static void wt_status_print_changed(struct wt_status *s) |
621 | { | |
9297f77e JL |
622 | int i, dirty_submodules; |
623 | int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules); | |
50b7e70f JH |
624 | |
625 | if (!worktree_changes) | |
626 | return; | |
627 | ||
9297f77e | 628 | wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules); |
50b7e70f JH |
629 | |
630 | for (i = 0; i < s->change.nr; i++) { | |
631 | struct wt_status_change_data *d; | |
632 | struct string_list_item *it; | |
633 | it = &(s->change.items[i]); | |
634 | d = it->util; | |
4d4d5726 JH |
635 | if (!d->worktree_status || |
636 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f JH |
637 | continue; |
638 | wt_status_print_change_data(s, WT_STATUS_CHANGED, it); | |
639 | } | |
640 | wt_status_print_trailer(s); | |
c91f0d92 JK |
641 | } |
642 | ||
f17a5d34 | 643 | static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted) |
ac8d5afc PY |
644 | { |
645 | struct child_process sm_summary; | |
646 | char summary_limit[64]; | |
647 | char index[PATH_MAX]; | |
66dbfd55 GV |
648 | const char *env[] = { NULL, NULL }; |
649 | const char *argv[8]; | |
650 | ||
651 | env[0] = index; | |
652 | argv[0] = "submodule"; | |
653 | argv[1] = "summary"; | |
654 | argv[2] = uncommitted ? "--files" : "--cached"; | |
655 | argv[3] = "--for-status"; | |
656 | argv[4] = "--summary-limit"; | |
657 | argv[5] = summary_limit; | |
658 | argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"); | |
659 | argv[7] = NULL; | |
ac8d5afc | 660 | |
d249b098 | 661 | sprintf(summary_limit, "%d", s->submodule_summary); |
ac8d5afc PY |
662 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file); |
663 | ||
664 | memset(&sm_summary, 0, sizeof(sm_summary)); | |
665 | sm_summary.argv = argv; | |
666 | sm_summary.env = env; | |
667 | sm_summary.git_cmd = 1; | |
668 | sm_summary.no_stdin = 1; | |
669 | fflush(s->fp); | |
670 | sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */ | |
671 | run_command(&sm_summary); | |
672 | } | |
673 | ||
1b908b6f JH |
674 | static void wt_status_print_other(struct wt_status *s, |
675 | struct string_list *l, | |
676 | const char *what, | |
677 | const char *how) | |
c91f0d92 | 678 | { |
c91f0d92 | 679 | int i; |
f285a2d7 | 680 | struct strbuf buf = STRBUF_INIT; |
323d0530 NTND |
681 | static struct string_list output = STRING_LIST_INIT_DUP; |
682 | struct column_options copts; | |
c91f0d92 | 683 | |
1282988b | 684 | if (!l->nr) |
76378683 | 685 | return; |
c91f0d92 | 686 | |
1b908b6f JH |
687 | wt_status_print_other_header(s, what, how); |
688 | ||
689 | for (i = 0; i < l->nr; i++) { | |
76378683 | 690 | struct string_list_item *it; |
323d0530 | 691 | const char *path; |
1b908b6f | 692 | it = &(l->items[i]); |
323d0530 NTND |
693 | path = quote_path(it->string, strlen(it->string), |
694 | &buf, s->prefix); | |
695 | if (column_active(s->colopts)) { | |
696 | string_list_append(&output, path); | |
697 | continue; | |
698 | } | |
b926c0d1 JN |
699 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); |
700 | status_printf_more(s, color(WT_STATUS_UNTRACKED, s), | |
323d0530 | 701 | "%s\n", path); |
c91f0d92 | 702 | } |
323d0530 NTND |
703 | |
704 | strbuf_release(&buf); | |
705 | if (!column_active(s->colopts)) | |
706 | return; | |
707 | ||
708 | strbuf_addf(&buf, "%s#\t%s", | |
709 | color(WT_STATUS_HEADER, s), | |
710 | color(WT_STATUS_UNTRACKED, s)); | |
711 | memset(&copts, 0, sizeof(copts)); | |
712 | copts.padding = 1; | |
713 | copts.indent = buf.buf; | |
714 | if (want_color(s->use_color)) | |
715 | copts.nl = GIT_COLOR_RESET "\n"; | |
716 | print_columns(&output, s->colopts, &copts); | |
717 | string_list_clear(&output, 0); | |
367c9886 | 718 | strbuf_release(&buf); |
c91f0d92 JK |
719 | } |
720 | ||
721 | static void wt_status_print_verbose(struct wt_status *s) | |
722 | { | |
723 | struct rev_info rev; | |
32962c9b | 724 | struct setup_revision_opt opt; |
99a12694 | 725 | |
c91f0d92 | 726 | init_revisions(&rev, NULL); |
5ec11af6 | 727 | DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV); |
32962c9b JH |
728 | |
729 | memset(&opt, 0, sizeof(opt)); | |
730 | opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference; | |
731 | setup_revisions(0, NULL, &rev, &opt); | |
732 | ||
c91f0d92 JK |
733 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
734 | rev.diffopt.detect_rename = 1; | |
4ba0cb27 KH |
735 | rev.diffopt.file = s->fp; |
736 | rev.diffopt.close_file = 0; | |
4f672ad6 JK |
737 | /* |
738 | * If we're not going to stdout, then we definitely don't | |
739 | * want color, since we are going to the commit message | |
740 | * file (and even the "auto" setting won't work, since it | |
741 | * will have checked isatty on stdout). | |
742 | */ | |
743 | if (s->fp != stdout) | |
f1c96261 | 744 | rev.diffopt.use_color = 0; |
c91f0d92 JK |
745 | run_diff_index(&rev, 1); |
746 | } | |
747 | ||
b6975ab5 JH |
748 | static void wt_status_print_tracking(struct wt_status *s) |
749 | { | |
750 | struct strbuf sb = STRBUF_INIT; | |
751 | const char *cp, *ep; | |
752 | struct branch *branch; | |
753 | ||
754 | assert(s->branch && !s->is_initial); | |
755 | if (prefixcmp(s->branch, "refs/heads/")) | |
756 | return; | |
757 | branch = branch_get(s->branch + 11); | |
758 | if (!format_tracking_info(branch, &sb)) | |
759 | return; | |
760 | ||
761 | for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) | |
d249b098 | 762 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), |
b6975ab5 | 763 | "# %.*s", (int)(ep - cp), cp); |
d249b098 | 764 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); |
b6975ab5 JH |
765 | } |
766 | ||
83c750ac LK |
767 | static int has_unmerged(struct wt_status *s) |
768 | { | |
769 | int i; | |
770 | ||
771 | for (i = 0; i < s->change.nr; i++) { | |
772 | struct wt_status_change_data *d; | |
773 | d = s->change.items[i].util; | |
774 | if (d->stagemask) | |
775 | return 1; | |
776 | } | |
777 | return 0; | |
778 | } | |
779 | ||
780 | static void show_merge_in_progress(struct wt_status *s, | |
781 | struct wt_status_state *state, | |
782 | const char *color) | |
783 | { | |
784 | if (has_unmerged(s)) { | |
785 | status_printf_ln(s, color, _("You have unmerged paths.")); | |
786 | if (advice_status_hints) | |
787 | status_printf_ln(s, color, | |
788 | _(" (fix conflicts and run \"git commit\")")); | |
789 | } else { | |
790 | status_printf_ln(s, color, | |
791 | _("All conflicts fixed but you are still merging.")); | |
792 | if (advice_status_hints) | |
793 | status_printf_ln(s, color, | |
794 | _(" (use \"git commit\" to conclude merge)")); | |
795 | } | |
796 | wt_status_print_trailer(s); | |
797 | } | |
798 | ||
799 | static void show_am_in_progress(struct wt_status *s, | |
800 | struct wt_status_state *state, | |
801 | const char *color) | |
802 | { | |
803 | status_printf_ln(s, color, | |
804 | _("You are in the middle of an am session.")); | |
805 | if (state->am_empty_patch) | |
806 | status_printf_ln(s, color, | |
807 | _("The current patch is empty.")); | |
808 | if (advice_status_hints) { | |
809 | if (!state->am_empty_patch) | |
810 | status_printf_ln(s, color, | |
811 | _(" (fix conflicts and then run \"git am --resolved\")")); | |
812 | status_printf_ln(s, color, | |
813 | _(" (use \"git am --skip\" to skip this patch)")); | |
814 | status_printf_ln(s, color, | |
815 | _(" (use \"git am --abort\" to restore the original branch)")); | |
816 | } | |
817 | wt_status_print_trailer(s); | |
818 | } | |
819 | ||
2d1cceba LK |
820 | static char *read_line_from_git_path(const char *filename) |
821 | { | |
822 | struct strbuf buf = STRBUF_INIT; | |
823 | FILE *fp = fopen(git_path("%s", filename), "r"); | |
824 | if (!fp) { | |
825 | strbuf_release(&buf); | |
826 | return NULL; | |
827 | } | |
828 | strbuf_getline(&buf, fp, '\n'); | |
829 | if (!fclose(fp)) { | |
830 | return strbuf_detach(&buf, NULL); | |
831 | } else { | |
832 | strbuf_release(&buf); | |
833 | return NULL; | |
834 | } | |
835 | } | |
836 | ||
837 | static int split_commit_in_progress(struct wt_status *s) | |
838 | { | |
839 | int split_in_progress = 0; | |
840 | char *head = read_line_from_git_path("HEAD"); | |
841 | char *orig_head = read_line_from_git_path("ORIG_HEAD"); | |
842 | char *rebase_amend = read_line_from_git_path("rebase-merge/amend"); | |
843 | char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head"); | |
844 | ||
845 | if (!head || !orig_head || !rebase_amend || !rebase_orig_head || | |
846 | !s->branch || strcmp(s->branch, "HEAD")) | |
847 | return split_in_progress; | |
848 | ||
849 | if (!strcmp(rebase_amend, rebase_orig_head)) { | |
850 | if (strcmp(head, rebase_amend)) | |
851 | split_in_progress = 1; | |
852 | } else if (strcmp(orig_head, rebase_orig_head)) { | |
853 | split_in_progress = 1; | |
854 | } | |
855 | ||
856 | if (!s->amend && !s->nowarn && !s->workdir_dirty) | |
857 | split_in_progress = 0; | |
858 | ||
859 | free(head); | |
860 | free(orig_head); | |
861 | free(rebase_amend); | |
862 | free(rebase_orig_head); | |
863 | return split_in_progress; | |
864 | } | |
865 | ||
83c750ac LK |
866 | static void show_rebase_in_progress(struct wt_status *s, |
867 | struct wt_status_state *state, | |
868 | const char *color) | |
869 | { | |
870 | struct stat st; | |
871 | ||
872 | if (has_unmerged(s)) { | |
873 | status_printf_ln(s, color, _("You are currently rebasing.")); | |
874 | if (advice_status_hints) { | |
875 | status_printf_ln(s, color, | |
876 | _(" (fix conflicts and then run \"git rebase --continue\")")); | |
877 | status_printf_ln(s, color, | |
878 | _(" (use \"git rebase --skip\" to skip this patch)")); | |
879 | status_printf_ln(s, color, | |
880 | _(" (use \"git rebase --abort\" to check out the original branch)")); | |
881 | } | |
882 | } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) { | |
883 | status_printf_ln(s, color, _("You are currently rebasing.")); | |
884 | if (advice_status_hints) | |
885 | status_printf_ln(s, color, | |
886 | _(" (all conflicts fixed: run \"git rebase --continue\")")); | |
2d1cceba LK |
887 | } else if (split_commit_in_progress(s)) { |
888 | status_printf_ln(s, color, _("You are currently splitting a commit during a rebase.")); | |
889 | if (advice_status_hints) | |
890 | status_printf_ln(s, color, | |
891 | _(" (Once your working directory is clean, run \"git rebase --continue\")")); | |
83c750ac LK |
892 | } else { |
893 | status_printf_ln(s, color, _("You are currently editing a commit during a rebase.")); | |
894 | if (advice_status_hints && !s->amend) { | |
895 | status_printf_ln(s, color, | |
896 | _(" (use \"git commit --amend\" to amend the current commit)")); | |
897 | status_printf_ln(s, color, | |
898 | _(" (use \"git rebase --continue\" once you are satisfied with your changes)")); | |
899 | } | |
900 | } | |
901 | wt_status_print_trailer(s); | |
902 | } | |
903 | ||
904 | static void show_cherry_pick_in_progress(struct wt_status *s, | |
905 | struct wt_status_state *state, | |
906 | const char *color) | |
907 | { | |
908 | status_printf_ln(s, color, _("You are currently cherry-picking.")); | |
909 | if (advice_status_hints) { | |
910 | if (has_unmerged(s)) | |
911 | status_printf_ln(s, color, | |
912 | _(" (fix conflicts and run \"git commit\")")); | |
913 | else | |
914 | status_printf_ln(s, color, | |
915 | _(" (all conflicts fixed: run \"git commit\")")); | |
916 | } | |
917 | wt_status_print_trailer(s); | |
918 | } | |
919 | ||
920 | static void show_bisect_in_progress(struct wt_status *s, | |
921 | struct wt_status_state *state, | |
922 | const char *color) | |
923 | { | |
924 | status_printf_ln(s, color, _("You are currently bisecting.")); | |
925 | if (advice_status_hints) | |
926 | status_printf_ln(s, color, | |
927 | _(" (use \"git bisect reset\" to get back to the original branch)")); | |
928 | wt_status_print_trailer(s); | |
929 | } | |
930 | ||
931 | static void wt_status_print_state(struct wt_status *s) | |
932 | { | |
3cd9d1b2 | 933 | const char *state_color = color(WT_STATUS_HEADER, s); |
83c750ac LK |
934 | struct wt_status_state state; |
935 | struct stat st; | |
936 | ||
937 | memset(&state, 0, sizeof(state)); | |
938 | ||
939 | if (!stat(git_path("MERGE_HEAD"), &st)) { | |
940 | state.merge_in_progress = 1; | |
941 | } else if (!stat(git_path("rebase-apply"), &st)) { | |
942 | if (!stat(git_path("rebase-apply/applying"), &st)) { | |
943 | state.am_in_progress = 1; | |
944 | if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size) | |
945 | state.am_empty_patch = 1; | |
946 | } else { | |
947 | state.rebase_in_progress = 1; | |
948 | } | |
949 | } else if (!stat(git_path("rebase-merge"), &st)) { | |
950 | if (!stat(git_path("rebase-merge/interactive"), &st)) | |
951 | state.rebase_interactive_in_progress = 1; | |
952 | else | |
953 | state.rebase_in_progress = 1; | |
954 | } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) { | |
955 | state.cherry_pick_in_progress = 1; | |
956 | } | |
957 | if (!stat(git_path("BISECT_LOG"), &st)) | |
958 | state.bisect_in_progress = 1; | |
959 | ||
960 | if (state.merge_in_progress) | |
961 | show_merge_in_progress(s, &state, state_color); | |
962 | else if (state.am_in_progress) | |
963 | show_am_in_progress(s, &state, state_color); | |
964 | else if (state.rebase_in_progress || state.rebase_interactive_in_progress) | |
965 | show_rebase_in_progress(s, &state, state_color); | |
966 | else if (state.cherry_pick_in_progress) | |
967 | show_cherry_pick_in_progress(s, &state, state_color); | |
968 | if (state.bisect_in_progress) | |
969 | show_bisect_in_progress(s, &state, state_color); | |
970 | } | |
971 | ||
c91f0d92 JK |
972 | void wt_status_print(struct wt_status *s) |
973 | { | |
1d282327 AA |
974 | const char *branch_color = color(WT_STATUS_ONBRANCH, s); |
975 | const char *branch_status_color = color(WT_STATUS_HEADER, s); | |
98bf8a47 | 976 | |
bda324cf | 977 | if (s->branch) { |
355ec7a1 | 978 | const char *on_what = _("On branch "); |
bda324cf | 979 | const char *branch_name = s->branch; |
cc44c765 | 980 | if (!prefixcmp(branch_name, "refs/heads/")) |
bda324cf JH |
981 | branch_name += 11; |
982 | else if (!strcmp(branch_name, "HEAD")) { | |
983 | branch_name = ""; | |
1d282327 | 984 | branch_status_color = color(WT_STATUS_NOBRANCH, s); |
355ec7a1 | 985 | on_what = _("Not currently on any branch."); |
bda324cf | 986 | } |
b926c0d1 JN |
987 | status_printf(s, color(WT_STATUS_HEADER, s), ""); |
988 | status_printf_more(s, branch_status_color, "%s", on_what); | |
989 | status_printf_more(s, branch_color, "%s\n", branch_name); | |
b6975ab5 JH |
990 | if (!s->is_initial) |
991 | wt_status_print_tracking(s); | |
bda324cf | 992 | } |
c91f0d92 | 993 | |
83c750ac | 994 | wt_status_print_state(s); |
c91f0d92 | 995 | if (s->is_initial) { |
b926c0d1 | 996 | status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); |
b3b298af | 997 | status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit")); |
b926c0d1 | 998 | status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); |
c91f0d92 JK |
999 | } |
1000 | ||
c1e255b7 | 1001 | wt_status_print_updated(s); |
228e7b5d | 1002 | wt_status_print_unmerged(s); |
c91f0d92 | 1003 | wt_status_print_changed(s); |
46a958b3 JL |
1004 | if (s->submodule_summary && |
1005 | (!s->ignore_submodule_arg || | |
1006 | strcmp(s->ignore_submodule_arg, "all"))) { | |
f17a5d34 JL |
1007 | wt_status_print_submodule_summary(s, 0); /* staged */ |
1008 | wt_status_print_submodule_summary(s, 1); /* unstaged */ | |
1009 | } | |
2381e39e | 1010 | if (s->show_untracked_files) { |
50bd8b7e | 1011 | wt_status_print_other(s, &s->untracked, _("Untracked files"), "add"); |
2381e39e | 1012 | if (s->show_ignored_files) |
50bd8b7e | 1013 | wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f"); |
2381e39e | 1014 | } else if (s->commitable) |
355ec7a1 | 1015 | status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"), |
980bde38 | 1016 | advice_status_hints |
355ec7a1 | 1017 | ? _(" (use -u option to show untracked files)") : ""); |
c91f0d92 | 1018 | |
1324fb6f | 1019 | if (s->verbose) |
c91f0d92 | 1020 | wt_status_print_verbose(s); |
6e458bf6 JR |
1021 | if (!s->commitable) { |
1022 | if (s->amend) | |
355ec7a1 | 1023 | status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes")); |
37d07f8f JH |
1024 | else if (s->nowarn) |
1025 | ; /* nothing */ | |
50bd8b7e NTND |
1026 | else if (s->workdir_dirty) { |
1027 | if (advice_status_hints) | |
1028 | printf(_("no changes added to commit " | |
1029 | "(use \"git add\" and/or \"git commit -a\")\n")); | |
1030 | else | |
1031 | printf(_("no changes added to commit\n")); | |
1032 | } else if (s->untracked.nr) { | |
1033 | if (advice_status_hints) | |
1034 | printf(_("nothing added to commit but untracked files " | |
1035 | "present (use \"git add\" to track)\n")); | |
1036 | else | |
1037 | printf(_("nothing added to commit but untracked files present\n")); | |
1038 | } else if (s->is_initial) { | |
1039 | if (advice_status_hints) | |
1040 | printf(_("nothing to commit (create/copy files " | |
1041 | "and use \"git add\" to track)\n")); | |
1042 | else | |
1043 | printf(_("nothing to commit\n")); | |
1044 | } else if (!s->show_untracked_files) { | |
1045 | if (advice_status_hints) | |
1046 | printf(_("nothing to commit (use -u to show untracked files)\n")); | |
1047 | else | |
1048 | printf(_("nothing to commit\n")); | |
1049 | } else | |
1050 | printf(_("nothing to commit, working directory clean\n")); | |
6e458bf6 | 1051 | } |
c91f0d92 | 1052 | } |
84dbe7b8 | 1053 | |
3207a3a2 | 1054 | static void wt_shortstatus_unmerged(struct string_list_item *it, |
84dbe7b8 MG |
1055 | struct wt_status *s) |
1056 | { | |
1057 | struct wt_status_change_data *d = it->util; | |
1058 | const char *how = "??"; | |
1059 | ||
1060 | switch (d->stagemask) { | |
1061 | case 1: how = "DD"; break; /* both deleted */ | |
1062 | case 2: how = "AU"; break; /* added by us */ | |
1063 | case 3: how = "UD"; break; /* deleted by them */ | |
1064 | case 4: how = "UA"; break; /* added by them */ | |
1065 | case 5: how = "DU"; break; /* deleted by us */ | |
1066 | case 6: how = "AA"; break; /* both added */ | |
1067 | case 7: how = "UU"; break; /* both modified */ | |
1068 | } | |
3fe2a894 | 1069 | color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how); |
3207a3a2 | 1070 | if (s->null_termination) { |
3fe2a894 | 1071 | fprintf(stdout, " %s%c", it->string, 0); |
84dbe7b8 MG |
1072 | } else { |
1073 | struct strbuf onebuf = STRBUF_INIT; | |
1074 | const char *one; | |
1075 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
3fe2a894 | 1076 | printf(" %s\n", one); |
84dbe7b8 MG |
1077 | strbuf_release(&onebuf); |
1078 | } | |
1079 | } | |
1080 | ||
3207a3a2 | 1081 | static void wt_shortstatus_status(struct string_list_item *it, |
84dbe7b8 MG |
1082 | struct wt_status *s) |
1083 | { | |
1084 | struct wt_status_change_data *d = it->util; | |
1085 | ||
3fe2a894 MG |
1086 | if (d->index_status) |
1087 | color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status); | |
1088 | else | |
1089 | putchar(' '); | |
1090 | if (d->worktree_status) | |
1091 | color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status); | |
1092 | else | |
1093 | putchar(' '); | |
1094 | putchar(' '); | |
3207a3a2 | 1095 | if (s->null_termination) { |
84dbe7b8 MG |
1096 | fprintf(stdout, "%s%c", it->string, 0); |
1097 | if (d->head_path) | |
1098 | fprintf(stdout, "%s%c", d->head_path, 0); | |
1099 | } else { | |
1100 | struct strbuf onebuf = STRBUF_INIT; | |
1101 | const char *one; | |
1102 | if (d->head_path) { | |
1103 | one = quote_path(d->head_path, -1, &onebuf, s->prefix); | |
dbfdc625 KB |
1104 | if (*one != '"' && strchr(one, ' ') != NULL) { |
1105 | putchar('"'); | |
1106 | strbuf_addch(&onebuf, '"'); | |
1107 | one = onebuf.buf; | |
1108 | } | |
84dbe7b8 MG |
1109 | printf("%s -> ", one); |
1110 | strbuf_release(&onebuf); | |
1111 | } | |
1112 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
dbfdc625 KB |
1113 | if (*one != '"' && strchr(one, ' ') != NULL) { |
1114 | putchar('"'); | |
1115 | strbuf_addch(&onebuf, '"'); | |
1116 | one = onebuf.buf; | |
1117 | } | |
84dbe7b8 MG |
1118 | printf("%s\n", one); |
1119 | strbuf_release(&onebuf); | |
1120 | } | |
1121 | } | |
1122 | ||
3207a3a2 | 1123 | static void wt_shortstatus_other(struct string_list_item *it, |
2381e39e | 1124 | struct wt_status *s, const char *sign) |
84dbe7b8 | 1125 | { |
3207a3a2 | 1126 | if (s->null_termination) { |
2381e39e | 1127 | fprintf(stdout, "%s %s%c", sign, it->string, 0); |
84dbe7b8 MG |
1128 | } else { |
1129 | struct strbuf onebuf = STRBUF_INIT; | |
1130 | const char *one; | |
1131 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
c1909e72 | 1132 | color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign); |
3fe2a894 | 1133 | printf(" %s\n", one); |
84dbe7b8 MG |
1134 | strbuf_release(&onebuf); |
1135 | } | |
1136 | } | |
1137 | ||
05a59a08 DKF |
1138 | static void wt_shortstatus_print_tracking(struct wt_status *s) |
1139 | { | |
1140 | struct branch *branch; | |
1141 | const char *header_color = color(WT_STATUS_HEADER, s); | |
1142 | const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s); | |
1143 | const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s); | |
1144 | ||
1145 | const char *base; | |
1146 | const char *branch_name; | |
1147 | int num_ours, num_theirs; | |
1148 | ||
1149 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## "); | |
1150 | ||
1151 | if (!s->branch) | |
1152 | return; | |
1153 | branch_name = s->branch; | |
1154 | ||
1155 | if (!prefixcmp(branch_name, "refs/heads/")) | |
1156 | branch_name += 11; | |
1157 | else if (!strcmp(branch_name, "HEAD")) { | |
98f5e24b | 1158 | branch_name = _("HEAD (no branch)"); |
05a59a08 DKF |
1159 | branch_color_local = color(WT_STATUS_NOBRANCH, s); |
1160 | } | |
1161 | ||
1162 | branch = branch_get(s->branch + 11); | |
1163 | if (s->is_initial) | |
98f5e24b | 1164 | color_fprintf(s->fp, header_color, _("Initial commit on ")); |
05a59a08 | 1165 | if (!stat_tracking_info(branch, &num_ours, &num_theirs)) { |
a5985237 JK |
1166 | color_fprintf(s->fp, branch_color_local, "%s", branch_name); |
1167 | fputc(s->null_termination ? '\0' : '\n', s->fp); | |
05a59a08 DKF |
1168 | return; |
1169 | } | |
1170 | ||
1171 | base = branch->merge[0]->dst; | |
1172 | base = shorten_unambiguous_ref(base, 0); | |
1173 | color_fprintf(s->fp, branch_color_local, "%s", branch_name); | |
1174 | color_fprintf(s->fp, header_color, "..."); | |
1175 | color_fprintf(s->fp, branch_color_remote, "%s", base); | |
1176 | ||
1177 | color_fprintf(s->fp, header_color, " ["); | |
1178 | if (!num_ours) { | |
98f5e24b | 1179 | color_fprintf(s->fp, header_color, _("behind ")); |
05a59a08 DKF |
1180 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); |
1181 | } else if (!num_theirs) { | |
98f5e24b | 1182 | color_fprintf(s->fp, header_color, _("ahead ")); |
05a59a08 DKF |
1183 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); |
1184 | } else { | |
98f5e24b | 1185 | color_fprintf(s->fp, header_color, _("ahead ")); |
05a59a08 | 1186 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); |
98f5e24b | 1187 | color_fprintf(s->fp, header_color, _(", behind ")); |
05a59a08 DKF |
1188 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); |
1189 | } | |
1190 | ||
a5985237 JK |
1191 | color_fprintf(s->fp, header_color, "]"); |
1192 | fputc(s->null_termination ? '\0' : '\n', s->fp); | |
05a59a08 DKF |
1193 | } |
1194 | ||
d4a6bf1f | 1195 | void wt_shortstatus_print(struct wt_status *s) |
84dbe7b8 MG |
1196 | { |
1197 | int i; | |
05a59a08 | 1198 | |
d4a6bf1f | 1199 | if (s->show_branch) |
05a59a08 DKF |
1200 | wt_shortstatus_print_tracking(s); |
1201 | ||
84dbe7b8 MG |
1202 | for (i = 0; i < s->change.nr; i++) { |
1203 | struct wt_status_change_data *d; | |
1204 | struct string_list_item *it; | |
1205 | ||
1206 | it = &(s->change.items[i]); | |
1207 | d = it->util; | |
1208 | if (d->stagemask) | |
3207a3a2 | 1209 | wt_shortstatus_unmerged(it, s); |
84dbe7b8 | 1210 | else |
3207a3a2 | 1211 | wt_shortstatus_status(it, s); |
84dbe7b8 MG |
1212 | } |
1213 | for (i = 0; i < s->untracked.nr; i++) { | |
1214 | struct string_list_item *it; | |
1215 | ||
1216 | it = &(s->untracked.items[i]); | |
3207a3a2 | 1217 | wt_shortstatus_other(it, s, "??"); |
2381e39e JH |
1218 | } |
1219 | for (i = 0; i < s->ignored.nr; i++) { | |
1220 | struct string_list_item *it; | |
1221 | ||
1222 | it = &(s->ignored.items[i]); | |
3207a3a2 | 1223 | wt_shortstatus_other(it, s, "!!"); |
84dbe7b8 MG |
1224 | } |
1225 | } | |
4a7cc2fd | 1226 | |
3207a3a2 | 1227 | void wt_porcelain_print(struct wt_status *s) |
4a7cc2fd JK |
1228 | { |
1229 | s->use_color = 0; | |
8661768f JK |
1230 | s->relative_paths = 0; |
1231 | s->prefix = NULL; | |
d4a6bf1f | 1232 | wt_shortstatus_print(s); |
4a7cc2fd | 1233 | } |