@matheusbsbs / matheus-bagliano / commits / 3a946802bb
Fork 0

git-status: quote LF in its output

Otherwise, commit log template would get the remainder of the filename start on a new line unquoted and the log gets messed up. I initially considered using the full quote_c_style(), but the output from the command is primarily for human consumption so chose to leave other control characters and bytes with high-bits unmolested. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 8, 2006 at 13:20 UTC 3a946802bb2c1106d12861849414f66fdba62303
1 file changed +53 -11
wt-status.c
+53 -11
@@ -64,31 +64,70 @@ static void wt_status_print_trailer(void)
64 color_printf_ln(color(WT_STATUS_HEADER), "#");
65 }
66
67 +static const char *quote_crlf(const char *in, char *buf, size_t sz)
68 +{
69 + const char *scan;
70 + char *out;
71 + const char *ret = in;
72 +
73 + for (scan = in, out = buf; *scan; scan++) {
74 + int ch = *scan;
75 + int quoted;
76 +
77 + switch (ch) {
78 + case '\n':
79 + quoted = 'n';
80 + break;
81 + case '\r':
82 + quoted = 'r';
83 + break;
84 + default:
85 + *out++ = ch;
86 + continue;
87 + }
88 + *out++ = '\\';
89 + *out++ = quoted;
90 + ret = buf;
91 + }
92 + *out = '\0';
93 + return ret;
94 +}
95 +
96 static void wt_status_print_filepair(int t, struct diff_filepair *p)
97 {
98 const char *c = color(t);
99 + const char *one, *two;
100 + char onebuf[PATH_MAX], twobuf[PATH_MAX];
101 +
102 + one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
103 + two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
104 +
105 color_printf(color(WT_STATUS_HEADER), "#\t");
106 switch (p->status) {
107 case DIFF_STATUS_ADDED:
73 - color_printf(c, "new file: %s", p->one->path); break;
108 + color_printf(c, "new file: %s", one);
109 + break;
110 case DIFF_STATUS_COPIED:
75 - color_printf(c, "copied: %s -> %s",
76 - p->one->path, p->two->path);
111 + color_printf(c, "copied: %s -> %s", one, two);
112 break;
113 case DIFF_STATUS_DELETED:
79 - color_printf(c, "deleted: %s", p->one->path); break;
114 + color_printf(c, "deleted: %s", one);
115 + break;
116 case DIFF_STATUS_MODIFIED:
81 - color_printf(c, "modified: %s", p->one->path); break;
117 + color_printf(c, "modified: %s", one);
118 + break;
119 case DIFF_STATUS_RENAMED:
83 - color_printf(c, "renamed: %s -> %s",
84 - p->one->path, p->two->path);
120 + color_printf(c, "renamed: %s -> %s", one, two);
121 break;
122 case DIFF_STATUS_TYPE_CHANGED:
87 - color_printf(c, "typechange: %s", p->one->path); break;
123 + color_printf(c, "typechange: %s", one);
124 + break;
125 case DIFF_STATUS_UNKNOWN:
89 - color_printf(c, "unknown: %s", p->one->path); break;
126 + color_printf(c, "unknown: %s", one);
127 + break;
128 case DIFF_STATUS_UNMERGED:
91 - color_printf(c, "unmerged: %s", p->one->path); break;
129 + color_printf(c, "unmerged: %s", one);
130 + break;
131 default:
132 die("bug: unhandled diff status %c", p->status);
133 }
@@ -134,6 +173,8 @@ static void wt_status_print_changed_cb(struct diff_queue_struct *q,
173 void wt_status_print_initial(struct wt_status *s)
174 {
175 int i;
176 + char buf[PATH_MAX];
177 +
178 read_cache();
179 if (active_nr) {
180 s->commitable = 1;
@@ -143,7 +184,8 @@ void wt_status_print_initial(struct wt_status *s)
184 for (i = 0; i < active_nr; i++) {
185 color_printf(color(WT_STATUS_HEADER), "#\t");
186 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
146 - active_cache[i]->name);
187 + quote_crlf(active_cache[i]->name,
188 + buf, sizeof(buf)));
189 }
190 if (active_nr)
191 wt_status_print_trailer();