bash: update 'git stash' completion
[git/jnareb-git/bp-gitweb.git] / wt-status.c
blob38eb24536b34e40af4c43eb1631e08624e8993d6
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"
13 static char default_wt_status_colors[][COLOR_MAXLEN] = {
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 */
19 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
22 static const char *color(int slot, struct wt_status *s)
24 return s->use_color > 0 ? s->color_palette[slot] : "";
27 void wt_status_prepare(struct wt_status *s)
29 unsigned char sha1[20];
30 const char *head;
32 memset(s, 0, sizeof(*s));
33 memcpy(s->color_palette, default_wt_status_colors,
34 sizeof(default_wt_status_colors));
35 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
36 s->use_color = -1;
37 s->relative_paths = 1;
38 head = resolve_ref("HEAD", sha1, 0, NULL);
39 s->branch = head ? xstrdup(head) : NULL;
40 s->reference = "HEAD";
41 s->fp = stdout;
42 s->index_file = get_index_file();
43 s->change.strdup_strings = 1;
44 s->untracked.strdup_strings = 1;
47 static void wt_status_print_unmerged_header(struct wt_status *s)
49 const char *c = color(WT_STATUS_HEADER, s);
50 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
51 if (!advice_status_hints)
52 return;
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, "#");
61 static void wt_status_print_cached_header(struct wt_status *s)
63 const char *c = color(WT_STATUS_HEADER, s);
64 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
65 if (!advice_status_hints)
66 return;
67 if (!s->is_initial) {
68 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
69 } else {
70 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
72 color_fprintf_ln(s->fp, c, "#");
75 static void wt_status_print_dirty_header(struct wt_status *s,
76 int has_deleted)
78 const char *c = color(WT_STATUS_HEADER, s);
79 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
80 if (!advice_status_hints)
81 return;
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)");
86 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
87 color_fprintf_ln(s->fp, c, "#");
90 static void wt_status_print_untracked_header(struct wt_status *s)
92 const char *c = color(WT_STATUS_HEADER, s);
93 color_fprintf_ln(s->fp, c, "# Untracked files:");
94 if (!advice_status_hints)
95 return;
96 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
97 color_fprintf_ln(s->fp, c, "#");
100 static void wt_status_print_trailer(struct wt_status *s)
102 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
105 #define quote_path quote_path_relative
107 static void wt_status_print_unmerged_data(struct wt_status *s,
108 struct string_list_item *it)
110 const char *c = color(WT_STATUS_UNMERGED, s);
111 struct wt_status_change_data *d = it->util;
112 struct strbuf onebuf = STRBUF_INIT;
113 const char *one, *how = "bug";
115 one = quote_path(it->string, -1, &onebuf, s->prefix);
116 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
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;
126 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
127 strbuf_release(&onebuf);
130 static void wt_status_print_change_data(struct wt_status *s,
131 int change_type,
132 struct string_list_item *it)
134 struct wt_status_change_data *d = it->util;
135 const char *c = color(change_type, s);
136 int status = status;
137 char *one_name;
138 char *two_name;
139 const char *one, *two;
140 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
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;
154 one = quote_path(one_name, -1, &onebuf, s->prefix);
155 two = quote_path(two_name, -1, &twobuf, s->prefix);
157 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
158 switch (status) {
159 case DIFF_STATUS_ADDED:
160 color_fprintf(s->fp, c, "new file: %s", one);
161 break;
162 case DIFF_STATUS_COPIED:
163 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
164 break;
165 case DIFF_STATUS_DELETED:
166 color_fprintf(s->fp, c, "deleted: %s", one);
167 break;
168 case DIFF_STATUS_MODIFIED:
169 color_fprintf(s->fp, c, "modified: %s", one);
170 break;
171 case DIFF_STATUS_RENAMED:
172 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
173 break;
174 case DIFF_STATUS_TYPE_CHANGED:
175 color_fprintf(s->fp, c, "typechange: %s", one);
176 break;
177 case DIFF_STATUS_UNKNOWN:
178 color_fprintf(s->fp, c, "unknown: %s", one);
179 break;
180 case DIFF_STATUS_UNMERGED:
181 color_fprintf(s->fp, c, "unmerged: %s", one);
182 break;
183 default:
184 die("bug: unhandled diff status %c", status);
186 fprintf(s->fp, "\n");
187 strbuf_release(&onebuf);
188 strbuf_release(&twobuf);
191 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
192 struct diff_options *options,
193 void *data)
195 struct wt_status *s = data;
196 int i;
198 if (!q->nr)
199 return;
200 s->workdir_dirty = 1;
201 for (i = 0; i < q->nr; i++) {
202 struct diff_filepair *p;
203 struct string_list_item *it;
204 struct wt_status_change_data *d;
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;
213 if (!d->worktree_status)
214 d->worktree_status = p->status;
218 static int unmerged_mask(const char *path)
220 int pos, mask;
221 struct cache_entry *ce;
223 pos = cache_name_pos(path, strlen(path));
224 if (0 <= pos)
225 return 0;
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));
235 return mask;
238 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
239 struct diff_options *options,
240 void *data)
242 struct wt_status *s = data;
243 int i;
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;
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;
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;
264 case DIFF_STATUS_UNMERGED:
265 d->stagemask = unmerged_mask(p->two->path);
266 break;
271 static void wt_status_collect_changes_worktree(struct wt_status *s)
273 struct rev_info rev;
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);
283 static void wt_status_collect_changes_index(struct wt_status *s)
285 struct rev_info rev;
287 init_revisions(&rev, NULL);
288 setup_revisions(0, NULL, &rev,
289 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
290 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
291 rev.diffopt.format_callback = wt_status_collect_updated_cb;
292 rev.diffopt.format_callback_data = s;
293 rev.diffopt.detect_rename = 1;
294 rev.diffopt.rename_limit = 200;
295 rev.diffopt.break_opt = 0;
296 run_diff_index(&rev, 1);
299 static void wt_status_collect_changes_initial(struct wt_status *s)
301 int i;
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];
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;
314 if (ce_stage(ce)) {
315 d->index_status = DIFF_STATUS_UNMERGED;
316 d->stagemask |= (1 << (ce_stage(ce) - 1));
318 else
319 d->index_status = DIFF_STATUS_ADDED;
323 static void wt_status_collect_untracked(struct wt_status *s)
325 int i;
326 struct dir_struct dir;
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);
336 fill_directory(&dir, NULL);
337 for (i = 0; i < dir.nr; i++) {
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);
346 void wt_status_collect(struct wt_status *s)
348 wt_status_collect_changes_worktree(s);
350 if (s->is_initial)
351 wt_status_collect_changes_initial(s);
352 else
353 wt_status_collect_changes_index(s);
354 wt_status_collect_untracked(s);
357 static void wt_status_print_unmerged(struct wt_status *s)
359 int shown_header = 0;
360 int i;
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;
373 wt_status_print_unmerged_data(s, it);
375 if (shown_header)
376 wt_status_print_trailer(s);
380 static void wt_status_print_updated(struct wt_status *s)
382 int shown_header = 0;
383 int i;
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;
398 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
400 if (shown_header)
401 wt_status_print_trailer(s);
405 * -1 : has delete
406 * 0 : no change
407 * 1 : some change but no delete
409 static int wt_status_check_worktree_changes(struct wt_status *s)
411 int i;
412 int changes = 0;
414 for (i = 0; i < s->change.nr; i++) {
415 struct wt_status_change_data *d;
416 d = s->change.items[i].util;
417 if (!d->worktree_status ||
418 d->worktree_status == DIFF_STATUS_UNMERGED)
419 continue;
420 changes = 1;
421 if (d->worktree_status == DIFF_STATUS_DELETED)
422 return -1;
424 return changes;
427 static void wt_status_print_changed(struct wt_status *s)
429 int i;
430 int worktree_changes = wt_status_check_worktree_changes(s);
432 if (!worktree_changes)
433 return;
435 wt_status_print_dirty_header(s, worktree_changes < 0);
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;
442 if (!d->worktree_status ||
443 d->worktree_status == DIFF_STATUS_UNMERGED)
444 continue;
445 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
447 wt_status_print_trailer(s);
450 static void wt_status_print_submodule_summary(struct wt_status *s)
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
467 sprintf(summary_limit, "%d", s->submodule_summary);
468 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
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);
480 static void wt_status_print_untracked(struct wt_status *s)
482 int i;
483 struct strbuf buf = STRBUF_INIT;
485 if (!s->untracked.nr)
486 return;
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]);
492 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
493 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
494 quote_path(it->string, strlen(it->string),
495 &buf, s->prefix));
497 strbuf_release(&buf);
500 static void wt_status_print_verbose(struct wt_status *s)
502 struct rev_info rev;
504 init_revisions(&rev, NULL);
505 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
506 setup_revisions(0, NULL, &rev,
507 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
508 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
509 rev.diffopt.detect_rename = 1;
510 rev.diffopt.file = s->fp;
511 rev.diffopt.close_file = 0;
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).
518 if (s->fp != stdout)
519 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
520 run_diff_index(&rev, 1);
523 static void wt_status_print_tracking(struct wt_status *s)
525 struct strbuf sb = STRBUF_INIT;
526 const char *cp, *ep;
527 struct branch *branch;
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;
536 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
537 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
538 "# %.*s", (int)(ep - cp), cp);
539 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
542 void wt_status_print(struct wt_status *s)
544 unsigned char sha1[20];
545 const char *branch_color = color(WT_STATUS_HEADER, s);
547 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
548 if (s->branch) {
549 const char *on_what = "On branch ";
550 const char *branch_name = s->branch;
551 if (!prefixcmp(branch_name, "refs/heads/"))
552 branch_name += 11;
553 else if (!strcmp(branch_name, "HEAD")) {
554 branch_name = "";
555 branch_color = color(WT_STATUS_NOBRANCH, s);
556 on_what = "Not currently on any branch.";
558 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
559 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
560 if (!s->is_initial)
561 wt_status_print_tracking(s);
564 wt_status_collect(s);
566 if (s->is_initial) {
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), "#");
572 wt_status_print_updated(s);
573 wt_status_print_unmerged(s);
574 wt_status_print_changed(s);
575 if (s->submodule_summary)
576 wt_status_print_submodule_summary(s);
577 if (s->show_untracked_files)
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");
582 if (s->verbose)
583 wt_status_print_verbose(s);
584 if (!s->commitable) {
585 if (s->amend)
586 fprintf(s->fp, "# No changes\n");
587 else if (s->nowarn)
588 ; /* nothing */
589 else if (s->workdir_dirty)
590 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
591 else if (s->untracked.nr)
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");
595 else if (!s->show_untracked_files)
596 printf("nothing to commit (use -u to show untracked files)\n");
597 else
598 printf("nothing to commit (working directory clean)\n");