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 */
18 static const char* use_add_msg
= "use \"git add <file>...\" to incrementally add content to commit";
20 static int parse_status_slot(const char *var
, int offset
)
22 if (!strcasecmp(var
+offset
, "header"))
23 return WT_STATUS_HEADER
;
24 if (!strcasecmp(var
+offset
, "updated")
25 || !strcasecmp(var
+offset
, "added"))
26 return WT_STATUS_UPDATED
;
27 if (!strcasecmp(var
+offset
, "changed"))
28 return WT_STATUS_CHANGED
;
29 if (!strcasecmp(var
+offset
, "untracked"))
30 return WT_STATUS_UNTRACKED
;
31 die("bad config variable '%s'", var
);
34 static const char* color(int slot
)
36 return wt_status_use_color
? wt_status_colors
[slot
] : "";
39 void wt_status_prepare(struct wt_status
*s
)
41 unsigned char sha1
[20];
44 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
45 s
->branch
= head
? xstrdup(head
) : NULL
;
47 s
->reference
= "HEAD";
56 static void wt_status_print_header(const char *main
, const char *sub
)
58 const char *c
= color(WT_STATUS_HEADER
);
59 color_printf_ln(c
, "# %s:", main
);
60 color_printf_ln(c
, "# (%s)", sub
);
61 color_printf_ln(c
, "#");
64 static void wt_status_print_trailer(void)
66 color_printf_ln(color(WT_STATUS_HEADER
), "#");
69 static const char *quote_crlf(const char *in
, char *buf
, size_t sz
)
75 for (scan
= in
, out
= buf
; *scan
; scan
++) {
98 static void wt_status_print_filepair(int t
, struct diff_filepair
*p
)
100 const char *c
= color(t
);
101 const char *one
, *two
;
102 char onebuf
[PATH_MAX
], twobuf
[PATH_MAX
];
104 one
= quote_crlf(p
->one
->path
, onebuf
, sizeof(onebuf
));
105 two
= quote_crlf(p
->two
->path
, twobuf
, sizeof(twobuf
));
107 color_printf(color(WT_STATUS_HEADER
), "#\t");
109 case DIFF_STATUS_ADDED
:
110 color_printf(c
, "new file: %s", one
);
112 case DIFF_STATUS_COPIED
:
113 color_printf(c
, "copied: %s -> %s", one
, two
);
115 case DIFF_STATUS_DELETED
:
116 color_printf(c
, "deleted: %s", one
);
118 case DIFF_STATUS_MODIFIED
:
119 color_printf(c
, "modified: %s", one
);
121 case DIFF_STATUS_RENAMED
:
122 color_printf(c
, "renamed: %s -> %s", one
, two
);
124 case DIFF_STATUS_TYPE_CHANGED
:
125 color_printf(c
, "typechange: %s", one
);
127 case DIFF_STATUS_UNKNOWN
:
128 color_printf(c
, "unknown: %s", one
);
130 case DIFF_STATUS_UNMERGED
:
131 color_printf(c
, "unmerged: %s", one
);
134 die("bug: unhandled diff status %c", p
->status
);
139 static void wt_status_print_updated_cb(struct diff_queue_struct
*q
,
140 struct diff_options
*options
,
143 struct wt_status
*s
= data
;
144 int shown_header
= 0;
146 for (i
= 0; i
< q
->nr
; i
++) {
147 if (q
->queue
[i
]->status
== 'U')
150 wt_status_print_header("Added but not yet committed",
155 wt_status_print_filepair(WT_STATUS_UPDATED
, q
->queue
[i
]);
158 wt_status_print_trailer();
161 static void wt_status_print_changed_cb(struct diff_queue_struct
*q
,
162 struct diff_options
*options
,
165 struct wt_status
*s
= data
;
168 s
->workdir_clean
= 0;
169 wt_status_print_header("Changed but not added", use_add_msg
);
171 for (i
= 0; i
< q
->nr
; i
++)
172 wt_status_print_filepair(WT_STATUS_CHANGED
, q
->queue
[i
]);
174 wt_status_print_trailer();
177 void wt_status_print_initial(struct wt_status
*s
)
185 wt_status_print_header("Added but not yet committed",
188 for (i
= 0; i
< active_nr
; i
++) {
189 color_printf(color(WT_STATUS_HEADER
), "#\t");
190 color_printf_ln(color(WT_STATUS_UPDATED
), "new file: %s",
191 quote_crlf(active_cache
[i
]->name
,
195 wt_status_print_trailer();
198 static void wt_status_print_updated(struct wt_status
*s
)
201 init_revisions(&rev
, NULL
);
202 setup_revisions(0, NULL
, &rev
, s
->reference
);
203 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
204 rev
.diffopt
.format_callback
= wt_status_print_updated_cb
;
205 rev
.diffopt
.format_callback_data
= s
;
206 rev
.diffopt
.detect_rename
= 1;
207 run_diff_index(&rev
, 1);
210 static void wt_status_print_changed(struct wt_status
*s
)
213 init_revisions(&rev
, "");
214 setup_revisions(0, NULL
, &rev
, NULL
);
215 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
216 rev
.diffopt
.format_callback
= wt_status_print_changed_cb
;
217 rev
.diffopt
.format_callback_data
= s
;
218 run_diff_files(&rev
, 0);
221 static void wt_status_print_untracked(struct wt_status
*s
)
223 struct dir_struct dir
;
226 int shown_header
= 0;
228 memset(&dir
, 0, sizeof(dir
));
230 dir
.exclude_per_dir
= ".gitignore";
232 dir
.show_other_directories
= 1;
233 dir
.hide_empty_directories
= 1;
235 x
= git_path("info/exclude");
237 add_excludes_from_file(&dir
, x
);
239 read_directory(&dir
, ".", "", 0);
240 for(i
= 0; i
< dir
.nr
; i
++) {
241 /* check for matching entry, which is unmerged; lifted from
242 * builtin-ls-files:show_other_files */
243 struct dir_entry
*ent
= dir
.entries
[i
];
244 int pos
= cache_name_pos(ent
->name
, ent
->len
);
245 struct cache_entry
*ce
;
247 die("bug in wt_status_print_untracked");
249 if (pos
< active_nr
) {
250 ce
= active_cache
[pos
];
251 if (ce_namelen(ce
) == ent
->len
&&
252 !memcmp(ce
->name
, ent
->name
, ent
->len
))
256 s
->workdir_clean
= 0;
257 wt_status_print_header("Untracked files", use_add_msg
);
260 color_printf(color(WT_STATUS_HEADER
), "#\t");
261 color_printf_ln(color(WT_STATUS_UNTRACKED
), "%.*s",
262 ent
->len
, ent
->name
);
266 static void wt_status_print_verbose(struct wt_status
*s
)
269 init_revisions(&rev
, NULL
);
270 setup_revisions(0, NULL
, &rev
, s
->reference
);
271 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
272 rev
.diffopt
.detect_rename
= 1;
273 run_diff_index(&rev
, 1);
276 void wt_status_print(struct wt_status
*s
)
278 unsigned char sha1
[20];
279 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
282 color_printf_ln(color(WT_STATUS_HEADER
),
283 "# On branch %s", s
->branch
);
286 color_printf_ln(color(WT_STATUS_HEADER
), "#");
287 color_printf_ln(color(WT_STATUS_HEADER
), "# Initial commit");
288 color_printf_ln(color(WT_STATUS_HEADER
), "#");
289 wt_status_print_initial(s
);
292 wt_status_print_updated(s
);
296 wt_status_print_changed(s
);
297 wt_status_print_untracked(s
);
299 if (s
->verbose
&& !s
->is_initial
)
300 wt_status_print_verbose(s
);
301 if (!s
->commitable
) {
303 printf("# No changes\n");
304 else if (s
->workdir_clean
)
306 ? "nothing to commit\n"
307 : "nothing to commit (working directory matches HEAD)\n");
309 printf("no changes added to commit (use \"git add\" and/or \"git commit [-a|-i|-o]\")\n");
313 int git_status_config(const char *k
, const char *v
)
315 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
316 wt_status_use_color
= git_config_colorbool(k
, v
);
319 if (!strncmp(k
, "status.color.", 13) || !strncmp(k
, "color.status", 13)) {
320 int slot
= parse_status_slot(k
, 13);
321 color_parse(v
, k
, wt_status_colors
[slot
]);
323 return git_default_config(k
, v
);