Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 31
- `merge --into <branch>` when no copy has the branch open (e.g. the main copy cannot switch to `main`: a vendored `.dpcli` there, the submodule on the feature branch): a fast-forward moves the ref without a checkout (`update-ref` with the old value); a diverged target is refused with the hint to merge it into the source first.

## 30
- `update` moves `.dpcli` to the release tag when it already has the release's VERSION but sits on another commit (an own PR squash-merged, the branch commit still checked out); it used to report "up to date" and commit the pointer at the branch commit.

Expand Down
2 changes: 1 addition & 1 deletion lib/dpcli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
else in the current copy. Exit codes: 0 ok, 1 error/check failed, 2 arguments.
"""

VERSION = 30 # bump on every change: older copies re-exec from the main copy (reexec.maybe_reexec)
VERSION = 31 # bump on every change: older copies re-exec from the main copy (reexec.maybe_reexec)
19 changes: 14 additions & 5 deletions lib/dpcli/commands/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,20 @@ def cmd_merge(a):
raise DpError(f"{br} is not a dpcli module or task branch: give --into <branch>")
guard_main(into, bool(a.into))
path = gitx.copy_of(into)
if not path:
raise DpError(f"branch {into} is not checked out anywhere: a copy with it open is needed")
subj = gitx.gitr("log", "-1", "--format=%s", br, "--").stdout.strip()
h, n, kind = gitx.do_merge(path, br, a.message or lang_commit(f"Merge {br}: {subj}", f"Слияние {br}: {subj}"), ff=False, trailers=a.trailer)
out = f"{into}: {br} already merged" if kind == "up-to-date" else f"{into} {h}: merged {br}, files {n}"
if not path: # no copy can open it (e.g. a vendored .dpcli on main vs the submodule): a fast-forward needs none
old = gitx.gitr("rev-parse", f"refs/heads/{into}").stdout.strip()
if not old or gitx.gitr("merge-base", "--is-ancestor", old, br).returncode:
raise DpError(f"branch {into} is open in no copy and {br} is not a fast-forward of it: "
f"merge {into} into {br} first, or open {into} in a copy")
r = gitx.gitr("update-ref", f"refs/heads/{into}", br, old) # with the old value: no lost concurrent update
if r.returncode:
raise DpError(f"{into}: update-ref failed: {gitx.gerr(r)}")
kind = "up-to-date" if gitx.git("rev-parse", br) == old else "ff"
out = f"{into}: fast-forward to {br} ({gitx.git('rev-parse', '--short', br)}), no copy needed"
else:
subj = gitx.gitr("log", "-1", "--format=%s", br, "--").stdout.strip()
h, n, kind = gitx.do_merge(path, br, a.message or lang_commit(f"Merge {br}: {subj}", f"Слияние {br}: {subj}"), ff=False, trailers=a.trailer)
out = f"{into}: {br} already merged" if kind == "up-to-date" else f"{into} {h}: merged {br}, files {n}"
if a.push:
r = gitx.gitr("push", "origin", into, cwd=path)
if r.returncode:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_merge_no_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""merge --into a branch no copy has open: a fast-forward moves the ref without a checkout, anything else is refused."""
import unittest

from helpers import Project, git


class MergeNoCopy(unittest.TestCase):
def test_fast_forward_without_a_copy(self):
p = Project()
self.addCleanup(p.cleanup)
r = p.root
base = git("rev-parse", "HEAD", cwd=r)
for br in ("feat", "other"):
git("switch", "-q", "-c", br, base, cwd=r)
(r / f"{br}.txt").write_text(br)
git("add", f"{br}.txt", cwd=r)
git("commit", "-q", "-m", br, cwd=r)
git("switch", "-q", "feat", cwd=r) # main is open nowhere (the copy cannot switch to it)
self.assertIn("fast-forward", p.ok("merge", "feat", "--into", "main"))
self.assertEqual(git("rev-parse", "main", cwd=r), git("rev-parse", "feat", cwd=r))
code, _, err = p.run("merge", "other", "--into", "main") # diverged: needs a real merge
self.assertEqual(code, 1)
self.assertIn("not a fast-forward", err)
self.assertEqual(git("rev-parse", "main", cwd=r), git("rev-parse", "feat", cwd=r))


if __name__ == "__main__":
unittest.main()
Loading