@matheusbsbs / matheus-bagliano / commits / 367c98866c
Fork 0

git status: show relative paths when run in a subdirectory

To show the relative paths, the function formerly called quote_crlf() (now called quote_path()) takes the prefix as an additional argument. While at it, the static buffers were replaced by strbufs. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 11, 2007 at 17:35 UTC 367c98866c340bc9cf5cfa88c3b69f027165fc44
5 files changed +146 -29
builtin-commit.c
+7 -6
@@ -118,11 +118,12 @@ static char *prepare_index(const char **files, const char *prefix)
118 return next_index_lock->filename;
119 }
120
121 -static int run_status(FILE *fp, const char *index_file)
121 +static int run_status(FILE *fp, const char *index_file, const char *prefix)
122 {
123 struct wt_status s;
124
125 wt_status_prepare(&s);
126 + s.prefix = prefix;
127
128 if (amend) {
129 s.amend = 1;
@@ -140,7 +141,7 @@ static int run_status(FILE *fp, const char *index_file)
141
142 static const char sign_off_header[] = "Signed-off-by: ";
143
143 -static int prepare_log_message(const char *index_file)
144 +static int prepare_log_message(const char *index_file, const char *prefix)
145 {
146 struct stat statbuf;
147 int commitable;
@@ -216,7 +217,7 @@ static int prepare_log_message(const char *index_file)
217 if (only_include_assumed)
218 fprintf(fp, "# %s\n", only_include_assumed);
219
219 - commitable = run_status(fp, index_file);
220 + commitable = run_status(fp, index_file, prefix);
221
222 fclose(fp);
223
@@ -409,7 +410,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
410
411 index_file = prepare_index(argv, prefix);
412
412 - commitable = run_status(stdout, index_file);
413 + commitable = run_status(stdout, index_file, prefix);
414
415 rollback_lock_file(&lock_file);
416
@@ -503,8 +504,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
504 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
505 exit(1);
506
506 - if (!prepare_log_message(index_file) && !in_merge) {
507 - run_status(stdout, index_file);
507 + if (!prepare_log_message(index_file, prefix) && !in_merge) {
508 + run_status(stdout, index_file, prefix);
509 unlink(commit_editmsg);
510 return 1;
511 }
builtin-runstatus.c
+1
@@ -14,6 +14,7 @@ int cmd_runstatus(int argc, const char **argv, const char *prefix)
14
15 git_config(git_status_config);
16 wt_status_prepare(&s);
17 + s.prefix = prefix;
18
19 for (i = 1; i < argc; i++) {
20 if (!strcmp(argv[i], "--color"))
t/t7502-status.sh new
+91
@@ -0,0 +1,91 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 Johannes E. Schindelin
4 +#
5 +
6 +test_description='git-status'
7 +
8 +. ./test-lib.sh
9 +
10 +test_expect_success 'setup' '
11 + : > tracked &&
12 + : > modified &&
13 + mkdir dir1 &&
14 + : > dir1/tracked &&
15 + : > dir1/modified &&
16 + mkdir dir2 &&
17 + : > dir1/tracked &&
18 + : > dir1/modified &&
19 + git add . &&
20 + test_tick &&
21 + git commit -m initial &&
22 + : > untracked &&
23 + : > dir1/untracked &&
24 + : > dir2/untracked &&
25 + echo 1 > dir1/modified &&
26 + echo 2 > dir2/modified &&
27 + echo 3 > dir2/added &&
28 + git add dir2/added
29 +'
30 +
31 +cat > expect << \EOF
32 +# On branch master
33 +# Changes to be committed:
34 +# (use "git reset HEAD <file>..." to unstage)
35 +#
36 +# new file: dir2/added
37 +#
38 +# Changed but not updated:
39 +# (use "git add <file>..." to update what will be committed)
40 +#
41 +# modified: dir1/modified
42 +#
43 +# Untracked files:
44 +# (use "git add <file>..." to include in what will be committed)
45 +#
46 +# dir1/untracked
47 +# dir2/modified
48 +# dir2/untracked
49 +# expect
50 +# output
51 +# untracked
52 +EOF
53 +
54 +test_expect_success 'status' '
55 +
56 + git status > output &&
57 + git diff expect output
58 +
59 +'
60 +
61 +cat > expect << \EOF
62 +# On branch master
63 +# Changes to be committed:
64 +# (use "git reset HEAD <file>..." to unstage)
65 +#
66 +# new file: ../dir2/added
67 +#
68 +# Changed but not updated:
69 +# (use "git add <file>..." to update what will be committed)
70 +#
71 +# modified: ../dir1/modified
72 +#
73 +# Untracked files:
74 +# (use "git add <file>..." to include in what will be committed)
75 +#
76 +# untracked
77 +# ../dir2/modified
78 +# ../dir2/untracked
79 +# ../expect
80 +# ../output
81 +# ../untracked
82 +EOF
83 +
84 +test_expect_success 'status with relative paths' '
85 +
86 + (cd dir1 && git status) > output &&
87 + git diff expect output
88 +
89 +'
90 +
91 +test_done
wt-status.c
+46 -23
@@ -81,33 +81,46 @@ static void wt_status_print_trailer(struct wt_status *s)
81 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
82 }
83
84 -static const char *quote_crlf(const char *in, char *buf, size_t sz)
84 +static char *quote_path(const char *in, int len,
85 + struct strbuf *out, const char *prefix)
86 {
86 - const char *scan;
87 - char *out;
88 - const char *ret = in;
87 + if (len > 0)
88 + strbuf_grow(out, len);
89 + strbuf_setlen(out, 0);
90 +
91 + if (prefix) {
92 + int off = 0;
93 + while (prefix[off] && off < len && prefix[off] == in[off])
94 + if (prefix[off] == '/') {
95 + prefix += off + 1;
96 + in += off + 1;
97 + len -= off + 1;
98 + off = 0;
99 + } else
100 + off++;
101 +
102 + for (; *prefix; prefix++)
103 + if (*prefix == '/')
104 + strbuf_addstr(out, "../");
105 + }
106
90 - for (scan = in, out = buf; *scan; scan++) {
91 - int ch = *scan;
92 - int quoted;
107 + for (; (len < 0 && *in) || len > 0; in++, len--) {
108 + int ch = *in;
109
110 switch (ch) {
111 case '\n':
96 - quoted = 'n';
112 + strbuf_addstr(out, "\\n");
113 break;
114 case '\r':
99 - quoted = 'r';
115 + strbuf_addstr(out, "\\r");
116 break;
117 default:
102 - *out++ = ch;
118 + strbuf_addch(out, ch);
119 continue;
120 }
105 - *out++ = '\\';
106 - *out++ = quoted;
107 - ret = buf;
121 }
109 - *out = '\0';
110 - return ret;
122 +
123 + return out->buf;
124 }
125
126 static void wt_status_print_filepair(struct wt_status *s,
@@ -115,10 +128,12 @@ static void wt_status_print_filepair(struct wt_status *s,
128 {
129 const char *c = color(t);
130 const char *one, *two;
118 - char onebuf[PATH_MAX], twobuf[PATH_MAX];
131 + struct strbuf onebuf, twobuf;
132
120 - one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
121 - two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
133 + strbuf_init(&onebuf, 0);
134 + strbuf_init(&twobuf, 0);
135 + one = quote_path(p->one->path, -1, &onebuf, s->prefix);
136 + two = quote_path(p->two->path, -1, &twobuf, s->prefix);
137
138 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
139 switch (p->status) {
@@ -150,6 +165,8 @@ static void wt_status_print_filepair(struct wt_status *s,
165 die("bug: unhandled diff status %c", p->status);
166 }
167 fprintf(s->fp, "\n");
168 + strbuf_release(&onebuf);
169 + strbuf_release(&twobuf);
170 }
171
172 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
@@ -204,8 +221,9 @@ static void wt_read_cache(struct wt_status *s)
221 static void wt_status_print_initial(struct wt_status *s)
222 {
223 int i;
207 - char buf[PATH_MAX];
224 + struct strbuf buf;
225
226 + strbuf_init(&buf, 0);
227 wt_read_cache(s);
228 if (active_nr) {
229 s->commitable = 1;
@@ -214,11 +232,12 @@ static void wt_status_print_initial(struct wt_status *s)
232 for (i = 0; i < active_nr; i++) {
233 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
234 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
217 - quote_crlf(active_cache[i]->name,
218 - buf, sizeof(buf)));
235 + quote_path(active_cache[i]->name, -1,
236 + &buf, s->prefix));
237 }
238 if (active_nr)
239 wt_status_print_trailer(s);
240 + strbuf_release(&buf);
241 }
242
243 static void wt_status_print_updated(struct wt_status *s)
@@ -252,7 +271,9 @@ static void wt_status_print_untracked(struct wt_status *s)
271 struct dir_struct dir;
272 int i;
273 int shown_header = 0;
274 + struct strbuf buf;
275
276 + strbuf_init(&buf, 0);
277 memset(&dir, 0, sizeof(dir));
278
279 if (!s->untracked) {
@@ -284,9 +305,11 @@ static void wt_status_print_untracked(struct wt_status *s)
305 shown_header = 1;
306 }
307 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
287 - color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%.*s",
288 - ent->len, ent->name);
308 + color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
309 + quote_path(ent->name, ent->len,
310 + &buf, s->prefix));
311 }
312 + strbuf_release(&buf);
313 }
314
315 static void wt_status_print_verbose(struct wt_status *s)
wt-status.h
+1
@@ -23,6 +23,7 @@ struct wt_status {
23 int workdir_untracked;
24 const char *index_file;
25 FILE *fp;
26 + const char *prefix;
27 };
28
29 int git_status_config(const char *var, const char *value);