2015-06-22 52 views
4

我正在嘗試調試Mercurial擴展。這個擴展添加了一些執行pull時應該執行的代碼。原作者通過更改存儲庫對象的類來設置此掛鉤。在Hg擴展中重載拉命令

下面是相關的代碼(這實際上是一個有效的水銀擴展名):

def reposetup(ui, repo): 
    class myrepo(repo.__class__): 
     def pull(self, remote, heads=None, force=False): 
      print "pull called" 
      return super(myrepo, self).pull(remote, heads, force) 

    print "reposetup called" 
    if repo.local(): 
     print "repo is local" 
     repo.__class__ = myrepo 

當我執行與本擴展的hg pull啓用,這裏是輸出:

# hg pull 
reposetup called 
repo is local 
pulling from ssh://hgbox/myrepo 
reposetup called 
searching for changes 
no changes found 

這是在pull命令中注入擴展代碼的合理方法?爲什麼「拉來電」的聲明從未達到過?

我在Windows 7上使用Mercurial 3.4.1與python 2.7.5。

回答

5

根據代碼(mercurial/extensions.py),這是只有合理的方式來擴展存儲庫對象(https://www.mercurial-scm.org/repo/hg/file/ff5172c83002/mercurial/extensions.py#l227)。

不過,我看着代碼和localrepo對象不會出現有在這一點上pull方法,所以我懷疑這就是爲什麼你的「拉名爲」 print語句一直沒有出現 - 沒有什麼要求,因爲它是預計不存在!

有很好的方法可以將代碼注入拉取決於你想要完成什麼。例如,如果您只是想只要一拉發出運行的東西,而是喜歡包裹exchange.pull功能:

extensions.wrapfunction(exchange, 'pull', my_pull_function) 

爲了您的具體使用情況下,我建議用下面的創建方法代碼:

def expull(orig, repo, remote, *args, **kwargs): 
    transferprojrc(repo.ui, repo, remote) 
    return orig(repo, remote, *args, **kwargs) 

在extsetup方法添加一行:

extensions.wrapfunction(exchange, 'pull', expull) 

最後,在reposetup方法,你可以完全去除projrcrepo類的東西。希望這會讓你找到你想要的行爲。

+0

感謝您的回答。我試圖用你的建議來調整代碼,但目前爲止沒有成功(我不是Python開發人員,有時候我有點失落)。實際上,我試圖完成的是讓projrc擴展工作。我提交了[問題](https://bitbucket.org/aragost/projrc/issue/2/no-projrc-update-on-pull#comment-18753387)描述了我的問題,但由於我沒有得到反饋我試圖自己修復它(在SO的幫助下,顯然!)。 – barjak

+2

我檢出了mercurial的代碼,並且在localrepo類中沒有看到任何拉出方法,這可能是爲什麼代碼沒有運行的更好解釋。現在我會更新我的答案,提供一些更具體的想法,我知道你想要完成什麼。 – ryanmce

+2

感謝您的跟進。我可以按照你使用'exchange :: pull'的建議來工作。我爲Projrc項目創建了一個[pull request](https://bitbucket.org/aragost/projrc/pull-request/5/make-projrc-compatible-with-mercurial-32/diff)。你介意看看我的差異,並告訴我,如果你認爲沒關係? – barjak