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