11 int wt_status_use_color
= 0;
12 static char wt_status_colors
[][COLOR_MAXLEN
] = {
13 "", /* WT_STATUS_HEADER: normal */
14 "\033[32m", /* WT_STATUS_UPDATED: green */
15 "\033[31m", /* WT_STATUS_CHANGED: red */
16 "\033[31m", /* WT_STATUS_UNTRACKED: red */
19 static const char use_add_msg
[] =
20 "use \"git add <file>...\" to update what will be committed";
21 static const char use_add_rm_msg
[] =
22 "use \"git add/rm <file>...\" to update what will be committed";
23 static const char use_add_to_include_msg
[] =
24 "use \"git add <file>...\" to include in what will be committed";
25 static const char *excludes_file
;
27 static int parse_status_slot(const char *var
, int offset
)
29 if (!strcasecmp(var
+offset
, "header"))
30 return WT_STATUS_HEADER
;
31 if (!strcasecmp(var
+offset
, "updated")
32 || !strcasecmp(var
+offset
, "added"))
33 return WT_STATUS_UPDATED
;
34 if (!strcasecmp(var
+offset
, "changed"))
35 return WT_STATUS_CHANGED
;
36 if (!strcasecmp(var
+offset
, "untracked"))
37 return WT_STATUS_UNTRACKED
;
38 die("bad config variable '%s'", var
);
41 static const char* color(int slot
)
43 return wt_status_use_color
? wt_status_colors
[slot
] : "";
46 void wt_status_prepare(struct wt_status
*s
)
48 unsigned char sha1
[20];
51 memset(s
, 0, sizeof(*s
));
52 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
53 s
->branch
= head
? xstrdup(head
) : NULL
;
54 s
->reference
= "HEAD";
56 s
->index_file
= get_index_file();
59 static void wt_status_print_cached_header(struct wt_status
*s
)
61 const char *c
= color(WT_STATUS_HEADER
);
62 color_fprintf_ln(s
->fp
, c
, "# Changes to be committed:");
64 color_fprintf_ln(s
->fp
, c
, "# (use \"git reset %s <file>...\" to unstage)", s
->reference
);
66 color_fprintf_ln(s
->fp
, c
, "# (use \"git rm --cached <file>...\" to unstage)");
68 color_fprintf_ln(s
->fp
, c
, "#");
71 static void wt_status_print_header(struct wt_status
*s
,
72 const char *main
, const char *sub
)
74 const char *c
= color(WT_STATUS_HEADER
);
75 color_fprintf_ln(s
->fp
, c
, "# %s:", main
);
76 color_fprintf_ln(s
->fp
, c
, "# (%s)", sub
);
77 color_fprintf_ln(s
->fp
, c
, "#");
80 static void wt_status_print_trailer(struct wt_status
*s
)
82 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
85 static const char *quote_crlf(const char *in
, char *buf
, size_t sz
)
91 for (scan
= in
, out
= buf
; *scan
; scan
++) {
114 static void wt_status_print_filepair(struct wt_status
*s
,
115 int t
, struct diff_filepair
*p
)
117 const char *c
= color(t
);
118 const char *one
, *two
;
119 char onebuf
[PATH_MAX
], twobuf
[PATH_MAX
];
121 one
= quote_crlf(p
->one
->path
, onebuf
, sizeof(onebuf
));
122 two
= quote_crlf(p
->two
->path
, twobuf
, sizeof(twobuf
));
124 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
126 case DIFF_STATUS_ADDED
:
127 color_fprintf(s
->fp
, c
, "new file: %s", one
);
129 case DIFF_STATUS_COPIED
:
130 color_fprintf(s
->fp
, c
, "copied: %s -> %s", one
, two
);
132 case DIFF_STATUS_DELETED
:
133 color_fprintf(s
->fp
, c
, "deleted: %s", one
);
135 case DIFF_STATUS_MODIFIED
:
136 color_fprintf(s
->fp
, c
, "modified: %s", one
);
138 case DIFF_STATUS_RENAMED
:
139 color_fprintf(s
->fp
, c
, "renamed: %s -> %s", one
, two
);
141 case DIFF_STATUS_TYPE_CHANGED
:
142 color_fprintf(s
->fp
, c
, "typechange: %s", one
);
144 case DIFF_STATUS_UNKNOWN
:
145 color_fprintf(s
->fp
, c
, "unknown: %s", one
);
147 case DIFF_STATUS_UNMERGED
:
148 color_fprintf(s
->fp
, c
, "unmerged: %s", one
);
151 die("bug: unhandled diff status %c", p
->status
);
153 fprintf(s
->fp
, "\n");
156 static void wt_status_print_updated_cb(struct diff_queue_struct
*q
,
157 struct diff_options
*options
,
160 struct wt_status
*s
= data
;
161 int shown_header
= 0;
163 for (i
= 0; i
< q
->nr
; i
++) {
164 if (q
->queue
[i
]->status
== 'U')
167 wt_status_print_cached_header(s
);
171 wt_status_print_filepair(s
, WT_STATUS_UPDATED
, q
->queue
[i
]);
174 wt_status_print_trailer(s
);
177 static void wt_status_print_changed_cb(struct diff_queue_struct
*q
,
178 struct diff_options
*options
,
181 struct wt_status
*s
= data
;
184 const char *msg
= use_add_msg
;
185 s
->workdir_dirty
= 1;
186 for (i
= 0; i
< q
->nr
; i
++)
187 if (q
->queue
[i
]->status
== DIFF_STATUS_DELETED
) {
188 msg
= use_add_rm_msg
;
191 wt_status_print_header(s
, "Changed but not updated", msg
);
193 for (i
= 0; i
< q
->nr
; i
++)
194 wt_status_print_filepair(s
, WT_STATUS_CHANGED
, q
->queue
[i
]);
196 wt_status_print_trailer(s
);
199 static void wt_read_cache(struct wt_status
*s
)
202 read_cache_from(s
->index_file
);
205 static void wt_status_print_initial(struct wt_status
*s
)
213 wt_status_print_cached_header(s
);
215 for (i
= 0; i
< active_nr
; i
++) {
216 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
217 color_fprintf_ln(s
->fp
, color(WT_STATUS_UPDATED
), "new file: %s",
218 quote_crlf(active_cache
[i
]->name
,
222 wt_status_print_trailer(s
);
225 static void wt_status_print_updated(struct wt_status
*s
)
228 init_revisions(&rev
, NULL
);
229 setup_revisions(0, NULL
, &rev
, s
->reference
);
230 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
231 rev
.diffopt
.format_callback
= wt_status_print_updated_cb
;
232 rev
.diffopt
.format_callback_data
= s
;
233 rev
.diffopt
.detect_rename
= 1;
234 rev
.diffopt
.rename_limit
= 100;
236 run_diff_index(&rev
, 1);
239 static void wt_status_print_changed(struct wt_status
*s
)
242 init_revisions(&rev
, "");
243 setup_revisions(0, NULL
, &rev
, NULL
);
244 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
245 rev
.diffopt
.format_callback
= wt_status_print_changed_cb
;
246 rev
.diffopt
.format_callback_data
= s
;
248 run_diff_files(&rev
, 0);
251 static void wt_status_print_untracked(struct wt_status
*s
)
253 struct dir_struct dir
;
256 int shown_header
= 0;
258 memset(&dir
, 0, sizeof(dir
));
260 dir
.exclude_per_dir
= ".gitignore";
262 dir
.show_other_directories
= 1;
263 dir
.hide_empty_directories
= 1;
265 x
= git_path("info/exclude");
267 add_excludes_from_file(&dir
, x
);
268 if (excludes_file
&& file_exists(excludes_file
))
269 add_excludes_from_file(&dir
, excludes_file
);
271 read_directory(&dir
, ".", "", 0, NULL
);
272 for(i
= 0; i
< dir
.nr
; i
++) {
273 /* check for matching entry, which is unmerged; lifted from
274 * builtin-ls-files:show_other_files */
275 struct dir_entry
*ent
= dir
.entries
[i
];
276 int pos
= cache_name_pos(ent
->name
, ent
->len
);
277 struct cache_entry
*ce
;
279 die("bug in wt_status_print_untracked");
281 if (pos
< active_nr
) {
282 ce
= active_cache
[pos
];
283 if (ce_namelen(ce
) == ent
->len
&&
284 !memcmp(ce
->name
, ent
->name
, ent
->len
))
288 s
->workdir_untracked
= 1;
289 wt_status_print_header(s
, "Untracked files",
290 use_add_to_include_msg
);
293 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
294 color_fprintf_ln(s
->fp
, color(WT_STATUS_UNTRACKED
), "%.*s",
295 ent
->len
, ent
->name
);
299 static void wt_status_print_verbose(struct wt_status
*s
)
302 init_revisions(&rev
, NULL
);
303 setup_revisions(0, NULL
, &rev
, s
->reference
);
304 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
305 rev
.diffopt
.detect_rename
= 1;
307 run_diff_index(&rev
, 1);
310 void wt_status_print(struct wt_status
*s
)
312 unsigned char sha1
[20];
313 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
316 const char *on_what
= "On branch ";
317 const char *branch_name
= s
->branch
;
318 if (!prefixcmp(branch_name
, "refs/heads/"))
320 else if (!strcmp(branch_name
, "HEAD")) {
322 on_what
= "Not currently on any branch.";
324 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
),
325 "# %s%s", on_what
, branch_name
);
329 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
330 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "# Initial commit");
331 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
332 wt_status_print_initial(s
);
335 wt_status_print_updated(s
);
338 wt_status_print_changed(s
);
339 wt_status_print_untracked(s
);
341 if (s
->verbose
&& !s
->is_initial
)
342 wt_status_print_verbose(s
);
343 if (!s
->commitable
) {
345 fprintf(s
->fp
, "# No changes\n");
346 else if (s
->workdir_dirty
)
347 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
348 else if (s
->workdir_untracked
)
349 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
350 else if (s
->is_initial
)
351 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
353 printf("nothing to commit (working directory clean)\n");
357 int git_status_config(const char *k
, const char *v
)
359 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
360 wt_status_use_color
= git_config_colorbool(k
, v
);
363 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
364 int slot
= parse_status_slot(k
, 13);
365 color_parse(v
, k
, wt_status_colors
[slot
]);
367 if (!strcmp(k
, "core.excludesfile")) {
369 die("core.excludesfile without value");
370 excludes_file
= xstrdup(v
);
373 return git_default_config(k
, v
);