Patchwork D8287: fix: add a -s option to format a revision and its descendants

login
register
mail settings
Submitter phabricator
Date March 13, 2020, 7:33 p.m.
Message ID <differential-rev-PHID-DREV-wzsvycv45zqqawlri5ry-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/45771/
State Superseded
Headers show

Comments

phabricator - March 13, 2020, 7:33 p.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  `hg fix -r abc123` will format that commit but not its
  descendants. That seems expected given the option name (`-r`), but
  it's very rarely what the user wants to do. The problem is that any
  descendants of that commit will not be formatted, leaving them as
  orphans that are hard to evolve. They are hard to evolve because the
  new parent will have formatting changes that the orphan doesn't have.
  
  I talked to Danny Hooper (who wrote most of the fix extension) about
  the problem and we agreed that deprecating `-r` in favor of a new `-s`
  argument (mimicing rebase's `-s`) would be a good way of reducing the
  risk that users end up with these hard-to-evolve orphans. So that's
  what this patch implements.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8287

AFFECTED FILES
  hgext/fix.py
  tests/test-fix-topology.t
  tests/test-fix.t

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel
phabricator - March 13, 2020, 9:02 p.m.
hooper added a comment.


  D8283 <https://phab.mercurial-scm.org/D8283>:D8288 <https://phab.mercurial-scm.org/D8288> all looks good to me.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8287/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8287

To: martinvonz, #hg-reviewers
Cc: hooper, mercurial-devel
phabricator - March 13, 2020, 10:41 p.m.
mharbison72 added a comment.


  Thanks for this!

INLINE COMMENTS

> fix.py:221
> +    [],
> +    _(b'fix descendants of these revisions'),
> +    _(b'REV'),

nit: is `descendants` usually self referential?  (The revset is, but IDK if that's what a casual reader would expect here.)  The same option for rebase says:

  rebase the specified changeset and descendants

> fix.py:412-413
> +    elif opts[b'source']:
> +        source_revs = scmutil.revrange(repo, opts[b'source'])
> +        revs = set(repo.revs(b'%ld::', source_revs))
> +        if wdirrev in source_revs:

Any idea why rebase seems to convert the `source` arg to a list before passing to `revrange()`, and also does `repo.revs(b'(%ld)::', src) or src`?  IDK if it's trying to handle something that should be handled here for consistent behavior.

https://www.mercurial-scm.org/repo/hg/file/tip/hgext/rebase.py#l1264

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8287/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8287

To: martinvonz, #hg-reviewers
Cc: mharbison72, hooper, mercurial-devel
phabricator - March 13, 2020, 11:23 p.m.
martinvonz added inline comments.

INLINE COMMENTS

> mharbison72 wrote in fix.py:221
> nit: is `descendants` usually self referential?  (The revset is, but IDK if that's what a casual reader would expect here.)  The same option for rebase says:
> 
>   rebase the specified changeset and descendants

Ah, good point. I'll copy the text from rebase. Thanks!

> mharbison72 wrote in fix.py:412-413
> Any idea why rebase seems to convert the `source` arg to a list before passing to `revrange()`, and also does `repo.revs(b'(%ld)::', src) or src`?  IDK if it's trying to handle something that should be handled here for consistent behavior.
> 
> https://www.mercurial-scm.org/repo/hg/file/tip/hgext/rebase.py#l1264

> Any idea why rebase seems to convert the source arg to a list before passing to revrange(),

I'm pretty sure that's because rebase's `--source` argument is not list-valued, i.e. `hg rebase --source foo --source bar ...` will rebase only `bar::` (I didn't test that, though). We should probably change that, even though it would be a small BC (same thing with `--base`).

> and also does repo.revs(b'(%ld)::', src) or src?

Oh, maybe that `or src` is to handle the `wdir` case I mention two lines down here? Let me check... Yep, that's it: D8057 <https://phab.mercurial-scm.org/D8057> (written by me just ~6 weeks ago and I had already forgotten :P). We can't replicate that trick here because we support multiple source revisions. We should instead remember to handle that correctly if we teach rebase to support multiple source revisions.

> IDK if it's trying to handle something that should be handled here for consistent behavior.

Good point and thanks for checking!

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8287/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D8287

To: martinvonz, #hg-reviewers
Cc: mharbison72, hooper, mercurial-devel

Patch

diff --git a/tests/test-fix.t b/tests/test-fix.t
--- a/tests/test-fix.t
+++ b/tests/test-fix.t
@@ -104,12 +104,13 @@ 
   
   options ([+] can be repeated):
   
-      --all          fix all non-public non-obsolete revisions
-      --base REV [+] revisions to diff against (overrides automatic selection,
-                     and applies to every revision being fixed)
-   -r --rev REV [+]  revisions to fix
-   -w --working-dir  fix the working directory
-      --whole        always fix every line of a file
+      --all            fix all non-public non-obsolete revisions
+      --base REV [+]   revisions to diff against (overrides automatic selection,
+                       and applies to every revision being fixed)
+   -r --rev REV [+]    revisions to fix
+   -s --source REV [+] fix descendants of these revisions
+   -w --working-dir    fix the working directory
+      --whole          always fix every line of a file
   
   (some details hidden, use --verbose to show complete help)
 
diff --git a/tests/test-fix-topology.t b/tests/test-fix-topology.t
--- a/tests/test-fix-topology.t
+++ b/tests/test-fix-topology.t
@@ -21,6 +21,7 @@ 
   $ cat >> $HGRCPATH <<EOF
   > [extensions]
   > fix =
+  > strip =
   > [fix]
   > uppercase-whole-file:command="$PYTHON" $UPPERCASEPY
   > uppercase-whole-file:pattern=set:**
@@ -262,6 +263,111 @@ 
 
   $ cd ..
 
+
+Test the --source option. We only do this with obsstore on to avoid duplicating
+test code. We rely on the other tests to prove that obsolescence is not an
+important factor here.
+
+#if obsstore-on
+  $ hg init source-arg
+  $ cd source-arg
+  $ printf "aaaa\n" > a
+  $ hg commit -Am "change A"
+  adding a
+  $ printf "bbbb\n" > b
+  $ hg commit -Am "change B"
+  adding b
+  $ printf "cccc\n" > c
+  $ hg commit -Am "change C"
+  adding c
+  $ hg checkout 0
+  0 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  $ printf "dddd\n" > d
+  $ hg commit -Am "change D"
+  adding d
+  created new head
+  $ hg log --graph --template '{rev} {desc}\n'
+  @  3 change D
+  |
+  | o  2 change C
+  | |
+  | o  1 change B
+  |/
+  o  0 change A
+  
+
+Test passing 'wdir()' to --source
+  $ printf "xxxx\n" > x
+  $ hg add x
+  $ hg fix -s 'wdir()'
+  $ cat *
+  aaaa
+  dddd
+  XXXX
+
+Test passing '.' to --source
+  $ printf "xxxx\n" > x
+  $ hg fix -s .
+  $ hg log --graph --template '{rev} {desc}\n'
+  @  4 change D
+  |
+  | o  2 change C
+  | |
+  | o  1 change B
+  |/
+  o  0 change A
+  
+  $ cat *
+  aaaa
+  DDDD
+  XXXX
+  $ hg strip -qf 4
+  $ hg co -q 3
+
+Test passing other branch to --source
+  $ printf "xxxx\n" > x
+  $ hg add x
+  $ hg fix -s 2
+  $ hg log --graph --template '{rev} {desc}\n'
+  o  4 change C
+  |
+  | @  3 change D
+  | |
+  o |  1 change B
+  |/
+  o  0 change A
+  
+  $ hg cat -r 4 b c
+  bbbb
+  CCCC
+  $ cat *
+  aaaa
+  dddd
+  xxxx
+  $ hg strip -qf 4
+
+Test passing multiple revisions to --source
+  $ hg fix -s '2 + .'
+  $ hg log --graph --template '{rev} {desc}\n'
+  @  5 change D
+  |
+  | o  4 change C
+  | |
+  | o  1 change B
+  |/
+  o  0 change A
+  
+  $ hg cat -r 4 b c
+  bbbb
+  CCCC
+  $ cat *
+  aaaa
+  DDDD
+  XXXX
+
+  $ cd ..
+#endif
+
 The --all flag should fix anything that wouldn't cause a problem if you fixed
 it, including the working copy. Obsolete revisions are not fixed because that
 could cause divergence. Public revisions would cause an abort because they are
diff --git a/hgext/fix.py b/hgext/fix.py
--- a/hgext/fix.py
+++ b/hgext/fix.py
@@ -214,6 +214,13 @@ 
     _(b'REV'),
 )
 revopt = (b'r', b'rev', [], _(b'revisions to fix'), _(b'REV'))
+sourceopt = (
+    b's',
+    b'source',
+    [],
+    _(b'fix descendants of these revisions'),
+    _(b'REV'),
+)
 wdiropt = (b'w', b'working-dir', False, _(b'fix the working directory'))
 wholeopt = (b'', b'whole', False, _(b'always fix every line of a file'))
 usage = _(b'[OPTION]... [FILE]...')
@@ -221,7 +228,7 @@ 
 
 @command(
     b'fix',
-    [allopt, baseopt, revopt, wdiropt, wholeopt],
+    [allopt, baseopt, revopt, sourceopt, wdiropt, wholeopt],
     usage,
     helpcategory=command.CATEGORY_FILE_CONTENTS,
 )
@@ -250,7 +257,9 @@ 
     """
     opts = pycompat.byteskwargs(opts)
     cmdutil.check_at_most_one_arg(opts, b'all', b'rev')
-    cmdutil.check_incompatible_arguments(opts, b'working_dir', [b'all'])
+    cmdutil.check_incompatible_arguments(
+        opts, b'working_dir', [b'all', b'source']
+    )
 
     with repo.wlock(), repo.lock(), repo.transaction(b'fix'):
         revstofix = getrevstofix(ui, repo, opts)
@@ -399,6 +408,14 @@ 
     """Returns the set of revision numbers that should be fixed"""
     if opts[b'all']:
         revs = repo.revs(b'(not public() and not obsolete()) or wdir()')
+    elif opts[b'source']:
+        source_revs = scmutil.revrange(repo, opts[b'source'])
+        revs = set(repo.revs(b'%ld::', source_revs))
+        if wdirrev in source_revs:
+            # `wdir()::` is currently empty, so manuall add wdir
+            revs.add(wdirrev)
+        if repo[b'.'].rev() in revs:
+            revs.add(wdirrev)
     else:
         revs = set(scmutil.revrange(repo, opts[b'rev']))
         if opts.get(b'working_dir'):