2012-09-14 82 views
2

所以,我想做一個基本的mercurial擴展,將一個字符串附加到提交消息。下面顯示的代碼被放置在一個文件中 - myextension.py幷包含在.hgrc中。如何編寫基本的mercurial擴展?

當我運行hg commit -m "A message"時,提交編輯器打開並顯示消息"A message APPENDED"按預期方式。但是,如果我嘗試通過按CTRL + X中止提交,提交仍會隨着完整的,現在附加的消息發生。

我在這裏做錯了什麼?

from mercurial import commands, extensions 

def commit(originalcommit, ui, repo, *pats, **opts): 

    if not opts["message"]: 
     return originalcommit(ui, repo, *pats, **opts) 
    else: 
     opts["force_editor"] = True 
     opts["message"] += " APPENDED" 
     return originalcommit(ui, repo, *pats, **opts) 

def uisetup(ui): 
    extensions.wrapcommand(commands.table, 'commit', commit) 

回答

1

我認爲這是正確的行爲,因爲這善變 通行證編輯器臨時文件已經包含提交信息。您可以覆蓋 這部分由猴子修補cmdutil.commitforceeditor 功能擴展:

from mercurial import commands, extensions, cmdutil, util 

cfe = cmdutil.commitforceeditor 

def commitforceeditor(repo, ctx, subs): 
    text = cfe(repo, ctx, subs) 

    # Do not commit unless the commit message differs from 
    # the one you specified on the command line 
    if ctx.description() == text.strip(): 
     raise util.Abort("empty commit message") 
    else: 
     return text 

def commit(originalcommit, ui, repo, *pats, **opts): 
    if not opts["message"]: 
     return originalcommit(ui, repo, *pats, **opts) 
    else: 
     opts["force_editor"] = True 
     opts["message"] += " APPENDED" 

     # monkey-patch 
     cmdutil.commitforceeditor = commitforceeditor 

     return originalcommit(ui, repo, *pats, **opts) 

def uisetup(ui): 
    extensions.wrapcommand(commands.table, 'commit', commit)