]>
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); |
4d4d5726 JH |
50 | color_fprintf_ln(s->fp, c, "# Unmerged paths:"); |
51 | if (!s->is_initial) | |
52 | color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference); | |
53 | else | |
54 | color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)"); | |
55 | color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to mark resolution)"); | |
56 | color_fprintf_ln(s->fp, c, "#"); | |
57 | } | |
58 | ||
f26a0012 | 59 | static void wt_status_print_cached_header(struct wt_status *s) |
3c1eb9cb | 60 | { |
d249b098 | 61 | const char *c = color(WT_STATUS_HEADER, s); |
f26a0012 | 62 | color_fprintf_ln(s->fp, c, "# Changes to be committed:"); |
ff58b9aa | 63 | if (!s->is_initial) { |
f26a0012 | 64 | color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference); |
3c1eb9cb | 65 | } else { |
f26a0012 | 66 | color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)"); |
3c1eb9cb | 67 | } |
f26a0012 | 68 | color_fprintf_ln(s->fp, c, "#"); |
3c1eb9cb JR |
69 | } |
70 | ||
bb914b14 AM |
71 | static void wt_status_print_dirty_header(struct wt_status *s, |
72 | int has_deleted) | |
c91f0d92 | 73 | { |
d249b098 | 74 | const char *c = color(WT_STATUS_HEADER, s); |
bb914b14 AM |
75 | color_fprintf_ln(s->fp, c, "# Changed but not updated:"); |
76 | if (!has_deleted) | |
77 | color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)"); | |
78 | else | |
79 | color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)"); | |
4d6e4c4d | 80 | color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)"); |
bb914b14 AM |
81 | color_fprintf_ln(s->fp, c, "#"); |
82 | } | |
83 | ||
84 | static void wt_status_print_untracked_header(struct wt_status *s) | |
85 | { | |
d249b098 | 86 | const char *c = color(WT_STATUS_HEADER, s); |
bb914b14 AM |
87 | color_fprintf_ln(s->fp, c, "# Untracked files:"); |
88 | color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)"); | |
f26a0012 | 89 | color_fprintf_ln(s->fp, c, "#"); |
c91f0d92 JK |
90 | } |
91 | ||
f26a0012 | 92 | static void wt_status_print_trailer(struct wt_status *s) |
c91f0d92 | 93 | { |
d249b098 | 94 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); |
c91f0d92 JK |
95 | } |
96 | ||
a734d0b1 | 97 | #define quote_path quote_path_relative |
3a946802 | 98 | |
4d4d5726 JH |
99 | static void wt_status_print_unmerged_data(struct wt_status *s, |
100 | struct string_list_item *it) | |
101 | { | |
d249b098 | 102 | const char *c = color(WT_STATUS_UNMERGED, s); |
4d4d5726 JH |
103 | struct wt_status_change_data *d = it->util; |
104 | struct strbuf onebuf = STRBUF_INIT; | |
105 | const char *one, *how = "bug"; | |
106 | ||
107 | one = quote_path(it->string, -1, &onebuf, s->prefix); | |
d249b098 | 108 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); |
4d4d5726 JH |
109 | switch (d->stagemask) { |
110 | case 1: how = "both deleted:"; break; | |
111 | case 2: how = "added by us:"; break; | |
112 | case 3: how = "deleted by them:"; break; | |
113 | case 4: how = "added by them:"; break; | |
114 | case 5: how = "deleted by us:"; break; | |
115 | case 6: how = "both added:"; break; | |
116 | case 7: how = "both modified:"; break; | |
117 | } | |
118 | color_fprintf(s->fp, c, "%-20s%s\n", how, one); | |
119 | strbuf_release(&onebuf); | |
120 | } | |
121 | ||
50b7e70f JH |
122 | static void wt_status_print_change_data(struct wt_status *s, |
123 | int change_type, | |
124 | struct string_list_item *it) | |
c91f0d92 | 125 | { |
50b7e70f | 126 | struct wt_status_change_data *d = it->util; |
d249b098 | 127 | const char *c = color(change_type, s); |
50b7e70f JH |
128 | int status = status; |
129 | char *one_name; | |
130 | char *two_name; | |
3a946802 | 131 | const char *one, *two; |
f285a2d7 | 132 | struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT; |
3a946802 | 133 | |
50b7e70f JH |
134 | one_name = two_name = it->string; |
135 | switch (change_type) { | |
136 | case WT_STATUS_UPDATED: | |
137 | status = d->index_status; | |
138 | if (d->head_path) | |
139 | one_name = d->head_path; | |
140 | break; | |
141 | case WT_STATUS_CHANGED: | |
142 | status = d->worktree_status; | |
143 | break; | |
144 | } | |
145 | ||
146 | one = quote_path(one_name, -1, &onebuf, s->prefix); | |
147 | two = quote_path(two_name, -1, &twobuf, s->prefix); | |
3a946802 | 148 | |
d249b098 | 149 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); |
50b7e70f | 150 | switch (status) { |
c91f0d92 | 151 | case DIFF_STATUS_ADDED: |
f26a0012 | 152 | color_fprintf(s->fp, c, "new file: %s", one); |
3a946802 | 153 | break; |
c91f0d92 | 154 | case DIFF_STATUS_COPIED: |
f26a0012 | 155 | color_fprintf(s->fp, c, "copied: %s -> %s", one, two); |
c91f0d92 JK |
156 | break; |
157 | case DIFF_STATUS_DELETED: | |
f26a0012 | 158 | color_fprintf(s->fp, c, "deleted: %s", one); |
3a946802 | 159 | break; |
c91f0d92 | 160 | case DIFF_STATUS_MODIFIED: |
f26a0012 | 161 | color_fprintf(s->fp, c, "modified: %s", one); |
3a946802 | 162 | break; |
c91f0d92 | 163 | case DIFF_STATUS_RENAMED: |
f26a0012 | 164 | color_fprintf(s->fp, c, "renamed: %s -> %s", one, two); |
c91f0d92 JK |
165 | break; |
166 | case DIFF_STATUS_TYPE_CHANGED: | |
f26a0012 | 167 | color_fprintf(s->fp, c, "typechange: %s", one); |
3a946802 | 168 | break; |
c91f0d92 | 169 | case DIFF_STATUS_UNKNOWN: |
f26a0012 | 170 | color_fprintf(s->fp, c, "unknown: %s", one); |
3a946802 | 171 | break; |
c91f0d92 | 172 | case DIFF_STATUS_UNMERGED: |
f26a0012 | 173 | color_fprintf(s->fp, c, "unmerged: %s", one); |
3a946802 | 174 | break; |
c91f0d92 | 175 | default: |
50b7e70f | 176 | die("bug: unhandled diff status %c", status); |
c91f0d92 | 177 | } |
f26a0012 | 178 | fprintf(s->fp, "\n"); |
367c9886 JS |
179 | strbuf_release(&onebuf); |
180 | strbuf_release(&twobuf); | |
c91f0d92 JK |
181 | } |
182 | ||
50b7e70f JH |
183 | static void wt_status_collect_changed_cb(struct diff_queue_struct *q, |
184 | struct diff_options *options, | |
185 | void *data) | |
c91f0d92 JK |
186 | { |
187 | struct wt_status *s = data; | |
c91f0d92 | 188 | int i; |
50b7e70f JH |
189 | |
190 | if (!q->nr) | |
191 | return; | |
192 | s->workdir_dirty = 1; | |
c91f0d92 | 193 | for (i = 0; i < q->nr; i++) { |
50b7e70f JH |
194 | struct diff_filepair *p; |
195 | struct string_list_item *it; | |
196 | struct wt_status_change_data *d; | |
197 | ||
198 | p = q->queue[i]; | |
199 | it = string_list_insert(p->one->path, &s->change); | |
200 | d = it->util; | |
201 | if (!d) { | |
202 | d = xcalloc(1, sizeof(*d)); | |
203 | it->util = d; | |
c91f0d92 | 204 | } |
50b7e70f JH |
205 | if (!d->worktree_status) |
206 | d->worktree_status = p->status; | |
c91f0d92 | 207 | } |
c91f0d92 JK |
208 | } |
209 | ||
4d4d5726 JH |
210 | static int unmerged_mask(const char *path) |
211 | { | |
212 | int pos, mask; | |
213 | struct cache_entry *ce; | |
214 | ||
215 | pos = cache_name_pos(path, strlen(path)); | |
216 | if (0 <= pos) | |
217 | return 0; | |
218 | ||
219 | mask = 0; | |
220 | pos = -pos-1; | |
221 | while (pos < active_nr) { | |
222 | ce = active_cache[pos++]; | |
223 | if (strcmp(ce->name, path) || !ce_stage(ce)) | |
224 | break; | |
225 | mask |= (1 << (ce_stage(ce) - 1)); | |
226 | } | |
227 | return mask; | |
228 | } | |
229 | ||
50b7e70f JH |
230 | static void wt_status_collect_updated_cb(struct diff_queue_struct *q, |
231 | struct diff_options *options, | |
232 | void *data) | |
c91f0d92 | 233 | { |
6e458bf6 | 234 | struct wt_status *s = data; |
c91f0d92 | 235 | int i; |
50b7e70f JH |
236 | |
237 | for (i = 0; i < q->nr; i++) { | |
238 | struct diff_filepair *p; | |
239 | struct string_list_item *it; | |
240 | struct wt_status_change_data *d; | |
241 | ||
242 | p = q->queue[i]; | |
243 | it = string_list_insert(p->two->path, &s->change); | |
244 | d = it->util; | |
245 | if (!d) { | |
246 | d = xcalloc(1, sizeof(*d)); | |
247 | it->util = d; | |
248 | } | |
249 | if (!d->index_status) | |
250 | d->index_status = p->status; | |
251 | switch (p->status) { | |
252 | case DIFF_STATUS_COPIED: | |
253 | case DIFF_STATUS_RENAMED: | |
254 | d->head_path = xstrdup(p->one->path); | |
255 | break; | |
4d4d5726 JH |
256 | case DIFF_STATUS_UNMERGED: |
257 | d->stagemask = unmerged_mask(p->two->path); | |
258 | break; | |
50b7e70f | 259 | } |
6e458bf6 | 260 | } |
c91f0d92 JK |
261 | } |
262 | ||
50b7e70f | 263 | static void wt_status_collect_changes_worktree(struct wt_status *s) |
c91f0d92 JK |
264 | { |
265 | struct rev_info rev; | |
50b7e70f JH |
266 | |
267 | init_revisions(&rev, NULL); | |
268 | setup_revisions(0, NULL, &rev, NULL); | |
269 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
270 | rev.diffopt.format_callback = wt_status_collect_changed_cb; | |
271 | rev.diffopt.format_callback_data = s; | |
272 | run_diff_files(&rev, 0); | |
273 | } | |
274 | ||
275 | static void wt_status_collect_changes_index(struct wt_status *s) | |
276 | { | |
277 | struct rev_info rev; | |
278 | ||
c91f0d92 | 279 | init_revisions(&rev, NULL); |
c1e255b7 JK |
280 | setup_revisions(0, NULL, &rev, |
281 | s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference); | |
c91f0d92 | 282 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
50b7e70f | 283 | rev.diffopt.format_callback = wt_status_collect_updated_cb; |
c91f0d92 JK |
284 | rev.diffopt.format_callback_data = s; |
285 | rev.diffopt.detect_rename = 1; | |
50705915 | 286 | rev.diffopt.rename_limit = 200; |
f714fb84 | 287 | rev.diffopt.break_opt = 0; |
c91f0d92 JK |
288 | run_diff_index(&rev, 1); |
289 | } | |
290 | ||
50b7e70f JH |
291 | static void wt_status_collect_changes_initial(struct wt_status *s) |
292 | { | |
293 | int i; | |
294 | ||
295 | for (i = 0; i < active_nr; i++) { | |
296 | struct string_list_item *it; | |
297 | struct wt_status_change_data *d; | |
298 | struct cache_entry *ce = active_cache[i]; | |
299 | ||
300 | it = string_list_insert(ce->name, &s->change); | |
301 | d = it->util; | |
302 | if (!d) { | |
303 | d = xcalloc(1, sizeof(*d)); | |
304 | it->util = d; | |
305 | } | |
4d4d5726 | 306 | if (ce_stage(ce)) { |
50b7e70f | 307 | d->index_status = DIFF_STATUS_UNMERGED; |
4d4d5726 JH |
308 | d->stagemask |= (1 << (ce_stage(ce) - 1)); |
309 | } | |
50b7e70f JH |
310 | else |
311 | d->index_status = DIFF_STATUS_ADDED; | |
312 | } | |
313 | } | |
314 | ||
76378683 JH |
315 | static void wt_status_collect_untracked(struct wt_status *s) |
316 | { | |
317 | int i; | |
318 | struct dir_struct dir; | |
319 | ||
320 | if (!s->show_untracked_files) | |
321 | return; | |
322 | memset(&dir, 0, sizeof(dir)); | |
323 | if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES) | |
324 | dir.flags |= | |
325 | DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; | |
326 | setup_standard_excludes(&dir); | |
327 | ||
328 | fill_directory(&dir, NULL); | |
329 | for(i = 0; i < dir.nr; i++) { | |
330 | struct dir_entry *ent = dir.entries[i]; | |
331 | if (!cache_name_is_other(ent->name, ent->len)) | |
332 | continue; | |
333 | s->workdir_untracked = 1; | |
334 | string_list_insert(ent->name, &s->untracked); | |
335 | } | |
336 | } | |
337 | ||
338 | void wt_status_collect(struct wt_status *s) | |
50b7e70f JH |
339 | { |
340 | wt_status_collect_changes_worktree(s); | |
341 | ||
342 | if (s->is_initial) | |
343 | wt_status_collect_changes_initial(s); | |
344 | else | |
345 | wt_status_collect_changes_index(s); | |
76378683 | 346 | wt_status_collect_untracked(s); |
50b7e70f JH |
347 | } |
348 | ||
4d4d5726 JH |
349 | static void wt_status_print_unmerged(struct wt_status *s) |
350 | { | |
351 | int shown_header = 0; | |
352 | int i; | |
353 | ||
354 | for (i = 0; i < s->change.nr; i++) { | |
355 | struct wt_status_change_data *d; | |
356 | struct string_list_item *it; | |
357 | it = &(s->change.items[i]); | |
358 | d = it->util; | |
359 | if (!d->stagemask) | |
360 | continue; | |
361 | if (!shown_header) { | |
362 | wt_status_print_unmerged_header(s); | |
363 | shown_header = 1; | |
364 | } | |
365 | wt_status_print_unmerged_data(s, it); | |
366 | } | |
367 | if (shown_header) | |
368 | wt_status_print_trailer(s); | |
369 | ||
370 | } | |
371 | ||
50b7e70f JH |
372 | static void wt_status_print_updated(struct wt_status *s) |
373 | { | |
374 | int shown_header = 0; | |
375 | int i; | |
376 | ||
377 | for (i = 0; i < s->change.nr; i++) { | |
378 | struct wt_status_change_data *d; | |
379 | struct string_list_item *it; | |
380 | it = &(s->change.items[i]); | |
381 | d = it->util; | |
382 | if (!d->index_status || | |
383 | d->index_status == DIFF_STATUS_UNMERGED) | |
384 | continue; | |
385 | if (!shown_header) { | |
386 | wt_status_print_cached_header(s); | |
387 | s->commitable = 1; | |
388 | shown_header = 1; | |
389 | } | |
390 | wt_status_print_change_data(s, WT_STATUS_UPDATED, it); | |
391 | } | |
392 | if (shown_header) | |
393 | wt_status_print_trailer(s); | |
394 | } | |
395 | ||
396 | /* | |
397 | * -1 : has delete | |
398 | * 0 : no change | |
399 | * 1 : some change but no delete | |
400 | */ | |
401 | static int wt_status_check_worktree_changes(struct wt_status *s) | |
402 | { | |
403 | int i; | |
404 | int changes = 0; | |
405 | ||
406 | for (i = 0; i < s->change.nr; i++) { | |
407 | struct wt_status_change_data *d; | |
408 | d = s->change.items[i].util; | |
4d4d5726 JH |
409 | if (!d->worktree_status || |
410 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f JH |
411 | continue; |
412 | changes = 1; | |
413 | if (d->worktree_status == DIFF_STATUS_DELETED) | |
414 | return -1; | |
415 | } | |
416 | return changes; | |
417 | } | |
418 | ||
c91f0d92 JK |
419 | static void wt_status_print_changed(struct wt_status *s) |
420 | { | |
50b7e70f JH |
421 | int i; |
422 | int worktree_changes = wt_status_check_worktree_changes(s); | |
423 | ||
424 | if (!worktree_changes) | |
425 | return; | |
426 | ||
427 | wt_status_print_dirty_header(s, worktree_changes < 0); | |
428 | ||
429 | for (i = 0; i < s->change.nr; i++) { | |
430 | struct wt_status_change_data *d; | |
431 | struct string_list_item *it; | |
432 | it = &(s->change.items[i]); | |
433 | d = it->util; | |
4d4d5726 JH |
434 | if (!d->worktree_status || |
435 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
50b7e70f JH |
436 | continue; |
437 | wt_status_print_change_data(s, WT_STATUS_CHANGED, it); | |
438 | } | |
439 | wt_status_print_trailer(s); | |
c91f0d92 JK |
440 | } |
441 | ||
ac8d5afc PY |
442 | static void wt_status_print_submodule_summary(struct wt_status *s) |
443 | { | |
444 | struct child_process sm_summary; | |
445 | char summary_limit[64]; | |
446 | char index[PATH_MAX]; | |
447 | const char *env[] = { index, NULL }; | |
448 | const char *argv[] = { | |
449 | "submodule", | |
450 | "summary", | |
451 | "--cached", | |
452 | "--for-status", | |
453 | "--summary-limit", | |
454 | summary_limit, | |
455 | s->amend ? "HEAD^" : "HEAD", | |
456 | NULL | |
457 | }; | |
458 | ||
d249b098 | 459 | sprintf(summary_limit, "%d", s->submodule_summary); |
ac8d5afc PY |
460 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file); |
461 | ||
462 | memset(&sm_summary, 0, sizeof(sm_summary)); | |
463 | sm_summary.argv = argv; | |
464 | sm_summary.env = env; | |
465 | sm_summary.git_cmd = 1; | |
466 | sm_summary.no_stdin = 1; | |
467 | fflush(s->fp); | |
468 | sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */ | |
469 | run_command(&sm_summary); | |
470 | } | |
471 | ||
6e458bf6 | 472 | static void wt_status_print_untracked(struct wt_status *s) |
c91f0d92 | 473 | { |
c91f0d92 | 474 | int i; |
f285a2d7 | 475 | struct strbuf buf = STRBUF_INIT; |
c91f0d92 | 476 | |
76378683 JH |
477 | if (!s->untracked.nr) |
478 | return; | |
c91f0d92 | 479 | |
76378683 JH |
480 | wt_status_print_untracked_header(s); |
481 | for (i = 0; i < s->untracked.nr; i++) { | |
482 | struct string_list_item *it; | |
483 | it = &(s->untracked.items[i]); | |
d249b098 JH |
484 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t"); |
485 | color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", | |
76378683 JH |
486 | quote_path(it->string, strlen(it->string), |
487 | &buf, s->prefix)); | |
c91f0d92 | 488 | } |
367c9886 | 489 | strbuf_release(&buf); |
c91f0d92 JK |
490 | } |
491 | ||
492 | static void wt_status_print_verbose(struct wt_status *s) | |
493 | { | |
494 | struct rev_info rev; | |
99a12694 | 495 | |
c91f0d92 | 496 | init_revisions(&rev, NULL); |
5ec11af6 | 497 | DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV); |
1324fb6f JK |
498 | setup_revisions(0, NULL, &rev, |
499 | s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference); | |
c91f0d92 JK |
500 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
501 | rev.diffopt.detect_rename = 1; | |
4ba0cb27 KH |
502 | rev.diffopt.file = s->fp; |
503 | rev.diffopt.close_file = 0; | |
4f672ad6 JK |
504 | /* |
505 | * If we're not going to stdout, then we definitely don't | |
506 | * want color, since we are going to the commit message | |
507 | * file (and even the "auto" setting won't work, since it | |
508 | * will have checked isatty on stdout). | |
509 | */ | |
510 | if (s->fp != stdout) | |
511 | DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF); | |
c91f0d92 JK |
512 | run_diff_index(&rev, 1); |
513 | } | |
514 | ||
b6975ab5 JH |
515 | static void wt_status_print_tracking(struct wt_status *s) |
516 | { | |
517 | struct strbuf sb = STRBUF_INIT; | |
518 | const char *cp, *ep; | |
519 | struct branch *branch; | |
520 | ||
521 | assert(s->branch && !s->is_initial); | |
522 | if (prefixcmp(s->branch, "refs/heads/")) | |
523 | return; | |
524 | branch = branch_get(s->branch + 11); | |
525 | if (!format_tracking_info(branch, &sb)) | |
526 | return; | |
527 | ||
528 | for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) | |
d249b098 | 529 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), |
b6975ab5 | 530 | "# %.*s", (int)(ep - cp), cp); |
d249b098 | 531 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); |
b6975ab5 JH |
532 | } |
533 | ||
c91f0d92 JK |
534 | void wt_status_print(struct wt_status *s) |
535 | { | |
98bf8a47 | 536 | unsigned char sha1[20]; |
d249b098 | 537 | const char *branch_color = color(WT_STATUS_HEADER, s); |
98bf8a47 | 538 | |
950ce2e2 | 539 | s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0; |
bda324cf JH |
540 | if (s->branch) { |
541 | const char *on_what = "On branch "; | |
542 | const char *branch_name = s->branch; | |
cc44c765 | 543 | if (!prefixcmp(branch_name, "refs/heads/")) |
bda324cf JH |
544 | branch_name += 11; |
545 | else if (!strcmp(branch_name, "HEAD")) { | |
546 | branch_name = ""; | |
d249b098 | 547 | branch_color = color(WT_STATUS_NOBRANCH, s); |
bda324cf JH |
548 | on_what = "Not currently on any branch."; |
549 | } | |
d249b098 | 550 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# "); |
950ce2e2 | 551 | color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name); |
b6975ab5 JH |
552 | if (!s->is_initial) |
553 | wt_status_print_tracking(s); | |
bda324cf | 554 | } |
c91f0d92 | 555 | |
76378683 | 556 | wt_status_collect(s); |
50b7e70f | 557 | |
c91f0d92 | 558 | if (s->is_initial) { |
d249b098 JH |
559 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); |
560 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit"); | |
561 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#"); | |
c91f0d92 JK |
562 | } |
563 | ||
4d4d5726 | 564 | wt_status_print_unmerged(s); |
c1e255b7 | 565 | wt_status_print_updated(s); |
c91f0d92 | 566 | wt_status_print_changed(s); |
d249b098 | 567 | if (s->submodule_summary) |
ac8d5afc | 568 | wt_status_print_submodule_summary(s); |
d249b098 | 569 | if (s->show_untracked_files) |
6c2ce048 MSO |
570 | wt_status_print_untracked(s); |
571 | else if (s->commitable) | |
572 | fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n"); | |
c91f0d92 | 573 | |
1324fb6f | 574 | if (s->verbose) |
c91f0d92 | 575 | wt_status_print_verbose(s); |
6e458bf6 JR |
576 | if (!s->commitable) { |
577 | if (s->amend) | |
f26a0012 | 578 | fprintf(s->fp, "# No changes\n"); |
37d07f8f JH |
579 | else if (s->nowarn) |
580 | ; /* nothing */ | |
2a3a3c24 | 581 | else if (s->workdir_dirty) |
dcbc7bbe | 582 | printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n"); |
76378683 | 583 | else if (s->untracked.nr) |
2a3a3c24 JR |
584 | printf("nothing added to commit but untracked files present (use \"git add\" to track)\n"); |
585 | else if (s->is_initial) | |
586 | printf("nothing to commit (create/copy files and use \"git add\" to track)\n"); | |
d249b098 | 587 | else if (!s->show_untracked_files) |
6c2ce048 | 588 | printf("nothing to commit (use -u to show untracked files)\n"); |
2a3a3c24 JR |
589 | else |
590 | printf("nothing to commit (working directory clean)\n"); | |
6e458bf6 | 591 | } |
c91f0d92 | 592 | } |