add path filtering support to 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 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
diff --git a/got/got.c b/got/got.c
index f5d0166..17cf228 100644
--- a/got/got.c
+++ b/got/got.c
@@ -370,63 +370,21 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
}
static const struct got_error *
-detect_change(int *changed, struct got_commit_object *commit,
- const char *path, struct got_repository *repo)
-{
- const struct got_error *err = NULL;
- struct got_commit_object *pcommit = NULL;
- struct got_tree_object *tree = NULL, *ptree = NULL;
- struct got_object_qid *pid;
-
- *changed = 0;
-
- err = got_object_open_as_tree(&tree, repo, commit->tree_id);
- if (err)
- return err;
-
- pid = SIMPLEQ_FIRST(&commit->parent_ids);
- if (pid == NULL) {
- *changed = 1;
- goto done;
- }
-
- err = got_object_open_as_commit(&pcommit, repo, pid->id);
- if (err)
- goto done;
-
- err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
- if (err)
- goto done;
-
- err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
-done:
- if (tree)
- got_object_tree_close(tree);
- if (ptree)
- got_object_tree_close(ptree);
- if (pcommit)
- got_object_commit_close(pcommit);
- return err;
-}
-
-static const struct got_error *
print_commits(struct got_object *root_obj, struct got_object_id *root_id,
struct got_repository *repo, char *path, int show_patch, int limit,
int first_parent_traversal)
{
const struct got_error *err;
struct got_commit_graph *graph;
- int ncommits, found_path = 0;
- int is_root_path = (strcmp(path, "/") == 0);
- err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
- repo);
+ err = got_commit_graph_open(&graph, root_id, path,
+ first_parent_traversal, repo);
if (err)
return err;
- err = got_commit_graph_iter_start(graph, root_id);
+ err = got_commit_graph_iter_start(graph, root_id, repo);
if (err)
goto done;
- do {
+ while (1) {
struct got_commit_object *commit;
struct got_object_id *id;
@@ -438,8 +396,7 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
}
if (err->code != GOT_ERR_ITER_NEED_MORE)
break;
- err = got_commit_graph_fetch_commits(&ncommits,
- graph, 1, repo);
+ err = got_commit_graph_fetch_commits(graph, 1, repo);
if (err)
break;
else
@@ -451,52 +408,11 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
err = got_object_open_as_commit(&commit, repo, id);
if (err)
break;
- if (!is_root_path) {
- struct got_object_id *obj_id = NULL;
- int changed;
-
- err = detect_change(&changed, commit, path, repo);
- if (err) {
- if (err->code == GOT_ERR_NO_OBJ) {
- /*
- * History of the path stops here
- * on the current commit's branch.
- * Keep logging on other branches.
- */
- err = NULL;
- got_object_commit_close(commit);
- continue;
- }
- return err;
- }
- if (!changed) {
- got_object_commit_close(commit);
- continue;
- }
-
- err = got_object_id_by_path(&obj_id, repo, id, path);
- if (err) {
- if (err->code == GOT_ERR_NO_OBJ && found_path) {
- /*
- * History of the path stops here
- * on the current commit's branch.
- * Keep logging on other branches.
- */
- err = NULL;
- got_object_commit_close(commit);
- continue;
- }
- got_object_commit_close(commit);
- return err;
- }
- found_path = 1;
- free(obj_id);
- }
err = print_commit(commit, id, repo, show_patch);
got_object_commit_close(commit);
if (err || (limit && --limit == 0))
break;
- } while (ncommits > 0);
+ }
done:
got_commit_graph_close(graph);
return err;
diff --git a/include/got_commit_graph.h b/include/got_commit_graph.h
index 80e6b81..fbe86e3 100644
--- a/include/got_commit_graph.h
+++ b/include/got_commit_graph.h
@@ -17,15 +17,16 @@
struct got_commit_graph;
const struct got_error *got_commit_graph_open(struct got_commit_graph **,
- struct got_object_id *commit_id, int, struct got_repository *repo);
+ struct got_object_id *commit_id, const char *, int,
+ struct got_repository *repo);
void got_commit_graph_close(struct got_commit_graph *);
const struct got_error *
-got_commit_graph_fetch_commits(int *, struct got_commit_graph *, int,
+got_commit_graph_fetch_commits(struct got_commit_graph *, int,
struct got_repository *);
const struct got_error *got_commit_graph_fetch_commits_up_to(int *,
struct got_commit_graph *, struct got_object_id *, struct got_repository *);
const struct got_error *got_commit_graph_iter_start(
- struct got_commit_graph *, struct got_object_id *);
+ struct got_commit_graph *, struct got_object_id *, struct got_repository *);
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 352d0b0..43f6b03 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -162,8 +162,8 @@ const struct got_tree_entries *got_object_tree_get_entries(
/*
* Compare two trees and indicate whether the entry at the specified path
- * differs. The path must not be the root path "/"; got_object_id_dup() can
- * be used to compare the tree roots instead.
+ * differs between them. The path must not be the root path "/"; the function
+ * got_object_id_cmp() should be used instead to compare the tree roots.
*/
const struct got_error *got_object_tree_path_changed(int *,
struct got_tree_object *, struct got_tree_object *, const char *,
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 65e8cca..830f24e 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -34,6 +34,7 @@
#include "got_lib_inflate.h"
#include "got_lib_object.h"
#include "got_lib_object_idset.h"
+#include "got_lib_path.h"
struct got_commit_graph_node {
struct got_object_id id;
@@ -91,6 +92,9 @@ struct got_commit_graph {
size_t ntips;
#define GOT_COMMIT_GRAPH_MIN_TIPS 100 /* minimum amount of tips to allocate */
+ /* Path of tree entry of interest to the API user. */
+ char *path;
+
/* The next commit to return when the API user asks for one. */
struct got_commit_graph_node *iter_node;
@@ -99,7 +103,7 @@ struct got_commit_graph {
};
static struct got_commit_graph *
-alloc_graph(void)
+alloc_graph(const char *path)
{
struct got_commit_graph *graph;
@@ -107,8 +111,15 @@ alloc_graph(void)
if (graph == NULL)
return NULL;
+ graph->path = strdup(path);
+ if (graph->path == NULL) {
+ free(graph);
+ return NULL;
+ }
+
graph->node_ids = got_object_idset_alloc();
if (graph->node_ids == NULL) {
+ free(graph->path);
free(graph);
return NULL;
}
@@ -116,6 +127,7 @@ alloc_graph(void)
graph->open_branches = got_object_idset_alloc();
if (graph->open_branches == NULL) {
got_object_idset_free(graph->node_ids);
+ free(graph->path);
free(graph);
return NULL;
}
@@ -150,6 +162,58 @@ is_root_node(struct got_commit_graph_node *node)
}
#endif
+static const struct got_error *
+detect_changed_path(int *changed, struct got_commit_object *commit,
+ struct got_object_id *commit_id, const char *path,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_commit_object *pcommit = NULL;
+ struct got_tree_object *tree = NULL, *ptree = NULL;
+ struct got_object_qid *pid;
+
+ if (got_path_is_root_dir(path)) {
+ *changed = 1;
+ return NULL;
+ }
+
+ *changed = 0;
+
+ err = got_object_open_as_tree(&tree, repo, commit->tree_id);
+ if (err)
+ return err;
+
+ pid = SIMPLEQ_FIRST(&commit->parent_ids);
+ if (pid == NULL) {
+ struct got_object_id *obj_id;
+ err = got_object_id_by_path(&obj_id, repo, commit_id, path);
+ if (err)
+ err = NULL;
+ else
+ *changed = 1;
+ free(obj_id);
+ goto done;
+ }
+
+ err = got_object_open_as_commit(&pcommit, repo, pid->id);
+ if (err)
+ goto done;
+
+ err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
+ if (err)
+ goto done;
+
+ err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
+done:
+ if (tree)
+ got_object_tree_close(tree);
+ if (ptree)
+ got_object_tree_close(ptree);
+ if (pcommit)
+ got_object_commit_close(pcommit);
+ return err;
+}
+
static void
add_node_to_iter_list(struct got_commit_graph *graph,
struct got_commit_graph_node *node,
@@ -230,15 +294,26 @@ 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,
+close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
+{
+ const struct got_error *err;
+
+ err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
+ if (err && err->code != GOT_ERR_NO_OBJ)
+ return err;
+ return NULL;
+}
+
+static const struct got_error *
+advance_branch(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(NULL, graph->open_branches, commit_id);
- if (err && err->code != GOT_ERR_NO_OBJ)
+ err = close_branch(graph, commit_id);
+ if (err)
return err;
if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
@@ -278,11 +353,13 @@ free_node(struct got_commit_graph_node *node)
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)
+ struct got_commit_object *commit, struct got_commit_graph_node *child_node,
+ struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_commit_graph_node *node;
struct got_object_qid *pid;
+ int changed = 0, branch_done = 0;
*new_node = NULL;
@@ -314,8 +391,29 @@ add_node(struct got_commit_graph_node **new_node,
return err;
}
- add_node_to_iter_list(graph, node, child_node);
- err = advance_open_branches(graph, node, commit_id, commit);
+ err = detect_changed_path(&changed, commit, commit_id, graph->path,
+ repo);
+ if (err) {
+ if (err->code == GOT_ERR_NO_OBJ) {
+ /*
+ * History of the path stops here on the current
+ * branch. Keep going on other branches.
+ */
+ err = NULL;
+ branch_done = 1;
+ } else {
+ free_node(node);
+ return err;
+ }
+ }
+
+ if (changed)
+ add_node_to_iter_list(graph, node, child_node);
+
+ if (branch_done)
+ err = close_branch(graph, commit_id);
+ else
+ err = advance_branch(graph, node, commit_id, commit);
if (err)
free_node(node);
else
@@ -326,8 +424,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, int first_parent_traversal,
- struct got_repository *repo)
+ struct got_object_id *commit_id, const char *path,
+ int first_parent_traversal, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_commit_object *commit;
@@ -338,7 +436,16 @@ got_commit_graph_open(struct got_commit_graph **graph,
if (err)
return err;
- *graph = alloc_graph();
+ /* The path must exist in our initial commit. */
+ if (!got_path_is_root_dir(path)) {
+ struct got_object_id *obj_id;
+ err = got_object_id_by_path(&obj_id, repo, commit_id, path);
+ if (err)
+ return err;
+ free(obj_id);
+ }
+
+ *graph = alloc_graph(path);
if (*graph == NULL) {
got_object_commit_close(commit);
return got_error_from_errno();
@@ -347,7 +454,8 @@ got_commit_graph_open(struct got_commit_graph **graph,
if (first_parent_traversal)
(*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
- err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
+ err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
+ repo);
got_object_commit_close(commit);
if (err) {
got_commit_graph_close(*graph);
@@ -374,8 +482,8 @@ gather_branch_tips(struct got_object_id *id, void *data, void *arg)
static const struct got_error *
fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
- struct got_commit_graph *graph, struct got_repository *repo,
- struct got_object_id *wanted_id)
+ struct got_object_id **changed_id, struct got_commit_graph *graph,
+ struct got_repository *repo, struct got_object_id *wanted_id)
{
const struct got_error *err;
struct gather_branch_tips_arg arg;
@@ -384,6 +492,8 @@ fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
*ncommits = 0;
if (wanted_id_added)
*wanted_id_added = 0;
+ if (changed_id)
+ *changed_id = NULL;
arg.ntips = got_object_idset_num_elements(graph->open_branches);
if (arg.ntips == 0)
@@ -412,6 +522,7 @@ fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
struct got_object_id *commit_id;
struct got_commit_graph_node *child_node, *new_node;
struct got_commit_object *commit;
+ int changed;
commit_id = &graph->tips[i].id;
child_node = graph->tips[i].node;
@@ -420,7 +531,21 @@ fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
if (err)
break;
- err = add_node(&new_node, graph, commit_id, commit, child_node);
+ err = detect_changed_path(&changed, commit, commit_id,
+ graph->path, repo);
+ if (err) {
+ got_object_commit_close(commit);
+ if (err->code != GOT_ERR_NO_OBJ)
+ break;
+ err = close_branch(graph, commit_id);
+ if (err)
+ break;
+ continue;
+ }
+ if (changed && changed_id && *changed_id == NULL)
+ *changed_id = commit_id;
+ err = add_node(&new_node, graph, commit_id, commit, child_node,
+ repo);
got_object_commit_close(commit);
if (err)
break;
@@ -434,22 +559,22 @@ fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
}
const struct got_error *
-got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
- int limit, struct got_repository *repo)
+got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
+ struct got_repository *repo)
{
const struct got_error *err;
- int ncommits;
-
- *nfetched = 0;
+ int nfetched = 0, ncommits;
+ struct got_object_id *changed_id = NULL;
- while (*nfetched < limit) {
+ while (nfetched < limit) {
err = fetch_commits_from_open_branches(&ncommits, NULL,
- graph, repo, NULL);
+ &changed_id, graph, repo, NULL);
if (err)
return err;
if (ncommits == 0)
break;
- *nfetched += ncommits;
+ if (changed_id)
+ nfetched += ncommits;
}
return NULL;
@@ -470,7 +595,7 @@ got_commit_graph_fetch_commits_up_to(int *nfetched,
while (!wanted_id_added) {
err = fetch_commits_from_open_branches(&ncommits,
- &wanted_id_added, graph, repo, wanted_id);
+ &wanted_id_added, NULL, graph, repo, wanted_id);
if (err)
return err;
if (ncommits == 0)
@@ -495,19 +620,51 @@ got_commit_graph_close(struct got_commit_graph *graph)
got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
got_object_idset_free(graph->node_ids);
free(graph->tips);
+ free(graph->path);
free(graph);
}
const struct got_error *
got_commit_graph_iter_start(struct got_commit_graph *graph,
- struct got_object_id *id)
+ struct got_object_id *id, struct got_repository *repo)
{
+ const struct got_error *err = NULL;
struct got_commit_graph_node *start_node;
+ struct got_commit_object *commit;
+ int changed;
start_node = got_object_idset_get(graph->node_ids, id);
if (start_node == NULL)
return got_error(GOT_ERR_NO_OBJ);
+
+ err = got_object_open_as_commit(&commit, repo, &start_node->id);
+ if (err)
+ return err;
+
+ err = detect_changed_path(&changed, commit, &start_node->id,
+ graph->path, repo);
+ if (err) {
+ got_object_commit_close(commit);
+ return err;
+ }
+
+ if (!changed) {
+ /* Locate first commit which changed graph->path. */
+ struct got_object_id *changed_id = NULL;
+ while (changed_id == NULL) {
+ int ncommits;
+ err = fetch_commits_from_open_branches(&ncommits, NULL,
+ &changed_id, graph, repo, NULL);
+ if (err) {
+ got_object_commit_close(commit);
+ return err;
+ }
+ }
+ start_node = got_object_idset_get(graph->node_ids, changed_id);
+ }
+ got_object_commit_close(commit);
+
graph->iter_node = start_node;
return NULL;
}
diff --git a/lib/got_lib_path.h b/lib/got_lib_path.h
index 1d1d155..46595f8 100644
--- a/lib/got_lib_path.h
+++ b/lib/got_lib_path.h
@@ -48,3 +48,6 @@ const struct got_error *got_canonpath(const char *, char *, size_t);
*/
const struct got_error *got_path_skip_common_ancestor(char **, const char *,
const char *);
+
+/* Determine whether a path points to the root directory "/" . */
+int got_path_is_root_dir(const char *);
diff --git a/lib/path.c b/lib/path.c
index e874cb7..c78fdb2 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -133,3 +133,9 @@ got_path_skip_common_ancestor(char **child, const char *parent_abspath,
}
return NULL;
}
+
+int
+got_path_is_root_dir(const char *path)
+{
+ return (path[0] == '/' && path[1] == '\0');
+}
diff --git a/tog/tog.c b/tog/tog.c
index 76613e9..b727865 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -764,23 +764,21 @@ queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
const struct got_error *err = NULL;
struct got_object_id *id;
struct commit_queue_entry *entry;
- int nfetched, nqueued = 0, found_obj = 0;
+ int nqueued = 0, found_obj = 0;
int is_root_path = strcmp(path, "/") == 0;
- err = got_commit_graph_iter_start(graph, start_id);
+ err = got_commit_graph_iter_start(graph, start_id, repo);
if (err)
return err;
entry = TAILQ_LAST(&commits->head, commit_queue_head);
if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
- int nfetched;
-
/* Start ID's commit is already on the queue; skip over it. */
err = got_commit_graph_iter_next(&id, graph);
if (err && err->code != GOT_ERR_ITER_NEED_MORE)
return err;
- err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
+ err = got_commit_graph_fetch_commits(graph, 1, repo);
if (err)
return err;
}
@@ -796,8 +794,7 @@ queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
err = NULL;
break;
}
- err = got_commit_graph_fetch_commits(&nfetched,
- graph, 1, repo);
+ err = got_commit_graph_fetch_commits(graph, 1, repo);
if (err)
return err;
continue;
@@ -1167,7 +1164,7 @@ open_log_view(struct tog_view *view, struct got_object_id *start_id,
return err;
/* The graph contains all commits. */
- err = got_commit_graph_open(&s->graph, head_id, 0, repo);
+ err = got_commit_graph_open(&s->graph, head_id, "/", 0, repo);
if (err)
goto done;
/* The commit queue contains a subset of commits filtered by path. */