Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c95e8e3

Browse files
authored
Merge pull request #6359 from albfan/fix-log-example
fix log example
2 parents d7336ae + c97989e commit c95e8e3

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

examples/log.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static int add_revision(struct log_state *s, const char *revstr);
5050
/** log_options holds other command line options that affect log output */
5151
struct log_options {
5252
int show_diff;
53+
int show_oneline;
5354
int show_log_size;
5455
int skip, limit;
5556
int min_parents, max_parents;
@@ -81,9 +82,11 @@ int lg2_log(git_repository *repo, int argc, char *argv[])
8182
git_commit *commit = NULL;
8283
git_pathspec *ps = NULL;
8384

85+
memset(&s, 0, sizeof(s));
86+
8487
/** Parse arguments and set up revwalker. */
85-
last_arg = parse_options(&s, &opt, argc, argv);
8688
s.repo = repo;
89+
last_arg = parse_options(&s, &opt, argc, argv);
8790

8891
diffopts.pathspec.strings = &argv[last_arg];
8992
diffopts.pathspec.count = argc - last_arg;
@@ -335,34 +338,45 @@ static void print_commit(git_commit *commit, struct log_options *opts)
335338
const char *scan, *eol;
336339

337340
git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
338-
printf("commit %s\n", buf);
339341

340-
if (opts->show_log_size) {
341-
printf("log size %d\n", (int)strlen(git_commit_message(commit)));
342-
}
342+
if (opts->show_oneline) {
343+
printf("%s ", buf);
344+
} else {
345+
printf("commit %s\n", buf);
343346

344-
if ((count = (int)git_commit_parentcount(commit)) > 1) {
345-
printf("Merge:");
346-
for (i = 0; i < count; ++i) {
347-
git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
348-
printf(" %s", buf);
347+
if (opts->show_log_size) {
348+
printf("log size %d\n", (int)strlen(git_commit_message(commit)));
349+
}
350+
351+
if ((count = (int)git_commit_parentcount(commit)) > 1) {
352+
printf("Merge:");
353+
for (i = 0; i < count; ++i) {
354+
git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
355+
printf(" %s", buf);
356+
}
357+
printf("\n");
349358
}
350-
printf("\n");
351-
}
352359

353-
if ((sig = git_commit_author(commit)) != NULL) {
354-
printf("Author: %s <%s>\n", sig->name, sig->email);
355-
print_time(&sig->when, "Date: ");
360+
if ((sig = git_commit_author(commit)) != NULL) {
361+
printf("Author: %s <%s>\n", sig->name, sig->email);
362+
print_time(&sig->when, "Date: ");
363+
}
364+
printf("\n");
356365
}
357-
printf("\n");
358366

359367
for (scan = git_commit_message(commit); scan && *scan; ) {
360368
for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */;
361369

362-
printf(" %.*s\n", (int)(eol - scan), scan);
370+
if (opts->show_oneline)
371+
printf("%.*s\n", (int)(eol - scan), scan);
372+
else
373+
printf(" %.*s\n", (int)(eol - scan), scan);
363374
scan = *eol ? eol + 1 : NULL;
375+
if (opts->show_oneline)
376+
break;
364377
}
365-
printf("\n");
378+
if (!opts->show_oneline)
379+
printf("\n");
366380
}
367381

368382
/** Helper to find how many files in a commit changed from its nth parent. */
@@ -407,8 +421,6 @@ static int parse_options(
407421
struct log_state *s, struct log_options *opt, int argc, char **argv)
408422
{
409423
struct args_info args = ARGS_INFO_INIT;
410-
411-
memset(s, 0, sizeof(*s));
412424
s->sorting = GIT_SORT_TIME;
413425

414426
memset(opt, 0, sizeof(*opt));
@@ -424,7 +436,7 @@ static int parse_options(
424436
else
425437
/** Try failed revision parse as filename. */
426438
break;
427-
} else if (!match_arg_separator(&args)) {
439+
} else if (match_arg_separator(&args)) {
428440
break;
429441
}
430442
else if (!strcmp(a, "--date-order"))
@@ -474,6 +486,8 @@ static int parse_options(
474486
opt->show_diff = 1;
475487
else if (!strcmp(a, "--log-size"))
476488
opt->show_log_size = 1;
489+
else if (!strcmp(a, "--oneline"))
490+
opt->show_oneline = 1;
477491
else
478492
usage("Unsupported argument", a);
479493
}

0 commit comments

Comments
 (0)