implement 'got log -p' option
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
diff --git a/got/Makefile b/got/Makefile
index a65d4db..abe2f70 100644
--- a/got/Makefile
+++ b/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
diff --git a/got/got.1 b/got/got.1
index a6835cf..84b212f 100644
--- a/got/got.1
+++ b/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
diff --git a/got/got.c b/got/got.c
index 13b85d1..d519a17 100644
--- a/got/got.c
+++ b/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 got_object_id *id,
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_object_id *root_id,
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);