comparison mercurial/cmdutil.py @ 21419:272785489ed3

cmdutil: enhance "getcommiteditor()" for specific usages in MQ This patch introduces "finishdesc" and "extramsg" arguments into "getcommiteditor()" for specific usages in MQ. "finishdesc" will be used to treat the commit message as "[mq]; patch-file-name" (default MQ commit message), if it is left as empty, instead of aborting commit process. "extramsg" will be used to show the line below: HG: Leave message empty to use default message instead of: HG: Leave message empty to abort commit
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sun, 11 May 2014 00:49:36 +0900
parents 308aaeb956e2
children bee0e1cffdd3
comparison
equal deleted inserted replaced
21418:d4b8fc753455 21419:272785489ed3
107 except IOError, inst: 107 except IOError, inst:
108 raise util.Abort(_("can't read commit message '%s': %s") % 108 raise util.Abort(_("can't read commit message '%s': %s") %
109 (logfile, inst.strerror)) 109 (logfile, inst.strerror))
110 return message 110 return message
111 111
112 def getcommiteditor(edit=False, **opts): 112 def getcommiteditor(edit=False, finishdesc=None, extramsg=None, **opts):
113 """get appropriate commit message editor according to '--edit' option""" 113 """get appropriate commit message editor according to '--edit' option
114 if edit: 114
115 return commitforceeditor 115 'finishdesc' is a function to be called with edited commit message
116 (= 'description' of the new changeset) just after editing, but
117 before checking empty-ness. It should return actual text to be
118 stored into history. This allows to change description before
119 storing.
120
121 'extramsg' is a extra message to be shown in the editor instead of
122 'Leave message empty to abort commit' line. 'HG: ' prefix and EOL
123 is automatically added.
124
125 'getcommiteditor' returns 'commitforceeditor' regardless of
126 'edit', if one of 'finishdesc' or 'extramsg' is specified, because
127 they are specific for usage in MQ.
128 """
129 if edit or finishdesc or extramsg:
130 return lambda r, c, s: commitforceeditor(r, c, s,
131 finishdesc=finishdesc,
132 extramsg=extramsg)
116 else: 133 else:
117 return commiteditor 134 return commiteditor
118 135
119 def loglimit(opts): 136 def loglimit(opts):
120 """get the log limit according to option -l/--limit""" 137 """get the log limit according to option -l/--limit"""
2128 def commiteditor(repo, ctx, subs): 2145 def commiteditor(repo, ctx, subs):
2129 if ctx.description(): 2146 if ctx.description():
2130 return ctx.description() 2147 return ctx.description()
2131 return commitforceeditor(repo, ctx, subs) 2148 return commitforceeditor(repo, ctx, subs)
2132 2149
2133 def commitforceeditor(repo, ctx, subs): 2150 def commitforceeditor(repo, ctx, subs, finishdesc=None, extramsg=None):
2134 edittext = [] 2151 edittext = []
2135 modified, added, removed = ctx.modified(), ctx.added(), ctx.removed() 2152 modified, added, removed = ctx.modified(), ctx.added(), ctx.removed()
2136 if ctx.description(): 2153 if ctx.description():
2137 edittext.append(ctx.description()) 2154 edittext.append(ctx.description())
2138 edittext.append("") 2155 edittext.append("")
2139 edittext.append("") # Empty line between message and comments. 2156 edittext.append("") # Empty line between message and comments.
2140 edittext.append(_("HG: Enter commit message." 2157 edittext.append(_("HG: Enter commit message."
2141 " Lines beginning with 'HG:' are removed.")) 2158 " Lines beginning with 'HG:' are removed."))
2142 edittext.append(_("HG: Leave message empty to abort commit.")) 2159 if extramsg:
2160 edittext.append("HG: %s" % extramsg)
2161 else:
2162 edittext.append(_("HG: Leave message empty to abort commit."))
2143 edittext.append("HG: --") 2163 edittext.append("HG: --")
2144 edittext.append(_("HG: user: %s") % ctx.user()) 2164 edittext.append(_("HG: user: %s") % ctx.user())
2145 if ctx.p2(): 2165 if ctx.p2():
2146 edittext.append(_("HG: branch merge")) 2166 edittext.append(_("HG: branch merge"))
2147 if ctx.branch(): 2167 if ctx.branch():
2160 os.chdir(repo.root) 2180 os.chdir(repo.root)
2161 text = repo.ui.edit("\n".join(edittext), ctx.user(), ctx.extra()) 2181 text = repo.ui.edit("\n".join(edittext), ctx.user(), ctx.extra())
2162 text = re.sub("(?m)^HG:.*(\n|$)", "", text) 2182 text = re.sub("(?m)^HG:.*(\n|$)", "", text)
2163 os.chdir(olddir) 2183 os.chdir(olddir)
2164 2184
2185 if finishdesc:
2186 text = finishdesc(text)
2165 if not text.strip(): 2187 if not text.strip():
2166 raise util.Abort(_("empty commit message")) 2188 raise util.Abort(_("empty commit message"))
2167 2189
2168 return text 2190 return text
2169 2191