builtin-name-rev.c: split deeply nested part from the main function
The main function of this command implementation tries to do too many things. Split out a handling of single input line into a separate function to reduce nesting level and clutter. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Aug 2, 2008 at 11:04 UTC
e8b55fab6237b30494aa1905c3209d5bf5494a29
1 file changed
+41
-37
builtin-name-rev.c
+41
-37
@@ -176,6 +176,45 @@ static char const * const name_rev_usage[] = {
176
NULL
177
};
178
179
+static void name_rev_line(char *p, struct name_ref_data *data)
180
+{
181
+ int forty = 0;
182
+ char *p_start;
183
+ for (p_start = p; *p; p++) {
184
+#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
185
+ if (!ishex(*p))
186
+ forty = 0;
187
+ else if (++forty == 40 &&
188
+ !ishex(*(p+1))) {
189
+ unsigned char sha1[40];
190
+ const char *name = NULL;
191
+ char c = *(p+1);
192
+
193
+ forty = 0;
194
+
195
+ *(p+1) = 0;
196
+ if (!get_sha1(p - 39, sha1)) {
197
+ struct object *o =
198
+ lookup_object(sha1);
199
+ if (o)
200
+ name = get_rev_name(o);
201
+ }
202
+ *(p+1) = c;
203
+
204
+ if (!name)
205
+ continue;
206
+
207
+ fwrite(p_start, p - p_start + 1, 1, stdout);
208
+ printf(" (%s)", name);
209
+ p_start = p + 1;
210
+ }
211
+ }
212
+
213
+ /* flush */
214
+ if (p_start != p)
215
+ fwrite(p_start, p - p_start, 1, stdout);
216
+}
217
+
218
int cmd_name_rev(int argc, const char **argv, const char *prefix)
219
{
220
struct object_array revs = { 0, 0, NULL };
@@ -234,47 +273,12 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
273
274
if (transform_stdin) {
275
char buffer[2048];
237
- char *p, *p_start;
276
277
while (!feof(stdin)) {
240
- int forty = 0;
241
- p = fgets(buffer, sizeof(buffer), stdin);
278
+ char *p = fgets(buffer, sizeof(buffer), stdin);
279
if (!p)
280
break;
244
-
245
- for (p_start = p; *p; p++) {
246
-#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
247
- if (!ishex(*p))
248
- forty = 0;
249
- else if (++forty == 40 &&
250
- !ishex(*(p+1))) {
251
- unsigned char sha1[40];
252
- const char *name = NULL;
253
- char c = *(p+1);
254
-
255
- forty = 0;
256
-
257
- *(p+1) = 0;
258
- if (!get_sha1(p - 39, sha1)) {
259
- struct object *o =
260
- lookup_object(sha1);
261
- if (o)
262
- name = get_rev_name(o);
263
- }
264
- *(p+1) = c;
265
-
266
- if (!name)
267
- continue;
268
-
269
- fwrite(p_start, p - p_start + 1, 1, stdout);
270
- printf(" (%s)", name);
271
- p_start = p + 1;
272
- }
273
- }
274
-
275
- /* flush */
276
- if (p_start != p)
277
- fwrite(p_start, p - p_start, 1, stdout);
281
+ name_rev_line(p, &data);
282
}
283
} else if (all) {
284
int i, max;