do not store commits in the commit graph; saves memory
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
diff --git a/got/got.c b/got/got.c
index 82edaba..e89b380 100644
--- a/got/got.c
+++ b/got/got.c
@@ -388,10 +388,11 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
if (err)
return err;
do {
+ struct got_object *obj;
struct got_commit_object *commit;
struct got_object_id *id;
- err = got_commit_graph_iter_next(&commit, &id, graph);
+ err = got_commit_graph_iter_next(&id, graph);
if (err) {
if (err->code != GOT_ERR_ITER_NEED_MORE)
break;
@@ -402,9 +403,19 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
else
continue;
}
- if (commit == NULL)
+ if (id == NULL)
break;
+ err = got_object_open(&obj, repo, id);
+ if (err)
+ return err;
+
+ err = got_object_commit_open(&commit, repo, obj);
+ got_object_close(obj);
+ if (err)
+ return err;
+
err = print_commit(commit, id, repo, show_patch, verbose);
+ got_object_commit_close(commit);
if (err || (limit && --limit == 0))
break;
} while (ncommits > 0);
diff --git a/include/got_commit_graph.h b/include/got_commit_graph.h
index a8916ce..f18494c 100644
--- a/include/got_commit_graph.h
+++ b/include/got_commit_graph.h
@@ -25,5 +25,5 @@ got_commit_graph_fetch_commits(int *, struct got_commit_graph *, int,
struct got_repository *);
const struct got_error *got_commit_graph_iter_start(
struct got_commit_graph *, struct got_object_id *);
-const struct got_error *got_commit_graph_iter_next(struct got_commit_object **,
- struct got_object_id **, struct got_commit_graph *);
+const struct got_error *got_commit_graph_iter_next(struct got_object_id **,
+ struct got_commit_graph *);
diff --git a/include/got_object.h b/include/got_object.h
index 69cba31..9eb68be 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -35,10 +35,12 @@ struct got_parent_id {
struct got_object_id *id;
};
+SIMPLEQ_HEAD(got_object_id_list, got_parent_id);
+
struct got_commit_object {
struct got_object_id *tree_id;
unsigned int nparents;
- SIMPLEQ_HEAD(, got_parent_id) parent_ids;
+ struct got_object_id_list parent_ids;
char *author;
time_t author_time; /* local time */
char *author_tzoff; /* timezone offset description */
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 1f3c145..53c0fcf 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -44,9 +44,12 @@ struct got_commit_graph_node {
* Adjacencies of a graph node are either parent (older) commits,
* and child (younger) commits.
*/
- struct got_commit_object *commit; /* contains list of parents */
+ int nparents;
+ struct got_object_id_list parent_ids;
int nchildren;
- SIMPLEQ_HEAD(, got_parent_id) child_ids;
+ struct got_object_id_list child_ids;
+
+ time_t commit_timestamp;
/* Used during graph iteration. */
TAILQ_ENTRY(got_commit_graph_node) entry;
@@ -118,7 +121,7 @@ is_head_node(struct got_commit_graph_node *node)
static int
is_merge_point(struct got_commit_graph_node *node)
{
- return node->commit->nparents > 1;
+ return node->nparents > 1;
}
int
@@ -131,21 +134,7 @@ is_branch_point(struct got_commit_graph_node *node)
static int
is_root_node(struct got_commit_graph_node *node)
{
- return node->commit->nparents == 0;
-}
-
-static int
-compare_commits(struct got_commit_object *c1, struct got_commit_object *c2)
-{
- time_t t1, t2;
-
- t1 = c1->committer_time;
- t2 = c2->committer_time;
- if (t1 == t2)
- return 0;
- else if (t1 < t2)
- return -1;
- return 1;
+ return node->nparents == 0;
}
static void
@@ -160,16 +149,14 @@ add_iteration_candidate(struct got_commit_graph *graph,
}
TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
- int cmp = compare_commits(node->commit, n->commit);
- if (cmp < 0) {
+ if (node->commit_timestamp < n->commit_timestamp) {
next = TAILQ_NEXT(n, entry);
if (next == NULL) {
TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
node, entry);
break;
}
- cmp = compare_commits(node->commit, next->commit);
- if (cmp >= 0) {
+ if (node->commit_timestamp >= next->commit_timestamp) {
TAILQ_INSERT_BEFORE(next, node, entry);
break;
}
@@ -181,12 +168,33 @@ add_iteration_candidate(struct got_commit_graph *graph,
}
static const struct got_error *
+add_vertex(struct got_object_id_list *id_list, struct got_object_id *id)
+{
+ struct got_parent_id *pid;
+
+ pid = calloc(1, sizeof(*pid));
+ if (pid == NULL)
+ return got_error_from_errno();
+
+ pid->id = got_object_id_dup(id);
+ if (pid->id == NULL) {
+ const struct got_error *err = got_error_from_errno();
+ free(pid);
+ return err;
+ }
+
+ SIMPLEQ_INSERT_TAIL(id_list, pid, entry);
+ 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_object_id *child_commit_id)
{
const struct got_error *err = NULL;
struct got_commit_graph_node *node, *existing_node;
+ struct got_parent_id *pid;
*new_node = NULL;
@@ -195,8 +203,15 @@ add_node(struct got_commit_graph_node **new_node,
return got_error_from_errno();
memcpy(&node->id, commit_id, sizeof(node->id));
- node->commit = commit;
+ SIMPLEQ_INIT(&node->parent_ids);
SIMPLEQ_INIT(&node->child_ids);
+ SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
+ err = add_vertex(&node->parent_ids, pid->id);
+ if (err)
+ return err;
+ node->nparents++;
+ }
+ node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
err = got_object_idset_add((void **)(&existing_node),
graph->node_ids, &node->id, node);
@@ -226,7 +241,7 @@ add_node(struct got_commit_graph_node **new_node,
}
if (child_commit_id) {
- struct got_parent_id *child, *cid;
+ struct got_parent_id *cid;
/* Prevent linking to self. */
if (got_object_id_cmp(commit_id, child_commit_id) == 0)
@@ -238,17 +253,11 @@ add_node(struct got_commit_graph_node **new_node,
return got_error(GOT_ERR_BAD_OBJ_ID);
}
- child = calloc(1, sizeof(*child));
- if (child == NULL)
- return got_error_from_errno();
- child->id = got_object_id_dup(child_commit_id);
- if (child->id == NULL) {
- err = got_error_from_errno();
- free(child);
+ err = add_vertex(&node->child_ids, child_commit_id);
+ if (err)
return err;
- }
- SIMPLEQ_INSERT_TAIL(&node->child_ids, child, entry);
node->nchildren++;
+
}
return err;
@@ -285,6 +294,7 @@ got_commit_graph_open(struct got_commit_graph **graph,
}
err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
+ got_object_commit_close(commit);
if (err) {
got_commit_graph_close(*graph);
*graph = NULL;
@@ -374,6 +384,7 @@ fetch_commits_from_open_branches(int *ncommits,
err = add_node(&new_node, graph, commit_id, commit,
&child_node->id);
+ got_object_commit_close(commit);
if (err) {
if (err->code != GOT_ERR_OBJ_EXISTS)
break;
@@ -381,8 +392,6 @@ fetch_commits_from_open_branches(int *ncommits,
}
if (new_node)
(*ncommits)++;
- else
- got_object_commit_close(commit);
}
free(branches);
@@ -415,7 +424,6 @@ static void
free_graph_node(struct got_object_id *id, void *data, void *arg)
{
struct got_commit_graph_node *node = data;
- got_object_commit_close(node->commit);
while (!SIMPLEQ_EMPTY(&node->child_ids)) {
struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
@@ -452,7 +460,7 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
}
/* Put all known parents of this commit on the candidate list. */
- SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
+ SIMPLEQ_FOREACH(pid, &start_node->parent_ids, entry) {
node = got_object_idset_get(graph->node_ids, pid->id);
if (node)
add_iteration_candidate(graph, node);
@@ -462,14 +470,13 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
}
const struct got_error *
-got_commit_graph_iter_next(struct got_commit_object **commit,
- struct got_object_id **id, struct got_commit_graph *graph)
+got_commit_graph_iter_next(struct got_object_id **id,
+ struct got_commit_graph *graph)
{
struct got_commit_graph_node *node;
if (graph->iter_node == NULL) {
/* We are done interating, or iteration was not started. */
- *commit = NULL;
*id = NULL;
return NULL;
}
@@ -477,7 +484,6 @@ got_commit_graph_iter_next(struct got_commit_object **commit,
if (TAILQ_EMPTY(&graph->iter_candidates)) {
if (is_root_node(graph->iter_node) &&
got_object_idset_num_elements(graph->open_branches) == 0) {
- *commit = graph->iter_node->commit;
*id = &graph->iter_node->id;
/* We are done interating. */
graph->iter_node = NULL;
@@ -486,7 +492,6 @@ got_commit_graph_iter_next(struct got_commit_object **commit,
return got_error(GOT_ERR_ITER_NEED_MORE);
}
- *commit = graph->iter_node->commit;
*id = &graph->iter_node->id;
node = TAILQ_FIRST(&graph->iter_candidates);
TAILQ_REMOVE(&graph->iter_candidates, node, entry);