]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "wt-status.h" | |
3 | #include "object.h" | |
4 | #include "dir.h" | |
5 | #include "commit.h" | |
6 | #include "diff.h" | |
7 | #include "revision.h" | |
8 | #include "diffcore.h" | |
9 | #include "quote.h" | |
10 | #include "run-command.h" | |
11 | #include "remote.h" | |
12 | #include "refs.h" | |
13 | #include "submodule.h" | |
14 | #include "column.h" | |
15 | #include "strbuf.h" | |
16 | ||
17 | static char default_wt_status_colors[][COLOR_MAXLEN] = { | |
18 | GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */ | |
19 | GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */ | |
20 | GIT_COLOR_RED, /* WT_STATUS_CHANGED */ | |
21 | GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */ | |
22 | GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */ | |
23 | GIT_COLOR_RED, /* WT_STATUS_UNMERGED */ | |
24 | GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */ | |
25 | GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */ | |
26 | GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */ | |
27 | }; | |
28 | ||
29 | static const char *color(int slot, struct wt_status *s) | |
30 | { | |
31 | const char *c = ""; | |
32 | if (want_color(s->use_color)) | |
33 | c = s->color_palette[slot]; | |
34 | if (slot == WT_STATUS_ONBRANCH && color_is_nil(c)) | |
35 | c = s->color_palette[WT_STATUS_HEADER]; | |
36 | return c; | |
37 | } | |
38 | ||
39 | static void status_vprintf(struct wt_status *s, int at_bol, const char *color, | |
40 | const char *fmt, va_list ap, const char *trail) | |
41 | { | |
42 | struct strbuf sb = STRBUF_INIT; | |
43 | struct strbuf linebuf = STRBUF_INIT; | |
44 | const char *line, *eol; | |
45 | ||
46 | strbuf_vaddf(&sb, fmt, ap); | |
47 | if (!sb.len) { | |
48 | strbuf_addch(&sb, comment_line_char); | |
49 | if (!trail) | |
50 | strbuf_addch(&sb, ' '); | |
51 | color_print_strbuf(s->fp, color, &sb); | |
52 | if (trail) | |
53 | fprintf(s->fp, "%s", trail); | |
54 | strbuf_release(&sb); | |
55 | return; | |
56 | } | |
57 | for (line = sb.buf; *line; line = eol + 1) { | |
58 | eol = strchr(line, '\n'); | |
59 | ||
60 | strbuf_reset(&linebuf); | |
61 | if (at_bol) { | |
62 | strbuf_addch(&linebuf, comment_line_char); | |
63 | if (*line != '\n' && *line != '\t') | |
64 | strbuf_addch(&linebuf, ' '); | |
65 | } | |
66 | if (eol) | |
67 | strbuf_add(&linebuf, line, eol - line); | |
68 | else | |
69 | strbuf_addstr(&linebuf, line); | |
70 | color_print_strbuf(s->fp, color, &linebuf); | |
71 | if (eol) | |
72 | fprintf(s->fp, "\n"); | |
73 | else | |
74 | break; | |
75 | at_bol = 1; | |
76 | } | |
77 | if (trail) | |
78 | fprintf(s->fp, "%s", trail); | |
79 | strbuf_release(&linebuf); | |
80 | strbuf_release(&sb); | |
81 | } | |
82 | ||
83 | void status_printf_ln(struct wt_status *s, const char *color, | |
84 | const char *fmt, ...) | |
85 | { | |
86 | va_list ap; | |
87 | ||
88 | va_start(ap, fmt); | |
89 | status_vprintf(s, 1, color, fmt, ap, "\n"); | |
90 | va_end(ap); | |
91 | } | |
92 | ||
93 | void status_printf(struct wt_status *s, const char *color, | |
94 | const char *fmt, ...) | |
95 | { | |
96 | va_list ap; | |
97 | ||
98 | va_start(ap, fmt); | |
99 | status_vprintf(s, 1, color, fmt, ap, NULL); | |
100 | va_end(ap); | |
101 | } | |
102 | ||
103 | static void status_printf_more(struct wt_status *s, const char *color, | |
104 | const char *fmt, ...) | |
105 | { | |
106 | va_list ap; | |
107 | ||
108 | va_start(ap, fmt); | |
109 | status_vprintf(s, 0, color, fmt, ap, NULL); | |
110 | va_end(ap); | |
111 | } | |
112 | ||
113 | void wt_status_prepare(struct wt_status *s) | |
114 | { | |
115 | unsigned char sha1[20]; | |
116 | ||
117 | memset(s, 0, sizeof(*s)); | |
118 | memcpy(s->color_palette, default_wt_status_colors, | |
119 | sizeof(default_wt_status_colors)); | |
120 | s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; | |
121 | s->use_color = -1; | |
122 | s->relative_paths = 1; | |
123 | s->branch = resolve_refdup("HEAD", sha1, 0, NULL); | |
124 | s->reference = "HEAD"; | |
125 | s->fp = stdout; | |
126 | s->index_file = get_index_file(); | |
127 | s->change.strdup_strings = 1; | |
128 | s->untracked.strdup_strings = 1; | |
129 | s->ignored.strdup_strings = 1; | |
130 | s->show_branch = -1; /* unspecified */ | |
131 | } | |
132 | ||
133 | static void wt_status_print_unmerged_header(struct wt_status *s) | |
134 | { | |
135 | int i; | |
136 | int del_mod_conflict = 0; | |
137 | int both_deleted = 0; | |
138 | int not_deleted = 0; | |
139 | const char *c = color(WT_STATUS_HEADER, s); | |
140 | ||
141 | status_printf_ln(s, c, _("Unmerged paths:")); | |
142 | ||
143 | for (i = 0; i < s->change.nr; i++) { | |
144 | struct string_list_item *it = &(s->change.items[i]); | |
145 | struct wt_status_change_data *d = it->util; | |
146 | ||
147 | switch (d->stagemask) { | |
148 | case 0: | |
149 | break; | |
150 | case 1: | |
151 | both_deleted = 1; | |
152 | break; | |
153 | case 3: | |
154 | case 5: | |
155 | del_mod_conflict = 1; | |
156 | break; | |
157 | default: | |
158 | not_deleted = 1; | |
159 | break; | |
160 | } | |
161 | } | |
162 | ||
163 | if (!advice_status_hints) | |
164 | return; | |
165 | if (s->whence != FROM_COMMIT) | |
166 | ; | |
167 | else if (!s->is_initial) | |
168 | status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference); | |
169 | else | |
170 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); | |
171 | ||
172 | if (!both_deleted) { | |
173 | if (!del_mod_conflict) | |
174 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)")); | |
175 | else | |
176 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); | |
177 | } else if (!del_mod_conflict && !not_deleted) { | |
178 | status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)")); | |
179 | } else { | |
180 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)")); | |
181 | } | |
182 | status_printf_ln(s, c, ""); | |
183 | } | |
184 | ||
185 | static void wt_status_print_cached_header(struct wt_status *s) | |
186 | { | |
187 | const char *c = color(WT_STATUS_HEADER, s); | |
188 | ||
189 | status_printf_ln(s, c, _("Changes to be committed:")); | |
190 | if (!advice_status_hints) | |
191 | return; | |
192 | if (s->whence != FROM_COMMIT) | |
193 | ; /* NEEDSWORK: use "git reset --unresolve"??? */ | |
194 | else if (!s->is_initial) | |
195 | status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference); | |
196 | else | |
197 | status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)")); | |
198 | status_printf_ln(s, c, ""); | |
199 | } | |
200 | ||
201 | static void wt_status_print_dirty_header(struct wt_status *s, | |
202 | int has_deleted, | |
203 | int has_dirty_submodules) | |
204 | { | |
205 | const char *c = color(WT_STATUS_HEADER, s); | |
206 | ||
207 | status_printf_ln(s, c, _("Changes not staged for commit:")); | |
208 | if (!advice_status_hints) | |
209 | return; | |
210 | if (!has_deleted) | |
211 | status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)")); | |
212 | else | |
213 | status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)")); | |
214 | status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)")); | |
215 | if (has_dirty_submodules) | |
216 | status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)")); | |
217 | status_printf_ln(s, c, ""); | |
218 | } | |
219 | ||
220 | static void wt_status_print_other_header(struct wt_status *s, | |
221 | const char *what, | |
222 | const char *how) | |
223 | { | |
224 | const char *c = color(WT_STATUS_HEADER, s); | |
225 | status_printf_ln(s, c, "%s:", what); | |
226 | if (!advice_status_hints) | |
227 | return; | |
228 | status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how); | |
229 | status_printf_ln(s, c, ""); | |
230 | } | |
231 | ||
232 | static void wt_status_print_trailer(struct wt_status *s) | |
233 | { | |
234 | status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); | |
235 | } | |
236 | ||
237 | #define quote_path quote_path_relative | |
238 | ||
239 | static void wt_status_print_unmerged_data(struct wt_status *s, | |
240 | struct string_list_item *it) | |
241 | { | |
242 | const char *c = color(WT_STATUS_UNMERGED, s); | |
243 | struct wt_status_change_data *d = it->util; | |
244 | struct strbuf onebuf = STRBUF_INIT; | |
245 | const char *one, *how = _("bug"); | |
246 | ||
247 | one = quote_path(it->string, s->prefix, &onebuf); | |
248 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); | |
249 | switch (d->stagemask) { | |
250 | case 1: how = _("both deleted:"); break; | |
251 | case 2: how = _("added by us:"); break; | |
252 | case 3: how = _("deleted by them:"); break; | |
253 | case 4: how = _("added by them:"); break; | |
254 | case 5: how = _("deleted by us:"); break; | |
255 | case 6: how = _("both added:"); break; | |
256 | case 7: how = _("both modified:"); break; | |
257 | } | |
258 | status_printf_more(s, c, "%-20s%s\n", how, one); | |
259 | strbuf_release(&onebuf); | |
260 | } | |
261 | ||
262 | static void wt_status_print_change_data(struct wt_status *s, | |
263 | int change_type, | |
264 | struct string_list_item *it) | |
265 | { | |
266 | struct wt_status_change_data *d = it->util; | |
267 | const char *c = color(change_type, s); | |
268 | int status; | |
269 | char *one_name; | |
270 | char *two_name; | |
271 | const char *one, *two; | |
272 | struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT; | |
273 | struct strbuf extra = STRBUF_INIT; | |
274 | ||
275 | one_name = two_name = it->string; | |
276 | switch (change_type) { | |
277 | case WT_STATUS_UPDATED: | |
278 | status = d->index_status; | |
279 | if (d->head_path) | |
280 | one_name = d->head_path; | |
281 | break; | |
282 | case WT_STATUS_CHANGED: | |
283 | if (d->new_submodule_commits || d->dirty_submodule) { | |
284 | strbuf_addstr(&extra, " ("); | |
285 | if (d->new_submodule_commits) | |
286 | strbuf_addf(&extra, _("new commits, ")); | |
287 | if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) | |
288 | strbuf_addf(&extra, _("modified content, ")); | |
289 | if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) | |
290 | strbuf_addf(&extra, _("untracked content, ")); | |
291 | strbuf_setlen(&extra, extra.len - 2); | |
292 | strbuf_addch(&extra, ')'); | |
293 | } | |
294 | status = d->worktree_status; | |
295 | break; | |
296 | default: | |
297 | die("BUG: unhandled change_type %d in wt_status_print_change_data", | |
298 | change_type); | |
299 | } | |
300 | ||
301 | one = quote_path(one_name, s->prefix, &onebuf); | |
302 | two = quote_path(two_name, s->prefix, &twobuf); | |
303 | ||
304 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); | |
305 | switch (status) { | |
306 | case DIFF_STATUS_ADDED: | |
307 | status_printf_more(s, c, _("new file: %s"), one); | |
308 | break; | |
309 | case DIFF_STATUS_COPIED: | |
310 | status_printf_more(s, c, _("copied: %s -> %s"), one, two); | |
311 | break; | |
312 | case DIFF_STATUS_DELETED: | |
313 | status_printf_more(s, c, _("deleted: %s"), one); | |
314 | break; | |
315 | case DIFF_STATUS_MODIFIED: | |
316 | status_printf_more(s, c, _("modified: %s"), one); | |
317 | break; | |
318 | case DIFF_STATUS_RENAMED: | |
319 | status_printf_more(s, c, _("renamed: %s -> %s"), one, two); | |
320 | break; | |
321 | case DIFF_STATUS_TYPE_CHANGED: | |
322 | status_printf_more(s, c, _("typechange: %s"), one); | |
323 | break; | |
324 | case DIFF_STATUS_UNKNOWN: | |
325 | status_printf_more(s, c, _("unknown: %s"), one); | |
326 | break; | |
327 | case DIFF_STATUS_UNMERGED: | |
328 | status_printf_more(s, c, _("unmerged: %s"), one); | |
329 | break; | |
330 | default: | |
331 | die(_("bug: unhandled diff status %c"), status); | |
332 | } | |
333 | if (extra.len) { | |
334 | status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf); | |
335 | strbuf_release(&extra); | |
336 | } | |
337 | status_printf_more(s, GIT_COLOR_NORMAL, "\n"); | |
338 | strbuf_release(&onebuf); | |
339 | strbuf_release(&twobuf); | |
340 | } | |
341 | ||
342 | static void wt_status_collect_changed_cb(struct diff_queue_struct *q, | |
343 | struct diff_options *options, | |
344 | void *data) | |
345 | { | |
346 | struct wt_status *s = data; | |
347 | int i; | |
348 | ||
349 | if (!q->nr) | |
350 | return; | |
351 | s->workdir_dirty = 1; | |
352 | for (i = 0; i < q->nr; i++) { | |
353 | struct diff_filepair *p; | |
354 | struct string_list_item *it; | |
355 | struct wt_status_change_data *d; | |
356 | ||
357 | p = q->queue[i]; | |
358 | it = string_list_insert(&s->change, p->one->path); | |
359 | d = it->util; | |
360 | if (!d) { | |
361 | d = xcalloc(1, sizeof(*d)); | |
362 | it->util = d; | |
363 | } | |
364 | if (!d->worktree_status) | |
365 | d->worktree_status = p->status; | |
366 | d->dirty_submodule = p->two->dirty_submodule; | |
367 | if (S_ISGITLINK(p->two->mode)) | |
368 | d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1); | |
369 | } | |
370 | } | |
371 | ||
372 | static int unmerged_mask(const char *path) | |
373 | { | |
374 | int pos, mask; | |
375 | const struct cache_entry *ce; | |
376 | ||
377 | pos = cache_name_pos(path, strlen(path)); | |
378 | if (0 <= pos) | |
379 | return 0; | |
380 | ||
381 | mask = 0; | |
382 | pos = -pos-1; | |
383 | while (pos < active_nr) { | |
384 | ce = active_cache[pos++]; | |
385 | if (strcmp(ce->name, path) || !ce_stage(ce)) | |
386 | break; | |
387 | mask |= (1 << (ce_stage(ce) - 1)); | |
388 | } | |
389 | return mask; | |
390 | } | |
391 | ||
392 | static void wt_status_collect_updated_cb(struct diff_queue_struct *q, | |
393 | struct diff_options *options, | |
394 | void *data) | |
395 | { | |
396 | struct wt_status *s = data; | |
397 | int i; | |
398 | ||
399 | for (i = 0; i < q->nr; i++) { | |
400 | struct diff_filepair *p; | |
401 | struct string_list_item *it; | |
402 | struct wt_status_change_data *d; | |
403 | ||
404 | p = q->queue[i]; | |
405 | it = string_list_insert(&s->change, p->two->path); | |
406 | d = it->util; | |
407 | if (!d) { | |
408 | d = xcalloc(1, sizeof(*d)); | |
409 | it->util = d; | |
410 | } | |
411 | if (!d->index_status) | |
412 | d->index_status = p->status; | |
413 | switch (p->status) { | |
414 | case DIFF_STATUS_COPIED: | |
415 | case DIFF_STATUS_RENAMED: | |
416 | d->head_path = xstrdup(p->one->path); | |
417 | break; | |
418 | case DIFF_STATUS_UNMERGED: | |
419 | d->stagemask = unmerged_mask(p->two->path); | |
420 | break; | |
421 | } | |
422 | } | |
423 | } | |
424 | ||
425 | static void wt_status_collect_changes_worktree(struct wt_status *s) | |
426 | { | |
427 | struct rev_info rev; | |
428 | ||
429 | init_revisions(&rev, NULL); | |
430 | setup_revisions(0, NULL, &rev, NULL); | |
431 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
432 | DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES); | |
433 | if (!s->show_untracked_files) | |
434 | DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
435 | if (s->ignore_submodule_arg) { | |
436 | DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG); | |
437 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); | |
438 | } | |
439 | rev.diffopt.format_callback = wt_status_collect_changed_cb; | |
440 | rev.diffopt.format_callback_data = s; | |
441 | init_pathspec(&rev.prune_data, s->pathspec); | |
442 | run_diff_files(&rev, 0); | |
443 | } | |
444 | ||
445 | static void wt_status_collect_changes_index(struct wt_status *s) | |
446 | { | |
447 | struct rev_info rev; | |
448 | struct setup_revision_opt opt; | |
449 | ||
450 | init_revisions(&rev, NULL); | |
451 | memset(&opt, 0, sizeof(opt)); | |
452 | opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference; | |
453 | setup_revisions(0, NULL, &rev, &opt); | |
454 | ||
455 | if (s->ignore_submodule_arg) { | |
456 | DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG); | |
457 | handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg); | |
458 | } | |
459 | ||
460 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
461 | rev.diffopt.format_callback = wt_status_collect_updated_cb; | |
462 | rev.diffopt.format_callback_data = s; | |
463 | rev.diffopt.detect_rename = 1; | |
464 | rev.diffopt.rename_limit = 200; | |
465 | rev.diffopt.break_opt = 0; | |
466 | init_pathspec(&rev.prune_data, s->pathspec); | |
467 | run_diff_index(&rev, 1); | |
468 | } | |
469 | ||
470 | static void wt_status_collect_changes_initial(struct wt_status *s) | |
471 | { | |
472 | struct pathspec pathspec; | |
473 | int i; | |
474 | ||
475 | init_pathspec(&pathspec, s->pathspec); | |
476 | for (i = 0; i < active_nr; i++) { | |
477 | struct string_list_item *it; | |
478 | struct wt_status_change_data *d; | |
479 | const struct cache_entry *ce = active_cache[i]; | |
480 | ||
481 | if (!ce_path_match(ce, &pathspec)) | |
482 | continue; | |
483 | it = string_list_insert(&s->change, ce->name); | |
484 | d = it->util; | |
485 | if (!d) { | |
486 | d = xcalloc(1, sizeof(*d)); | |
487 | it->util = d; | |
488 | } | |
489 | if (ce_stage(ce)) { | |
490 | d->index_status = DIFF_STATUS_UNMERGED; | |
491 | d->stagemask |= (1 << (ce_stage(ce) - 1)); | |
492 | } | |
493 | else | |
494 | d->index_status = DIFF_STATUS_ADDED; | |
495 | } | |
496 | free_pathspec(&pathspec); | |
497 | } | |
498 | ||
499 | static void wt_status_collect_untracked(struct wt_status *s) | |
500 | { | |
501 | int i; | |
502 | struct dir_struct dir; | |
503 | struct timeval t_begin; | |
504 | ||
505 | if (!s->show_untracked_files) | |
506 | return; | |
507 | ||
508 | if (advice_status_u_option) | |
509 | gettimeofday(&t_begin, NULL); | |
510 | ||
511 | memset(&dir, 0, sizeof(dir)); | |
512 | if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES) | |
513 | dir.flags |= | |
514 | DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES; | |
515 | if (s->show_ignored_files) | |
516 | dir.flags |= DIR_SHOW_IGNORED_TOO; | |
517 | setup_standard_excludes(&dir); | |
518 | ||
519 | fill_directory(&dir, s->pathspec); | |
520 | ||
521 | for (i = 0; i < dir.nr; i++) { | |
522 | struct dir_entry *ent = dir.entries[i]; | |
523 | if (cache_name_is_other(ent->name, ent->len) && | |
524 | match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL)) | |
525 | string_list_insert(&s->untracked, ent->name); | |
526 | free(ent); | |
527 | } | |
528 | ||
529 | for (i = 0; i < dir.ignored_nr; i++) { | |
530 | struct dir_entry *ent = dir.ignored[i]; | |
531 | if (cache_name_is_other(ent->name, ent->len) && | |
532 | match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL)) | |
533 | string_list_insert(&s->ignored, ent->name); | |
534 | free(ent); | |
535 | } | |
536 | ||
537 | free(dir.entries); | |
538 | free(dir.ignored); | |
539 | clear_directory(&dir); | |
540 | ||
541 | if (advice_status_u_option) { | |
542 | struct timeval t_end; | |
543 | gettimeofday(&t_end, NULL); | |
544 | s->untracked_in_ms = | |
545 | (uint64_t)t_end.tv_sec * 1000 + t_end.tv_usec / 1000 - | |
546 | ((uint64_t)t_begin.tv_sec * 1000 + t_begin.tv_usec / 1000); | |
547 | } | |
548 | } | |
549 | ||
550 | void wt_status_collect(struct wt_status *s) | |
551 | { | |
552 | wt_status_collect_changes_worktree(s); | |
553 | ||
554 | if (s->is_initial) | |
555 | wt_status_collect_changes_initial(s); | |
556 | else | |
557 | wt_status_collect_changes_index(s); | |
558 | wt_status_collect_untracked(s); | |
559 | } | |
560 | ||
561 | static void wt_status_print_unmerged(struct wt_status *s) | |
562 | { | |
563 | int shown_header = 0; | |
564 | int i; | |
565 | ||
566 | for (i = 0; i < s->change.nr; i++) { | |
567 | struct wt_status_change_data *d; | |
568 | struct string_list_item *it; | |
569 | it = &(s->change.items[i]); | |
570 | d = it->util; | |
571 | if (!d->stagemask) | |
572 | continue; | |
573 | if (!shown_header) { | |
574 | wt_status_print_unmerged_header(s); | |
575 | shown_header = 1; | |
576 | } | |
577 | wt_status_print_unmerged_data(s, it); | |
578 | } | |
579 | if (shown_header) | |
580 | wt_status_print_trailer(s); | |
581 | ||
582 | } | |
583 | ||
584 | static void wt_status_print_updated(struct wt_status *s) | |
585 | { | |
586 | int shown_header = 0; | |
587 | int i; | |
588 | ||
589 | for (i = 0; i < s->change.nr; i++) { | |
590 | struct wt_status_change_data *d; | |
591 | struct string_list_item *it; | |
592 | it = &(s->change.items[i]); | |
593 | d = it->util; | |
594 | if (!d->index_status || | |
595 | d->index_status == DIFF_STATUS_UNMERGED) | |
596 | continue; | |
597 | if (!shown_header) { | |
598 | wt_status_print_cached_header(s); | |
599 | s->commitable = 1; | |
600 | shown_header = 1; | |
601 | } | |
602 | wt_status_print_change_data(s, WT_STATUS_UPDATED, it); | |
603 | } | |
604 | if (shown_header) | |
605 | wt_status_print_trailer(s); | |
606 | } | |
607 | ||
608 | /* | |
609 | * -1 : has delete | |
610 | * 0 : no change | |
611 | * 1 : some change but no delete | |
612 | */ | |
613 | static int wt_status_check_worktree_changes(struct wt_status *s, | |
614 | int *dirty_submodules) | |
615 | { | |
616 | int i; | |
617 | int changes = 0; | |
618 | ||
619 | *dirty_submodules = 0; | |
620 | ||
621 | for (i = 0; i < s->change.nr; i++) { | |
622 | struct wt_status_change_data *d; | |
623 | d = s->change.items[i].util; | |
624 | if (!d->worktree_status || | |
625 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
626 | continue; | |
627 | if (!changes) | |
628 | changes = 1; | |
629 | if (d->dirty_submodule) | |
630 | *dirty_submodules = 1; | |
631 | if (d->worktree_status == DIFF_STATUS_DELETED) | |
632 | changes = -1; | |
633 | } | |
634 | return changes; | |
635 | } | |
636 | ||
637 | static void wt_status_print_changed(struct wt_status *s) | |
638 | { | |
639 | int i, dirty_submodules; | |
640 | int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules); | |
641 | ||
642 | if (!worktree_changes) | |
643 | return; | |
644 | ||
645 | wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules); | |
646 | ||
647 | for (i = 0; i < s->change.nr; i++) { | |
648 | struct wt_status_change_data *d; | |
649 | struct string_list_item *it; | |
650 | it = &(s->change.items[i]); | |
651 | d = it->util; | |
652 | if (!d->worktree_status || | |
653 | d->worktree_status == DIFF_STATUS_UNMERGED) | |
654 | continue; | |
655 | wt_status_print_change_data(s, WT_STATUS_CHANGED, it); | |
656 | } | |
657 | wt_status_print_trailer(s); | |
658 | } | |
659 | ||
660 | static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted) | |
661 | { | |
662 | struct child_process sm_summary; | |
663 | char summary_limit[64]; | |
664 | char index[PATH_MAX]; | |
665 | const char *env[] = { NULL, NULL }; | |
666 | const char *argv[8]; | |
667 | ||
668 | env[0] = index; | |
669 | argv[0] = "submodule"; | |
670 | argv[1] = "summary"; | |
671 | argv[2] = uncommitted ? "--files" : "--cached"; | |
672 | argv[3] = "--for-status"; | |
673 | argv[4] = "--summary-limit"; | |
674 | argv[5] = summary_limit; | |
675 | argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"); | |
676 | argv[7] = NULL; | |
677 | ||
678 | sprintf(summary_limit, "%d", s->submodule_summary); | |
679 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file); | |
680 | ||
681 | memset(&sm_summary, 0, sizeof(sm_summary)); | |
682 | sm_summary.argv = argv; | |
683 | sm_summary.env = env; | |
684 | sm_summary.git_cmd = 1; | |
685 | sm_summary.no_stdin = 1; | |
686 | fflush(s->fp); | |
687 | sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */ | |
688 | run_command(&sm_summary); | |
689 | } | |
690 | ||
691 | static void wt_status_print_other(struct wt_status *s, | |
692 | struct string_list *l, | |
693 | const char *what, | |
694 | const char *how) | |
695 | { | |
696 | int i; | |
697 | struct strbuf buf = STRBUF_INIT; | |
698 | static struct string_list output = STRING_LIST_INIT_DUP; | |
699 | struct column_options copts; | |
700 | ||
701 | if (!l->nr) | |
702 | return; | |
703 | ||
704 | wt_status_print_other_header(s, what, how); | |
705 | ||
706 | for (i = 0; i < l->nr; i++) { | |
707 | struct string_list_item *it; | |
708 | const char *path; | |
709 | it = &(l->items[i]); | |
710 | path = quote_path(it->string, s->prefix, &buf); | |
711 | if (column_active(s->colopts)) { | |
712 | string_list_append(&output, path); | |
713 | continue; | |
714 | } | |
715 | status_printf(s, color(WT_STATUS_HEADER, s), "\t"); | |
716 | status_printf_more(s, color(WT_STATUS_UNTRACKED, s), | |
717 | "%s\n", path); | |
718 | } | |
719 | ||
720 | strbuf_release(&buf); | |
721 | if (!column_active(s->colopts)) | |
722 | return; | |
723 | ||
724 | strbuf_addf(&buf, "%s#\t%s", | |
725 | color(WT_STATUS_HEADER, s), | |
726 | color(WT_STATUS_UNTRACKED, s)); | |
727 | memset(&copts, 0, sizeof(copts)); | |
728 | copts.padding = 1; | |
729 | copts.indent = buf.buf; | |
730 | if (want_color(s->use_color)) | |
731 | copts.nl = GIT_COLOR_RESET "\n"; | |
732 | print_columns(&output, s->colopts, &copts); | |
733 | string_list_clear(&output, 0); | |
734 | strbuf_release(&buf); | |
735 | } | |
736 | ||
737 | static void wt_status_print_verbose(struct wt_status *s) | |
738 | { | |
739 | struct rev_info rev; | |
740 | struct setup_revision_opt opt; | |
741 | ||
742 | init_revisions(&rev, NULL); | |
743 | DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV); | |
744 | ||
745 | memset(&opt, 0, sizeof(opt)); | |
746 | opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference; | |
747 | setup_revisions(0, NULL, &rev, &opt); | |
748 | ||
749 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; | |
750 | rev.diffopt.detect_rename = 1; | |
751 | rev.diffopt.file = s->fp; | |
752 | rev.diffopt.close_file = 0; | |
753 | /* | |
754 | * If we're not going to stdout, then we definitely don't | |
755 | * want color, since we are going to the commit message | |
756 | * file (and even the "auto" setting won't work, since it | |
757 | * will have checked isatty on stdout). | |
758 | */ | |
759 | if (s->fp != stdout) | |
760 | rev.diffopt.use_color = 0; | |
761 | run_diff_index(&rev, 1); | |
762 | } | |
763 | ||
764 | static void wt_status_print_tracking(struct wt_status *s) | |
765 | { | |
766 | struct strbuf sb = STRBUF_INIT; | |
767 | const char *cp, *ep; | |
768 | struct branch *branch; | |
769 | ||
770 | assert(s->branch && !s->is_initial); | |
771 | if (prefixcmp(s->branch, "refs/heads/")) | |
772 | return; | |
773 | branch = branch_get(s->branch + 11); | |
774 | if (!format_tracking_info(branch, &sb)) | |
775 | return; | |
776 | ||
777 | for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1) | |
778 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), | |
779 | "%c %.*s", comment_line_char, | |
780 | (int)(ep - cp), cp); | |
781 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c", | |
782 | comment_line_char); | |
783 | } | |
784 | ||
785 | static int has_unmerged(struct wt_status *s) | |
786 | { | |
787 | int i; | |
788 | ||
789 | for (i = 0; i < s->change.nr; i++) { | |
790 | struct wt_status_change_data *d; | |
791 | d = s->change.items[i].util; | |
792 | if (d->stagemask) | |
793 | return 1; | |
794 | } | |
795 | return 0; | |
796 | } | |
797 | ||
798 | static void show_merge_in_progress(struct wt_status *s, | |
799 | struct wt_status_state *state, | |
800 | const char *color) | |
801 | { | |
802 | if (has_unmerged(s)) { | |
803 | status_printf_ln(s, color, _("You have unmerged paths.")); | |
804 | if (advice_status_hints) | |
805 | status_printf_ln(s, color, | |
806 | _(" (fix conflicts and run \"git commit\")")); | |
807 | } else { | |
808 | status_printf_ln(s, color, | |
809 | _("All conflicts fixed but you are still merging.")); | |
810 | if (advice_status_hints) | |
811 | status_printf_ln(s, color, | |
812 | _(" (use \"git commit\" to conclude merge)")); | |
813 | } | |
814 | wt_status_print_trailer(s); | |
815 | } | |
816 | ||
817 | static void show_am_in_progress(struct wt_status *s, | |
818 | struct wt_status_state *state, | |
819 | const char *color) | |
820 | { | |
821 | status_printf_ln(s, color, | |
822 | _("You are in the middle of an am session.")); | |
823 | if (state->am_empty_patch) | |
824 | status_printf_ln(s, color, | |
825 | _("The current patch is empty.")); | |
826 | if (advice_status_hints) { | |
827 | if (!state->am_empty_patch) | |
828 | status_printf_ln(s, color, | |
829 | _(" (fix conflicts and then run \"git am --continue\")")); | |
830 | status_printf_ln(s, color, | |
831 | _(" (use \"git am --skip\" to skip this patch)")); | |
832 | status_printf_ln(s, color, | |
833 | _(" (use \"git am --abort\" to restore the original branch)")); | |
834 | } | |
835 | wt_status_print_trailer(s); | |
836 | } | |
837 | ||
838 | static char *read_line_from_git_path(const char *filename) | |
839 | { | |
840 | struct strbuf buf = STRBUF_INIT; | |
841 | FILE *fp = fopen(git_path("%s", filename), "r"); | |
842 | if (!fp) { | |
843 | strbuf_release(&buf); | |
844 | return NULL; | |
845 | } | |
846 | strbuf_getline(&buf, fp, '\n'); | |
847 | if (!fclose(fp)) { | |
848 | return strbuf_detach(&buf, NULL); | |
849 | } else { | |
850 | strbuf_release(&buf); | |
851 | return NULL; | |
852 | } | |
853 | } | |
854 | ||
855 | static int split_commit_in_progress(struct wt_status *s) | |
856 | { | |
857 | int split_in_progress = 0; | |
858 | char *head = read_line_from_git_path("HEAD"); | |
859 | char *orig_head = read_line_from_git_path("ORIG_HEAD"); | |
860 | char *rebase_amend = read_line_from_git_path("rebase-merge/amend"); | |
861 | char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head"); | |
862 | ||
863 | if (!head || !orig_head || !rebase_amend || !rebase_orig_head || | |
864 | !s->branch || strcmp(s->branch, "HEAD")) | |
865 | return split_in_progress; | |
866 | ||
867 | if (!strcmp(rebase_amend, rebase_orig_head)) { | |
868 | if (strcmp(head, rebase_amend)) | |
869 | split_in_progress = 1; | |
870 | } else if (strcmp(orig_head, rebase_orig_head)) { | |
871 | split_in_progress = 1; | |
872 | } | |
873 | ||
874 | if (!s->amend && !s->nowarn && !s->workdir_dirty) | |
875 | split_in_progress = 0; | |
876 | ||
877 | free(head); | |
878 | free(orig_head); | |
879 | free(rebase_amend); | |
880 | free(rebase_orig_head); | |
881 | return split_in_progress; | |
882 | } | |
883 | ||
884 | static void show_rebase_in_progress(struct wt_status *s, | |
885 | struct wt_status_state *state, | |
886 | const char *color) | |
887 | { | |
888 | struct stat st; | |
889 | ||
890 | if (has_unmerged(s)) { | |
891 | if (state->branch) | |
892 | status_printf_ln(s, color, | |
893 | _("You are currently rebasing branch '%s' on '%s'."), | |
894 | state->branch, | |
895 | state->onto); | |
896 | else | |
897 | status_printf_ln(s, color, | |
898 | _("You are currently rebasing.")); | |
899 | if (advice_status_hints) { | |
900 | status_printf_ln(s, color, | |
901 | _(" (fix conflicts and then run \"git rebase --continue\")")); | |
902 | status_printf_ln(s, color, | |
903 | _(" (use \"git rebase --skip\" to skip this patch)")); | |
904 | status_printf_ln(s, color, | |
905 | _(" (use \"git rebase --abort\" to check out the original branch)")); | |
906 | } | |
907 | } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) { | |
908 | if (state->branch) | |
909 | status_printf_ln(s, color, | |
910 | _("You are currently rebasing branch '%s' on '%s'."), | |
911 | state->branch, | |
912 | state->onto); | |
913 | else | |
914 | status_printf_ln(s, color, | |
915 | _("You are currently rebasing.")); | |
916 | if (advice_status_hints) | |
917 | status_printf_ln(s, color, | |
918 | _(" (all conflicts fixed: run \"git rebase --continue\")")); | |
919 | } else if (split_commit_in_progress(s)) { | |
920 | if (state->branch) | |
921 | status_printf_ln(s, color, | |
922 | _("You are currently splitting a commit while rebasing branch '%s' on '%s'."), | |
923 | state->branch, | |
924 | state->onto); | |
925 | else | |
926 | status_printf_ln(s, color, | |
927 | _("You are currently splitting a commit during a rebase.")); | |
928 | if (advice_status_hints) | |
929 | status_printf_ln(s, color, | |
930 | _(" (Once your working directory is clean, run \"git rebase --continue\")")); | |
931 | } else { | |
932 | if (state->branch) | |
933 | status_printf_ln(s, color, | |
934 | _("You are currently editing a commit while rebasing branch '%s' on '%s'."), | |
935 | state->branch, | |
936 | state->onto); | |
937 | else | |
938 | status_printf_ln(s, color, | |
939 | _("You are currently editing a commit during a rebase.")); | |
940 | if (advice_status_hints && !s->amend) { | |
941 | status_printf_ln(s, color, | |
942 | _(" (use \"git commit --amend\" to amend the current commit)")); | |
943 | status_printf_ln(s, color, | |
944 | _(" (use \"git rebase --continue\" once you are satisfied with your changes)")); | |
945 | } | |
946 | } | |
947 | wt_status_print_trailer(s); | |
948 | } | |
949 | ||
950 | static void show_cherry_pick_in_progress(struct wt_status *s, | |
951 | struct wt_status_state *state, | |
952 | const char *color) | |
953 | { | |
954 | status_printf_ln(s, color, _("You are currently cherry-picking.")); | |
955 | if (advice_status_hints) { | |
956 | if (has_unmerged(s)) | |
957 | status_printf_ln(s, color, | |
958 | _(" (fix conflicts and run \"git cherry-pick --continue\")")); | |
959 | else | |
960 | status_printf_ln(s, color, | |
961 | _(" (all conflicts fixed: run \"git cherry-pick --continue\")")); | |
962 | status_printf_ln(s, color, | |
963 | _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)")); | |
964 | } | |
965 | wt_status_print_trailer(s); | |
966 | } | |
967 | ||
968 | static void show_revert_in_progress(struct wt_status *s, | |
969 | struct wt_status_state *state, | |
970 | const char *color) | |
971 | { | |
972 | status_printf_ln(s, color, _("You are currently reverting commit %s."), | |
973 | find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV)); | |
974 | if (advice_status_hints) { | |
975 | if (has_unmerged(s)) | |
976 | status_printf_ln(s, color, | |
977 | _(" (fix conflicts and run \"git revert --continue\")")); | |
978 | else | |
979 | status_printf_ln(s, color, | |
980 | _(" (all conflicts fixed: run \"git revert --continue\")")); | |
981 | status_printf_ln(s, color, | |
982 | _(" (use \"git revert --abort\" to cancel the revert operation)")); | |
983 | } | |
984 | wt_status_print_trailer(s); | |
985 | } | |
986 | ||
987 | static void show_bisect_in_progress(struct wt_status *s, | |
988 | struct wt_status_state *state, | |
989 | const char *color) | |
990 | { | |
991 | if (state->branch) | |
992 | status_printf_ln(s, color, | |
993 | _("You are currently bisecting, started from branch '%s'."), | |
994 | state->branch); | |
995 | else | |
996 | status_printf_ln(s, color, | |
997 | _("You are currently bisecting.")); | |
998 | if (advice_status_hints) | |
999 | status_printf_ln(s, color, | |
1000 | _(" (use \"git bisect reset\" to get back to the original branch)")); | |
1001 | wt_status_print_trailer(s); | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * Extract branch information from rebase/bisect | |
1006 | */ | |
1007 | static char *read_and_strip_branch(const char *path) | |
1008 | { | |
1009 | struct strbuf sb = STRBUF_INIT; | |
1010 | unsigned char sha1[20]; | |
1011 | ||
1012 | if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0) | |
1013 | goto got_nothing; | |
1014 | ||
1015 | while (&sb.len && sb.buf[sb.len - 1] == '\n') | |
1016 | strbuf_setlen(&sb, sb.len - 1); | |
1017 | if (!sb.len) | |
1018 | goto got_nothing; | |
1019 | if (!prefixcmp(sb.buf, "refs/heads/")) | |
1020 | strbuf_remove(&sb,0, strlen("refs/heads/")); | |
1021 | else if (!prefixcmp(sb.buf, "refs/")) | |
1022 | ; | |
1023 | else if (!get_sha1_hex(sb.buf, sha1)) { | |
1024 | const char *abbrev; | |
1025 | abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV); | |
1026 | strbuf_reset(&sb); | |
1027 | strbuf_addstr(&sb, abbrev); | |
1028 | } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */ | |
1029 | goto got_nothing; | |
1030 | else /* bisect */ | |
1031 | ; | |
1032 | return strbuf_detach(&sb, NULL); | |
1033 | ||
1034 | got_nothing: | |
1035 | strbuf_release(&sb); | |
1036 | return NULL; | |
1037 | } | |
1038 | ||
1039 | struct grab_1st_switch_cbdata { | |
1040 | struct strbuf buf; | |
1041 | unsigned char nsha1[20]; | |
1042 | }; | |
1043 | ||
1044 | static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1, | |
1045 | const char *email, unsigned long timestamp, int tz, | |
1046 | const char *message, void *cb_data) | |
1047 | { | |
1048 | struct grab_1st_switch_cbdata *cb = cb_data; | |
1049 | const char *target = NULL, *end; | |
1050 | ||
1051 | if (prefixcmp(message, "checkout: moving from ")) | |
1052 | return 0; | |
1053 | message += strlen("checkout: moving from "); | |
1054 | target = strstr(message, " to "); | |
1055 | if (!target) | |
1056 | return 0; | |
1057 | target += strlen(" to "); | |
1058 | strbuf_reset(&cb->buf); | |
1059 | hashcpy(cb->nsha1, nsha1); | |
1060 | for (end = target; *end && *end != '\n'; end++) | |
1061 | ; | |
1062 | strbuf_add(&cb->buf, target, end - target); | |
1063 | return 1; | |
1064 | } | |
1065 | ||
1066 | static void wt_status_get_detached_from(struct wt_status_state *state) | |
1067 | { | |
1068 | struct grab_1st_switch_cbdata cb; | |
1069 | struct commit *commit; | |
1070 | unsigned char sha1[20]; | |
1071 | char *ref = NULL; | |
1072 | ||
1073 | strbuf_init(&cb.buf, 0); | |
1074 | if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) { | |
1075 | strbuf_release(&cb.buf); | |
1076 | return; | |
1077 | } | |
1078 | ||
1079 | if (dwim_ref(cb.buf.buf, cb.buf.len, sha1, &ref) == 1 && | |
1080 | /* sha1 is a commit? match without further lookup */ | |
1081 | (!hashcmp(cb.nsha1, sha1) || | |
1082 | /* perhaps sha1 is a tag, try to dereference to a commit */ | |
1083 | ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL && | |
1084 | !hashcmp(cb.nsha1, commit->object.sha1)))) { | |
1085 | int ofs; | |
1086 | if (!prefixcmp(ref, "refs/tags/")) | |
1087 | ofs = strlen("refs/tags/"); | |
1088 | else if (!prefixcmp(ref, "refs/remotes/")) | |
1089 | ofs = strlen("refs/remotes/"); | |
1090 | else | |
1091 | ofs = 0; | |
1092 | state->detached_from = xstrdup(ref + ofs); | |
1093 | } else | |
1094 | state->detached_from = | |
1095 | xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV)); | |
1096 | hashcpy(state->detached_sha1, cb.nsha1); | |
1097 | ||
1098 | free(ref); | |
1099 | strbuf_release(&cb.buf); | |
1100 | } | |
1101 | ||
1102 | void wt_status_get_state(struct wt_status_state *state, | |
1103 | int get_detached_from) | |
1104 | { | |
1105 | struct stat st; | |
1106 | unsigned char sha1[20]; | |
1107 | ||
1108 | if (!stat(git_path("MERGE_HEAD"), &st)) { | |
1109 | state->merge_in_progress = 1; | |
1110 | } else if (!stat(git_path("rebase-apply"), &st)) { | |
1111 | if (!stat(git_path("rebase-apply/applying"), &st)) { | |
1112 | state->am_in_progress = 1; | |
1113 | if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size) | |
1114 | state->am_empty_patch = 1; | |
1115 | } else { | |
1116 | state->rebase_in_progress = 1; | |
1117 | state->branch = read_and_strip_branch("rebase-apply/head-name"); | |
1118 | state->onto = read_and_strip_branch("rebase-apply/onto"); | |
1119 | } | |
1120 | } else if (!stat(git_path("rebase-merge"), &st)) { | |
1121 | if (!stat(git_path("rebase-merge/interactive"), &st)) | |
1122 | state->rebase_interactive_in_progress = 1; | |
1123 | else | |
1124 | state->rebase_in_progress = 1; | |
1125 | state->branch = read_and_strip_branch("rebase-merge/head-name"); | |
1126 | state->onto = read_and_strip_branch("rebase-merge/onto"); | |
1127 | } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) { | |
1128 | state->cherry_pick_in_progress = 1; | |
1129 | } | |
1130 | if (!stat(git_path("BISECT_LOG"), &st)) { | |
1131 | state->bisect_in_progress = 1; | |
1132 | state->branch = read_and_strip_branch("BISECT_START"); | |
1133 | } | |
1134 | if (!stat(git_path("REVERT_HEAD"), &st) && | |
1135 | !get_sha1("REVERT_HEAD", sha1)) { | |
1136 | state->revert_in_progress = 1; | |
1137 | hashcpy(state->revert_head_sha1, sha1); | |
1138 | } | |
1139 | ||
1140 | if (get_detached_from) | |
1141 | wt_status_get_detached_from(state); | |
1142 | } | |
1143 | ||
1144 | static void wt_status_print_state(struct wt_status *s, | |
1145 | struct wt_status_state *state) | |
1146 | { | |
1147 | const char *state_color = color(WT_STATUS_HEADER, s); | |
1148 | if (state->merge_in_progress) | |
1149 | show_merge_in_progress(s, state, state_color); | |
1150 | else if (state->am_in_progress) | |
1151 | show_am_in_progress(s, state, state_color); | |
1152 | else if (state->rebase_in_progress || state->rebase_interactive_in_progress) | |
1153 | show_rebase_in_progress(s, state, state_color); | |
1154 | else if (state->cherry_pick_in_progress) | |
1155 | show_cherry_pick_in_progress(s, state, state_color); | |
1156 | else if (state->revert_in_progress) | |
1157 | show_revert_in_progress(s, state, state_color); | |
1158 | if (state->bisect_in_progress) | |
1159 | show_bisect_in_progress(s, state, state_color); | |
1160 | } | |
1161 | ||
1162 | void wt_status_print(struct wt_status *s) | |
1163 | { | |
1164 | const char *branch_color = color(WT_STATUS_ONBRANCH, s); | |
1165 | const char *branch_status_color = color(WT_STATUS_HEADER, s); | |
1166 | struct wt_status_state state; | |
1167 | ||
1168 | memset(&state, 0, sizeof(state)); | |
1169 | wt_status_get_state(&state, | |
1170 | s->branch && !strcmp(s->branch, "HEAD")); | |
1171 | ||
1172 | if (s->branch) { | |
1173 | const char *on_what = _("On branch "); | |
1174 | const char *branch_name = s->branch; | |
1175 | if (!prefixcmp(branch_name, "refs/heads/")) | |
1176 | branch_name += 11; | |
1177 | else if (!strcmp(branch_name, "HEAD")) { | |
1178 | branch_status_color = color(WT_STATUS_NOBRANCH, s); | |
1179 | if (state.rebase_in_progress || state.rebase_interactive_in_progress) { | |
1180 | on_what = _("rebase in progress; onto "); | |
1181 | branch_name = state.onto; | |
1182 | } else if (state.detached_from) { | |
1183 | unsigned char sha1[20]; | |
1184 | branch_name = state.detached_from; | |
1185 | if (!get_sha1("HEAD", sha1) && | |
1186 | !hashcmp(sha1, state.detached_sha1)) | |
1187 | on_what = _("HEAD detached at "); | |
1188 | else | |
1189 | on_what = _("HEAD detached from "); | |
1190 | } else { | |
1191 | branch_name = ""; | |
1192 | on_what = _("Not currently on any branch."); | |
1193 | } | |
1194 | } | |
1195 | status_printf(s, color(WT_STATUS_HEADER, s), ""); | |
1196 | status_printf_more(s, branch_status_color, "%s", on_what); | |
1197 | status_printf_more(s, branch_color, "%s\n", branch_name); | |
1198 | if (!s->is_initial) | |
1199 | wt_status_print_tracking(s); | |
1200 | } | |
1201 | ||
1202 | wt_status_print_state(s, &state); | |
1203 | free(state.branch); | |
1204 | free(state.onto); | |
1205 | free(state.detached_from); | |
1206 | ||
1207 | if (s->is_initial) { | |
1208 | status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); | |
1209 | status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit")); | |
1210 | status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); | |
1211 | } | |
1212 | ||
1213 | wt_status_print_updated(s); | |
1214 | wt_status_print_unmerged(s); | |
1215 | wt_status_print_changed(s); | |
1216 | if (s->submodule_summary && | |
1217 | (!s->ignore_submodule_arg || | |
1218 | strcmp(s->ignore_submodule_arg, "all"))) { | |
1219 | wt_status_print_submodule_summary(s, 0); /* staged */ | |
1220 | wt_status_print_submodule_summary(s, 1); /* unstaged */ | |
1221 | } | |
1222 | if (s->show_untracked_files) { | |
1223 | wt_status_print_other(s, &s->untracked, _("Untracked files"), "add"); | |
1224 | if (s->show_ignored_files) | |
1225 | wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f"); | |
1226 | if (advice_status_u_option && 2000 < s->untracked_in_ms) { | |
1227 | status_printf_ln(s, GIT_COLOR_NORMAL, ""); | |
1228 | status_printf_ln(s, GIT_COLOR_NORMAL, | |
1229 | _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n" | |
1230 | "may speed it up, but you have to be careful not to forget to add\n" | |
1231 | "new files yourself (see 'git help status')."), | |
1232 | s->untracked_in_ms / 1000.0); | |
1233 | } | |
1234 | } else if (s->commitable) | |
1235 | status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"), | |
1236 | advice_status_hints | |
1237 | ? _(" (use -u option to show untracked files)") : ""); | |
1238 | ||
1239 | if (s->verbose) | |
1240 | wt_status_print_verbose(s); | |
1241 | if (!s->commitable) { | |
1242 | if (s->amend) | |
1243 | status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes")); | |
1244 | else if (s->nowarn) | |
1245 | ; /* nothing */ | |
1246 | else if (s->workdir_dirty) { | |
1247 | if (advice_status_hints) | |
1248 | printf(_("no changes added to commit " | |
1249 | "(use \"git add\" and/or \"git commit -a\")\n")); | |
1250 | else | |
1251 | printf(_("no changes added to commit\n")); | |
1252 | } else if (s->untracked.nr) { | |
1253 | if (advice_status_hints) | |
1254 | printf(_("nothing added to commit but untracked files " | |
1255 | "present (use \"git add\" to track)\n")); | |
1256 | else | |
1257 | printf(_("nothing added to commit but untracked files present\n")); | |
1258 | } else if (s->is_initial) { | |
1259 | if (advice_status_hints) | |
1260 | printf(_("nothing to commit (create/copy files " | |
1261 | "and use \"git add\" to track)\n")); | |
1262 | else | |
1263 | printf(_("nothing to commit\n")); | |
1264 | } else if (!s->show_untracked_files) { | |
1265 | if (advice_status_hints) | |
1266 | printf(_("nothing to commit (use -u to show untracked files)\n")); | |
1267 | else | |
1268 | printf(_("nothing to commit\n")); | |
1269 | } else | |
1270 | printf(_("nothing to commit, working directory clean\n")); | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | static void wt_shortstatus_unmerged(struct string_list_item *it, | |
1275 | struct wt_status *s) | |
1276 | { | |
1277 | struct wt_status_change_data *d = it->util; | |
1278 | const char *how = "??"; | |
1279 | ||
1280 | switch (d->stagemask) { | |
1281 | case 1: how = "DD"; break; /* both deleted */ | |
1282 | case 2: how = "AU"; break; /* added by us */ | |
1283 | case 3: how = "UD"; break; /* deleted by them */ | |
1284 | case 4: how = "UA"; break; /* added by them */ | |
1285 | case 5: how = "DU"; break; /* deleted by us */ | |
1286 | case 6: how = "AA"; break; /* both added */ | |
1287 | case 7: how = "UU"; break; /* both modified */ | |
1288 | } | |
1289 | color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how); | |
1290 | if (s->null_termination) { | |
1291 | fprintf(stdout, " %s%c", it->string, 0); | |
1292 | } else { | |
1293 | struct strbuf onebuf = STRBUF_INIT; | |
1294 | const char *one; | |
1295 | one = quote_path(it->string, s->prefix, &onebuf); | |
1296 | printf(" %s\n", one); | |
1297 | strbuf_release(&onebuf); | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | static void wt_shortstatus_status(struct string_list_item *it, | |
1302 | struct wt_status *s) | |
1303 | { | |
1304 | struct wt_status_change_data *d = it->util; | |
1305 | ||
1306 | if (d->index_status) | |
1307 | color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status); | |
1308 | else | |
1309 | putchar(' '); | |
1310 | if (d->worktree_status) | |
1311 | color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status); | |
1312 | else | |
1313 | putchar(' '); | |
1314 | putchar(' '); | |
1315 | if (s->null_termination) { | |
1316 | fprintf(stdout, "%s%c", it->string, 0); | |
1317 | if (d->head_path) | |
1318 | fprintf(stdout, "%s%c", d->head_path, 0); | |
1319 | } else { | |
1320 | struct strbuf onebuf = STRBUF_INIT; | |
1321 | const char *one; | |
1322 | if (d->head_path) { | |
1323 | one = quote_path(d->head_path, s->prefix, &onebuf); | |
1324 | if (*one != '"' && strchr(one, ' ') != NULL) { | |
1325 | putchar('"'); | |
1326 | strbuf_addch(&onebuf, '"'); | |
1327 | one = onebuf.buf; | |
1328 | } | |
1329 | printf("%s -> ", one); | |
1330 | strbuf_release(&onebuf); | |
1331 | } | |
1332 | one = quote_path(it->string, s->prefix, &onebuf); | |
1333 | if (*one != '"' && strchr(one, ' ') != NULL) { | |
1334 | putchar('"'); | |
1335 | strbuf_addch(&onebuf, '"'); | |
1336 | one = onebuf.buf; | |
1337 | } | |
1338 | printf("%s\n", one); | |
1339 | strbuf_release(&onebuf); | |
1340 | } | |
1341 | } | |
1342 | ||
1343 | static void wt_shortstatus_other(struct string_list_item *it, | |
1344 | struct wt_status *s, const char *sign) | |
1345 | { | |
1346 | if (s->null_termination) { | |
1347 | fprintf(stdout, "%s %s%c", sign, it->string, 0); | |
1348 | } else { | |
1349 | struct strbuf onebuf = STRBUF_INIT; | |
1350 | const char *one; | |
1351 | one = quote_path(it->string, s->prefix, &onebuf); | |
1352 | color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign); | |
1353 | printf(" %s\n", one); | |
1354 | strbuf_release(&onebuf); | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | static void wt_shortstatus_print_tracking(struct wt_status *s) | |
1359 | { | |
1360 | struct branch *branch; | |
1361 | const char *header_color = color(WT_STATUS_HEADER, s); | |
1362 | const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s); | |
1363 | const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s); | |
1364 | ||
1365 | const char *base; | |
1366 | const char *branch_name; | |
1367 | int num_ours, num_theirs; | |
1368 | int upstream_is_gone = 0; | |
1369 | ||
1370 | color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## "); | |
1371 | ||
1372 | if (!s->branch) | |
1373 | return; | |
1374 | branch_name = s->branch; | |
1375 | ||
1376 | if (!prefixcmp(branch_name, "refs/heads/")) | |
1377 | branch_name += 11; | |
1378 | else if (!strcmp(branch_name, "HEAD")) { | |
1379 | branch_name = _("HEAD (no branch)"); | |
1380 | branch_color_local = color(WT_STATUS_NOBRANCH, s); | |
1381 | } | |
1382 | ||
1383 | branch = branch_get(s->branch + 11); | |
1384 | if (s->is_initial) | |
1385 | color_fprintf(s->fp, header_color, _("Initial commit on ")); | |
1386 | ||
1387 | color_fprintf(s->fp, branch_color_local, "%s", branch_name); | |
1388 | ||
1389 | switch (stat_tracking_info(branch, &num_ours, &num_theirs)) { | |
1390 | case 0: | |
1391 | /* no base */ | |
1392 | fputc(s->null_termination ? '\0' : '\n', s->fp); | |
1393 | return; | |
1394 | case -1: | |
1395 | /* with "gone" base */ | |
1396 | upstream_is_gone = 1; | |
1397 | break; | |
1398 | default: | |
1399 | /* Stop reporting if neither side has changes. */ | |
1400 | if (!num_ours && !num_theirs) { | |
1401 | fputc(s->null_termination ? '\0' : '\n', s->fp); | |
1402 | return; | |
1403 | } | |
1404 | /* with base */ | |
1405 | break; | |
1406 | } | |
1407 | ||
1408 | base = branch->merge[0]->dst; | |
1409 | base = shorten_unambiguous_ref(base, 0); | |
1410 | color_fprintf(s->fp, header_color, "..."); | |
1411 | color_fprintf(s->fp, branch_color_remote, "%s", base); | |
1412 | ||
1413 | color_fprintf(s->fp, header_color, " ["); | |
1414 | if (upstream_is_gone) { | |
1415 | color_fprintf(s->fp, header_color, _("gone")); | |
1416 | } else if (!num_ours) { | |
1417 | color_fprintf(s->fp, header_color, _("behind ")); | |
1418 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); | |
1419 | } else if (!num_theirs) { | |
1420 | color_fprintf(s->fp, header_color, _("ahead ")); | |
1421 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); | |
1422 | } else { | |
1423 | color_fprintf(s->fp, header_color, _("ahead ")); | |
1424 | color_fprintf(s->fp, branch_color_local, "%d", num_ours); | |
1425 | color_fprintf(s->fp, header_color, _(", behind ")); | |
1426 | color_fprintf(s->fp, branch_color_remote, "%d", num_theirs); | |
1427 | } | |
1428 | ||
1429 | color_fprintf(s->fp, header_color, "]"); | |
1430 | fputc(s->null_termination ? '\0' : '\n', s->fp); | |
1431 | } | |
1432 | ||
1433 | void wt_shortstatus_print(struct wt_status *s) | |
1434 | { | |
1435 | int i; | |
1436 | ||
1437 | if (s->show_branch) | |
1438 | wt_shortstatus_print_tracking(s); | |
1439 | ||
1440 | for (i = 0; i < s->change.nr; i++) { | |
1441 | struct wt_status_change_data *d; | |
1442 | struct string_list_item *it; | |
1443 | ||
1444 | it = &(s->change.items[i]); | |
1445 | d = it->util; | |
1446 | if (d->stagemask) | |
1447 | wt_shortstatus_unmerged(it, s); | |
1448 | else | |
1449 | wt_shortstatus_status(it, s); | |
1450 | } | |
1451 | for (i = 0; i < s->untracked.nr; i++) { | |
1452 | struct string_list_item *it; | |
1453 | ||
1454 | it = &(s->untracked.items[i]); | |
1455 | wt_shortstatus_other(it, s, "??"); | |
1456 | } | |
1457 | for (i = 0; i < s->ignored.nr; i++) { | |
1458 | struct string_list_item *it; | |
1459 | ||
1460 | it = &(s->ignored.items[i]); | |
1461 | wt_shortstatus_other(it, s, "!!"); | |
1462 | } | |
1463 | } | |
1464 | ||
1465 | void wt_porcelain_print(struct wt_status *s) | |
1466 | { | |
1467 | s->use_color = 0; | |
1468 | s->relative_paths = 0; | |
1469 | s->prefix = NULL; | |
1470 | wt_shortstatus_print(s); | |
1471 | } |