Refactor print-functions in builtin-branch
This moves the guts of print_ref_list() into a revamped print_ref_info(), which at the same time gets renamed to print_ref_item(). Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Lars Hjemli committed
Jan 3, 2007 at 21:10 UTC
af0e4ac0ecf7cad57d9405f188ce0944e7f9b166
1 file changed
+40
-43
builtin-branch.c
+40
-43
@@ -231,29 +231,54 @@ static int ref_cmp(const void *r1, const void *r2)
231
return strcmp(c1->name, c2->name);
232
}
233
234
-static void print_ref_info(const unsigned char *sha1, int abbrev)
234
+static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
235
+ int abbrev, int current)
236
{
237
+ char c;
238
+ int color;
239
struct commit *commit;
240
char subject[256];
241
242
+ switch (item->kind) {
243
+ case REF_LOCAL_BRANCH:
244
+ color = COLOR_BRANCH_LOCAL;
245
+ break;
246
+ case REF_REMOTE_BRANCH:
247
+ color = COLOR_BRANCH_REMOTE;
248
+ break;
249
+ default:
250
+ color = COLOR_BRANCH_PLAIN;
251
+ break;
252
+ }
253
240
- commit = lookup_commit(sha1);
241
- if (commit && !parse_commit(commit))
242
- pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
243
- subject, sizeof(subject), 0,
244
- NULL, NULL, 0);
245
- else
246
- strcpy(subject, " **** invalid ref ****");
254
+ c = ' ';
255
+ if (current) {
256
+ c = '*';
257
+ color = COLOR_BRANCH_CURRENT;
258
+ }
259
248
- printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
260
+ if (verbose) {
261
+ commit = lookup_commit(item->sha1);
262
+ if (commit && !parse_commit(commit))
263
+ pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
264
+ subject, sizeof(subject), 0,
265
+ NULL, NULL, 0);
266
+ else
267
+ strcpy(subject, " **** invalid ref ****");
268
+ printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color),
269
+ maxwidth, item->name,
270
+ branch_get_color(COLOR_BRANCH_RESET),
271
+ find_unique_abbrev(item->sha1, abbrev), subject);
272
+ } else {
273
+ printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
274
+ branch_get_color(COLOR_BRANCH_RESET));
275
+ }
276
}
277
278
static void print_ref_list(int kinds, int verbose, int abbrev)
279
{
280
int i;
254
- char c;
281
struct ref_list ref_list;
256
- int color;
282
283
memset(&ref_list, 0, sizeof(ref_list));
284
ref_list.kinds = kinds;
@@ -262,38 +287,10 @@ static void print_ref_list(int kinds, int verbose, int abbrev)
287
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
288
289
for (i = 0; i < ref_list.index; i++) {
265
- switch( ref_list.list[i].kind ) {
266
- case REF_LOCAL_BRANCH:
267
- color = COLOR_BRANCH_LOCAL;
268
- break;
269
- case REF_REMOTE_BRANCH:
270
- color = COLOR_BRANCH_REMOTE;
271
- break;
272
- default:
273
- color = COLOR_BRANCH_PLAIN;
274
- break;
275
- }
276
-
277
- c = ' ';
278
- if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
279
- !strcmp(ref_list.list[i].name, head)) {
280
- c = '*';
281
- color = COLOR_BRANCH_CURRENT;
282
- }
283
-
284
- if (verbose) {
285
- printf("%c %s%-*s%s", c,
286
- branch_get_color(color),
287
- ref_list.maxwidth,
288
- ref_list.list[i].name,
289
- branch_get_color(COLOR_BRANCH_RESET));
290
- print_ref_info(ref_list.list[i].sha1, abbrev);
291
- }
292
- else
293
- printf("%c %s%s%s\n", c,
294
- branch_get_color(color),
295
- ref_list.list[i].name,
296
- branch_get_color(COLOR_BRANCH_RESET));
290
+ int current = (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
291
+ !strcmp(ref_list.list[i].name, head);
292
+ print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
293
+ abbrev, current);
294
}
295
296
free_ref_list(&ref_list);