Patchwork [6,of,6,V2] histedit: move autoverb rule to the commit it matches

login
register
mail settings
Submitter Sean Farley
Date June 30, 2016, 9:58 p.m.
Message ID <edadb62f9204708f34ab.1467323935@laptop.office.atlassian.com>
Download mbox | patch
Permalink /patch/15682/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Sean Farley - June 30, 2016, 9:58 p.m.
# HG changeset patch
# User Sean Farley <sean@farley.io>
# Date 1466546329 25200
#      Tue Jun 21 14:58:49 2016 -0700
# Node ID edadb62f9204708f34abd85b33a6efe99945e789
# Parent  bf3cabc7104d684d550a6c692f526b35ab8bfbc0
# EXP-Topic autoverb
histedit: move autoverb rule to the commit it matches

Inspired by how 'git rebase -i' works, we move the autoverb to the
commit line summary that it matches. We do this by iterating over all
rules and inserting each non-autoverb line into a key in an ordered
dictionary. If we find an autoverb line later, we then search for the
matching key and append it to the list (which is the value of each key
in the dictionary). If we can't find a previous line to move to, then we
leave the rule in the same spot.

Tests have been updated but the diff looks a little messy because we
need to change one of the summary lines so that it will actually move to
a new spot. On top of that, we added -q flags to future some of the
output and needed to change the file it modified so that it wouldn't
cause a conflict.
Yuya Nishihara - July 3, 2016, 7:02 a.m.
On Thu, 30 Jun 2016 14:58:55 -0700, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean@farley.io>
> # Date 1466546329 25200
> #      Tue Jun 21 14:58:49 2016 -0700
> # Node ID edadb62f9204708f34abd85b33a6efe99945e789
> # Parent  bf3cabc7104d684d550a6c692f526b35ab8bfbc0
> # EXP-Topic autoverb
> histedit: move autoverb rule to the commit it matches

The change looks fine and the functionality has been reviewed. Queued the
series, thanks.

> +        newact = util.sortdict()
>          for act in actions:
>              ctx = repo[act.node]
>              summary = _getsummary(ctx)
>              fword = summary.split(' ', 1)[0].lower()
> +            added = False
> +
>              # if it doesn't end with the special character '!' just skip this
>              if fword.endswith('!'):
>                  fword = fword[:-1]
>                  if fword in primaryactions | secondaryactions | tertiaryactions:
>                      act.verb = fword
> +                    # get the target summary
> +                    tsum = summary[len(fword) + 1:].lstrip()
> +                    # safe but slow: reverse iterate over the actions so we
> +                    # don't clash on two commits having the same summary
> +                    for na, l in reversed(list(newact.iteritems())):
> +                        actx = repo[na.node]
> +                        asum = _getsummary(actx)
> +                        if asum == tsum:
> +                            added = True
> +                            l.append(act)
> +                            break

newact isn't accessed as a dict. Perhaps we want a dict of last-tsum => index
and a list of [na, ..l].
Sean Farley - July 5, 2016, 9:01 p.m.
Yuya Nishihara <yuya@tcha.org> writes:

> On Thu, 30 Jun 2016 14:58:55 -0700, Sean Farley wrote:
>> # HG changeset patch
>> # User Sean Farley <sean@farley.io>
>> # Date 1466546329 25200
>> #      Tue Jun 21 14:58:49 2016 -0700
>> # Node ID edadb62f9204708f34abd85b33a6efe99945e789
>> # Parent  bf3cabc7104d684d550a6c692f526b35ab8bfbc0
>> # EXP-Topic autoverb
>> histedit: move autoverb rule to the commit it matches
>
> The change looks fine and the functionality has been reviewed. Queued the
> series, thanks.
>
>> +        newact = util.sortdict()
>>          for act in actions:
>>              ctx = repo[act.node]
>>              summary = _getsummary(ctx)
>>              fword = summary.split(' ', 1)[0].lower()
>> +            added = False
>> +
>>              # if it doesn't end with the special character '!' just skip this
>>              if fword.endswith('!'):
>>                  fword = fword[:-1]
>>                  if fword in primaryactions | secondaryactions | tertiaryactions:
>>                      act.verb = fword
>> +                    # get the target summary
>> +                    tsum = summary[len(fword) + 1:].lstrip()
>> +                    # safe but slow: reverse iterate over the actions so we
>> +                    # don't clash on two commits having the same summary
>> +                    for na, l in reversed(list(newact.iteritems())):
>> +                        actx = repo[na.node]
>> +                        asum = _getsummary(actx)
>> +                        if asum == tsum:
>> +                            added = True
>> +                            l.append(act)
>> +                            break
>
> newact isn't accessed as a dict. Perhaps we want a dict of last-tsum => index
> and a list of [na, ..l].

I think I see what you mean but I'm pretty strapped for time so if
someone could do that, that would be great :-) Otherwise, I'll put it in
my queue.

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -1314,19 +1314,42 @@  def ruleeditor(repo, ui, actions, editco
     """open an editor to edit rules
 
     rules are in the format [ [act, ctx], ...] like in state.rules
     """
     if repo.ui.configbool("experimental", "histedit.autoverb"):
+        newact = util.sortdict()
         for act in actions:
             ctx = repo[act.node]
             summary = _getsummary(ctx)
             fword = summary.split(' ', 1)[0].lower()
+            added = False
+
             # if it doesn't end with the special character '!' just skip this
             if fword.endswith('!'):
                 fword = fword[:-1]
                 if fword in primaryactions | secondaryactions | tertiaryactions:
                     act.verb = fword
+                    # get the target summary
+                    tsum = summary[len(fword) + 1:].lstrip()
+                    # safe but slow: reverse iterate over the actions so we
+                    # don't clash on two commits having the same summary
+                    for na, l in reversed(list(newact.iteritems())):
+                        actx = repo[na.node]
+                        asum = _getsummary(actx)
+                        if asum == tsum:
+                            added = True
+                            l.append(act)
+                            break
+
+            if not added:
+                newact[act] = []
+
+        # copy over and flatten the new list
+        actions = []
+        for na, l in newact.iteritems():
+            actions.append(na)
+            actions += l
 
     rules = '\n'.join([act.torule() for act in actions])
     rules += '\n\n'
     rules += editcomment
     rules = ui.edit(rules, ui.username(), {'prefix': 'histedit'})
diff --git a/tests/test-histedit-arguments.t b/tests/test-histedit-arguments.t
--- a/tests/test-histedit-arguments.t
+++ b/tests/test-histedit-arguments.t
@@ -505,36 +505,35 @@  amend should not be blocked by the ongoi
 Test autoverb feature
 
   $ hg init autoverb
   $ cd autoverb
   $ echo alpha >> alpha
-  $ hg addr
-  adding alpha
-  $ hg ci -m one
+  $ hg ci -qAm one
   $ echo alpha >> alpha
-  $ hg ci -m two
-  $ echo alpha >> alpha
-  $ hg ci -m "roll! three"
+  $ hg ci -qm two
+  $ echo beta >> beta
+  $ hg ci -qAm "roll! one"
 
   $ hg log --style compact --graph
-  @  2[tip]   1b0b0b04c8fe   1970-01-01 00:00 +0000   test
-  |    roll! three
+  @  2[tip]   4f34d0f8b5fa   1970-01-01 00:00 +0000   test
+  |    roll! one
   |
   o  1   579e40513370   1970-01-01 00:00 +0000   test
   |    two
   |
   o  0   6058cbb6cfd7   1970-01-01 00:00 +0000   test
        one
   
 
 Check that 'roll' is selected by default
 
-  $ HGEDITOR=cat hg histedit 1 --config experimental.histedit.autoverb=True
+  $ HGEDITOR=cat hg histedit 0 --config experimental.histedit.autoverb=True
+  pick 6058cbb6cfd7 0 one
+  roll 4f34d0f8b5fa 2 roll! one
   pick 579e40513370 1 two
-  roll 1b0b0b04c8fe 2 roll! three
   
-  # Edit history between 579e40513370 and 1b0b0b04c8fe
+  # Edit history between 6058cbb6cfd7 and 4f34d0f8b5fa
   #
   # Commits are listed from least to most recent
   #
   # You can reorder changesets by reordering the lines
   #