Skip to content

Commit

Permalink
Repair stg repair with amended first patch
Browse files Browse the repository at this point in the history
When `stg repair` searches for commits to "patchify", it looks for
commits that are children of known patch commits. This left an unhandled
edge case where if only one patch was applied, and that patch was
amended with `git commit --amend`, the amended commit would not be
patchified and, worse, that commit would become orphaned.

`stg repair` is modified to look for the last known stack base commit in
addition to known patch commits such that children of the base commit
will now be patchified.

An added benefit of identifying the stack's base commit is that it
signals the point beyond which no patches will be found and thus allows
searching to stop and a potentially large amount of work avoided. This
can have a significant impact on the performance of running `stg repair`
in a repositories with long histories.

Repairs #163

Signed-off-by: Peter Grayson <pete@jpgrayson.net>
  • Loading branch information
jpgrayson committed Dec 8, 2021
1 parent 043b19d commit 879b5c2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 20 deletions.
54 changes: 34 additions & 20 deletions stgit/commands/repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,47 @@ def func(parser, options, args):
break
else:
maybe_patchify.append(c)

c = c.data.parent

if stack.base == c:
# Reaching the original stack base can happen if, for example, the first
# applied patch is amended. In this case, any commits descending from the
# stack base should be patchified.
patchify.extend(maybe_patchify)
maybe_patchify = []

# Once the base commit has been found, we know that no existing patches
# can be found be searching further.
break

applied.reverse()
patchify.reverse()

# Find patches unreachable behind a merge.
merge = c
todo = set([c])
seen = set()
unreachable = set()
while todo:
c = todo.pop()
seen.add(c)
todo |= set(c.data.parents) - seen
if any(stack.patches[pn] == c for pn in patches):
unreachable.add(c)
if unreachable:
out.warn(
(
'%d patch%s are hidden below the merge commit'
% (len(unreachable), ['es', ''][len(unreachable) == 1])
),
'%s,' % merge.sha1,
'and will be considered unapplied.',
)
if c != stack.base:
merge = c
todo = set([c])
seen = set()
unreachable = set()
while todo:
c = todo.pop()
seen.add(c)
todo |= set(c.data.parents) - seen
if any(stack.patches[pn] == c for pn in patches):
unreachable.add(c)
if unreachable:
out.warn(
(
'%d patch%s are hidden below the merge commit'
% (len(unreachable), ['es', ''][len(unreachable) == 1])
),
'%s,' % merge.sha1,
'and will be considered unapplied.',
)

# Make patches of any linear sequence of commits on top of a patch.
if applied and patchify:
if patchify:
out.start(
'Creating %d new patch%s' % (len(patchify), ['es', ''][len(patchify) == 1])
)
Expand Down
72 changes: 72 additions & 0 deletions t/t1307-repair-amend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/sh
test_description='Test "stg repair" of amended commits'
. ./test-lib.sh

test_expect_success 'Initialize the StGit patch' '
stg init &&
echo "hello" > foo.txt &&
stg add foo.txt &&
stg new -m p0 &&
stg refresh
'

test_expect_success 'Amend patch commit with git' '
echo "amended" >> foo.txt &&
git commit -a --amend -m "p0 amended"
'

test_expect_success 'Repair amended patch' '
stg repair &&
test "$(echo $(stg series --noprefix --applied))" = "p0-amended" &&
test "$(echo $(stg series --noprefix --unapplied))" = "p0" &&
test "$(tail -n1 foo.txt)" = "amended"
'

test_expect_success 'Reset to original patch' '
stg delete p0-amended &&
stg push p0
'

test_expect_success 'Add more applied and unapplied patches' '
stg new -m p1 &&
echo "from p1" >> foo.txt &&
stg refresh &&
stg new -m p2 &&
echo "from p2" >> foo.txt &&
stg refresh &&
stg pop p2 &&
test "$(echo $(stg series --noprefix --applied))" = "p0 p1" &&
test "$(echo $(stg series --noprefix --unapplied))" = "p2"
'

test_expect_success 'Amend middle patch' '
echo "p1 amended" >> foo.txt &&
git commit -a --amend -m "p1 amended"
'

test_expect_success 'Repair amended middle patch' '
stg repair &&
test "$(echo $(stg series --noprefix --applied))" = "p0 p1-amended" &&
test "$(echo $(stg series --noprefix --unapplied))" = "p1 p2" &&
test "$(tail -n1 foo.txt)" = "p1 amended"
'

test_expect_success 'Reset to non-amended patches' '
stg delete p1-amended &&
stg pop -a
'

test_expect_success 'Add commit onto stack base' '
echo "new commit" > foo.txt &&
git add foo.txt &&
git commit -m "add foo"
'

test_expect_success 'Repair new commit on stack base' '
stg repair &&
test "$(echo $(stg series --noprefix --applied))" = "" &&
test "$(echo $(stg series --noprefix --unapplied))" = "p0 p1 p2" &&
test "$(tail -n1 foo.txt)" = "new commit"
'

test_done

0 comments on commit 879b5c2

Please sign in to comment.