handle no-op changes during 'got rebase -c'
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
diff --git a/got/got.1 b/got/got.1
index 6c0eb6c..c9f0107 100644
--- a/got/got.1
+++ b/got/got.1
@@ -571,6 +571,9 @@ Alternatively, the rebase operation may be aborted which will leave
.Ar branch
unmodified and the work tree switched back to its original branch.
.Pp
+If a merge conflict is resolved in a way which renders the merged
+change into a no-op change, the corresponding commit will be elided.
+.Pp
.Cm got rebase
will refuse to run if certain preconditions are not met.
If the work tree contains multiple base commits it must first be updated
diff --git a/got/got.c b/got/got.c
index e6bdb5c..93c26bf 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3207,9 +3207,11 @@ show_rebase_progress(struct got_commit_object *commit,
if (err)
goto done;
- err = got_object_id_str(&new_id_str, new_id);
- if (err)
- goto done;
+ if (new_id) {
+ err = got_object_id_str(&new_id_str, new_id);
+ if (err)
+ goto done;
+ }
logmsg0 = strdup(got_object_commit_get_logmsg(commit));
if (logmsg0 == NULL) {
@@ -3222,7 +3224,8 @@ show_rebase_progress(struct got_commit_object *commit,
logmsg++;
old_id_str[12] = '\0';
- new_id_str[12] = '\0';
+ if (new_id_str)
+ new_id_str[12] = '\0';
len = strlen(logmsg);
if (len > 42)
len = 42;
@@ -3230,7 +3233,8 @@ show_rebase_progress(struct got_commit_object *commit,
nl = strchr(logmsg, '\n');
if (nl)
*nl = '\0';
- printf("%s -> %s: %s\n", old_id_str, new_id_str, logmsg);
+ printf("%s -> %s: %s\n", old_id_str,
+ new_id_str ? new_id_str : "no-op change", logmsg);
done:
free(old_id_str);
free(new_id_str);
@@ -3264,6 +3268,33 @@ rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
}
static const struct got_error *
+rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
+ struct got_object_id *commit_id, struct got_repository *repo)
+{
+ const struct got_error *error;
+ struct got_commit_object *commit;
+ struct got_object_id *new_commit_id;
+
+ error = got_object_open_as_commit(&commit, repo, commit_id);
+ if (error)
+ return error;
+
+ error = got_worktree_rebase_commit(&new_commit_id, worktree,
+ tmp_branch, commit, commit_id, repo);
+ if (error) {
+ if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
+ goto done;
+ error = show_rebase_progress(commit, commit_id, NULL);
+ } else {
+ error = show_rebase_progress(commit, commit_id, new_commit_id);
+ free(new_commit_id);
+ }
+done:
+ got_object_commit_close(commit);
+ return error;
+}
+
+static const struct got_error *
cmd_rebase(int argc, char *argv[])
{
const struct got_error *error = NULL;
@@ -3353,52 +3384,21 @@ cmd_rebase(int argc, char *argv[])
}
if (continue_rebase) {
- struct got_object_id *new_commit_id;
-
error = got_worktree_rebase_continue(&resume_commit_id,
&new_base_branch, &tmp_branch, &branch, worktree, repo);
if (error)
goto done;
- error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
- got_worktree_get_base_commit_id(worktree),
- resume_commit_id, repo);
- if (error)
- goto done;
- if (yca_id == NULL) {
- error = got_error_msg(GOT_ERR_ANCESTRY,
- "cannot determine common ancestor commit for "
- "continued rebase operation");
- goto done;
- }
- error = got_object_open_as_commit(&commit, repo,
- resume_commit_id);
- if (error)
- goto done;
- error = got_worktree_rebase_commit(&new_commit_id, worktree,
- tmp_branch, commit, resume_commit_id, repo);
+ error = rebase_commit(worktree, tmp_branch, resume_commit_id,
+ repo);
if (error)
goto done;
- error = show_rebase_progress(commit, resume_commit_id,
- new_commit_id);
- free(new_commit_id);
- free(resume_commit_id);
- resume_commit_id = got_object_id_dup(SIMPLEQ_FIRST(
- got_object_commit_get_parent_ids(commit))->id);
- if (resume_commit_id == NULL) {
+ yca_id = got_object_id_dup(resume_commit_id);
+ if (yca_id == NULL) {
error = got_error_from_errno("got_object_id_dup");
goto done;
}
- got_object_commit_close(commit);
- commit = NULL;
- commit_id = resume_commit_id;
- if (got_object_id_cmp(resume_commit_id, yca_id) == 0) {
- error = rebase_complete(worktree, branch,
- new_base_branch, tmp_branch, repo);
- /* YCA has been reached; we are done. */
- goto done;
- }
} else {
error = got_ref_open(&branch, repo, argv[0], 0);
if (error != NULL)
@@ -3416,10 +3416,13 @@ cmd_rebase(int argc, char *argv[])
"is already contained in work tree's branch");
goto done;
}
+ }
- error = got_ref_resolve(&branch_head_commit_id, repo, branch);
- if (error)
- goto done;
+ error = got_ref_resolve(&branch_head_commit_id, repo, branch);
+ if (error)
+ goto done;
+
+ if (!continue_rebase) {
error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
got_worktree_get_base_commit_id(worktree),
branch_head_commit_id, repo);
@@ -3436,9 +3439,9 @@ cmd_rebase(int argc, char *argv[])
&tmp_branch, worktree, branch, repo);
if (error)
goto done;
- commit_id = branch_head_commit_id;
}
+ commit_id = branch_head_commit_id;
error = got_object_open_as_commit(&commit, repo, commit_id);
if (error)
goto done;
@@ -3477,14 +3480,16 @@ cmd_rebase(int argc, char *argv[])
}
if (SIMPLEQ_EMPTY(&commits)) {
- error = got_error(GOT_ERR_EMPTY_REBASE);
+ if (continue_rebase)
+ error = rebase_complete(worktree, branch,
+ new_base_branch, tmp_branch, repo);
+ else
+ error = got_error(GOT_ERR_EMPTY_REBASE);
goto done;
}
pid = NULL;
SIMPLEQ_FOREACH(qid, &commits, entry) {
- struct got_object_id *new_commit_id;
-
commit_id = qid->id;
parent_id = pid ? pid->id : yca_id;
pid = qid;
@@ -3498,20 +3503,9 @@ cmd_rebase(int argc, char *argv[])
if (rebase_status == GOT_STATUS_CONFLICT)
break;
- error = got_object_open_as_commit(&commit, repo, commit_id);
- if (error)
- goto done;
- error = got_worktree_rebase_commit(&new_commit_id, worktree,
- tmp_branch, commit, commit_id, repo);
+ error = rebase_commit(worktree, tmp_branch, commit_id, repo);
if (error)
goto done;
- error = show_rebase_progress(commit, commit_id, new_commit_id);
- free(new_commit_id);
- got_object_commit_close(commit);
- commit = NULL;
- if (error)
- goto done;
-
}
if (rebase_status == GOT_STATUS_CONFLICT) {
diff --git a/lib/worktree.c b/lib/worktree.c
index 4d551f4..d2d0842 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -3803,8 +3803,16 @@ got_worktree_rebase_commit(struct got_object_id **new_commit_id,
got_object_commit_get_committer(orig_commit),
collect_rebase_commit_msg, orig_commit,
rebase_status, NULL, repo);
- if (err)
+ if (err) {
+ if (err->code == GOT_ERR_COMMIT_NO_CHANGES) {
+ /* No-op change; commit will be elided. */
+ err = got_ref_delete(commit_ref, repo);
+ if (err)
+ goto done;
+ err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
+ }
goto done;
+ }
err = got_ref_delete(commit_ref, repo);
if (err)
diff --git a/regress/cmdline/rebase.sh b/regress/cmdline/rebase.sh
index ca79164..e1eaa01 100755
--- a/regress/cmdline/rebase.sh
+++ b/regress/cmdline/rebase.sh
@@ -387,7 +387,113 @@ function test_rebase_abort {
test_done "$testroot" "$ret"
}
-run_test test_rebase_basic
-run_test test_rebase_ancestry_check
-run_test test_rebase_continue
-run_test test_rebase_abort
+function test_rebase_no_op_change {
+ local testroot=`test_init rebase_no_op_change`
+ local init_commit=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && git checkout -q -b newbranch)
+ echo "modified alpha on branch" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "committing to alpha on newbranch"
+ local orig_commit1=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && git checkout -q master)
+ echo "modified alpha on master" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "committing to alpha on master"
+ local master_commit=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got rebase newbranch > $testroot/stdout \
+ 2> $testroot/stderr)
+
+ echo "C alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: conflicts must be resolved before rebase can be resumed" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "<<<<<<< commit $orig_commit1" > $testroot/content.expected
+ echo "modified alpha on branch" >> $testroot/content.expected
+ echo "=======" >> $testroot/content.expected
+ echo "modified alpha on master" >> $testroot/content.expected
+ echo '>>>>>>> alpha' >> $testroot/content.expected
+ cat $testroot/wt/alpha > $testroot/content
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ echo "C alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # resolve the conflict
+ echo "modified alpha on master" > $testroot/wt/alpha
+
+ (cd $testroot/wt && got rebase -c > $testroot/stdout)
+
+ (cd $testroot/repo && git checkout -q newbranch)
+ local new_commit1=`git_show_head $testroot/repo`
+
+ local short_orig_commit1=`trim_obj_id 28 $orig_commit1`
+
+ echo -n "$short_orig_commit1 -> no-op change" \
+ > $testroot/stdout.expected
+ echo ": committing to alpha on newbranch" >> $testroot/stdout.expected
+ echo "Switching work tree to refs/heads/newbranch" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+
+ (cd $testroot/wt && got log -l2 | grep ^commit > $testroot/stdout)
+ echo "commit $master_commit (master, newbranch)" \
+ > $testroot/stdout.expected
+ echo "commit $init_commit" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+#run_test test_rebase_basic
+#run_test test_rebase_ancestry_check
+#run_test test_rebase_continue
+#run_test test_rebase_abort
+run_test test_rebase_no_op_change