##// END OF EJS Templates
rewriteutil: extract evolve code used to replace obsolete hashes in commits...
Matt Harbison -
r45993:0a57ef4b default
parent child Browse files
Show More
@@ -1,72 +1,115 b''
1 # rewriteutil.py - utility functions for rewriting changesets
1 # rewriteutil.py - utility functions for rewriting changesets
2 #
2 #
3 # Copyright 2017 Octobus <contact@octobus.net>
3 # Copyright 2017 Octobus <contact@octobus.net>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import re
11
10 from .i18n import _
12 from .i18n import _
11
13
12 from . import (
14 from . import (
13 error,
15 error,
14 node,
16 node,
15 obsolete,
17 obsolete,
18 obsutil,
16 revset,
19 revset,
20 scmutil,
17 )
21 )
18
22
19
23
24 sha1re = re.compile(br'\b[0-9a-f]{6,40}\b')
25
26
20 def precheck(repo, revs, action=b'rewrite'):
27 def precheck(repo, revs, action=b'rewrite'):
21 """check if revs can be rewritten
28 """check if revs can be rewritten
22 action is used to control the error message.
29 action is used to control the error message.
23
30
24 Make sure this function is called after taking the lock.
31 Make sure this function is called after taking the lock.
25 """
32 """
26 if node.nullrev in revs:
33 if node.nullrev in revs:
27 msg = _(b"cannot %s null changeset") % action
34 msg = _(b"cannot %s null changeset") % action
28 hint = _(b"no changeset checked out")
35 hint = _(b"no changeset checked out")
29 raise error.Abort(msg, hint=hint)
36 raise error.Abort(msg, hint=hint)
30
37
31 if len(repo[None].parents()) > 1:
38 if len(repo[None].parents()) > 1:
32 raise error.Abort(_(b"cannot %s while merging") % action)
39 raise error.Abort(_(b"cannot %s while merging") % action)
33
40
34 publicrevs = repo.revs(b'%ld and public()', revs)
41 publicrevs = repo.revs(b'%ld and public()', revs)
35 if publicrevs:
42 if publicrevs:
36 msg = _(b"cannot %s public changesets") % action
43 msg = _(b"cannot %s public changesets") % action
37 hint = _(b"see 'hg help phases' for details")
44 hint = _(b"see 'hg help phases' for details")
38 raise error.Abort(msg, hint=hint)
45 raise error.Abort(msg, hint=hint)
39
46
40 newunstable = disallowednewunstable(repo, revs)
47 newunstable = disallowednewunstable(repo, revs)
41 if newunstable:
48 if newunstable:
42 raise error.Abort(_(b"cannot %s changeset with children") % action)
49 raise error.Abort(_(b"cannot %s changeset with children") % action)
43
50
44
51
45 def disallowednewunstable(repo, revs):
52 def disallowednewunstable(repo, revs):
46 """Checks whether editing the revs will create new unstable changesets and
53 """Checks whether editing the revs will create new unstable changesets and
47 are we allowed to create them.
54 are we allowed to create them.
48
55
49 To allow new unstable changesets, set the config:
56 To allow new unstable changesets, set the config:
50 `experimental.evolution.allowunstable=True`
57 `experimental.evolution.allowunstable=True`
51 """
58 """
52 allowunstable = obsolete.isenabled(repo, obsolete.allowunstableopt)
59 allowunstable = obsolete.isenabled(repo, obsolete.allowunstableopt)
53 if allowunstable:
60 if allowunstable:
54 return revset.baseset()
61 return revset.baseset()
55 return repo.revs(b"(%ld::) - %ld", revs, revs)
62 return repo.revs(b"(%ld::) - %ld", revs, revs)
56
63
57
64
58 def skip_empty_successor(ui, command):
65 def skip_empty_successor(ui, command):
59 empty_successor = ui.config(b'rewrite', b'empty-successor')
66 empty_successor = ui.config(b'rewrite', b'empty-successor')
60 if empty_successor == b'skip':
67 if empty_successor == b'skip':
61 return True
68 return True
62 elif empty_successor == b'keep':
69 elif empty_successor == b'keep':
63 return False
70 return False
64 else:
71 else:
65 raise error.ConfigError(
72 raise error.ConfigError(
66 _(
73 _(
67 b"%s doesn't know how to handle config "
74 b"%s doesn't know how to handle config "
68 b"rewrite.empty-successor=%s (only 'skip' and 'keep' are "
75 b"rewrite.empty-successor=%s (only 'skip' and 'keep' are "
69 b"supported)"
76 b"supported)"
70 )
77 )
71 % (command, empty_successor)
78 % (command, empty_successor)
72 )
79 )
80
81
82 def update_hash_refs(repo, commitmsg):
83 """Replace all obsolete commit hashes in the message with the current hash.
84
85 If the obsolete commit was split or is divergent, the hash is not replaced
86 as there's no way to know which successor to choose.
87 """
88 cache = {}
89 sha1s = re.findall(sha1re, commitmsg)
90 unfi = repo.unfiltered()
91 for sha1 in sha1s:
92 fullnode = scmutil.resolvehexnodeidprefix(unfi, sha1)
93 if fullnode is None:
94 continue
95 ctx = unfi[fullnode]
96 if not ctx.obsolete():
97 continue
98
99 successors = obsutil.successorssets(repo, ctx.node(), cache=cache)
100
101 # We can't make any assumptions about how to update the hash if the
102 # cset in question was split or diverged.
103 if len(successors) == 1 and len(successors[0]) == 1:
104 newsha1 = node.hex(successors[0][0])
105 commitmsg = commitmsg.replace(sha1, newsha1[: len(sha1)])
106 else:
107 repo.ui.note(
108 _(
109 b'The stale commit message reference to %s could '
110 b'not be updated\n'
111 )
112 % sha1
113 )
114
115 return commitmsg
General Comments 0
You need to be logged in to leave comments. Login now