string_list: Fix argument order for string_list_insert
[git/git.git] / wt-status.c
CommitLineData
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 13static 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 22static 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
27void 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;
6cb3f6b2 45 s->ignored.strdup_strings = 1;
c91f0d92
JK
46}
47
4d4d5726
JH
48static void wt_status_print_unmerged_header(struct wt_status *s)
49{
d249b098 50 const char *c = color(WT_STATUS_HEADER, s);
3c588453 51
4d4d5726 52 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
edf563fb
JK
53 if (!advice_status_hints)
54 return;
3c588453
JH
55 if (s->in_merge)
56 ;
57 else if (!s->is_initial)
4d4d5726
JH
58 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
59 else
60 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
dd20f8af 61 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" as appropriate to mark resolution)");
4d4d5726
JH
62 color_fprintf_ln(s->fp, c, "#");
63}
64
f26a0012 65static void wt_status_print_cached_header(struct wt_status *s)
3c1eb9cb 66{
d249b098 67 const char *c = color(WT_STATUS_HEADER, s);
3c588453 68
f26a0012 69 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
edf563fb
JK
70 if (!advice_status_hints)
71 return;
3c588453
JH
72 if (s->in_merge)
73 ; /* NEEDSWORK: use "git reset --unresolve"??? */
74 else if (!s->is_initial)
f26a0012 75 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
3c588453 76 else
f26a0012 77 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
f26a0012 78 color_fprintf_ln(s->fp, c, "#");
3c1eb9cb
JR
79}
80
bb914b14 81static void wt_status_print_dirty_header(struct wt_status *s,
9297f77e
JL
82 int has_deleted,
83 int has_dirty_submodules)
c91f0d92 84{
d249b098 85 const char *c = color(WT_STATUS_HEADER, s);
3c588453 86
bb914b14 87 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
edf563fb
JK
88 if (!advice_status_hints)
89 return;
bb914b14
AM
90 if (!has_deleted)
91 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
92 else
93 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
4d6e4c4d 94 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
9297f77e
JL
95 if (has_dirty_submodules)
96 color_fprintf_ln(s->fp, c, "# (commit or discard the untracked or modified content in submodules)");
bb914b14
AM
97 color_fprintf_ln(s->fp, c, "#");
98}
99
1b908b6f
JH
100static void wt_status_print_other_header(struct wt_status *s,
101 const char *what,
102 const char *how)
bb914b14 103{
d249b098 104 const char *c = color(WT_STATUS_HEADER, s);
1b908b6f 105 color_fprintf_ln(s->fp, c, "# %s files:", what);
edf563fb
JK
106 if (!advice_status_hints)
107 return;
1b908b6f 108 color_fprintf_ln(s->fp, c, "# (use \"git %s <file>...\" to include in what will be committed)", how);
f26a0012 109 color_fprintf_ln(s->fp, c, "#");
c91f0d92
JK
110}
111
f26a0012 112static void wt_status_print_trailer(struct wt_status *s)
c91f0d92 113{
d249b098 114 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
c91f0d92
JK
115}
116
a734d0b1 117#define quote_path quote_path_relative
3a946802 118
4d4d5726
JH
119static void wt_status_print_unmerged_data(struct wt_status *s,
120 struct string_list_item *it)
121{
d249b098 122 const char *c = color(WT_STATUS_UNMERGED, s);
4d4d5726
JH
123 struct wt_status_change_data *d = it->util;
124 struct strbuf onebuf = STRBUF_INIT;
125 const char *one, *how = "bug";
126
127 one = quote_path(it->string, -1, &onebuf, s->prefix);
d249b098 128 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
4d4d5726
JH
129 switch (d->stagemask) {
130 case 1: how = "both deleted:"; break;
131 case 2: how = "added by us:"; break;
132 case 3: how = "deleted by them:"; break;
133 case 4: how = "added by them:"; break;
134 case 5: how = "deleted by us:"; break;
135 case 6: how = "both added:"; break;
136 case 7: how = "both modified:"; break;
137 }
138 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
139 strbuf_release(&onebuf);
140}
141
50b7e70f
JH
142static void wt_status_print_change_data(struct wt_status *s,
143 int change_type,
144 struct string_list_item *it)
c91f0d92 145{
50b7e70f 146 struct wt_status_change_data *d = it->util;
d249b098 147 const char *c = color(change_type, s);
50b7e70f
JH
148 int status = status;
149 char *one_name;
150 char *two_name;
3a946802 151 const char *one, *two;
f285a2d7 152 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
9297f77e 153 struct strbuf extra = STRBUF_INIT;
3a946802 154
50b7e70f
JH
155 one_name = two_name = it->string;
156 switch (change_type) {
157 case WT_STATUS_UPDATED:
158 status = d->index_status;
159 if (d->head_path)
160 one_name = d->head_path;
161 break;
162 case WT_STATUS_CHANGED:
9297f77e
JL
163 if (d->new_submodule_commits || d->dirty_submodule) {
164 strbuf_addstr(&extra, " (");
165 if (d->new_submodule_commits)
166 strbuf_addf(&extra, "new commits, ");
167 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
168 strbuf_addf(&extra, "modified content, ");
169 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
170 strbuf_addf(&extra, "untracked content, ");
171 strbuf_setlen(&extra, extra.len - 2);
172 strbuf_addch(&extra, ')');
173 }
50b7e70f
JH
174 status = d->worktree_status;
175 break;
176 }
177
178 one = quote_path(one_name, -1, &onebuf, s->prefix);
179 two = quote_path(two_name, -1, &twobuf, s->prefix);
3a946802 180
d249b098 181 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
50b7e70f 182 switch (status) {
c91f0d92 183 case DIFF_STATUS_ADDED:
f26a0012 184 color_fprintf(s->fp, c, "new file: %s", one);
3a946802 185 break;
c91f0d92 186 case DIFF_STATUS_COPIED:
f26a0012 187 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
c91f0d92
JK
188 break;
189 case DIFF_STATUS_DELETED:
f26a0012 190 color_fprintf(s->fp, c, "deleted: %s", one);
3a946802 191 break;
c91f0d92 192 case DIFF_STATUS_MODIFIED:
f26a0012 193 color_fprintf(s->fp, c, "modified: %s", one);
3a946802 194 break;
c91f0d92 195 case DIFF_STATUS_RENAMED:
f26a0012 196 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
c91f0d92
JK
197 break;
198 case DIFF_STATUS_TYPE_CHANGED:
f26a0012 199 color_fprintf(s->fp, c, "typechange: %s", one);
3a946802 200 break;
c91f0d92 201 case DIFF_STATUS_UNKNOWN:
f26a0012 202 color_fprintf(s->fp, c, "unknown: %s", one);
3a946802 203 break;
c91f0d92 204 case DIFF_STATUS_UNMERGED:
f26a0012 205 color_fprintf(s->fp, c, "unmerged: %s", one);
3a946802 206 break;
c91f0d92 207 default:
50b7e70f 208 die("bug: unhandled diff status %c", status);
c91f0d92 209 }
9297f77e
JL
210 if (extra.len) {
211 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
212 strbuf_release(&extra);
213 }
f26a0012 214 fprintf(s->fp, "\n");
367c9886
JS
215 strbuf_release(&onebuf);
216 strbuf_release(&twobuf);
c91f0d92
JK
217}
218
50b7e70f
JH
219static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
220 struct diff_options *options,
221 void *data)
c91f0d92
JK
222{
223 struct wt_status *s = data;
c91f0d92 224 int i;
50b7e70f
JH
225
226 if (!q->nr)
227 return;
228 s->workdir_dirty = 1;
c91f0d92 229 for (i = 0; i < q->nr; i++) {
50b7e70f
JH
230 struct diff_filepair *p;
231 struct string_list_item *it;
232 struct wt_status_change_data *d;
233
234 p = q->queue[i];
78a395d3 235 it = string_list_insert(&s->change, p->one->path);
50b7e70f
JH
236 d = it->util;
237 if (!d) {
238 d = xcalloc(1, sizeof(*d));
239 it->util = d;
c91f0d92 240 }
50b7e70f
JH
241 if (!d->worktree_status)
242 d->worktree_status = p->status;
9297f77e
JL
243 d->dirty_submodule = p->two->dirty_submodule;
244 if (S_ISGITLINK(p->two->mode))
245 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
c91f0d92 246 }
c91f0d92
JK
247}
248
4d4d5726
JH
249static int unmerged_mask(const char *path)
250{
251 int pos, mask;
252 struct cache_entry *ce;
253
254 pos = cache_name_pos(path, strlen(path));
255 if (0 <= pos)
256 return 0;
257
258 mask = 0;
259 pos = -pos-1;
260 while (pos < active_nr) {
261 ce = active_cache[pos++];
262 if (strcmp(ce->name, path) || !ce_stage(ce))
263 break;
264 mask |= (1 << (ce_stage(ce) - 1));
265 }
266 return mask;
267}
268
50b7e70f
JH
269static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
270 struct diff_options *options,
271 void *data)
c91f0d92 272{
6e458bf6 273 struct wt_status *s = data;
c91f0d92 274 int i;
50b7e70f
JH
275
276 for (i = 0; i < q->nr; i++) {
277 struct diff_filepair *p;
278 struct string_list_item *it;
279 struct wt_status_change_data *d;
280
281 p = q->queue[i];
78a395d3 282 it = string_list_insert(&s->change, p->two->path);
50b7e70f
JH
283 d = it->util;
284 if (!d) {
285 d = xcalloc(1, sizeof(*d));
286 it->util = d;
287 }
288 if (!d->index_status)
289 d->index_status = p->status;
290 switch (p->status) {
291 case DIFF_STATUS_COPIED:
292 case DIFF_STATUS_RENAMED:
293 d->head_path = xstrdup(p->one->path);
294 break;
4d4d5726
JH
295 case DIFF_STATUS_UNMERGED:
296 d->stagemask = unmerged_mask(p->two->path);
297 break;
50b7e70f 298 }
6e458bf6 299 }
c91f0d92
JK
300}
301
50b7e70f 302static void wt_status_collect_changes_worktree(struct wt_status *s)
c91f0d92
JK
303{
304 struct rev_info rev;
50b7e70f
JH
305
306 init_revisions(&rev, NULL);
307 setup_revisions(0, NULL, &rev, NULL);
308 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
9297f77e 309 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
3bfc4504
JL
310 if (!s->show_untracked_files)
311 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
50b7e70f
JH
312 rev.diffopt.format_callback = wt_status_collect_changed_cb;
313 rev.diffopt.format_callback_data = s;
76e2f7ce 314 rev.prune_data = s->pathspec;
50b7e70f
JH
315 run_diff_files(&rev, 0);
316}
317
318static void wt_status_collect_changes_index(struct wt_status *s)
319{
320 struct rev_info rev;
32962c9b 321 struct setup_revision_opt opt;
50b7e70f 322
c91f0d92 323 init_revisions(&rev, NULL);
32962c9b
JH
324 memset(&opt, 0, sizeof(opt));
325 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
326 setup_revisions(0, NULL, &rev, &opt);
327
c91f0d92 328 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
50b7e70f 329 rev.diffopt.format_callback = wt_status_collect_updated_cb;
c91f0d92
JK
330 rev.diffopt.format_callback_data = s;
331 rev.diffopt.detect_rename = 1;
50705915 332 rev.diffopt.rename_limit = 200;
f714fb84 333 rev.diffopt.break_opt = 0;
76e2f7ce 334 rev.prune_data = s->pathspec;
c91f0d92
JK
335 run_diff_index(&rev, 1);
336}
337
50b7e70f
JH
338static void wt_status_collect_changes_initial(struct wt_status *s)
339{
340 int i;
341
342 for (i = 0; i < active_nr; i++) {
343 struct string_list_item *it;
344 struct wt_status_change_data *d;
345 struct cache_entry *ce = active_cache[i];
346
76e2f7ce
JH
347 if (!ce_path_match(ce, s->pathspec))
348 continue;
78a395d3 349 it = string_list_insert(&s->change, ce->name);
50b7e70f
JH
350 d = it->util;
351 if (!d) {
352 d = xcalloc(1, sizeof(*d));
353 it->util = d;
354 }
4d4d5726 355 if (ce_stage(ce)) {
50b7e70f 356 d->index_status = DIFF_STATUS_UNMERGED;
4d4d5726
JH
357 d->stagemask |= (1 << (ce_stage(ce) - 1));
358 }
50b7e70f
JH
359 else
360 d->index_status = DIFF_STATUS_ADDED;
361 }
362}
363
76378683
JH
364static void wt_status_collect_untracked(struct wt_status *s)
365{
366 int i;
367 struct dir_struct dir;
368
369 if (!s->show_untracked_files)
370 return;
371 memset(&dir, 0, sizeof(dir));
372 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
373 dir.flags |=
374 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
375 setup_standard_excludes(&dir);
376
688cd6d2 377 fill_directory(&dir, s->pathspec);
eeefa7c9 378 for (i = 0; i < dir.nr; i++) {
76378683
JH
379 struct dir_entry *ent = dir.entries[i];
380 if (!cache_name_is_other(ent->name, ent->len))
381 continue;
76e2f7ce
JH
382 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
383 continue;
78a395d3 384 string_list_insert(&s->untracked, ent->name);
f5b26b1d 385 free(ent);
76378683 386 }
f5b26b1d 387
6cb3f6b2
JH
388 if (s->show_ignored_files) {
389 dir.nr = 0;
390 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
391 fill_directory(&dir, s->pathspec);
392 for (i = 0; i < dir.nr; i++) {
393 struct dir_entry *ent = dir.entries[i];
394 if (!cache_name_is_other(ent->name, ent->len))
395 continue;
396 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
397 continue;
78a395d3 398 string_list_insert(&s->ignored, ent->name);
6cb3f6b2
JH
399 free(ent);
400 }
401 }
402
f5b26b1d 403 free(dir.entries);
76378683
JH
404}
405
406void wt_status_collect(struct wt_status *s)
50b7e70f
JH
407{
408 wt_status_collect_changes_worktree(s);
409
410 if (s->is_initial)
411 wt_status_collect_changes_initial(s);
412 else
413 wt_status_collect_changes_index(s);
76378683 414 wt_status_collect_untracked(s);
50b7e70f
JH
415}
416
4d4d5726
JH
417static void wt_status_print_unmerged(struct wt_status *s)
418{
419 int shown_header = 0;
420 int i;
421
422 for (i = 0; i < s->change.nr; i++) {
423 struct wt_status_change_data *d;
424 struct string_list_item *it;
425 it = &(s->change.items[i]);
426 d = it->util;
427 if (!d->stagemask)
428 continue;
429 if (!shown_header) {
430 wt_status_print_unmerged_header(s);
431 shown_header = 1;
432 }
433 wt_status_print_unmerged_data(s, it);
434 }
435 if (shown_header)
436 wt_status_print_trailer(s);
437
438}
439
50b7e70f
JH
440static void wt_status_print_updated(struct wt_status *s)
441{
442 int shown_header = 0;
443 int i;
444
445 for (i = 0; i < s->change.nr; i++) {
446 struct wt_status_change_data *d;
447 struct string_list_item *it;
448 it = &(s->change.items[i]);
449 d = it->util;
450 if (!d->index_status ||
451 d->index_status == DIFF_STATUS_UNMERGED)
452 continue;
453 if (!shown_header) {
454 wt_status_print_cached_header(s);
455 s->commitable = 1;
456 shown_header = 1;
457 }
458 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
459 }
460 if (shown_header)
461 wt_status_print_trailer(s);
462}
463
464/*
465 * -1 : has delete
466 * 0 : no change
467 * 1 : some change but no delete
468 */
9297f77e
JL
469static int wt_status_check_worktree_changes(struct wt_status *s,
470 int *dirty_submodules)
50b7e70f
JH
471{
472 int i;
473 int changes = 0;
474
9297f77e
JL
475 *dirty_submodules = 0;
476
50b7e70f
JH
477 for (i = 0; i < s->change.nr; i++) {
478 struct wt_status_change_data *d;
479 d = s->change.items[i].util;
4d4d5726
JH
480 if (!d->worktree_status ||
481 d->worktree_status == DIFF_STATUS_UNMERGED)
50b7e70f 482 continue;
9297f77e
JL
483 if (!changes)
484 changes = 1;
485 if (d->dirty_submodule)
486 *dirty_submodules = 1;
50b7e70f 487 if (d->worktree_status == DIFF_STATUS_DELETED)
9297f77e 488 changes = -1;
50b7e70f
JH
489 }
490 return changes;
491}
492
c91f0d92
JK
493static void wt_status_print_changed(struct wt_status *s)
494{
9297f77e
JL
495 int i, dirty_submodules;
496 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
50b7e70f
JH
497
498 if (!worktree_changes)
499 return;
500
9297f77e 501 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
50b7e70f
JH
502
503 for (i = 0; i < s->change.nr; i++) {
504 struct wt_status_change_data *d;
505 struct string_list_item *it;
506 it = &(s->change.items[i]);
507 d = it->util;
4d4d5726
JH
508 if (!d->worktree_status ||
509 d->worktree_status == DIFF_STATUS_UNMERGED)
50b7e70f
JH
510 continue;
511 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
512 }
513 wt_status_print_trailer(s);
c91f0d92
JK
514}
515
f17a5d34 516static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
ac8d5afc
PY
517{
518 struct child_process sm_summary;
519 char summary_limit[64];
520 char index[PATH_MAX];
521 const char *env[] = { index, NULL };
522 const char *argv[] = {
523 "submodule",
524 "summary",
f17a5d34 525 uncommitted ? "--files" : "--cached",
ac8d5afc
PY
526 "--for-status",
527 "--summary-limit",
528 summary_limit,
f17a5d34 529 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
ac8d5afc
PY
530 NULL
531 };
532
d249b098 533 sprintf(summary_limit, "%d", s->submodule_summary);
ac8d5afc
PY
534 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
535
536 memset(&sm_summary, 0, sizeof(sm_summary));
537 sm_summary.argv = argv;
538 sm_summary.env = env;
539 sm_summary.git_cmd = 1;
540 sm_summary.no_stdin = 1;
541 fflush(s->fp);
542 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
543 run_command(&sm_summary);
544}
545
1b908b6f
JH
546static void wt_status_print_other(struct wt_status *s,
547 struct string_list *l,
548 const char *what,
549 const char *how)
c91f0d92 550{
c91f0d92 551 int i;
f285a2d7 552 struct strbuf buf = STRBUF_INIT;
c91f0d92 553
76378683
JH
554 if (!s->untracked.nr)
555 return;
c91f0d92 556
1b908b6f
JH
557 wt_status_print_other_header(s, what, how);
558
559 for (i = 0; i < l->nr; i++) {
76378683 560 struct string_list_item *it;
1b908b6f 561 it = &(l->items[i]);
d249b098
JH
562 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
563 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
76378683
JH
564 quote_path(it->string, strlen(it->string),
565 &buf, s->prefix));
c91f0d92 566 }
367c9886 567 strbuf_release(&buf);
c91f0d92
JK
568}
569
570static void wt_status_print_verbose(struct wt_status *s)
571{
572 struct rev_info rev;
32962c9b 573 struct setup_revision_opt opt;
99a12694 574
c91f0d92 575 init_revisions(&rev, NULL);
5ec11af6 576 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
32962c9b
JH
577
578 memset(&opt, 0, sizeof(opt));
579 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
580 setup_revisions(0, NULL, &rev, &opt);
581
c91f0d92
JK
582 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
583 rev.diffopt.detect_rename = 1;
4ba0cb27
KH
584 rev.diffopt.file = s->fp;
585 rev.diffopt.close_file = 0;
4f672ad6
JK
586 /*
587 * If we're not going to stdout, then we definitely don't
588 * want color, since we are going to the commit message
589 * file (and even the "auto" setting won't work, since it
590 * will have checked isatty on stdout).
591 */
592 if (s->fp != stdout)
593 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
c91f0d92
JK
594 run_diff_index(&rev, 1);
595}
596
b6975ab5
JH
597static void wt_status_print_tracking(struct wt_status *s)
598{
599 struct strbuf sb = STRBUF_INIT;
600 const char *cp, *ep;
601 struct branch *branch;
602
603 assert(s->branch && !s->is_initial);
604 if (prefixcmp(s->branch, "refs/heads/"))
605 return;
606 branch = branch_get(s->branch + 11);
607 if (!format_tracking_info(branch, &sb))
608 return;
609
610 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
d249b098 611 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
b6975ab5 612 "# %.*s", (int)(ep - cp), cp);
d249b098 613 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
b6975ab5
JH
614}
615
c91f0d92
JK
616void wt_status_print(struct wt_status *s)
617{
d249b098 618 const char *branch_color = color(WT_STATUS_HEADER, s);
98bf8a47 619
bda324cf
JH
620 if (s->branch) {
621 const char *on_what = "On branch ";
622 const char *branch_name = s->branch;
cc44c765 623 if (!prefixcmp(branch_name, "refs/heads/"))
bda324cf
JH
624 branch_name += 11;
625 else if (!strcmp(branch_name, "HEAD")) {
626 branch_name = "";
d249b098 627 branch_color = color(WT_STATUS_NOBRANCH, s);
bda324cf
JH
628 on_what = "Not currently on any branch.";
629 }
d249b098 630 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
950ce2e2 631 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
b6975ab5
JH
632 if (!s->is_initial)
633 wt_status_print_tracking(s);
bda324cf 634 }
c91f0d92
JK
635
636 if (s->is_initial) {
d249b098
JH
637 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
638 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
639 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
c91f0d92
JK
640 }
641
c1e255b7 642 wt_status_print_updated(s);
228e7b5d 643 wt_status_print_unmerged(s);
c91f0d92 644 wt_status_print_changed(s);
f17a5d34
JL
645 if (s->submodule_summary) {
646 wt_status_print_submodule_summary(s, 0); /* staged */
647 wt_status_print_submodule_summary(s, 1); /* unstaged */
648 }
2381e39e 649 if (s->show_untracked_files) {
1b908b6f 650 wt_status_print_other(s, &s->untracked, "Untracked", "add");
2381e39e
JH
651 if (s->show_ignored_files)
652 wt_status_print_other(s, &s->ignored, "Ignored", "add -f");
653 } else if (s->commitable)
980bde38
MG
654 fprintf(s->fp, "# Untracked files not listed%s\n",
655 advice_status_hints
656 ? " (use -u option to show untracked files)" : "");
c91f0d92 657
1324fb6f 658 if (s->verbose)
c91f0d92 659 wt_status_print_verbose(s);
6e458bf6
JR
660 if (!s->commitable) {
661 if (s->amend)
f26a0012 662 fprintf(s->fp, "# No changes\n");
37d07f8f
JH
663 else if (s->nowarn)
664 ; /* nothing */
2a3a3c24 665 else if (s->workdir_dirty)
980bde38
MG
666 printf("no changes added to commit%s\n",
667 advice_status_hints
668 ? " (use \"git add\" and/or \"git commit -a\")" : "");
76378683 669 else if (s->untracked.nr)
980bde38
MG
670 printf("nothing added to commit but untracked files present%s\n",
671 advice_status_hints
672 ? " (use \"git add\" to track)" : "");
2a3a3c24 673 else if (s->is_initial)
980bde38
MG
674 printf("nothing to commit%s\n", advice_status_hints
675 ? " (create/copy files and use \"git add\" to track)" : "");
d249b098 676 else if (!s->show_untracked_files)
980bde38
MG
677 printf("nothing to commit%s\n", advice_status_hints
678 ? " (use -u to show untracked files)" : "");
2a3a3c24 679 else
980bde38
MG
680 printf("nothing to commit%s\n", advice_status_hints
681 ? " (working directory clean)" : "");
6e458bf6 682 }
c91f0d92 683}
84dbe7b8
MG
684
685static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
686 struct wt_status *s)
687{
688 struct wt_status_change_data *d = it->util;
689 const char *how = "??";
690
691 switch (d->stagemask) {
692 case 1: how = "DD"; break; /* both deleted */
693 case 2: how = "AU"; break; /* added by us */
694 case 3: how = "UD"; break; /* deleted by them */
695 case 4: how = "UA"; break; /* added by them */
696 case 5: how = "DU"; break; /* deleted by us */
697 case 6: how = "AA"; break; /* both added */
698 case 7: how = "UU"; break; /* both modified */
699 }
3fe2a894 700 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
84dbe7b8 701 if (null_termination) {
3fe2a894 702 fprintf(stdout, " %s%c", it->string, 0);
84dbe7b8
MG
703 } else {
704 struct strbuf onebuf = STRBUF_INIT;
705 const char *one;
706 one = quote_path(it->string, -1, &onebuf, s->prefix);
3fe2a894 707 printf(" %s\n", one);
84dbe7b8
MG
708 strbuf_release(&onebuf);
709 }
710}
711
712static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
713 struct wt_status *s)
714{
715 struct wt_status_change_data *d = it->util;
716
3fe2a894
MG
717 if (d->index_status)
718 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
719 else
720 putchar(' ');
721 if (d->worktree_status)
722 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
723 else
724 putchar(' ');
725 putchar(' ');
84dbe7b8
MG
726 if (null_termination) {
727 fprintf(stdout, "%s%c", it->string, 0);
728 if (d->head_path)
729 fprintf(stdout, "%s%c", d->head_path, 0);
730 } else {
731 struct strbuf onebuf = STRBUF_INIT;
732 const char *one;
733 if (d->head_path) {
734 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
735 printf("%s -> ", one);
736 strbuf_release(&onebuf);
737 }
738 one = quote_path(it->string, -1, &onebuf, s->prefix);
739 printf("%s\n", one);
740 strbuf_release(&onebuf);
741 }
742}
743
2381e39e
JH
744static void wt_shortstatus_other(int null_termination, struct string_list_item *it,
745 struct wt_status *s, const char *sign)
84dbe7b8
MG
746{
747 if (null_termination) {
2381e39e 748 fprintf(stdout, "%s %s%c", sign, it->string, 0);
84dbe7b8
MG
749 } else {
750 struct strbuf onebuf = STRBUF_INIT;
751 const char *one;
752 one = quote_path(it->string, -1, &onebuf, s->prefix);
c1909e72 753 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
3fe2a894 754 printf(" %s\n", one);
84dbe7b8
MG
755 strbuf_release(&onebuf);
756 }
757}
758
759void wt_shortstatus_print(struct wt_status *s, int null_termination)
760{
761 int i;
762 for (i = 0; i < s->change.nr; i++) {
763 struct wt_status_change_data *d;
764 struct string_list_item *it;
765
766 it = &(s->change.items[i]);
767 d = it->util;
768 if (d->stagemask)
769 wt_shortstatus_unmerged(null_termination, it, s);
770 else
771 wt_shortstatus_status(null_termination, it, s);
772 }
773 for (i = 0; i < s->untracked.nr; i++) {
774 struct string_list_item *it;
775
776 it = &(s->untracked.items[i]);
2381e39e
JH
777 wt_shortstatus_other(null_termination, it, s, "??");
778 }
779 for (i = 0; i < s->ignored.nr; i++) {
780 struct string_list_item *it;
781
782 it = &(s->ignored.items[i]);
783 wt_shortstatus_other(null_termination, it, s, "!!");
84dbe7b8
MG
784 }
785}
4a7cc2fd
JK
786
787void wt_porcelain_print(struct wt_status *s, int null_termination)
788{
789 s->use_color = 0;
8661768f
JK
790 s->relative_paths = 0;
791 s->prefix = NULL;
4a7cc2fd
JK
792 wt_shortstatus_print(s, null_termination);
793}