Fix memory leak in --submodule-summary
[git/dscho.git] / wt-status.c
blob3c2f58072d699a6f4e195e3aa650afb6b44fac6b
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 rev.prune_data = s->pathspec;
281 run_diff_files(&rev, 0);
284 static void wt_status_collect_changes_index(struct wt_status *s)
286 struct rev_info rev;
288 init_revisions(&rev, NULL);
289 setup_revisions(0, NULL, &rev,
290 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
291 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
292 rev.diffopt.format_callback = wt_status_collect_updated_cb;
293 rev.diffopt.format_callback_data = s;
294 rev.diffopt.detect_rename = 1;
295 rev.diffopt.rename_limit = 200;
296 rev.diffopt.break_opt = 0;
297 rev.prune_data = s->pathspec;
298 run_diff_index(&rev, 1);
301 static void wt_status_collect_changes_initial(struct wt_status *s)
303 int i;
305 for (i = 0; i < active_nr; i++) {
306 struct string_list_item *it;
307 struct wt_status_change_data *d;
308 struct cache_entry *ce = active_cache[i];
310 if (!ce_path_match(ce, s->pathspec))
311 continue;
312 it = string_list_insert(ce->name, &s->change);
313 d = it->util;
314 if (!d) {
315 d = xcalloc(1, sizeof(*d));
316 it->util = d;
318 if (ce_stage(ce)) {
319 d->index_status = DIFF_STATUS_UNMERGED;
320 d->stagemask |= (1 << (ce_stage(ce) - 1));
322 else
323 d->index_status = DIFF_STATUS_ADDED;
327 static void wt_status_collect_untracked(struct wt_status *s)
329 int i;
330 struct dir_struct dir;
332 if (!s->show_untracked_files)
333 return;
334 memset(&dir, 0, sizeof(dir));
335 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
336 dir.flags |=
337 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
338 setup_standard_excludes(&dir);
340 fill_directory(&dir, NULL);
341 for (i = 0; i < dir.nr; i++) {
342 struct dir_entry *ent = dir.entries[i];
343 if (!cache_name_is_other(ent->name, ent->len))
344 continue;
345 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
346 continue;
347 s->workdir_untracked = 1;
348 string_list_insert(ent->name, &s->untracked);
352 void wt_status_collect(struct wt_status *s)
354 wt_status_collect_changes_worktree(s);
356 if (s->is_initial)
357 wt_status_collect_changes_initial(s);
358 else
359 wt_status_collect_changes_index(s);
360 wt_status_collect_untracked(s);
363 static void wt_status_print_unmerged(struct wt_status *s)
365 int shown_header = 0;
366 int i;
368 for (i = 0; i < s->change.nr; i++) {
369 struct wt_status_change_data *d;
370 struct string_list_item *it;
371 it = &(s->change.items[i]);
372 d = it->util;
373 if (!d->stagemask)
374 continue;
375 if (!shown_header) {
376 wt_status_print_unmerged_header(s);
377 shown_header = 1;
379 wt_status_print_unmerged_data(s, it);
381 if (shown_header)
382 wt_status_print_trailer(s);
386 static void wt_status_print_updated(struct wt_status *s)
388 int shown_header = 0;
389 int i;
391 for (i = 0; i < s->change.nr; i++) {
392 struct wt_status_change_data *d;
393 struct string_list_item *it;
394 it = &(s->change.items[i]);
395 d = it->util;
396 if (!d->index_status ||
397 d->index_status == DIFF_STATUS_UNMERGED)
398 continue;
399 if (!shown_header) {
400 wt_status_print_cached_header(s);
401 s->commitable = 1;
402 shown_header = 1;
404 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
406 if (shown_header)
407 wt_status_print_trailer(s);
411 * -1 : has delete
412 * 0 : no change
413 * 1 : some change but no delete
415 static int wt_status_check_worktree_changes(struct wt_status *s)
417 int i;
418 int changes = 0;
420 for (i = 0; i < s->change.nr; i++) {
421 struct wt_status_change_data *d;
422 d = s->change.items[i].util;
423 if (!d->worktree_status ||
424 d->worktree_status == DIFF_STATUS_UNMERGED)
425 continue;
426 changes = 1;
427 if (d->worktree_status == DIFF_STATUS_DELETED)
428 return -1;
430 return changes;
433 static void wt_status_print_changed(struct wt_status *s)
435 int i;
436 int worktree_changes = wt_status_check_worktree_changes(s);
438 if (!worktree_changes)
439 return;
441 wt_status_print_dirty_header(s, worktree_changes < 0);
443 for (i = 0; i < s->change.nr; i++) {
444 struct wt_status_change_data *d;
445 struct string_list_item *it;
446 it = &(s->change.items[i]);
447 d = it->util;
448 if (!d->worktree_status ||
449 d->worktree_status == DIFF_STATUS_UNMERGED)
450 continue;
451 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
453 wt_status_print_trailer(s);
456 static void wt_status_print_submodule_summary(struct wt_status *s)
458 struct child_process sm_summary;
459 char summary_limit[64];
460 char index[PATH_MAX];
461 const char *env[] = { index, NULL };
462 const char *argv[] = {
463 "submodule",
464 "summary",
465 "--cached",
466 "--for-status",
467 "--summary-limit",
468 summary_limit,
469 s->amend ? "HEAD^" : "HEAD",
470 NULL
473 sprintf(summary_limit, "%d", s->submodule_summary);
474 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
476 memset(&sm_summary, 0, sizeof(sm_summary));
477 sm_summary.argv = argv;
478 sm_summary.env = env;
479 sm_summary.git_cmd = 1;
480 sm_summary.no_stdin = 1;
481 fflush(s->fp);
482 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
483 run_command(&sm_summary);
486 static void wt_status_print_untracked(struct wt_status *s)
488 int i;
489 struct strbuf buf = STRBUF_INIT;
491 if (!s->untracked.nr)
492 return;
494 wt_status_print_untracked_header(s);
495 for (i = 0; i < s->untracked.nr; i++) {
496 struct string_list_item *it;
497 it = &(s->untracked.items[i]);
498 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
499 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
500 quote_path(it->string, strlen(it->string),
501 &buf, s->prefix));
503 strbuf_release(&buf);
506 static void wt_status_print_verbose(struct wt_status *s)
508 struct rev_info rev;
510 init_revisions(&rev, NULL);
511 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
512 setup_revisions(0, NULL, &rev,
513 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
514 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
515 rev.diffopt.detect_rename = 1;
516 rev.diffopt.file = s->fp;
517 rev.diffopt.close_file = 0;
519 * If we're not going to stdout, then we definitely don't
520 * want color, since we are going to the commit message
521 * file (and even the "auto" setting won't work, since it
522 * will have checked isatty on stdout).
524 if (s->fp != stdout)
525 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
526 run_diff_index(&rev, 1);
529 static void wt_status_print_tracking(struct wt_status *s)
531 struct strbuf sb = STRBUF_INIT;
532 const char *cp, *ep;
533 struct branch *branch;
535 assert(s->branch && !s->is_initial);
536 if (prefixcmp(s->branch, "refs/heads/"))
537 return;
538 branch = branch_get(s->branch + 11);
539 if (!format_tracking_info(branch, &sb))
540 return;
542 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
543 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
544 "# %.*s", (int)(ep - cp), cp);
545 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
548 void wt_status_print(struct wt_status *s)
550 const char *branch_color = color(WT_STATUS_HEADER, s);
552 if (s->branch) {
553 const char *on_what = "On branch ";
554 const char *branch_name = s->branch;
555 if (!prefixcmp(branch_name, "refs/heads/"))
556 branch_name += 11;
557 else if (!strcmp(branch_name, "HEAD")) {
558 branch_name = "";
559 branch_color = color(WT_STATUS_NOBRANCH, s);
560 on_what = "Not currently on any branch.";
562 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
563 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
564 if (!s->is_initial)
565 wt_status_print_tracking(s);
568 if (s->is_initial) {
569 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
570 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
571 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
574 wt_status_print_updated(s);
575 wt_status_print_unmerged(s);
576 wt_status_print_changed(s);
577 if (s->submodule_summary)
578 wt_status_print_submodule_summary(s);
579 if (s->show_untracked_files)
580 wt_status_print_untracked(s);
581 else if (s->commitable)
582 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
584 if (s->verbose)
585 wt_status_print_verbose(s);
586 if (!s->commitable) {
587 if (s->amend)
588 fprintf(s->fp, "# No changes\n");
589 else if (s->nowarn)
590 ; /* nothing */
591 else if (s->workdir_dirty)
592 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
593 else if (s->untracked.nr)
594 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
595 else if (s->is_initial)
596 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
597 else if (!s->show_untracked_files)
598 printf("nothing to commit (use -u to show untracked files)\n");
599 else
600 printf("nothing to commit (working directory clean)\n");