Add basic commit formatting to log output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
diff --git a/examples/log.c b/examples/log.c
index 5603d74..12c1d56 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -117,6 +117,32 @@ static int add_revision(struct log_state *s, const char *revstr)
return 0;
}
+static void print_time(const git_time *intime, const char *prefix)
+{
+ char sign, out[32];
+ struct tm intm;
+ int offset, hours, minutes;
+ time_t t;
+
+ offset = intime->offset;
+ if (offset < 0) {
+ sign = '-';
+ offset = -offset;
+ } else {
+ sign = '+';
+ }
+
+ hours = offset / 60;
+ minutes = offset % 60;
+
+ t = (time_t)intime->time + (intime->offset * 60);
+
+ gmtime_r(&t, &intm);
+ strftime(out, sizeof(out), "%a %b %d %T %Y", &intm);
+
+ printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes);
+}
+
struct log_options {
int show_diff;
int skip;
@@ -125,7 +151,6 @@ struct log_options {
git_time_t after;
char *author;
char *committer;
-
};
int main(int argc, char *argv[])
@@ -173,12 +198,39 @@ int main(int argc, char *argv[])
paths.count = argc - i;
while (!git_revwalk_next(&oid, s.walker)) {
+ const git_signature *sig;
+ const char *scan, *eol;
+
check(git_commit_lookup(&commit, s.repo, &oid),
"Failed to look up commit", NULL);
- git_commit_free(commit);
git_oid_tostr(buf, sizeof(buf), &oid);
- printf("%s\n", buf);
+ printf("commit %s\n", buf);
+
+ if ((count = (int)git_commit_parentcount(commit)) > 1) {
+ printf("Merge:");
+ for (i = 0; i < count; ++i) {
+ git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
+ printf(" %s", buf);
+ }
+ printf("\n");
+ }
+
+ if ((sig = git_commit_author(commit)) != NULL) {
+ printf("Author: %s <%s>\n", sig->name, sig->email);
+ print_time(&sig->when, "Date: ");
+ }
+ printf("\n");
+
+ for (scan = git_commit_message(commit); scan && *scan; ) {
+ for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */;
+
+ printf(" %.*s\n", (int)(eol - scan), scan);
+ scan = *eol ? eol + 1 : NULL;
+ }
+ printf("\n");
+
+ git_commit_free(commit);
}
git_revwalk_free(s.walker);