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