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