commit 79109fed13e01f6d05dc3f2440f8219324b130a6 from: Stefan Sperling date: Tue Mar 27 19:49:19 2018 UTC implement 'got log -p' option commit - 1e70621d6183f2a0cfe932867a0ab10416716517 commit + 79109fed13e01f6d05dc3f2440f8219324b130a6 blob - a65d4dbee29227ec4046e164cfdd079034bc1d3c blob + abe2f70def14a1346d10f4191b8224393aa0ed42 --- got/Makefile +++ got/Makefile @@ -1,8 +1,8 @@ .PATH:${.CURDIR}/../lib PROG= got -SRCS= got.c delta.c error.c fileindex.c object.c path.c pack.c \ - refs.c repository.c sha1.c worktree.c zbuf.c +SRCS= got.c delta.c diff.c diffreg.c error.c fileindex.c object.c \ + path.c pack.c refs.c repository.c sha1.c worktree.c zbuf.c CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib LDADD = -lutil -lz blob - a6835cf83d6ba5a723e73b9ba06015687c518723 blob + 84b212f11bdf093996b9fad66acea32f441cf7b2 --- got/got.1 +++ got/got.1 @@ -77,6 +77,9 @@ be checked out. .\"Show current status of files. .It Cm log Display history of the repository. +If the +.Fl p +flag is given, display the patch of modifications made in each commit. .El .Sh EXIT STATUS .Ex -std got blob - 13b85d189e296f21bdfcc167e616d268fafb20d2 blob + d519a175ce949911b9ac093255d03ff3b11d2c38 --- got/got.c +++ got/got.c @@ -31,6 +31,7 @@ #include "got_refs.h" #include "got_repository.h" #include "got_worktree.h" +#include "got_diff.h" #ifndef nitems #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) @@ -231,10 +232,58 @@ done: } static const struct got_error * -print_commit(struct got_commit_object *commit, struct got_object_id *id, +print_patch(struct got_commit_object *commit, struct got_object_id *id, struct got_repository *repo) { const struct got_error *err = NULL; + struct got_tree_object *tree1 = NULL, *tree2; + struct got_object *obj; + struct got_parent_id *pid; + + err = got_object_open(&obj, repo, commit->tree_id); + if (err) + return err; + + err = got_object_tree_open(&tree2, repo, obj); + got_object_close(obj); + if (err) + return err; + + pid = SIMPLEQ_FIRST(&commit->parent_ids); + if (pid != NULL) { + struct got_commit_object *pcommit; + + err = got_object_open(&obj, repo, pid->id); + if (err) + return err; + + err = got_object_commit_open(&pcommit, repo, obj); + got_object_close(obj); + if (err) + return err; + + err = got_object_open(&obj, repo, pcommit->tree_id); + got_object_commit_close(pcommit); + if (err) + return err; + err = got_object_tree_open(&tree1, repo, obj); + got_object_close(obj); + if (err) + return err; + } + + err = got_diff_tree(tree1, tree2, repo, stdout); + if (tree1) + got_object_tree_close(tree1); + got_object_tree_close(tree2); + return err; +} + +static const struct got_error * +print_commit(struct got_commit_object *commit, struct got_object_id *id, + struct got_repository *repo, int show_patch) +{ + const struct got_error *err = NULL; char *buf; err = got_object_id_str(&buf, id); @@ -246,8 +295,11 @@ print_commit(struct got_commit_object *commit, struct printf("Author: %s\n", commit->author); printf("\n%s\n", commit->logmsg); + if (show_patch) + err = print_patch(commit, id, repo); + free(buf); - return NULL; + return err; } struct commit_queue_entry { @@ -258,7 +310,7 @@ struct commit_queue_entry { static const struct got_error * print_commits(struct got_object *root_obj, struct got_object_id *root_id, - struct got_repository *repo) + struct got_repository *repo, int show_patch) { const struct got_error *err; struct got_commit_object *root_commit; @@ -287,7 +339,7 @@ print_commits(struct got_object *root_obj, struct got_ struct got_parent_id *pid; entry = TAILQ_FIRST(&commits); - err = print_commit(entry->commit, entry->id, repo); + err = print_commit(entry->commit, entry->id, repo, show_patch); if (err) break; @@ -350,16 +402,33 @@ cmd_log(int argc, char *argv[]) struct got_object_id *id; struct got_object *obj; char *repo_path = NULL; + int ch; + int show_patch = 0; #ifndef PROFILE if (pledge("stdio rpath wpath cpath", NULL) == -1) err(1, "pledge"); #endif - if (argc == 1) { + + while ((ch = getopt(argc, argv, "p")) != -1) { + switch (ch) { + case 'p': + show_patch = 1; + break; + default: + usage(); + /* NOTREACHED */ + } + } + + argc -= optind; + argv += optind; + + if (argc == 0) { repo_path = getcwd(NULL, 0); if (repo_path == NULL) err(1, "getcwd"); - } else if (argc == 2) + } else if (argc == 1) repo_path = argv[1]; else usage_log(); @@ -378,7 +447,7 @@ cmd_log(int argc, char *argv[]) if (error != NULL) return error; if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) - error = print_commits(obj, id, repo); + error = print_commits(obj, id, repo, show_patch); else error = got_error(GOT_ERR_OBJ_TYPE); got_object_close(obj);