baseline

yet another open-source distributed versioning control system
Log | Files | Refs

commit 98b16df79a33caf1ba0771f234e739fc68b093ae
parent 5541dd23b076e21e94c7d9cae7e36ce9094b8e6e
Author: Mohamed Aslan <maslan@sce.carleton.ca>
Date:   Sun,  5 Oct 2014 19:18:42 -0600

add support for output formatting (-f flag) in cmd-log.c

Diffstat:
Mcmd-log.c | 94++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 88 insertions(+), 6 deletions(-)

diff --git a/cmd-log.c b/cmd-log.c @@ -18,34 +18,116 @@ #include <stdlib.h> /* EXIT_SUCCESS */ #include <string.h> /* strdup(3) */ #include <time.h> /* ctime(3) */ +#include <unistd.h> /* getopt(3) */ #include "cmd.h" #include "session.h" #include "objects.h" +static char *default_fmt = + "commit: %i\n" + "author:\n" + "\tname: %an\n" + "\temail: %ae\n" + "\ttime: %at\n" + "committer:\n" + "\tname: %cn\n" + "\temail: %ce\n" + "\ttime: %ct\n" + "message:\n%m\n"; + int cmd_log(int argc, char **argv) { - char *head; - int done = 0; + char *head = NULL, *fmtstr = NULL; + char timestr[26]; + int ch, done = 0, fmt = 0, i, k = 0; struct session s; struct commit *comm; baseline_session_begin(&s, 0); + /* parse command line options */ + while ((ch = getopt(argc, argv, "f:")) != -1) { + switch (ch) { + case 'f': + fmt = 1; + fmtstr = strdup(optarg); + break; + default: + exit(EXIT_FAILURE); + } + } + argc -= optind; + argv += optind; + s.db_ops->branch_get_head(s.db_ctx, s.branch, &head); if (head == NULL) goto ret; + if (!fmt) + fmtstr = default_fmt; + do { + k++; comm = baseline_commit_new(); s.db_ops->select_commit(s.db_ctx, head, comm); - printf("commit: %s\n", comm->id); - printf("author:\n\tname: %s\n\temail: %s\n\ttime: %s", comm->author.name, comm->author.email, ctime(&(comm->author.timestamp))); - printf("committer:\n\tname: %s\n\temail: %s\n\ttime: %s", comm->committer.name, comm->committer.email, ctime(&(comm->committer.timestamp))); - printf("message:\n%s\n", comm->message); + for (i = 0 ; i<strlen(fmtstr) ; i++) { + if ((i < strlen(fmtstr) - 1) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'n')) { + printf("%d", k); + i++; + } + else if ((i < strlen(fmtstr) - 1) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'i')) { + printf("%s", comm->id); + i++; + } + else if ((i < strlen(fmtstr) - 2) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'a') && (fmtstr[i+2] == 'n')) { + printf("%s", comm->author.name); + i += 2; + } + else if ((i < strlen(fmtstr) - 2) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'a') && (fmtstr[i+2] == 'e')) { + printf("%s", comm->author.email); + i += 2; + } + else if ((i < strlen(fmtstr) - 2) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'a') && (fmtstr[i+2] == 't')) { + bzero(timestr, sizeof(timestr)); + ctime_r(&(comm->author.timestamp), timestr); + timestr[sizeof(timestr) - 2] = '\0'; + printf("%s", timestr); + i += 2; + } + else if ((i < strlen(fmtstr) - 2) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'c') && (fmtstr[i+2] == 'n')) { + printf("%s", comm->committer.name); + i += 2; + } + else if ((i < strlen(fmtstr) - 2) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'c') && (fmtstr[i+2] == 'e')) { + printf("%s", comm->committer.email); + i += 2; + } + else if ((i < strlen(fmtstr) - 2) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'c') && (fmtstr[i+2] == 't')) { + bzero(timestr, sizeof(timestr)); + ctime_r(&(comm->committer.timestamp), timestr); + timestr[sizeof(timestr) - 2] = '\0'; + printf("%s", timestr); + i += 2; + } + else if ((i < strlen(fmtstr) - 1) && (fmtstr[i] == '%') && (fmtstr[i + 1] == 'm')) { + printf("%s", comm->message); + i++; + } + else if ((i < strlen(fmtstr) - 1) && (fmtstr[i] == '\\') && (fmtstr[i + 1] == 'n')) { + printf("\n"); + i++; + } + else if ((i < strlen(fmtstr) - 1) && (fmtstr[i] == '\\') && (fmtstr[i + 1] == 't')) { + printf("\t"); + i++; + } + else + printf("%c", fmtstr[i]); + } if (comm->n_parents == 0) done = 1; else