Patchwork [2,of,2,V3] strip: take branch into account when selecting update target (issue5540)

login
register
mail settings
Submitter Paul Morelle
Date Oct. 10, 2017, 9:44 a.m.
Message ID <7681cb8ad2b5bca77955.1507628683@taranis.localdomain>
Download mbox | patch
Permalink /patch/24699/
State Accepted
Headers show

Comments

Paul Morelle - Oct. 10, 2017, 9:44 a.m.
# HG changeset patch
# User Paul Morelle <paul.morelle@octobus.net>
# Date 1507212785 -7200
#      Thu Oct 05 16:13:05 2017 +0200
# Node ID 7681cb8ad2b5bca779551e84676cd70d67366cdf
# Parent  adaf1c0e81c0d4f1f9dcf5c98de4410e21d76966
# EXP-Topic issue-5540
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 7681cb8ad2b5
strip: take branch into account when selecting update target (issue5540)

Test contributed by Matt Harbison

Keep the same behavior in most cases (i.e. first parent of the first root of
stripped changsets), but if the branch differs from wdir's, try to find another
parent of stripped commits that is on the same branch.
Matt Harbison - Oct. 11, 2017, 4:04 a.m.
On Tue, 10 Oct 2017 05:44:43 -0400, Paul Morelle  
<paul.morelle@octobus.net> wrote:

> # HG changeset patch
> # User Paul Morelle <paul.morelle@octobus.net>
> # Date 1507212785 -7200
> #      Thu Oct 05 16:13:05 2017 +0200
> # Node ID 7681cb8ad2b5bca779551e84676cd70d67366cdf
> # Parent  adaf1c0e81c0d4f1f9dcf5c98de4410e21d76966
> # EXP-Topic issue-5540
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r  
> 7681cb8ad2b5
> strip: take branch into account when selecting update target (issue5540)

Looks good to me, thanks.
Yuya Nishihara - Oct. 11, 2017, 1:01 p.m.
On Tue, 10 Oct 2017 11:44:43 +0200, Paul Morelle wrote:
> # HG changeset patch
> # User Paul Morelle <paul.morelle@octobus.net>
> # Date 1507212785 -7200
> #      Thu Oct 05 16:13:05 2017 +0200
> # Node ID 7681cb8ad2b5bca779551e84676cd70d67366cdf
> # Parent  adaf1c0e81c0d4f1f9dcf5c98de4410e21d76966
> # EXP-Topic issue-5540
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 7681cb8ad2b5
> strip: take branch into account when selecting update target (issue5540)
> 
> Test contributed by Matt Harbison
> 
> Keep the same behavior in most cases (i.e. first parent of the first root of
> stripped changsets), but if the branch differs from wdir's, try to find another
> parent of stripped commits that is on the same branch.
> 
> diff -r adaf1c0e81c0 -r 7681cb8ad2b5 hgext/strip.py
> --- a/hgext/strip.py	Thu Oct 05 15:11:34 2017 +0200
> +++ b/hgext/strip.py	Thu Oct 05 16:13:05 2017 +0200
> @@ -60,10 +60,20 @@
>  
>  def _findupdatetarget(repo, nodes):
>      unode, p2 = repo.changelog.parents(nodes[0])
> +    current_branch = repo[None].branch()

Nit: currentbranch per coding style.

>      if (util.safehasattr(repo, 'mq') and p2 != nullid
>          and p2 in [x.node for x in repo.mq.applied]):
>          unode = p2
> +    elif current_branch != repo[unode].branch():
> +        pwdir = 'parents(wdir())'
> +        revset = ('max(((parents(%ln::{0}) + {0}) - %ln::{0})'
> +                  ' and branch(%s))'
> +                 ).format(pwdir)

bytes.format() isn't available on Python 3. Instead, you can use %r to embed
a revset expression.

> +        branch_target = repo.revs(revset, nodes, nodes, current_branch)
> +        if branch_target:

Nit: s/branch_target/branchtarget/

> +            cl = repo.changelog
> +            unode = cl.node(branch_target.first())
Paul Morelle - Oct. 11, 2017, 2:38 p.m.
On 10/11/2017 03:01 PM, Yuya Nishihara wrote:
> On Tue, 10 Oct 2017 11:44:43 +0200, Paul Morelle wrote:
>>      if (util.safehasattr(repo, 'mq') and p2 != nullid
>>          and p2 in [x.node for x in repo.mq.applied]):
>>          unode = p2
>> +    elif current_branch != repo[unode].branch():
>> +        pwdir = 'parents(wdir())'
>> +        revset = ('max(((parents(%ln::{0}) + {0}) - %ln::{0})'
>> +                  ' and branch(%s))'
>> +                 ).format(pwdir)
> bytes.format() isn't available on Python 3. Instead, you can use %r to embed
> a revset expression.
Oh, something new to know about Mercurial and Python3.
Today I have learned that all the strings are transformed into bytes in
mercurial in Python3.
I will push a V4 including your remarks.
Thank you very much!

Patch

diff -r adaf1c0e81c0 -r 7681cb8ad2b5 hgext/strip.py
--- a/hgext/strip.py	Thu Oct 05 15:11:34 2017 +0200
+++ b/hgext/strip.py	Thu Oct 05 16:13:05 2017 +0200
@@ -60,10 +60,20 @@ 
 
 def _findupdatetarget(repo, nodes):
     unode, p2 = repo.changelog.parents(nodes[0])
+    current_branch = repo[None].branch()
 
     if (util.safehasattr(repo, 'mq') and p2 != nullid
         and p2 in [x.node for x in repo.mq.applied]):
         unode = p2
+    elif current_branch != repo[unode].branch():
+        pwdir = 'parents(wdir())'
+        revset = ('max(((parents(%ln::{0}) + {0}) - %ln::{0})'
+                  ' and branch(%s))'
+                 ).format(pwdir)
+        branch_target = repo.revs(revset, nodes, nodes, current_branch)
+        if branch_target:
+            cl = repo.changelog
+            unode = cl.node(branch_target.first())
 
     return unode
 
diff -r adaf1c0e81c0 -r 7681cb8ad2b5 tests/test-strip.t
--- a/tests/test-strip.t	Thu Oct 05 15:11:34 2017 +0200
+++ b/tests/test-strip.t	Thu Oct 05 16:13:05 2017 +0200
@@ -941,6 +941,214 @@ 
   abort: boom
   [255]
 
+test stripping a working directory parent doesn't switch named branches
+
+  $ hg log -G
+  @  changeset:   1:eca11cf91c71
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     commitB
+  |
+  o  changeset:   0:105141ef12d0
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     commitA
+  
+
+  $ hg branch new-branch
+  marked working directory as branch new-branch
+  (branches are permanent and global, did you want a bookmark?)
+  $ hg ci -m "start new branch"
+  $ echo 'foo' > foo.txt
+  $ hg ci -Aqm foo
+  $ hg up default
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ echo 'bar' > bar.txt
+  $ hg ci -Aqm bar
+  $ hg up new-branch
+  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ hg merge default
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+  $ hg log -G
+  @  changeset:   4:35358f982181
+  |  tag:         tip
+  |  parent:      1:eca11cf91c71
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     bar
+  |
+  | @  changeset:   3:f62c6c09b707
+  | |  branch:      new-branch
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     foo
+  | |
+  | o  changeset:   2:b1d33a8cadd9
+  |/   branch:      new-branch
+  |    user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    summary:     start new branch
+  |
+  o  changeset:   1:eca11cf91c71
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     commitB
+  |
+  o  changeset:   0:105141ef12d0
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     commitA
+  
+
+  $ hg strip --force -r 35358f982181
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  saved backup bundle to $TESTTMP/issue4736/.hg/strip-backup/35358f982181-50d992d4-backup.hg (glob)
+  $ hg log -G
+  @  changeset:   3:f62c6c09b707
+  |  branch:      new-branch
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     foo
+  |
+  o  changeset:   2:b1d33a8cadd9
+  |  branch:      new-branch
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     start new branch
+  |
+  o  changeset:   1:eca11cf91c71
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     commitB
+  |
+  o  changeset:   0:105141ef12d0
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     commitA
+  
+
+  $ hg up default
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ echo 'bar' > bar.txt
+  $ hg ci -Aqm bar
+  $ hg up new-branch
+  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ hg merge default
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+  $ hg ci -m merge
+  $ hg log -G
+  @    changeset:   5:4cf5e92caec2
+  |\   branch:      new-branch
+  | |  tag:         tip
+  | |  parent:      3:f62c6c09b707
+  | |  parent:      4:35358f982181
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     merge
+  | |
+  | o  changeset:   4:35358f982181
+  | |  parent:      1:eca11cf91c71
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     bar
+  | |
+  o |  changeset:   3:f62c6c09b707
+  | |  branch:      new-branch
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     foo
+  | |
+  o |  changeset:   2:b1d33a8cadd9
+  |/   branch:      new-branch
+  |    user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    summary:     start new branch
+  |
+  o  changeset:   1:eca11cf91c71
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     commitB
+  |
+  o  changeset:   0:105141ef12d0
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     commitA
+  
+
+  $ hg strip -r 35358f982181
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  saved backup bundle to $TESTTMP/issue4736/.hg/strip-backup/35358f982181-a6f020aa-backup.hg (glob)
+  $ hg log -G
+  @  changeset:   3:f62c6c09b707
+  |  branch:      new-branch
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     foo
+  |
+  o  changeset:   2:b1d33a8cadd9
+  |  branch:      new-branch
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     start new branch
+  |
+  o  changeset:   1:eca11cf91c71
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     commitB
+  |
+  o  changeset:   0:105141ef12d0
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     commitA
+  
+
+  $ hg pull -u $TESTTMP/issue4736/.hg/strip-backup/35358f982181-a6f020aa-backup.hg
+  pulling from $TESTTMP/issue4736/.hg/strip-backup/35358f982181-a6f020aa-backup.hg (glob)
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 2 changesets with 1 changes to 1 files
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
+  $ hg strip -k -r 35358f982181
+  saved backup bundle to $TESTTMP/issue4736/.hg/strip-backup/35358f982181-a6f020aa-backup.hg (glob)
+  $ hg log -G
+  @  changeset:   3:f62c6c09b707
+  |  branch:      new-branch
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     foo
+  |
+  o  changeset:   2:b1d33a8cadd9
+  |  branch:      new-branch
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     start new branch
+  |
+  o  changeset:   1:eca11cf91c71
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     commitB
+  |
+  o  changeset:   0:105141ef12d0
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     commitA
+  
+  $ hg diff
+  diff -r f62c6c09b707 bar.txt
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/bar.txt	Thu Jan 01 00:00:00 1970 +0000
+  @@ -0,0 +1,1 @@
+  +bar
+
 Use delayedstrip to strip inside a transaction
 
   $ cd $TESTTMP