speed up 'got rebase' ancestry check
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
diff --git a/got/got.c b/got/got.c
index 753ca96..45c7c68 100644
--- a/got/got.c
+++ b/got/got.c
@@ -708,7 +708,8 @@ check_linear_ancestry(struct got_object_id *commit_id,
static const struct got_error *
check_same_branch(struct got_object_id *commit_id,
- struct got_reference *head_ref, struct got_repository *repo)
+ struct got_reference *head_ref, struct got_object_id *yca_id,
+ struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_commit_graph *graph = NULL;
@@ -719,6 +720,15 @@ check_same_branch(struct got_object_id *commit_id,
if (err)
goto done;
+ if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
+ is_same_branch = 1;
+ goto done;
+ }
+ if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
+ is_same_branch = 1;
+ goto done;
+ }
+
err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
if (err)
goto done;
@@ -743,6 +753,8 @@ check_same_branch(struct got_object_id *commit_id,
}
if (id) {
+ if (yca_id && got_object_id_cmp(id, yca_id) == 0)
+ break;
if (got_object_id_cmp(id, commit_id) == 0) {
is_same_branch = 1;
break;
@@ -933,7 +945,7 @@ cmd_checkout(int argc, char *argv[])
free(commit_id);
goto done;
}
- error = check_same_branch(commit_id, head_ref, repo);
+ error = check_same_branch(commit_id, head_ref, NULL, repo);
if (error)
goto done;
error = got_worktree_set_base_commit_id(worktree, repo,
@@ -1185,7 +1197,7 @@ cmd_update(int argc, char *argv[])
free(head_commit_id);
if (error != NULL)
goto done;
- error = check_same_branch(commit_id, head_ref, repo);
+ error = check_same_branch(commit_id, head_ref, NULL, repo);
if (error)
goto done;
error = switch_head_ref(head_ref, commit_id, worktree, repo);
@@ -1199,7 +1211,7 @@ cmd_update(int argc, char *argv[])
error = got_error(GOT_ERR_BRANCH_MOVED);
goto done;
}
- error = check_same_branch(commit_id, head_ref, repo);
+ error = check_same_branch(commit_id, head_ref, NULL, repo);
if (error)
goto done;
}
@@ -3403,7 +3415,7 @@ cmd_cherrypick(int argc, char *argv[])
if (error != NULL)
goto done;
- error = check_same_branch(commit_id, head_ref, repo);
+ error = check_same_branch(commit_id, head_ref, NULL, repo);
if (error) {
if (error->code != GOT_ERR_ANCESTRY)
goto done;
@@ -3518,7 +3530,7 @@ cmd_backout(int argc, char *argv[])
if (error != NULL)
goto done;
- error = check_same_branch(commit_id, head_ref, repo);
+ error = check_same_branch(commit_id, head_ref, NULL, repo);
if (error)
goto done;
@@ -3925,19 +3937,6 @@ cmd_rebase(int argc, char *argv[])
error = got_ref_open(&branch, repo, argv[0], 0);
if (error != NULL)
goto done;
-
- error = check_same_branch(
- got_worktree_get_base_commit_id(worktree), branch, repo);
- if (error) {
- if (error->code != GOT_ERR_ANCESTRY)
- goto done;
- error = NULL;
- } else {
- error = got_error_msg(GOT_ERR_SAME_BRANCH,
- "specified branch resolves to a commit which "
- "is already contained in work tree's branch");
- goto done;
- }
}
error = got_ref_resolve(&branch_head_commit_id, repo, branch);
@@ -3945,9 +3944,11 @@ cmd_rebase(int argc, char *argv[])
goto done;
if (!continue_rebase) {
+ struct got_object_id *base_commit_id;
+
+ base_commit_id = got_worktree_get_base_commit_id(worktree);
error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
- got_worktree_get_base_commit_id(worktree),
- branch_head_commit_id, repo);
+ base_commit_id, branch_head_commit_id, repo);
if (error)
goto done;
if (yca_id == NULL) {
@@ -3957,6 +3958,17 @@ cmd_rebase(int argc, char *argv[])
goto done;
}
+ error = check_same_branch(base_commit_id, branch, yca_id, repo);
+ if (error) {
+ if (error->code != GOT_ERR_ANCESTRY)
+ goto done;
+ error = NULL;
+ } else {
+ error = got_error_msg(GOT_ERR_SAME_BRANCH,
+ "specified branch resolves to a commit which "
+ "is already contained in work tree's branch");
+ goto done;
+ }
error = got_worktree_rebase_prepare(&new_base_branch,
&tmp_branch, &fileindex, worktree, branch, repo);
if (error)