got log: allow first-parent traversal with the commit graph
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
diff --git a/got/got.1 b/got/got.1
index 3bcc62f..5afbf1c 100644
--- a/got/got.1
+++ b/got/got.1
@@ -84,7 +84,7 @@ was specified use the base name of the
.Ar path prefix .
.\".It Cm status
.\"Show current status of files.
-.It Cm log [ Fl p ] [ Fl c Ar commit ] [ Fl l Ar N ] [ Fl v ] [ Ar repository-path ]
+.It Cm log [ Fl p ] [ Fl c Ar commit ] [ Fl l Ar N ] [ Fl v ] [Fl f ] [ Ar repository-path ]
Display history of the repository.
If the
.Fl p
@@ -105,6 +105,11 @@ commits.
The
.Fl v
option enables verbose output.
+The
+.Fl f
+option restricts history traversal to the first parent of each commit.
+This shows the linear history of the current branch only, omitting any
+commits merged from other branches.
If the
.Ar repository path
is omitted, use the current working directory.
diff --git a/got/got.c b/got/got.c
index 62e4d6a..59821dd 100644
--- a/got/got.c
+++ b/got/got.c
@@ -375,13 +375,15 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
static const struct got_error *
print_commits(struct got_object *root_obj, struct got_object_id *root_id,
- struct got_repository *repo, int show_patch, int limit, int verbose)
+ struct got_repository *repo, int show_patch, int limit, int verbose,
+ int first_parent_traversal)
{
const struct got_error *err;
struct got_commit_graph *graph;
int ncommits;
- err = got_commit_graph_open(&graph, root_id, repo);
+ err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
+ repo);
if (err)
return err;
err = got_commit_graph_iter_start(graph, root_id);
@@ -425,7 +427,7 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
__dead static void
usage_log(void)
{
- fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
+ fprintf(stderr, "usage: %s log [-pf] [-c commit] [ -l N ] "
"[repository-path]\n", getprogname());
exit(1);
}
@@ -440,7 +442,7 @@ cmd_log(int argc, char *argv[])
char *repo_path = NULL;
char *start_commit = NULL;
int ch;
- int show_patch = 0, limit = 0, verbose = 0;
+ int show_patch = 0, limit = 0, verbose = 0, first_parent_traversal = 0;
const char *errstr;
#ifndef PROFILE
@@ -448,7 +450,7 @@ cmd_log(int argc, char *argv[])
err(1, "pledge");
#endif
- while ((ch = getopt(argc, argv, "pc:l:v")) != -1) {
+ while ((ch = getopt(argc, argv, "pc:l:vf")) != -1) {
switch (ch) {
case 'p':
show_patch = 1;
@@ -464,6 +466,9 @@ cmd_log(int argc, char *argv[])
case 'v':
verbose = 1;
break;
+ case 'f':
+ first_parent_traversal = 1;
+ break;
default:
usage();
/* NOTREACHED */
@@ -511,7 +516,7 @@ cmd_log(int argc, char *argv[])
return error;
if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
error = print_commits(obj, id, repo, show_patch, limit,
- verbose);
+ verbose, first_parent_traversal);
else
error = got_error(GOT_ERR_OBJ_TYPE);
got_object_close(obj);
diff --git a/include/got_commit_graph.h b/include/got_commit_graph.h
index a480124..80e6b81 100644
--- a/include/got_commit_graph.h
+++ b/include/got_commit_graph.h
@@ -17,7 +17,7 @@
struct got_commit_graph;
const struct got_error *got_commit_graph_open(struct got_commit_graph **,
- struct got_object_id *commit_id, struct got_repository *repo);
+ struct got_object_id *commit_id, int, struct got_repository *repo);
void got_commit_graph_close(struct got_commit_graph *);
const struct got_error *
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 5a88b22..9e8852e 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -64,6 +64,9 @@ struct got_commit_graph {
/* The commit at which traversal began (youngest commit in node_ids). */
struct got_commit_graph_node *head_node;
+ int flags;
+#define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
+
/*
* A set of object IDs of known parent commits which we have not yet
* traversed. Each commit ID in this set represents a branch in commit
@@ -220,6 +223,41 @@ add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
}
static const struct got_error *
+advance_open_branches(struct got_commit_graph *graph,
+ struct got_commit_graph_node *node,
+ struct got_object_id *commit_id, struct got_commit_object *commit)
+{
+ const struct got_error *err;
+ struct got_object_qid *qid;
+
+ err = got_object_idset_remove(graph->open_branches, commit_id);
+ if (err && err->code != GOT_ERR_NO_OBJ)
+ return err;
+
+ if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
+ qid = SIMPLEQ_FIRST(&commit->parent_ids);
+ if (qid == NULL)
+ return NULL;
+ err = got_object_idset_add(NULL, graph->open_branches, qid->id,
+ node);
+ if (err && err->code != GOT_ERR_OBJ_EXISTS)
+ return err;
+ return NULL;
+ }
+
+ SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
+ if (got_object_idset_get(graph->node_ids, qid->id))
+ continue; /* parent already traversed */
+ err = got_object_idset_add(NULL, graph->open_branches,
+ qid->id, node);
+ if (err && err->code != GOT_ERR_OBJ_EXISTS)
+ return err;
+ }
+
+ return NULL;
+}
+
+static const struct got_error *
add_node(struct got_commit_graph_node **new_node,
struct got_commit_graph *graph, struct got_object_id *commit_id,
struct got_commit_object *commit, struct got_commit_graph_node *child_node)
@@ -248,20 +286,8 @@ add_node(struct got_commit_graph_node **new_node,
err = got_object_idset_add((void **)(&existing_node),
graph->node_ids, &node->id, node);
if (err == NULL) {
- struct got_object_qid *qid;
-
add_node_to_iter_list(graph, node, child_node);
- err = got_object_idset_remove(graph->open_branches, commit_id);
- if (err && err->code != GOT_ERR_NO_OBJ)
- return err;
- SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
- if (got_object_idset_get(graph->node_ids, qid->id))
- continue; /* parent already traversed */
- err = got_object_idset_add(NULL, graph->open_branches,
- qid->id, node);
- if (err && err->code != GOT_ERR_OBJ_EXISTS)
- return err;
- }
+ err = advance_open_branches(graph, node, commit_id, commit);
*new_node = node;
} else if (err->code == GOT_ERR_OBJ_EXISTS) {
err = NULL;
@@ -296,7 +322,8 @@ add_node(struct got_commit_graph_node **new_node,
const struct got_error *
got_commit_graph_open(struct got_commit_graph **graph,
- struct got_object_id *commit_id, struct got_repository *repo)
+ struct got_object_id *commit_id, int first_parent_traversal,
+ struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_commit_object *commit;
@@ -313,6 +340,9 @@ got_commit_graph_open(struct got_commit_graph **graph,
return got_error_from_errno();
}
+ if (first_parent_traversal)
+ (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
+
err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
got_object_commit_close(commit);
if (err) {