🌐 AI搜索 & 代理 主页
blob: 38eb24536b34e40af4c43eb1631e08624e8993d6 [file] [log] [blame]
Junio C Hamano85023572006-12-19 22:34:121#include "cache.h"
Jeff Kingc91f0d92006-09-08 08:05:342#include "wt-status.h"
Jeff Kingc91f0d92006-09-08 08:05:343#include "object.h"
4#include "dir.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
8#include "diffcore.h"
Dmitry Potapova734d0b2008-03-07 02:30:589#include "quote.h"
Ping Yinac8d5af2008-04-12 15:05:3210#include "run-command.h"
Junio C Hamanob6975ab2008-07-02 07:52:1611#include "remote.h"
Jeff Kingc91f0d92006-09-08 08:05:3412
Junio C Hamano23900a92009-08-10 06:08:4013static char default_wt_status_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 21:53:4014 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 */
Junio C Hamano4d4d5722009-08-05 07:04:5119 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
Jeff Kingc91f0d92006-09-08 08:05:3420};
Junio C Hamano4d229652007-01-11 23:34:4121
Junio C Hamanod249b092009-08-10 04:59:3022static const char *color(int slot, struct wt_status *s)
Jeff Kingc91f0d92006-09-08 08:05:3423{
Junio C Hamano23900a92009-08-10 06:08:4024 return s->use_color > 0 ? s->color_palette[slot] : "";
Jeff Kingc91f0d92006-09-08 08:05:3425}
26
27void wt_status_prepare(struct wt_status *s)
28{
29 unsigned char sha1[20];
30 const char *head;
31
Junio C Hamanocc46a742007-02-10 00:22:4232 memset(s, 0, sizeof(*s));
Junio C Hamano23900a92009-08-10 06:08:4033 memcpy(s->color_palette, default_wt_status_colors,
34 sizeof(default_wt_status_colors));
Junio C Hamanod249b092009-08-10 04:59:3035 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
36 s->use_color = -1;
37 s->relative_paths = 1;
Junio C Hamano8da19772006-09-21 05:02:0138 head = resolve_ref("HEAD", sha1, 0, NULL);
Jeff Kingf62363f2006-09-18 02:13:5639 s->branch = head ? xstrdup(head) : NULL;
Jeff Kingc91f0d92006-09-08 08:05:3440 s->reference = "HEAD";
Kristian Høgsbergf26a0012007-09-18 00:06:4241 s->fp = stdout;
Kristian Høgsberg0f729f22007-09-18 00:06:4342 s->index_file = get_index_file();
Junio C Hamano50b7e702009-08-05 06:49:3343 s->change.strdup_strings = 1;
Junio C Hamano76378682009-08-10 07:36:3344 s->untracked.strdup_strings = 1;
Jeff Kingc91f0d92006-09-08 08:05:3445}
46
Junio C Hamano4d4d5722009-08-05 07:04:5147static void wt_status_print_unmerged_header(struct wt_status *s)
48{
Junio C Hamanod249b092009-08-10 04:59:3049 const char *c = color(WT_STATUS_HEADER, s);
Junio C Hamano4d4d5722009-08-05 07:04:5150 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
Jeff Kingedf563f2009-09-09 11:43:0351 if (!advice_status_hints)
52 return;
Junio C Hamano4d4d5722009-08-05 07:04:5153 if (!s->is_initial)
54 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
55 else
56 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
57 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to mark resolution)");
58 color_fprintf_ln(s->fp, c, "#");
59}
60
Kristian Høgsbergf26a0012007-09-18 00:06:4261static void wt_status_print_cached_header(struct wt_status *s)
Jürgen Rühle3c1eb9c2007-01-02 19:26:2162{
Junio C Hamanod249b092009-08-10 04:59:3063 const char *c = color(WT_STATUS_HEADER, s);
Kristian Høgsbergf26a0012007-09-18 00:06:4264 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
Jeff Kingedf563f2009-09-09 11:43:0365 if (!advice_status_hints)
66 return;
Jeff Kingff58b9a2008-02-12 05:45:1867 if (!s->is_initial) {
Kristian Høgsbergf26a0012007-09-18 00:06:4268 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
Jürgen Rühle3c1eb9c2007-01-02 19:26:2169 } else {
Kristian Høgsbergf26a0012007-09-18 00:06:4270 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
Jürgen Rühle3c1eb9c2007-01-02 19:26:2171 }
Kristian Høgsbergf26a0012007-09-18 00:06:4272 color_fprintf_ln(s->fp, c, "#");
Jürgen Rühle3c1eb9c2007-01-02 19:26:2173}
74
Anders Melchiorsenbb914b12008-09-07 22:05:0275static void wt_status_print_dirty_header(struct wt_status *s,
76 int has_deleted)
Jeff Kingc91f0d92006-09-08 08:05:3477{
Junio C Hamanod249b092009-08-10 04:59:3078 const char *c = color(WT_STATUS_HEADER, s);
Anders Melchiorsenbb914b12008-09-07 22:05:0279 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
Jeff Kingedf563f2009-09-09 11:43:0380 if (!advice_status_hints)
81 return;
Anders Melchiorsenbb914b12008-09-07 22:05:0282 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)");
Anders Melchiorsen4d6e4c42008-09-07 22:05:0386 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
Anders Melchiorsenbb914b12008-09-07 22:05:0287 color_fprintf_ln(s->fp, c, "#");
88}
89
90static void wt_status_print_untracked_header(struct wt_status *s)
91{
Junio C Hamanod249b092009-08-10 04:59:3092 const char *c = color(WT_STATUS_HEADER, s);
Anders Melchiorsenbb914b12008-09-07 22:05:0293 color_fprintf_ln(s->fp, c, "# Untracked files:");
Jeff Kingedf563f2009-09-09 11:43:0394 if (!advice_status_hints)
95 return;
Anders Melchiorsenbb914b12008-09-07 22:05:0296 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
Kristian Høgsbergf26a0012007-09-18 00:06:4297 color_fprintf_ln(s->fp, c, "#");
Jeff Kingc91f0d92006-09-08 08:05:3498}
99
Kristian Høgsbergf26a0012007-09-18 00:06:42100static void wt_status_print_trailer(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 08:05:34101{
Junio C Hamanod249b092009-08-10 04:59:30102 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
Jeff Kingc91f0d92006-09-08 08:05:34103}
104
Dmitry Potapova734d0b2008-03-07 02:30:58105#define quote_path quote_path_relative
Junio C Hamano3a946802006-11-08 21:20:46106
Junio C Hamano4d4d5722009-08-05 07:04:51107static void wt_status_print_unmerged_data(struct wt_status *s,
108 struct string_list_item *it)
109{
Junio C Hamanod249b092009-08-10 04:59:30110 const char *c = color(WT_STATUS_UNMERGED, s);
Junio C Hamano4d4d5722009-08-05 07:04:51111 struct wt_status_change_data *d = it->util;
112 struct strbuf onebuf = STRBUF_INIT;
113 const char *one, *how = "bug";
114
115 one = quote_path(it->string, -1, &onebuf, s->prefix);
Junio C Hamanod249b092009-08-10 04:59:30116 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
Junio C Hamano4d4d5722009-08-05 07:04:51117 switch (d->stagemask) {
118 case 1: how = "both deleted:"; break;
119 case 2: how = "added by us:"; break;
120 case 3: how = "deleted by them:"; break;
121 case 4: how = "added by them:"; break;
122 case 5: how = "deleted by us:"; break;
123 case 6: how = "both added:"; break;
124 case 7: how = "both modified:"; break;
125 }
126 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
127 strbuf_release(&onebuf);
128}
129
Junio C Hamano50b7e702009-08-05 06:49:33130static void wt_status_print_change_data(struct wt_status *s,
131 int change_type,
132 struct string_list_item *it)
Jeff Kingc91f0d92006-09-08 08:05:34133{
Junio C Hamano50b7e702009-08-05 06:49:33134 struct wt_status_change_data *d = it->util;
Junio C Hamanod249b092009-08-10 04:59:30135 const char *c = color(change_type, s);
Junio C Hamano50b7e702009-08-05 06:49:33136 int status = status;
137 char *one_name;
138 char *two_name;
Junio C Hamano3a946802006-11-08 21:20:46139 const char *one, *two;
Brandon Caseyf285a2d2008-10-09 19:12:12140 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
Junio C Hamano3a946802006-11-08 21:20:46141
Junio C Hamano50b7e702009-08-05 06:49:33142 one_name = two_name = it->string;
143 switch (change_type) {
144 case WT_STATUS_UPDATED:
145 status = d->index_status;
146 if (d->head_path)
147 one_name = d->head_path;
148 break;
149 case WT_STATUS_CHANGED:
150 status = d->worktree_status;
151 break;
152 }
153
154 one = quote_path(one_name, -1, &onebuf, s->prefix);
155 two = quote_path(two_name, -1, &twobuf, s->prefix);
Junio C Hamano3a946802006-11-08 21:20:46156
Junio C Hamanod249b092009-08-10 04:59:30157 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
Junio C Hamano50b7e702009-08-05 06:49:33158 switch (status) {
Jeff Kingc91f0d92006-09-08 08:05:34159 case DIFF_STATUS_ADDED:
Kristian Høgsbergf26a0012007-09-18 00:06:42160 color_fprintf(s->fp, c, "new file: %s", one);
Junio C Hamano3a946802006-11-08 21:20:46161 break;
Jeff Kingc91f0d92006-09-08 08:05:34162 case DIFF_STATUS_COPIED:
Kristian Høgsbergf26a0012007-09-18 00:06:42163 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
Jeff Kingc91f0d92006-09-08 08:05:34164 break;
165 case DIFF_STATUS_DELETED:
Kristian Høgsbergf26a0012007-09-18 00:06:42166 color_fprintf(s->fp, c, "deleted: %s", one);
Junio C Hamano3a946802006-11-08 21:20:46167 break;
Jeff Kingc91f0d92006-09-08 08:05:34168 case DIFF_STATUS_MODIFIED:
Kristian Høgsbergf26a0012007-09-18 00:06:42169 color_fprintf(s->fp, c, "modified: %s", one);
Junio C Hamano3a946802006-11-08 21:20:46170 break;
Jeff Kingc91f0d92006-09-08 08:05:34171 case DIFF_STATUS_RENAMED:
Kristian Høgsbergf26a0012007-09-18 00:06:42172 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
Jeff Kingc91f0d92006-09-08 08:05:34173 break;
174 case DIFF_STATUS_TYPE_CHANGED:
Kristian Høgsbergf26a0012007-09-18 00:06:42175 color_fprintf(s->fp, c, "typechange: %s", one);
Junio C Hamano3a946802006-11-08 21:20:46176 break;
Jeff Kingc91f0d92006-09-08 08:05:34177 case DIFF_STATUS_UNKNOWN:
Kristian Høgsbergf26a0012007-09-18 00:06:42178 color_fprintf(s->fp, c, "unknown: %s", one);
Junio C Hamano3a946802006-11-08 21:20:46179 break;
Jeff Kingc91f0d92006-09-08 08:05:34180 case DIFF_STATUS_UNMERGED:
Kristian Høgsbergf26a0012007-09-18 00:06:42181 color_fprintf(s->fp, c, "unmerged: %s", one);
Junio C Hamano3a946802006-11-08 21:20:46182 break;
Jeff Kingc91f0d92006-09-08 08:05:34183 default:
Junio C Hamano50b7e702009-08-05 06:49:33184 die("bug: unhandled diff status %c", status);
Jeff Kingc91f0d92006-09-08 08:05:34185 }
Kristian Høgsbergf26a0012007-09-18 00:06:42186 fprintf(s->fp, "\n");
Johannes Schindelin367c9882007-11-11 17:35:41187 strbuf_release(&onebuf);
188 strbuf_release(&twobuf);
Jeff Kingc91f0d92006-09-08 08:05:34189}
190
Junio C Hamano50b7e702009-08-05 06:49:33191static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
192 struct diff_options *options,
193 void *data)
Jeff Kingc91f0d92006-09-08 08:05:34194{
195 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 08:05:34196 int i;
Junio C Hamano50b7e702009-08-05 06:49:33197
198 if (!q->nr)
199 return;
200 s->workdir_dirty = 1;
Jeff Kingc91f0d92006-09-08 08:05:34201 for (i = 0; i < q->nr; i++) {
Junio C Hamano50b7e702009-08-05 06:49:33202 struct diff_filepair *p;
203 struct string_list_item *it;
204 struct wt_status_change_data *d;
205
206 p = q->queue[i];
207 it = string_list_insert(p->one->path, &s->change);
208 d = it->util;
209 if (!d) {
210 d = xcalloc(1, sizeof(*d));
211 it->util = d;
Jeff Kingc91f0d92006-09-08 08:05:34212 }
Junio C Hamano50b7e702009-08-05 06:49:33213 if (!d->worktree_status)
214 d->worktree_status = p->status;
Jeff Kingc91f0d92006-09-08 08:05:34215 }
Jeff Kingc91f0d92006-09-08 08:05:34216}
217
Junio C Hamano4d4d5722009-08-05 07:04:51218static int unmerged_mask(const char *path)
219{
220 int pos, mask;
221 struct cache_entry *ce;
222
223 pos = cache_name_pos(path, strlen(path));
224 if (0 <= pos)
225 return 0;
226
227 mask = 0;
228 pos = -pos-1;
229 while (pos < active_nr) {
230 ce = active_cache[pos++];
231 if (strcmp(ce->name, path) || !ce_stage(ce))
232 break;
233 mask |= (1 << (ce_stage(ce) - 1));
234 }
235 return mask;
236}
237
Junio C Hamano50b7e702009-08-05 06:49:33238static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
239 struct diff_options *options,
240 void *data)
Jeff Kingc91f0d92006-09-08 08:05:34241{
Jürgen Rühle6e458bf2007-01-02 19:26:22242 struct wt_status *s = data;
Jeff Kingc91f0d92006-09-08 08:05:34243 int i;
Junio C Hamano50b7e702009-08-05 06:49:33244
245 for (i = 0; i < q->nr; i++) {
246 struct diff_filepair *p;
247 struct string_list_item *it;
248 struct wt_status_change_data *d;
249
250 p = q->queue[i];
251 it = string_list_insert(p->two->path, &s->change);
252 d = it->util;
253 if (!d) {
254 d = xcalloc(1, sizeof(*d));
255 it->util = d;
256 }
257 if (!d->index_status)
258 d->index_status = p->status;
259 switch (p->status) {
260 case DIFF_STATUS_COPIED:
261 case DIFF_STATUS_RENAMED:
262 d->head_path = xstrdup(p->one->path);
263 break;
Junio C Hamano4d4d5722009-08-05 07:04:51264 case DIFF_STATUS_UNMERGED:
265 d->stagemask = unmerged_mask(p->two->path);
266 break;
Junio C Hamano50b7e702009-08-05 06:49:33267 }
Jürgen Rühle6e458bf2007-01-02 19:26:22268 }
Jeff Kingc91f0d92006-09-08 08:05:34269}
270
Junio C Hamano50b7e702009-08-05 06:49:33271static void wt_status_collect_changes_worktree(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 08:05:34272{
273 struct rev_info rev;
Junio C Hamano50b7e702009-08-05 06:49:33274
275 init_revisions(&rev, NULL);
276 setup_revisions(0, NULL, &rev, NULL);
277 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
278 rev.diffopt.format_callback = wt_status_collect_changed_cb;
279 rev.diffopt.format_callback_data = s;
280 run_diff_files(&rev, 0);
281}
282
283static void wt_status_collect_changes_index(struct wt_status *s)
284{
285 struct rev_info rev;
286
Jeff Kingc91f0d92006-09-08 08:05:34287 init_revisions(&rev, NULL);
Jeff Kingc1e255b2008-11-12 08:21:39288 setup_revisions(0, NULL, &rev,
289 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
Jeff Kingc91f0d92006-09-08 08:05:34290 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
Junio C Hamano50b7e702009-08-05 06:49:33291 rev.diffopt.format_callback = wt_status_collect_updated_cb;
Jeff Kingc91f0d92006-09-08 08:05:34292 rev.diffopt.format_callback_data = s;
293 rev.diffopt.detect_rename = 1;
Jeff King50705912008-04-30 17:24:43294 rev.diffopt.rename_limit = 200;
Jeff Kingf714fb82007-12-03 06:58:37295 rev.diffopt.break_opt = 0;
Jeff Kingc91f0d92006-09-08 08:05:34296 run_diff_index(&rev, 1);
297}
298
Junio C Hamano50b7e702009-08-05 06:49:33299static void wt_status_collect_changes_initial(struct wt_status *s)
300{
301 int i;
302
303 for (i = 0; i < active_nr; i++) {
304 struct string_list_item *it;
305 struct wt_status_change_data *d;
306 struct cache_entry *ce = active_cache[i];
307
308 it = string_list_insert(ce->name, &s->change);
309 d = it->util;
310 if (!d) {
311 d = xcalloc(1, sizeof(*d));
312 it->util = d;
313 }
Junio C Hamano4d4d5722009-08-05 07:04:51314 if (ce_stage(ce)) {
Junio C Hamano50b7e702009-08-05 06:49:33315 d->index_status = DIFF_STATUS_UNMERGED;
Junio C Hamano4d4d5722009-08-05 07:04:51316 d->stagemask |= (1 << (ce_stage(ce) - 1));
317 }
Junio C Hamano50b7e702009-08-05 06:49:33318 else
319 d->index_status = DIFF_STATUS_ADDED;
320 }
321}
322
Junio C Hamano76378682009-08-10 07:36:33323static void wt_status_collect_untracked(struct wt_status *s)
324{
325 int i;
326 struct dir_struct dir;
327
328 if (!s->show_untracked_files)
329 return;
330 memset(&dir, 0, sizeof(dir));
331 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
332 dir.flags |=
333 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
334 setup_standard_excludes(&dir);
335
336 fill_directory(&dir, NULL);
Brian Gianforcaroeeefa7c2009-09-01 05:35:10337 for (i = 0; i < dir.nr; i++) {
Junio C Hamano76378682009-08-10 07:36:33338 struct dir_entry *ent = dir.entries[i];
339 if (!cache_name_is_other(ent->name, ent->len))
340 continue;
341 s->workdir_untracked = 1;
342 string_list_insert(ent->name, &s->untracked);
343 }
344}
345
346void wt_status_collect(struct wt_status *s)
Junio C Hamano50b7e702009-08-05 06:49:33347{
348 wt_status_collect_changes_worktree(s);
349
350 if (s->is_initial)
351 wt_status_collect_changes_initial(s);
352 else
353 wt_status_collect_changes_index(s);
Junio C Hamano76378682009-08-10 07:36:33354 wt_status_collect_untracked(s);
Junio C Hamano50b7e702009-08-05 06:49:33355}
356
Junio C Hamano4d4d5722009-08-05 07:04:51357static void wt_status_print_unmerged(struct wt_status *s)
358{
359 int shown_header = 0;
360 int i;
361
362 for (i = 0; i < s->change.nr; i++) {
363 struct wt_status_change_data *d;
364 struct string_list_item *it;
365 it = &(s->change.items[i]);
366 d = it->util;
367 if (!d->stagemask)
368 continue;
369 if (!shown_header) {
370 wt_status_print_unmerged_header(s);
371 shown_header = 1;
372 }
373 wt_status_print_unmerged_data(s, it);
374 }
375 if (shown_header)
376 wt_status_print_trailer(s);
377
378}
379
Junio C Hamano50b7e702009-08-05 06:49:33380static void wt_status_print_updated(struct wt_status *s)
381{
382 int shown_header = 0;
383 int i;
384
385 for (i = 0; i < s->change.nr; i++) {
386 struct wt_status_change_data *d;
387 struct string_list_item *it;
388 it = &(s->change.items[i]);
389 d = it->util;
390 if (!d->index_status ||
391 d->index_status == DIFF_STATUS_UNMERGED)
392 continue;
393 if (!shown_header) {
394 wt_status_print_cached_header(s);
395 s->commitable = 1;
396 shown_header = 1;
397 }
398 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
399 }
400 if (shown_header)
401 wt_status_print_trailer(s);
402}
403
404/*
405 * -1 : has delete
406 * 0 : no change
407 * 1 : some change but no delete
408 */
409static int wt_status_check_worktree_changes(struct wt_status *s)
410{
411 int i;
412 int changes = 0;
413
414 for (i = 0; i < s->change.nr; i++) {
415 struct wt_status_change_data *d;
416 d = s->change.items[i].util;
Junio C Hamano4d4d5722009-08-05 07:04:51417 if (!d->worktree_status ||
418 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-05 06:49:33419 continue;
420 changes = 1;
421 if (d->worktree_status == DIFF_STATUS_DELETED)
422 return -1;
423 }
424 return changes;
425}
426
Jeff Kingc91f0d92006-09-08 08:05:34427static void wt_status_print_changed(struct wt_status *s)
428{
Junio C Hamano50b7e702009-08-05 06:49:33429 int i;
430 int worktree_changes = wt_status_check_worktree_changes(s);
431
432 if (!worktree_changes)
433 return;
434
435 wt_status_print_dirty_header(s, worktree_changes < 0);
436
437 for (i = 0; i < s->change.nr; i++) {
438 struct wt_status_change_data *d;
439 struct string_list_item *it;
440 it = &(s->change.items[i]);
441 d = it->util;
Junio C Hamano4d4d5722009-08-05 07:04:51442 if (!d->worktree_status ||
443 d->worktree_status == DIFF_STATUS_UNMERGED)
Junio C Hamano50b7e702009-08-05 06:49:33444 continue;
445 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
446 }
447 wt_status_print_trailer(s);
Jeff Kingc91f0d92006-09-08 08:05:34448}
449
Ping Yinac8d5af2008-04-12 15:05:32450static void wt_status_print_submodule_summary(struct wt_status *s)
451{
452 struct child_process sm_summary;
453 char summary_limit[64];
454 char index[PATH_MAX];
455 const char *env[] = { index, NULL };
456 const char *argv[] = {
457 "submodule",
458 "summary",
459 "--cached",
460 "--for-status",
461 "--summary-limit",
462 summary_limit,
463 s->amend ? "HEAD^" : "HEAD",
464 NULL
465 };
466
Junio C Hamanod249b092009-08-10 04:59:30467 sprintf(summary_limit, "%d", s->submodule_summary);
Ping Yinac8d5af2008-04-12 15:05:32468 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
469
470 memset(&sm_summary, 0, sizeof(sm_summary));
471 sm_summary.argv = argv;
472 sm_summary.env = env;
473 sm_summary.git_cmd = 1;
474 sm_summary.no_stdin = 1;
475 fflush(s->fp);
476 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
477 run_command(&sm_summary);
478}
479
Jürgen Rühle6e458bf2007-01-02 19:26:22480static void wt_status_print_untracked(struct wt_status *s)
Jeff Kingc91f0d92006-09-08 08:05:34481{
Jeff Kingc91f0d92006-09-08 08:05:34482 int i;
Brandon Caseyf285a2d2008-10-09 19:12:12483 struct strbuf buf = STRBUF_INIT;
Jeff Kingc91f0d92006-09-08 08:05:34484
Junio C Hamano76378682009-08-10 07:36:33485 if (!s->untracked.nr)
486 return;
Jeff Kingc91f0d92006-09-08 08:05:34487
Junio C Hamano76378682009-08-10 07:36:33488 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]);
Junio C Hamanod249b092009-08-10 04:59:30492 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
493 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
Junio C Hamano76378682009-08-10 07:36:33494 quote_path(it->string, strlen(it->string),
495 &buf, s->prefix));
Jeff Kingc91f0d92006-09-08 08:05:34496 }
Johannes Schindelin367c9882007-11-11 17:35:41497 strbuf_release(&buf);
Jeff Kingc91f0d92006-09-08 08:05:34498}
499
500static void wt_status_print_verbose(struct wt_status *s)
501{
502 struct rev_info rev;
Kristian Høgsberg99a12692007-11-22 02:54:49503
Jeff Kingc91f0d92006-09-08 08:05:34504 init_revisions(&rev, NULL);
Jeff King5ec11af2008-12-08 02:54:17505 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
Jeff King1324fb62008-11-12 08:23:37506 setup_revisions(0, NULL, &rev,
507 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
Jeff Kingc91f0d92006-09-08 08:05:34508 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
509 rev.diffopt.detect_rename = 1;
Kristian Høgsberg4ba0cb22008-03-10 17:58:26510 rev.diffopt.file = s->fp;
511 rev.diffopt.close_file = 0;
Jeff King4f672ad2008-10-26 04:49:35512 /*
513 * If we're not going to stdout, then we definitely don't
514 * want color, since we are going to the commit message
515 * file (and even the "auto" setting won't work, since it
516 * will have checked isatty on stdout).
517 */
518 if (s->fp != stdout)
519 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
Jeff Kingc91f0d92006-09-08 08:05:34520 run_diff_index(&rev, 1);
521}
522
Junio C Hamanob6975ab2008-07-02 07:52:16523static void wt_status_print_tracking(struct wt_status *s)
524{
525 struct strbuf sb = STRBUF_INIT;
526 const char *cp, *ep;
527 struct branch *branch;
528
529 assert(s->branch && !s->is_initial);
530 if (prefixcmp(s->branch, "refs/heads/"))
531 return;
532 branch = branch_get(s->branch + 11);
533 if (!format_tracking_info(branch, &sb))
534 return;
535
536 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
Junio C Hamanod249b092009-08-10 04:59:30537 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
Junio C Hamanob6975ab2008-07-02 07:52:16538 "# %.*s", (int)(ep - cp), cp);
Junio C Hamanod249b092009-08-10 04:59:30539 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
Junio C Hamanob6975ab2008-07-02 07:52:16540}
541
Jeff Kingc91f0d92006-09-08 08:05:34542void wt_status_print(struct wt_status *s)
543{
Jürgen Rühle98bf8a42007-01-02 19:26:23544 unsigned char sha1[20];
Junio C Hamanod249b092009-08-10 04:59:30545 const char *branch_color = color(WT_STATUS_HEADER, s);
Jürgen Rühle98bf8a42007-01-02 19:26:23546
Chris Parsons950ce2e2008-05-22 12:50:02547 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
Junio C Hamanobda324c2007-01-03 09:09:34548 if (s->branch) {
549 const char *on_what = "On branch ";
550 const char *branch_name = s->branch;
Junio C Hamanocc44c762007-02-20 09:53:29551 if (!prefixcmp(branch_name, "refs/heads/"))
Junio C Hamanobda324c2007-01-03 09:09:34552 branch_name += 11;
553 else if (!strcmp(branch_name, "HEAD")) {
554 branch_name = "";
Junio C Hamanod249b092009-08-10 04:59:30555 branch_color = color(WT_STATUS_NOBRANCH, s);
Junio C Hamanobda324c2007-01-03 09:09:34556 on_what = "Not currently on any branch.";
557 }
Junio C Hamanod249b092009-08-10 04:59:30558 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
Chris Parsons950ce2e2008-05-22 12:50:02559 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
Junio C Hamanob6975ab2008-07-02 07:52:16560 if (!s->is_initial)
561 wt_status_print_tracking(s);
Junio C Hamanobda324c2007-01-03 09:09:34562 }
Jeff Kingc91f0d92006-09-08 08:05:34563
Junio C Hamano76378682009-08-10 07:36:33564 wt_status_collect(s);
Junio C Hamano50b7e702009-08-05 06:49:33565
Jeff Kingc91f0d92006-09-08 08:05:34566 if (s->is_initial) {
Junio C Hamanod249b092009-08-10 04:59:30567 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), "#");
Jeff Kingc91f0d92006-09-08 08:05:34570 }
571
Jeff Kingc1e255b2008-11-12 08:21:39572 wt_status_print_updated(s);
Johannes Sixt228e7b52009-09-01 20:13:53573 wt_status_print_unmerged(s);
Jeff Kingc91f0d92006-09-08 08:05:34574 wt_status_print_changed(s);
Junio C Hamanod249b092009-08-10 04:59:30575 if (s->submodule_summary)
Ping Yinac8d5af2008-04-12 15:05:32576 wt_status_print_submodule_summary(s);
Junio C Hamanod249b092009-08-10 04:59:30577 if (s->show_untracked_files)
Marius Storm-Olsen6c2ce042008-06-05 12:22:56578 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");
Jeff Kingc91f0d92006-09-08 08:05:34581
Jeff King1324fb62008-11-12 08:23:37582 if (s->verbose)
Jeff Kingc91f0d92006-09-08 08:05:34583 wt_status_print_verbose(s);
Jürgen Rühle6e458bf2007-01-02 19:26:22584 if (!s->commitable) {
585 if (s->amend)
Kristian Høgsbergf26a0012007-09-18 00:06:42586 fprintf(s->fp, "# No changes\n");
Junio C Hamano37d07f82007-12-13 03:09:16587 else if (s->nowarn)
588 ; /* nothing */
Jürgen Rühle2a3a3c22007-01-10 22:25:03589 else if (s->workdir_dirty)
Nicolas Pitredcbc7bb2007-01-14 02:23:55590 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
Junio C Hamano76378682009-08-10 07:36:33591 else if (s->untracked.nr)
Jürgen Rühle2a3a3c22007-01-10 22:25:03592 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");
Junio C Hamanod249b092009-08-10 04:59:30595 else if (!s->show_untracked_files)
Marius Storm-Olsen6c2ce042008-06-05 12:22:56596 printf("nothing to commit (use -u to show untracked files)\n");
Jürgen Rühle2a3a3c22007-01-10 22:25:03597 else
598 printf("nothing to commit (working directory clean)\n");
Jürgen Rühle6e458bf2007-01-02 19:26:22599 }
Jeff Kingc91f0d92006-09-08 08:05:34600}