]>
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" |
c91f0d92 | 13 | |
23900a96 | 14 | static char default_wt_status_colors[][COLOR_MAXLEN] = { |
dc6ebd4c AL |
15 | GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */ |
16 | GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */ | |
17 | GIT_COLOR_RED, /* WT_STATUS_CHANGED */ | |
18 | GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */ | |
19 | GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */ | |
4d4d5726 | 20 | GIT_COLOR_RED, /* WT_STATUS_UNMERGED */ |
05a59a08 DKF |
21 | GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */ |
22 | GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */ | |
c91f0d92 | 23 | }; |
4d229653 | 24 | |
d249b098 | 25 | static const char *color(int slot, struct wt_status *s) |
c91f0d92 | 26 | { |
23900a96 | 27 | return s->use_color > 0 ? s->color_palette[slot] : ""; |
c91f0d92 JK |
28 | } |
29 | ||
30 | void wt_status_prepare(struct wt_status *s) | |
31 | { | |
32 | unsigned char sha1[20]; | |
33 | const char *head; | |
34 | ||
cc46a743 | 35 | memset(s, 0, sizeof(*s)); |
23900a96 JH |
36 | memcpy(s->color_palette, default_wt_status_colors, |
37 | sizeof(default_wt_status_colors)); | |
d249b098 JH |
38 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; |
39 | s->use_color = -1; | |
40 | s->relative_paths = 1; | |
8da19775 | 41 | head = resolve_ref("HEAD", sha1, 0, NULL); |
f62363fb | 42 | s->branch = head ? xstrdup(head) : NULL; |
c91f0d92 | 43 | s->reference = "HEAD"; |
f26a0012 | 44 | s->fp = stdout; |
0f729f21 | 45 | s->index_file = get_index_file(); |
50b7e70f | 46 | s->change.strdup_strings = 1; |
76378683 | 47 | s->untracked.strdup_strings = 1; |
6cb3f6b2 | 48 | s->ignored.strdup_strings = 1; |
c91f0d92 JK |
49 | } |
50 | ||
4d4d5726 JH |
51 | static void wt_status_print_unmerged_header(struct wt_status *s) |
52 | { | |
d249b098 | 53 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 54 | |
4d4d5726 | 55 | color_fprintf_ln(s->fp, c, "# Unmerged paths:"); |
edf563fb JK |
56 | if (!advice_status_hints) |
57 | return; | |
3c588453 JH |
58 | if (s->in_merge) |
59 | ; | |
60 | else if (!s->is_initial) | |
4d4d5726 JH |
61 | color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference); |
62 | else | |
63 | color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)"); | |
dd20f8af | 64 | color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" as appropriate to mark resolution)"); |
4d4d5726 JH |
65 | color_fprintf_ln(s->fp, c, "#"); |
66 | } | |
67 | ||
f26a0012 | 68 | static void wt_status_print_cached_header(struct wt_status *s) |
3c1eb9cb | 69 | { |
d249b098 | 70 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 71 | |
f26a0012 | 72 | color_fprintf_ln(s->fp, c, "# Changes to be committed:"); |
edf563fb JK |
73 | if (!advice_status_hints) |
74 | return; | |
3c588453 JH |
75 | if (s->in_merge) |
76 | ; /* NEEDSWORK: use "git reset --unresolve"??? */ | |
77 | else if (!s->is_initial) | |
f26a0012 | 78 | color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference); |
3c588453 | 79 | else |
f26a0012 | 80 | color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)"); |
f26a0012 | 81 | color_fprintf_ln(s->fp, c, "#"); |
3c1eb9cb JR |
82 | } |
83 | ||
bb914b14 | 84 | static void wt_status_print_dirty_header(struct wt_status *s, |
9297f77e JL |
85 | int has_deleted, |
86 | int has_dirty_submodules) | |
c91f0d92 | 87 | { |
d249b098 | 88 | const char *c = color(WT_STATUS_HEADER, s); |
3c588453 | 89 | |
bb914b14 | 90 | color_fprintf_ln(s->fp, c, "# Changed but not updated:"); |
edf563fb JK |
91 | if (!advice_status_hints) |
92 | return; | |
bb914b14 AM |
93 | if (!has_deleted) |
94 | color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)"); | |
95 | else | |
96 | color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)"); | |
4d6e4c4d | 97 | color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)"); |
9297f77e JL |
98 | if (has_dirty_submodules) |
99 | color_fprintf_ln(s->fp, c, "# (commit or discard the untracked or modified content in submodules)"); | |
bb914b14 AM |
100 | color_fprintf_ln(s->fp, c, "#"); |
101 | } | |
102 | ||
1b908b6f JH |
103 | static void wt_status_print_other_header(struct wt_status *s, |
104 | const char *what, | |
105 | const char *how) | |
bb914b14 | 106 | { |
d249b098 | 107 | const char *c = color(WT_STATUS_HEADER, s); |
1b908b6f | 108 | color_fprintf_ln(s->fp, c, "# %s files:", what); |
edf563fb JK |
109 | if (!advice_status_hints) |
110 | return; | |
1b908b6f | 111 | color_fprintf_ln(s->fp, c, "# (use \"git %s <file>...\" to include in what will be committed)", how); |
f26a0012 | 112 | color_fprintf_ln(s->fp, c, "#"); |
c91f0d92 JK |
113 | } |
114 | ||
f26a0012 | 115 | static void wt_status_print_trailer(struct wt_status *s) |
c91f0d92 | 116 | { |
d249b098 | 117 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); |
c91f0d92 JK |
118 | } |
119 | ||
a734d0b1 | 120 | #define quote_path quote_path_relative |
3a946802 | 121 | |
4d4d5726 JH |
122 | static void wt_status_print_unmerged_data(struct wt_status *s, |
123 | struct string_list_item *it) | |
124 | { | |
d249b098 | 125 | const char *c = color(WT_STATUS_UNMERGED, s); |
4d4d5726 JH |
126 | struct wt_status_change_data *d = it->util; |
127 | struct strbuf onebuf = STRBUF_INIT; | |
128 | const char *one, *how = "bug"; | |
129 | ||
130 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
d249b098 | 131 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); |
4d4d5726 JH |
132 | switch (d->stagemask) { |
133 | case 1: how = "both deleted:"; break; | |
134 | case 2: how = "added by us:"; break; | |
135 | case 3: how = "deleted by them:"; break; | |
136 | case 4: how = "added by them:"; break; | |
137 | case 5: how = "deleted by us:"; break; | |
138 | case 6: how = "both added:"; break; | |
139 | case 7: how = "both modified:"; break; | |
140 | } | |
141 | color_fprintf(s->fp, c, "%-20s%s\n", how, one); | |
142 | strbuf_release(&onebuf); | |
143 | } | |
144 | ||
50b7e70f JH |
145 | static void wt_status_print_change_data(struct wt_status *s, |
146 | int change_type, | |
147 | struct string_list_item *it) | |
c91f0d92 | 148 | { |
50b7e70f | 149 | struct wt_status_change_data *d = it->util; |
d249b098 | 150 | const char *c = color(change_type, s); |
50b7e70f JH |
151 | int status = status; |
152 | char *one_name; | |
153 | char *two_name; | |
3a946802 | 154 | const char *one, *two; |
f285a2d7 | 155 | struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT; |
9297f77e | 156 | struct strbuf extra = STRBUF_INIT; |
3a946802 | 157 | |
50b7e70f JH |
158 | one_name = two_name = it->string; |
159 | switch (change_type) { | |
160 | case WT_STATUS_UPDATED: | |
161 | status = d->index_status; | |
162 | if (d->head_path) | |
163 | one_name = d->head_path; | |
164 | break; | |
165 | case WT_STATUS_CHANGED: | |
9297f77e JL |
166 | if (d->new_submodule_commits || d->dirty_submodule) { |
167 | strbuf_addstr(&extra, " ("); | |
168 | if (d->new_submodule_commits) | |
169 | strbuf_addf(&extra, "new commits, "); | |
170 | if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) | |
171 | strbuf_addf(&extra, "modified content, "); | |
172 | if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) | |
173 | strbuf_addf(&extra, "untracked content, "); | |
174 | strbuf_setlen(&extra, extra.len - 2); | |
175 | strbuf_addch(&extra, ')'); | |
176 | } | |
50b7e70f JH |
177 | status = d->worktree_status; |
178 | break; | |
179 | } | |
180 | ||
181 | one = quote_path(one_name, -1, &onebuf, s->prefix); | |
182 | two = quote_path(two_name, -1, &twobuf, s->prefix); | |
3a946802 | 183 | |
d249b098 | 184 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); |
50b7e70f | 185 | switch (status) { |
c91f0d92 | 186 | case DIFF_STATUS_ADDED: |
f26a0012 | 187 | color_fprintf(s->fp, c, "new file: %s", one); |
3a946802 | 188 | break; |
c91f0d92 | 189 | case DIFF_STATUS_COPIED: |
f26a0012 | 190 | color_fprintf(s->fp, c, "copied: %s -> %s", one, two); |
c91f0d92 JK |
191 | break; |
192 | case DIFF_STATUS_DELETED: | |
f26a0012 | 193 | color_fprintf(s->fp, c, "deleted: %s", one); |
3a946802 | 194 | break; |
c91f0d92 | 195 | case DIFF_STATUS_MODIFIED: |
f26a0012 | 196 | color_fprintf(s->fp, c, "modified: %s", one); |
3a946802 | 197 | break; |
c91f0d92 | 198 | case DIFF_STATUS_RENAMED: |
f26a0012 | 199 | color_fprintf(s->fp, c, "renamed: %s -> %s", one, two); |
c91f0d92 JK |
200 | break; |
201 | case DIFF_STATUS_TYPE_CHANGED: | |
f26a0012 | 202 | color_fprintf(s->fp, c, "typechange: %s", one); |
3a946802 | 203 | break; |
c91f0d92 | 204 | case DIFF_STATUS_UNKNOWN: |
f26a0012 | 205 | color_fprintf(s->fp, c, "unknown: %s", one); |
3a946802 | 206 | break; |
c91f0d92 | 207 | case DIFF_STATUS_UNMERGED: |
f26a0012 | 208 | color_fprintf(s->fp, c, "unmerged: %s", one); |
3a946802 | 209 | break; |
c91f0d92 | 210 | default: |
50b7e70f | 211 | die("bug: unhandled diff status %c", status); |
c91f0d92 | 212 | } |
9297f77e JL |
213 | if (extra.len) { |
214 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf); | |
215 | strbuf_release(&extra); | |
216 | } | |
f26a0012 | 217 | fprintf(s->fp, "\n"); |
367c9886 JS |
218 | strbuf_release(&onebuf); |
219 | strbuf_release(&twobuf); | |
c91f0d92 JK |
220 | } |
221 | ||
50b7e70f JH |
222 | static void wt_status_collect_changed_cb(struct diff_queue_struct *q, |
223 | struct diff_options *options, | |
224 | void *data) | |
c91f0d92 JK |
225 | { |
226 | struct wt_status *s = data; | |
c91f0d92 | 227 | int i; |
50b7e70f JH |
228 | |
229 | if (!q->nr) | |
230 | return; | |
231 | s->workdir_dirty = 1; | |
c91f0d92 | 232 | for (i = 0; i < q->nr; i++) { |
50b7e70f JH |
233 | struct diff_filepair *p; |
234 | struct string_list_item *it; | |
235 | struct wt_status_change_data *d; | |
236 | ||
237 | p = q->queue[i]; | |
238 | it = string_list_insert(p->one->path, &s->change); | |
239 | d = it->util; | |
240 | if (!d) { | |
241 | d = xcalloc(1, sizeof(*d)); | |
242 | it->util = d; | |
c91f0d92 | 243 | } |
50b7e70f JH |
244 | if (!d->worktree_status) |
245 | d->worktree_status = p->status; | |
9297f77e JL |
246 | d->dirty_submodule = p->two->dirty_submodule; |
247 | if (S_ISGITLINK(p->two->mode)) | |
248 | d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1); | |
c91f0d92 | 249 | } |
c91f0d92 JK |
250 | } |
251 | ||
4d4d5726 JH |
252 | static int unmerged_mask(const char *path) |
253 | { | |
254 | int pos, mask; | |
255 | struct cache_entry *ce; | |
256 | ||
257 | pos = cache_name_pos(path, strlen(path)); | |
258 | if (0 <= pos) | |
259 | return 0; | |
260 | ||
261 | mask = 0; | |
262 | pos = -pos-1; | |
263 | while (pos < active_nr) { | |
264 | ce = active_cache[pos++]; | |
265 | if (strcmp(ce->name, path) || !ce_stage(ce)) | |
266 | break; | |
267 | mask |= (1 << (ce_stage(ce) - 1)); | |
268 | } | |
269 | return mask; | |
270 | } | |
271 | ||
50b7e70f JH |
272 | static void wt_status_collect_updated_cb(struct diff_queue_struct *q, |
273 | struct diff_options *options, | |
274 | void *data) | |
c91f0d92 | 275 | { |
6e458bf6 | 276 | struct wt_status *s = data; |
c91f0d92 | 277 | int i; |
50b7e70f JH |
278 | |
279 | for (i = 0; i < q->nr; i++) { | |
280 | struct diff_filepair *p; | |
281 | struct string_list_item *it; | |
282 | struct wt_status_change_data *d; | |
283 | ||
284 | p = q->queue[i]; | |
285 | it = string_list_insert(p->two->path, &s->change); | |
286 | d = it->util; | |
287 | if (!d) { | |
288 | d = xcalloc(1, sizeof(*d)); | |
289 | it->util = d; | |
290 | } | |
291 | if (!d->index_status) | |
292 | d->index_status = p->status; | |
293 | switch (p->status) { | |
294 | case DIFF_STATUS_COPIED: | |
295 | case DIFF_STATUS_RENAMED: | |
296 | d->head_path = xstrdup(p->one->path); | |
297 | break; | |
4d4d5726 JH |
298 | case DIFF_STATUS_UNMERGED: |
299 | d->stagemask = unmerged_mask(p->two->path); | |
300 | break; | |
50b7e70f | 301 | } |
6e458bf6 | 302 | } |
c91f0d92 JK |
303 | } |
304 | ||
50b7e70f | 305 | static void wt_status_collect_changes_worktree(struct wt_status *s) |
c91f0d92 JK |
306 | { |
307 | struct rev_info rev; | |
50b7e70f JH |
308 | |
309 | init_revisions(&rev, NULL); | |
310 | setup_revisions(0, NULL, &rev, NULL); | |
311 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
9297f77e | 312 | DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES); |
3bfc4504 JL |
313 | if (!s->show_untracked_files) |
314 | DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
50b7e70f JH |
315 | rev.diffopt.format_callback = wt_status_collect_changed_cb; |
316 | rev.diffopt.format_callback_data = s; | |
76e2f7ce | 317 | rev.prune_data = s->pathspec; |
50b7e70f JH |
318 | run_diff_files(&rev, 0); |
319 | } | |
320 | ||
321 | static void wt_status_collect_changes_index(struct wt_status *s) | |
322 | { | |
323 | struct rev_info rev; | |
32962c9b | 324 | struct setup_revision_opt opt; |
50b7e70f | 325 | |
c91f0d92 | 326 | init_revisions(&rev, NULL); |
32962c9b JH |
327 | memset(&opt, 0, sizeof(opt)); |
328 | opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference; | |
329 | setup_revisions(0, NULL, &rev, &opt); | |
330 | ||
c91f0d92 | 331 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
50b7e70f | 332 | rev.diffopt.format_callback = wt_status_collect_updated_cb; |
c91f0d92 JK |
333 | rev.diffopt.format_callback_data = s; |
334 | rev.diffopt.detect_rename = 1; | |
50705915 | 335 | rev.diffopt.rename_limit = 200; |
f714fb84 | 336 | rev.diffopt.break_opt = 0; |
76e2f7ce | 337 | rev.prune_data = s->pathspec; |
c91f0d92 JK |
338 | run_diff_index(&rev, 1); |
339 | } | |
340 | ||
50b7e70f JH |
341 | static void wt_status_collect_changes_initial(struct wt_status *s) |
342 | { | |
343 | int i; | |
344 | ||
345 | for (i = 0; i < active_nr; i++) { | |
346 | struct string_list_item *it; | |
347 | struct wt_status_change_data *d; | |
348 | struct cache_entry *ce = active_cache[i]; | |
349 | ||
76e2f7ce JH |
350 | if (!ce_path_match(ce, s->pathspec)) |
351 | continue; | |
50b7e70f JH |
352 | it = string_list_insert(ce->name, &s->change); |
353 | d = it->util; | |
354 | if (!d) { | |
355 | d = xcalloc(1, sizeof(*d)); | |
356 | it->util = d; | |
357 | } | |
4d4d5726 | 358 | if (ce_stage(ce)) { |
50b7e70f | 359 | d->index_status = DIFF_STATUS_UNMERGED; |
4d4d5726 JH |
360 | d->stagemask |= (1 << (ce_stage(ce) - 1)); |
361 | } | |
50b7e70f JH |
362 | else |
363 | d->index_status = DIFF_STATUS_ADDED; | |
364 | } | |
365 | } | |
366 | ||
76378683 JH |
367 | static void wt_status_collect_untracked(struct wt_status *s) |
368 | { | |
369 | int i; | |
370 | struct dir_struct dir; | |
371 | ||
372 | if (!s->show_untracked_files) | |
373 | return; | |
374 | memset(&dir, 0, sizeof(dir)); | |
375 | if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES) | |
376 | dir.flags |= | |
377 | DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; | |
378 | setup_standard_excludes(&dir); | |
379 | ||
688cd6d2 | 380 | fill_directory(&dir, s->pathspec); |
eeefa7c9 | 381 | for (i = 0; i < dir.nr; i++) { |
76378683 JH |
382 | struct dir_entry *ent = dir.entries[i]; |
383 | if (!cache_name_is_other(ent->name, ent->len)) | |
384 | continue; | |
76e2f7ce JH |
385 | if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL)) |
386 | continue; | |
76378683 | 387 | string_list_insert(ent->name, &s->untracked); |
f5b26b1d | 388 | free(ent); |
76378683 | 389 | } |
f5b26b1d | 390 | |
6cb3f6b2 JH |
391 | if (s->show_ignored_files) { |
392 | dir.nr = 0; | |
393 | dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES; | |
394 | fill_directory(&dir, s->pathspec); | |
395 | for (i = 0; i < dir.nr; i++) { | |
396 | struct dir_entry *ent = dir.entries[i]; | |
397 | if (!cache_name_is_other(ent->name, ent->len)) | |
398 | continue; | |
399 | if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL)) | |
400 | continue; | |
401 | string_list_insert(ent->name, &s->ignored); | |
402 | free(ent); | |
403 | } | |
404 | } | |
405 | ||
f5b26b1d | 406 | free(dir.entries); |
76378683 JH |
407 | } |
408 | ||
409 | void wt_status_collect(struct wt_status *s) | |
50b7e70f JH |
410 | { |
411 | wt_status_collect_changes_worktree(s); | |
412 | ||
413 | if (s->is_initial) | |
414 | wt_status_collect_changes_initial(s); | |
415 | else | |
416 | wt_status_collect_changes_index(s); | |
76378683 | 417 | wt_status_collect_untracked(s); |
50b7e70f JH |
418 | } |
419 | ||
4d4d5726 JH |
420 | static void wt_status_print_unmerged(struct wt_status *s) |
421 | { | |
422 | int shown_header = 0; | |
423 | int i; | |
424 | ||
425 | for (i = 0; i < s->change.nr; i++) { | |
426 | struct wt_status_change_data *d; | |
427 | struct string_list_item *it; | |
428 | it = &(s->change.items[i]); | |
429 | d = it->util; | |
430 | if (!d->stagemask) | |
431 | continue; | |
432 | if (!shown_header) { | |
433 | wt_status_print_unmerged_header(s); | |
434 | shown_header = 1; | |
435 | } | |
436 | wt_status_print_unmerged_data(s, it); | |
437 | } | |
438 | if (shown_header) | |
439 | wt_status_print_trailer(s); | |
440 | ||
441 | } | |
442 | ||
50b7e70f JH |
443 | static void wt_status_print_updated(struct wt_status *s) |
444 | { | |
445 | int shown_header = 0; | |
446 | int i; | |
447 | ||
448 | for (i = 0; i < s->change.nr; i++) { | |
449 | struct wt_status_change_data *d; | |
450 | struct string_list_item *it; | |
451 | it = &(s->change.items[i]); | |
452 | d = it->util; | |
453 | if (!d->index_status || | |
454 | d->index_status == DIFF_STATUS_UNMERGED) | |
455 | continue; | |
456 | if (!shown_header) { | |
457 | wt_status_print_cached_header(s); | |
458 | s->commitable = 1; | |
459 | shown_header = 1; | |
460 | } | |
461 | wt_status_print_change_data(s, WT_STATUS_UPDATED, it); | |
462 | } | |
463 | if (shown_header) | |
464 | wt_status_print_trailer(s); | |
465 | } | |
466 | ||
467 | /* | |
468 | * -1 : has delete | |
469 | * 0 : no change | |
470 | * 1 : some change but no delete | |
471 | */ | |
9297f77e JL |
472 | static int wt_status_check_worktree_changes(struct wt_status *s, |
473 | int *dirty_submodules) | |
50b7e70f JH |
474 | { |
475 | int i; | |
476 | int changes = 0; | |
477 | ||
9297f77e JL |
478 | *dirty_submodules = 0; |
479 | ||
50b7e70f JH |
480 | for (i = 0; i < s->change.nr; i++) { |
481 | struct wt_status_change_data *d; | |
482 | d = s->change.items[i].util; | |
4d4d5726 JH |
483 | if (!d->worktree_status || |
484 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f | 485 | continue; |
9297f77e JL |
486 | if (!changes) |
487 | changes = 1; | |
488 | if (d->dirty_submodule) | |
489 | *dirty_submodules = 1; | |
50b7e70f | 490 | if (d->worktree_status == DIFF_STATUS_DELETED) |
9297f77e | 491 | changes = -1; |
50b7e70f JH |
492 | } |
493 | return changes; | |
494 | } | |
495 | ||
c91f0d92 JK |
496 | static void wt_status_print_changed(struct wt_status *s) |
497 | { | |
9297f77e JL |
498 | int i, dirty_submodules; |
499 | int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules); | |
50b7e70f JH |
500 | |
501 | if (!worktree_changes) | |
502 | return; | |
503 | ||
9297f77e | 504 | wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules); |
50b7e70f JH |
505 | |
506 | for (i = 0; i < s->change.nr; i++) { | |
507 | struct wt_status_change_data *d; | |
508 | struct string_list_item *it; | |
509 | it = &(s->change.items[i]); | |
510 | d = it->util; | |
4d4d5726 JH |
511 | if (!d->worktree_status || |
512 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f JH |
513 | continue; |
514 | wt_status_print_change_data(s, WT_STATUS_CHANGED, it); | |
515 | } | |
516 | wt_status_print_trailer(s); | |
c91f0d92 JK |
517 | } |
518 | ||
f17a5d34 | 519 | static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted) |
ac8d5afc PY |
520 | { |
521 | struct child_process sm_summary; | |
522 | char summary_limit[64]; | |
523 | char index[PATH_MAX]; | |
66dbfd55 GV |
524 | const char *env[] = { NULL, NULL }; |
525 | const char *argv[8]; | |
526 | ||
527 | env[0] = index; | |
528 | argv[0] = "submodule"; | |
529 | argv[1] = "summary"; | |
530 | argv[2] = uncommitted ? "--files" : "--cached"; | |
531 | argv[3] = "--for-status"; | |
532 | argv[4] = "--summary-limit"; | |
533 | argv[5] = summary_limit; | |
534 | argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"); | |
535 | argv[7] = NULL; | |
ac8d5afc | 536 | |
d249b098 | 537 | sprintf(summary_limit, "%d", s->submodule_summary); |
ac8d5afc PY |
538 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file); |
539 | ||
540 | memset(&sm_summary, 0, sizeof(sm_summary)); | |
541 | sm_summary.argv = argv; | |
542 | sm_summary.env = env; | |
543 | sm_summary.git_cmd = 1; | |
544 | sm_summary.no_stdin = 1; | |
545 | fflush(s->fp); | |
546 | sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */ | |
547 | run_command(&sm_summary); | |
548 | } | |
549 | ||
1b908b6f JH |
550 | static void wt_status_print_other(struct wt_status *s, |
551 | struct string_list *l, | |
552 | const char *what, | |
553 | const char *how) | |
c91f0d92 | 554 | { |
c91f0d92 | 555 | int i; |
f285a2d7 | 556 | struct strbuf buf = STRBUF_INIT; |
c91f0d92 | 557 | |
76378683 JH |
558 | if (!s->untracked.nr) |
559 | return; | |
c91f0d92 | 560 | |
1b908b6f JH |
561 | wt_status_print_other_header(s, what, how); |
562 | ||
563 | for (i = 0; i < l->nr; i++) { | |
76378683 | 564 | struct string_list_item *it; |
1b908b6f | 565 | it = &(l->items[i]); |
d249b098 JH |
566 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); |
567 | color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", | |
76378683 JH |
568 | quote_path(it->string, strlen(it->string), |
569 | &buf, s->prefix)); | |
c91f0d92 | 570 | } |
367c9886 | 571 | strbuf_release(&buf); |
c91f0d92 JK |
572 | } |
573 | ||
574 | static void wt_status_print_verbose(struct wt_status *s) | |
575 | { | |
576 | struct rev_info rev; | |
32962c9b | 577 | struct setup_revision_opt opt; |
99a12694 | 578 | |
c91f0d92 | 579 | init_revisions(&rev, NULL); |
5ec11af6 | 580 | DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV); |
32962c9b JH |
581 | |
582 | memset(&opt, 0, sizeof(opt)); | |
583 | opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference; | |
584 | setup_revisions(0, NULL, &rev, &opt); | |
585 | ||
c91f0d92 JK |
586 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
587 | rev.diffopt.detect_rename = 1; | |
4ba0cb27 KH |
588 | rev.diffopt.file = s->fp; |
589 | rev.diffopt.close_file = 0; | |
4f672ad6 JK |
590 | /* |
591 | * If we're not going to stdout, then we definitely don't | |
592 | * want color, since we are going to the commit message | |
593 | * file (and even the "auto" setting won't work, since it | |
594 | * will have checked isatty on stdout). | |
595 | */ | |
596 | if (s->fp != stdout) | |
597 | DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF); | |
c91f0d92 JK |
598 | run_diff_index(&rev, 1); |
599 | } | |
600 | ||
b6975ab5 JH |
601 | static void wt_status_print_tracking(struct wt_status *s) |
602 | { | |
603 | struct strbuf sb = STRBUF_INIT; | |
604 | const char *cp, *ep; | |
605 | struct branch *branch; | |
606 | ||
607 | assert(s->branch && !s->is_initial); | |
608 | if (prefixcmp(s->branch, "refs/heads/")) | |
609 | return; | |
610 | branch = branch_get(s->branch + 11); | |
611 | if (!format_tracking_info(branch, &sb)) | |
612 | return; | |
613 | ||
614 | for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) | |
d249b098 | 615 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), |
b6975ab5 | 616 | "# %.*s", (int)(ep - cp), cp); |
d249b098 | 617 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); |
b6975ab5 JH |
618 | } |
619 | ||
c91f0d92 JK |
620 | void wt_status_print(struct wt_status *s) |
621 | { | |
d249b098 | 622 | const char *branch_color = color(WT_STATUS_HEADER, s); |
98bf8a47 | 623 | |
bda324cf JH |
624 | if (s->branch) { |
625 | const char *on_what = "On branch "; | |
626 | const char *branch_name = s->branch; | |
cc44c765 | 627 | if (!prefixcmp(branch_name, "refs/heads/")) |
bda324cf JH |
628 | branch_name += 11; |
629 | else if (!strcmp(branch_name, "HEAD")) { | |
630 | branch_name = ""; | |
d249b098 | 631 | branch_color = color(WT_STATUS_NOBRANCH, s); |
bda324cf JH |
632 | on_what = "Not currently on any branch."; |
633 | } | |
d249b098 | 634 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# "); |
950ce2e2 | 635 | color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name); |
b6975ab5 JH |
636 | if (!s->is_initial) |
637 | wt_status_print_tracking(s); | |
bda324cf | 638 | } |
c91f0d92 JK |
639 | |
640 | if (s->is_initial) { | |
d249b098 JH |
641 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); |
642 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit"); | |
643 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); | |
c91f0d92 JK |
644 | } |
645 | ||
c1e255b7 | 646 | wt_status_print_updated(s); |
228e7b5d | 647 | wt_status_print_unmerged(s); |
c91f0d92 | 648 | wt_status_print_changed(s); |
f17a5d34 JL |
649 | if (s->submodule_summary) { |
650 | wt_status_print_submodule_summary(s, 0); /* staged */ | |
651 | wt_status_print_submodule_summary(s, 1); /* unstaged */ | |
652 | } | |
2381e39e | 653 | if (s->show_untracked_files) { |
1b908b6f | 654 | wt_status_print_other(s, &s->untracked, "Untracked", "add"); |
2381e39e JH |
655 | if (s->show_ignored_files) |
656 | wt_status_print_other(s, &s->ignored, "Ignored", "add -f"); | |
657 | } else if (s->commitable) | |
980bde38 MG |
658 | fprintf(s->fp, "# Untracked files not listed%s\n", |
659 | advice_status_hints | |
660 | ? " (use -u option to show untracked files)" : ""); | |
c91f0d92 | 661 | |
1324fb6f | 662 | if (s->verbose) |
c91f0d92 | 663 | wt_status_print_verbose(s); |
6e458bf6 JR |
664 | if (!s->commitable) { |
665 | if (s->amend) | |
f26a0012 | 666 | fprintf(s->fp, "# No changes\n"); |
37d07f8f JH |
667 | else if (s->nowarn) |
668 | ; /* nothing */ | |
2a3a3c24 | 669 | else if (s->workdir_dirty) |
980bde38 MG |
670 | printf("no changes added to commit%s\n", |
671 | advice_status_hints | |
672 | ? " (use \"git add\" and/or \"git commit -a\")" : ""); | |
76378683 | 673 | else if (s->untracked.nr) |
980bde38 MG |
674 | printf("nothing added to commit but untracked files present%s\n", |
675 | advice_status_hints | |
676 | ? " (use \"git add\" to track)" : ""); | |
2a3a3c24 | 677 | else if (s->is_initial) |
980bde38 MG |
678 | printf("nothing to commit%s\n", advice_status_hints |
679 | ? " (create/copy files and use \"git add\" to track)" : ""); | |
d249b098 | 680 | else if (!s->show_untracked_files) |
980bde38 MG |
681 | printf("nothing to commit%s\n", advice_status_hints |
682 | ? " (use -u to show untracked files)" : ""); | |
2a3a3c24 | 683 | else |
980bde38 MG |
684 | printf("nothing to commit%s\n", advice_status_hints |
685 | ? " (working directory clean)" : ""); | |
6e458bf6 | 686 | } |
c91f0d92 | 687 | } |
84dbe7b8 MG |
688 | |
689 | static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it, | |
690 | struct wt_status *s) | |
691 | { | |
692 | struct wt_status_change_data *d = it->util; | |
693 | const char *how = "??"; | |
694 | ||
695 | switch (d->stagemask) { | |
696 | case 1: how = "DD"; break; /* both deleted */ | |
697 | case 2: how = "AU"; break; /* added by us */ | |
698 | case 3: how = "UD"; break; /* deleted by them */ | |
699 | case 4: how = "UA"; break; /* added by them */ | |
700 | case 5: how = "DU"; break; /* deleted by us */ | |
701 | case 6: how = "AA"; break; /* both added */ | |
702 | case 7: how = "UU"; break; /* both modified */ | |
703 | } | |
3fe2a894 | 704 | color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how); |
84dbe7b8 | 705 | if (null_termination) { |
3fe2a894 | 706 | fprintf(stdout, " %s%c", it->string, 0); |
84dbe7b8 MG |
707 | } else { |
708 | struct strbuf onebuf = STRBUF_INIT; | |
709 | const char *one; | |
710 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
3fe2a894 | 711 | printf(" %s\n", one); |
84dbe7b8 MG |
712 | strbuf_release(&onebuf); |
713 | } | |
714 | } | |
715 | ||
716 | static void wt_shortstatus_status(int null_termination, struct string_list_item *it, | |
717 | struct wt_status *s) | |
718 | { | |
719 | struct wt_status_change_data *d = it->util; | |
720 | ||
3fe2a894 MG |
721 | if (d->index_status) |
722 | color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status); | |
723 | else | |
724 | putchar(' '); | |
725 | if (d->worktree_status) | |
726 | color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status); | |
727 | else | |
728 | putchar(' '); | |
729 | putchar(' '); | |
84dbe7b8 MG |
730 | if (null_termination) { |
731 | fprintf(stdout, "%s%c", it->string, 0); | |
732 | if (d->head_path) | |
733 | fprintf(stdout, "%s%c", d->head_path, 0); | |
734 | } else { | |
735 | struct strbuf onebuf = STRBUF_INIT; | |
736 | const char *one; | |
737 | if (d->head_path) { | |
738 | one = quote_path(d->head_path, -1, &onebuf, s->prefix); | |
739 | printf("%s -> ", one); | |
740 | strbuf_release(&onebuf); | |
741 | } | |
742 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
743 | printf("%s\n", one); | |
744 | strbuf_release(&onebuf); | |
745 | } | |
746 | } | |
747 | ||
2381e39e JH |
748 | static void wt_shortstatus_other(int null_termination, struct string_list_item *it, |
749 | struct wt_status *s, const char *sign) | |
84dbe7b8 MG |
750 | { |
751 | if (null_termination) { | |
2381e39e | 752 | fprintf(stdout, "%s %s%c", sign, it->string, 0); |
84dbe7b8 MG |
753 | } else { |
754 | struct strbuf onebuf = STRBUF_INIT; | |
755 | const char *one; | |
756 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
c1909e72 | 757 | color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign); |
3fe2a894 | 758 | printf(" %s\n", one); |
84dbe7b8 MG |
759 | strbuf_release(&onebuf); |
760 | } | |
761 | } | |
762 | ||
05a59a08 DKF |
763 | static void wt_shortstatus_print_tracking(struct wt_status *s) |
764 | { | |
765 | struct branch *branch; | |
766 | const char *header_color = color(WT_STATUS_HEADER, s); | |
767 | const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s); | |
768 | const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s); | |
769 | ||
770 | const char *base; | |
771 | const char *branch_name; | |
772 | int num_ours, num_theirs; | |
773 | ||
774 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## "); | |
775 | ||
776 | if (!s->branch) | |
777 | return; | |
778 | branch_name = s->branch; | |
779 | ||
780 | if (!prefixcmp(branch_name, "refs/heads/")) | |
781 | branch_name += 11; | |
782 | else if (!strcmp(branch_name, "HEAD")) { | |
783 | branch_name = "HEAD (no branch)"; | |
784 | branch_color_local = color(WT_STATUS_NOBRANCH, s); | |
785 | } | |
786 | ||
787 | branch = branch_get(s->branch + 11); | |
788 | if (s->is_initial) | |
789 | color_fprintf(s->fp, header_color, "Initial commit on "); | |
790 | if (!stat_tracking_info(branch, &num_ours, &num_theirs)) { | |
791 | color_fprintf_ln(s->fp, branch_color_local, | |
792 | "%s", branch_name); | |
793 | return; | |
794 | } | |
795 | ||
796 | base = branch->merge[0]->dst; | |
797 | base = shorten_unambiguous_ref(base, 0); | |
798 | color_fprintf(s->fp, branch_color_local, "%s", branch_name); | |
799 | color_fprintf(s->fp, header_color, "..."); | |
800 | color_fprintf(s->fp, branch_color_remote, "%s", base); | |
801 | ||
802 | color_fprintf(s->fp, header_color, " ["); | |
803 | if (!num_ours) { | |
804 | color_fprintf(s->fp, header_color, "behind "); | |
805 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); | |
806 | } else if (!num_theirs) { | |
807 | color_fprintf(s->fp, header_color, "ahead "); | |
808 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); | |
809 | } else { | |
810 | color_fprintf(s->fp, header_color, "ahead "); | |
811 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); | |
812 | color_fprintf(s->fp, header_color, ", behind "); | |
813 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); | |
814 | } | |
815 | ||
816 | color_fprintf_ln(s->fp, header_color, "]"); | |
817 | } | |
818 | ||
819 | void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch) | |
84dbe7b8 MG |
820 | { |
821 | int i; | |
05a59a08 DKF |
822 | |
823 | if (show_branch) | |
824 | wt_shortstatus_print_tracking(s); | |
825 | ||
84dbe7b8 MG |
826 | for (i = 0; i < s->change.nr; i++) { |
827 | struct wt_status_change_data *d; | |
828 | struct string_list_item *it; | |
829 | ||
830 | it = &(s->change.items[i]); | |
831 | d = it->util; | |
832 | if (d->stagemask) | |
833 | wt_shortstatus_unmerged(null_termination, it, s); | |
834 | else | |
835 | wt_shortstatus_status(null_termination, it, s); | |
836 | } | |
837 | for (i = 0; i < s->untracked.nr; i++) { | |
838 | struct string_list_item *it; | |
839 | ||
840 | it = &(s->untracked.items[i]); | |
2381e39e JH |
841 | wt_shortstatus_other(null_termination, it, s, "??"); |
842 | } | |
843 | for (i = 0; i < s->ignored.nr; i++) { | |
844 | struct string_list_item *it; | |
845 | ||
846 | it = &(s->ignored.items[i]); | |
847 | wt_shortstatus_other(null_termination, it, s, "!!"); | |
84dbe7b8 MG |
848 | } |
849 | } | |
4a7cc2fd JK |
850 | |
851 | void wt_porcelain_print(struct wt_status *s, int null_termination) | |
852 | { | |
853 | s->use_color = 0; | |
8661768f JK |
854 | s->relative_paths = 0; |
855 | s->prefix = NULL; | |
05a59a08 | 856 | wt_shortstatus_print(s, null_termination, 0); |
4a7cc2fd | 857 | } |