git-commit.sh: convert run_status to a C builtin
This creates a new git-runstatus which should do roughly the same thing as the run_status function from git-commit.sh. Except for color support, the main focus has been to keep the output identical, so that it can be verified as correct and then used as a C platform for other improvements to the status printing code. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Jeff King committed
Sep 8, 2006 at 04:05 UTC
c91f0d92efb36d7b349f586cafafaf0e6ac3f5b2
10 files changed
+349
-100
.gitignore
+1
@@ -94,6 +94,7 @@ git-rev-list
94
git-rev-parse
95
git-revert
96
git-rm
97
+git-runstatus
98
git-send-email
99
git-send-pack
100
git-sh-setup
Makefile
+2
-1
@@ -252,7 +252,7 @@ LIB_OBJS = \
252
fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
253
write_or_die.o trace.o \
254
alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
255
- color.o
255
+ color.o wt-status.o
256
257
BUILTIN_OBJS = \
258
builtin-add.o \
@@ -286,6 +286,7 @@ BUILTIN_OBJS = \
286
builtin-rev-list.o \
287
builtin-rev-parse.o \
288
builtin-rm.o \
289
+ builtin-runstatus.o \
290
builtin-show-branch.o \
291
builtin-stripspace.o \
292
builtin-symbolic-ref.o \
builtin-runstatus.c
new
+34
@@ -0,0 +1,34 @@
1
+#include "wt-status.h"
2
+#include "cache.h"
3
+
4
+extern int wt_status_use_color;
5
+
6
+static const char runstatus_usage[] =
7
+"git-runstatus [--color|--nocolor] [--amend] [--verbose]";
8
+
9
+int cmd_runstatus(int argc, const char **argv, const char *prefix)
10
+{
11
+ struct wt_status s;
12
+ int i;
13
+
14
+ git_config(git_status_config);
15
+ wt_status_prepare(&s);
16
+
17
+ for (i = 1; i < argc; i++) {
18
+ if (!strcmp(argv[i], "--color"))
19
+ wt_status_use_color = 1;
20
+ else if (!strcmp(argv[i], "--nocolor"))
21
+ wt_status_use_color = 0;
22
+ else if (!strcmp(argv[i], "--amend")) {
23
+ s.amend = 1;
24
+ s.reference = "HEAD^1";
25
+ }
26
+ else if (!strcmp(argv[i], "--verbose"))
27
+ s.verbose = 1;
28
+ else
29
+ usage(runstatus_usage);
30
+ }
31
+
32
+ wt_status_print(&s);
33
+ return s.commitable ? 0 : 1;
34
+}
builtin.h
+1
@@ -47,6 +47,7 @@ extern int cmd_repo_config(int argc, const char **argv, const char *prefix);
47
extern int cmd_rev_list(int argc, const char **argv, const char *prefix);
48
extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
49
extern int cmd_rm(int argc, const char **argv, const char *prefix);
50
+extern int cmd_runstatus(int argc, const char **argv, const char *prefix);
51
extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
52
extern int cmd_show(int argc, const char **argv, const char *prefix);
53
extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
dir.c
+7
@@ -397,3 +397,10 @@ int read_directory(struct dir_struct *dir, const char *path, const char *base, i
397
qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
398
return dir->nr;
399
}
400
+
401
+int
402
+file_exists(const char *f)
403
+{
404
+ struct stat sb;
405
+ return stat(f, &sb) == 0;
406
+}
dir.h
+1
@@ -47,5 +47,6 @@ extern int excluded(struct dir_struct *, const char *);
47
extern void add_excludes_from_file(struct dir_struct *, const char *fname);
48
extern void add_exclude(const char *string, const char *base,
49
int baselen, struct exclude_list *which);
50
+extern int file_exists(const char *);
51
52
#endif
git-commit.sh
+7
-99
@@ -60,26 +60,6 @@ report () {
60
}
61
62
run_status () {
63
- (
64
- # We always show status for the whole tree.
65
- cd "$TOP"
66
-
67
- IS_INITIAL="$initial_commit"
68
- REFERENCE=HEAD
69
- case "$amend" in
70
- t)
71
- # If we are amending the initial commit, there
72
- # is no HEAD^1.
73
- if git-rev-parse --verify "HEAD^1" >/dev/null 2>&1
74
- then
75
- REFERENCE="HEAD^1"
76
- IS_INITIAL=
77
- else
78
- IS_INITIAL=t
79
- fi
80
- ;;
81
- esac
82
-
63
# If TMP_INDEX is defined, that means we are doing
64
# "--only" partial commit, and that index file is used
65
# to build the tree for the commit. Otherwise, if
@@ -96,85 +76,13 @@ run_status () {
76
export GIT_INDEX_FILE
77
fi
78
99
- case "$branch" in
100
- refs/heads/master) ;;
101
- *) echo "# On branch $branch" ;;
102
- esac
103
-
104
- if test -z "$IS_INITIAL"
105
- then
106
- git-diff-index -M --cached --name-status \
107
- --diff-filter=MDTCRA $REFERENCE |
108
- sed -e '
109
- s/\\/\\\\/g
110
- s/ /\\ /g
111
- ' |
112
- report "Updated but not checked in" "will commit"
113
- committable="$?"
114
- else
115
- echo '#
116
-# Initial commit
117
-#'
118
- git-ls-files |
119
- sed -e '
120
- s/\\/\\\\/g
121
- s/ /\\ /g
122
- s/^/A /
123
- ' |
124
- report "Updated but not checked in" "will commit"
125
-
126
- committable="$?"
127
- fi
128
-
129
- git-diff-files --name-status |
130
- sed -e '
131
- s/\\/\\\\/g
132
- s/ /\\ /g
133
- ' |
134
- report "Changed but not updated" \
135
- "use git-update-index to mark for commit"
136
-
137
- option=""
138
- if test -z "$untracked_files"; then
139
- option="--directory --no-empty-directory"
140
- fi
141
- hdr_shown=
142
- if test -f "$GIT_DIR/info/exclude"
143
- then
144
- git-ls-files --others $option \
145
- --exclude-from="$GIT_DIR/info/exclude" \
146
- --exclude-per-directory=.gitignore
147
- else
148
- git-ls-files --others $option \
149
- --exclude-per-directory=.gitignore
150
- fi |
151
- while read line; do
152
- if [ -z "$hdr_shown" ]; then
153
- echo '#'
154
- echo '# Untracked files:'
155
- echo '# (use "git add" to add to commit)'
156
- echo '#'
157
- hdr_shown=1
158
- fi
159
- echo "# $line"
160
- done
161
-
162
- if test -n "$verbose" -a -z "$IS_INITIAL"
163
- then
164
- git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
165
- fi
166
- case "$committable" in
167
- 0)
168
- case "$amend" in
169
- t)
170
- echo "# No changes" ;;
171
- *)
172
- echo "nothing to commit" ;;
173
- esac
174
- exit 1 ;;
175
- esac
176
- exit 0
177
- )
79
+ case "$status_only" in
80
+ t) color= ;;
81
+ *) color=--nocolor ;;
82
+ esac
83
+ git-runstatus ${color} \
84
+ ${verbose:+--verbose} \
85
+ ${amend:+--amend}
86
}
87
88
trap '
git.c
+1
@@ -252,6 +252,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
252
{ "rev-list", cmd_rev_list, RUN_SETUP },
253
{ "rev-parse", cmd_rev_parse, RUN_SETUP },
254
{ "rm", cmd_rm, RUN_SETUP },
255
+ { "runstatus", cmd_runstatus, RUN_SETUP },
256
{ "show-branch", cmd_show_branch, RUN_SETUP },
257
{ "show", cmd_show, RUN_SETUP | USE_PAGER },
258
{ "stripspace", cmd_stripspace },
wt-status.c
new
+271
@@ -0,0 +1,271 @@
1
+#include "wt-status.h"
2
+#include "color.h"
3
+#include "cache.h"
4
+#include "object.h"
5
+#include "dir.h"
6
+#include "commit.h"
7
+#include "diff.h"
8
+#include "revision.h"
9
+#include "diffcore.h"
10
+
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 */
17
+};
18
+
19
+static int parse_status_slot(const char *var, int offset)
20
+{
21
+ if (!strcasecmp(var+offset, "header"))
22
+ return WT_STATUS_HEADER;
23
+ if (!strcasecmp(var+offset, "updated"))
24
+ return WT_STATUS_UPDATED;
25
+ if (!strcasecmp(var+offset, "changed"))
26
+ return WT_STATUS_CHANGED;
27
+ if (!strcasecmp(var+offset, "untracked"))
28
+ return WT_STATUS_UNTRACKED;
29
+ die("bad config variable '%s'", var);
30
+}
31
+
32
+static const char* color(int slot)
33
+{
34
+ return wt_status_use_color ? wt_status_colors[slot] : "";
35
+}
36
+
37
+void wt_status_prepare(struct wt_status *s)
38
+{
39
+ unsigned char sha1[20];
40
+ const char *head;
41
+
42
+ s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
43
+
44
+ head = resolve_ref(git_path("HEAD"), sha1, 0);
45
+ s->branch = head ?
46
+ strdup(head + strlen(get_git_dir()) + 1) :
47
+ NULL;
48
+
49
+ s->reference = "HEAD";
50
+ s->amend = 0;
51
+ s->verbose = 0;
52
+ s->commitable = 0;
53
+}
54
+
55
+static void wt_status_print_header(const char *main, const char *sub)
56
+{
57
+ const char *c = color(WT_STATUS_HEADER);
58
+ color_printf_ln(c, "# %s:", main);
59
+ color_printf_ln(c, "# (%s)", sub);
60
+ color_printf_ln(c, "#");
61
+}
62
+
63
+static void wt_status_print_trailer(void)
64
+{
65
+ color_printf_ln(color(WT_STATUS_HEADER), "#");
66
+}
67
+
68
+static void wt_status_print_filepair(int t, struct diff_filepair *p)
69
+{
70
+ const char *c = color(t);
71
+ color_printf(color(WT_STATUS_HEADER), "#\t");
72
+ switch (p->status) {
73
+ case DIFF_STATUS_ADDED:
74
+ color_printf(c, "new file: %s", p->one->path); break;
75
+ case DIFF_STATUS_COPIED:
76
+ color_printf(c, "copied: %s -> %s",
77
+ p->one->path, p->two->path);
78
+ break;
79
+ case DIFF_STATUS_DELETED:
80
+ color_printf_ln(c, "deleted: %s", p->one->path); break;
81
+ case DIFF_STATUS_MODIFIED:
82
+ color_printf(c, "modified: %s", p->one->path); break;
83
+ case DIFF_STATUS_RENAMED:
84
+ color_printf(c, "renamed: %s -> %s",
85
+ p->one->path, p->two->path);
86
+ break;
87
+ case DIFF_STATUS_TYPE_CHANGED:
88
+ color_printf(c, "typechange: %s", p->one->path); break;
89
+ case DIFF_STATUS_UNKNOWN:
90
+ color_printf(c, "unknown: %s", p->one->path); break;
91
+ case DIFF_STATUS_UNMERGED:
92
+ color_printf(c, "unmerged: %s", p->one->path); break;
93
+ default:
94
+ die("bug: unhandled diff status %c", p->status);
95
+ }
96
+ printf("\n");
97
+}
98
+
99
+static void wt_status_print_updated_cb(struct diff_queue_struct *q,
100
+ struct diff_options *options,
101
+ void *data)
102
+{
103
+ struct wt_status *s = data;
104
+ int shown_header = 0;
105
+ int i;
106
+ if (q->nr) {
107
+ }
108
+ for (i = 0; i < q->nr; i++) {
109
+ if (q->queue[i]->status == 'U')
110
+ continue;
111
+ if (!shown_header) {
112
+ wt_status_print_header("Updated but not checked in",
113
+ "will commit");
114
+ s->commitable = 1;
115
+ shown_header = 1;
116
+ }
117
+ wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
118
+ }
119
+ if (shown_header)
120
+ wt_status_print_trailer();
121
+}
122
+
123
+static void wt_status_print_changed_cb(struct diff_queue_struct *q,
124
+ struct diff_options *options,
125
+ void *data)
126
+{
127
+ int i;
128
+ if (q->nr)
129
+ wt_status_print_header("Changed but not updated",
130
+ "use git-update-index to mark for commit");
131
+ for (i = 0; i < q->nr; i++)
132
+ wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
133
+ if (q->nr)
134
+ wt_status_print_trailer();
135
+}
136
+
137
+void wt_status_print_initial(struct wt_status *s)
138
+{
139
+ int i;
140
+ read_cache();
141
+ if (active_nr) {
142
+ s->commitable = 1;
143
+ wt_status_print_header("Updated but not checked in",
144
+ "will commit");
145
+ }
146
+ for (i = 0; i < active_nr; i++) {
147
+ color_printf(color(WT_STATUS_HEADER), "#\t");
148
+ color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
149
+ active_cache[i]->name);
150
+ }
151
+ if (active_nr)
152
+ wt_status_print_trailer();
153
+}
154
+
155
+static void wt_status_print_updated(struct wt_status *s)
156
+{
157
+ struct rev_info rev;
158
+ const char *argv[] = { NULL, NULL, NULL };
159
+ argv[1] = s->reference;
160
+ init_revisions(&rev, NULL);
161
+ setup_revisions(2, argv, &rev, NULL);
162
+ rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
163
+ rev.diffopt.format_callback = wt_status_print_updated_cb;
164
+ rev.diffopt.format_callback_data = s;
165
+ rev.diffopt.detect_rename = 1;
166
+ run_diff_index(&rev, 1);
167
+}
168
+
169
+static void wt_status_print_changed(struct wt_status *s)
170
+{
171
+ struct rev_info rev;
172
+ const char *argv[] = { NULL, NULL };
173
+ init_revisions(&rev, "");
174
+ setup_revisions(1, argv, &rev, NULL);
175
+ rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
176
+ rev.diffopt.format_callback = wt_status_print_changed_cb;
177
+ rev.diffopt.format_callback_data = s;
178
+ run_diff_files(&rev, 0);
179
+}
180
+
181
+static void wt_status_print_untracked(const struct wt_status *s)
182
+{
183
+ struct dir_struct dir;
184
+ const char *x;
185
+ int i;
186
+ int shown_header = 0;
187
+
188
+ memset(&dir, 0, sizeof(dir));
189
+
190
+ dir.exclude_per_dir = ".gitignore";
191
+ x = git_path("info/exclude");
192
+ if (file_exists(x))
193
+ add_excludes_from_file(&dir, x);
194
+
195
+ read_directory(&dir, ".", "", 0);
196
+ for(i = 0; i < dir.nr; i++) {
197
+ /* check for matching entry, which is unmerged; lifted from
198
+ * builtin-ls-files:show_other_files */
199
+ struct dir_entry *ent = dir.entries[i];
200
+ int pos = cache_name_pos(ent->name, ent->len);
201
+ struct cache_entry *ce;
202
+ if (0 <= pos)
203
+ die("bug in wt_status_print_untracked");
204
+ pos = -pos - 1;
205
+ if (pos < active_nr) {
206
+ ce = active_cache[pos];
207
+ if (ce_namelen(ce) == ent->len &&
208
+ !memcmp(ce->name, ent->name, ent->len))
209
+ continue;
210
+ }
211
+ if (!shown_header) {
212
+ wt_status_print_header("Untracked files",
213
+ "use \"git add\" to add to commit");
214
+ shown_header = 1;
215
+ }
216
+ color_printf(color(WT_STATUS_HEADER), "#\t");
217
+ color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
218
+ ent->len, ent->name);
219
+ }
220
+}
221
+
222
+static void wt_status_print_verbose(struct wt_status *s)
223
+{
224
+ struct rev_info rev;
225
+ const char *argv[] = { NULL, NULL, NULL };
226
+ argv[1] = s->reference;
227
+ init_revisions(&rev, NULL);
228
+ setup_revisions(2, argv, &rev, NULL);
229
+ rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
230
+ rev.diffopt.detect_rename = 1;
231
+ run_diff_index(&rev, 1);
232
+}
233
+
234
+void wt_status_print(struct wt_status *s)
235
+{
236
+ if (s->branch && strcmp(s->branch, "refs/heads/master"))
237
+ color_printf_ln(color(WT_STATUS_HEADER),
238
+ "# On branch %s", s->branch);
239
+
240
+ if (s->is_initial) {
241
+ color_printf_ln(color(WT_STATUS_HEADER), "#");
242
+ color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
243
+ color_printf_ln(color(WT_STATUS_HEADER), "#");
244
+ wt_status_print_initial(s);
245
+ }
246
+ else {
247
+ wt_status_print_updated(s);
248
+ discard_cache();
249
+ }
250
+
251
+ wt_status_print_changed(s);
252
+ wt_status_print_untracked(s);
253
+
254
+ if (s->verbose && !s->is_initial)
255
+ wt_status_print_verbose(s);
256
+ if (!s->commitable)
257
+ printf("%s\n", s->amend ? "# No changes" : "nothing to commit");
258
+}
259
+
260
+int git_status_config(const char *k, const char *v)
261
+{
262
+ if (!strcmp(k, "status.color")) {
263
+ wt_status_use_color = git_config_colorbool(k, v);
264
+ return 0;
265
+ }
266
+ if (!strncmp(k, "status.color.", 13)) {
267
+ int slot = parse_status_slot(k, 13);
268
+ color_parse(v, k, wt_status_colors[slot]);
269
+ }
270
+ return git_default_config(k, v);
271
+}
wt-status.h
new
+24
@@ -0,0 +1,24 @@
1
+#ifndef STATUS_H
2
+#define STATUS_H
3
+
4
+enum color_wt_status {
5
+ WT_STATUS_HEADER,
6
+ WT_STATUS_UPDATED,
7
+ WT_STATUS_CHANGED,
8
+ WT_STATUS_UNTRACKED,
9
+};
10
+
11
+struct wt_status {
12
+ int is_initial;
13
+ char *branch;
14
+ const char *reference;
15
+ int commitable;
16
+ int verbose;
17
+ int amend;
18
+};
19
+
20
+int git_status_config(const char *var, const char *value);
21
+void wt_status_prepare(struct wt_status *s);
22
+void wt_status_print(struct wt_status *s);
23
+
24
+#endif /* STATUS_H */