2011-07-06 16 views
5

मुझे Mercurial के लिए सरल हुक की आवश्यकता है जो पैटर्न का उपयोग करके टिप्पणी प्रतिबद्ध करता है। यहां मेरा हुक है:टोर्टोइज एचजी लॉग विंडो में हुक आउटपुट कैसे दिखाएं?

#!/usr/bin/env python 
# 
# save as .hg/check_whitespace.py and make executable 

import re 

def check_comment(comment): 
    # 
    print 'Checking comment...' 
    pattern = '^((Issue \d+:)|(No Issue:)).+' 
    if re.match(pattern, comment, flags=re.IGNORECASE): 
     return 1 
    else: 
     print >> sys.stderr, 'Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"' 
     return 0 

if __name__ == '__main__': 
    import os, sys 
    comment=os.popen('hg tip --template "{desc}"').read() 
    if not check_comment(comment): 
     sys.exit(1) 
sys.exit(0) 

यह काम करता है। जब मैं कंसोल से प्रतिबद्ध करता हूं तो यह त्रुटि संदेश 'Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"' भी दिखाता है। लेकिन जब मैं कछुए एचजी वर्कबेंच से प्रतिबद्ध करने की कोशिश करता हूं, केवल सिस्टम संदेश दिखाया जाता है: abort: pretxncommit.check_comment hook exited with status 1

मुझे उपयोगकर्ता को सूचित करने की आवश्यकता है कि क्या गलत है। क्या हुक से आउटपुट दिखाने के लिए कछुए एचजी को मजबूर करने का कोई तरीका है?

+0

बस एक अनुमान है, लेकिन क्या आपने sys.err के बजाय sys.out पर लिखने की कोशिश की है? – bbaja42

+0

हां। यह मदद नहीं की। –

उत्तर

5

मुझे इसे बाहरी हुक के बजाय इन-प्रोसेस हुक बनाकर काम करने के लिए मिला। हालांकि, प्रक्रिया hooks को काफी अलग तरीके से परिभाषित किया गया है।

सबसे पहले, पायथन फ़ाइल को केवल एक ही फ़ंक्शन की आवश्यकता होती है जिसे हुक परिभाषा में नाम से बुलाया जाएगा। हुक फ़ंक्शन ui, repo, और hooktype ऑब्जेक्ट्स पारित किया गया है। यह हुक के प्रकार के आधार पर अतिरिक्त वस्तुओं को भी पारित किया जाता है। pretrxncommit के लिए, यह node, parent1, और parent2 पारित किया गया है, लेकिन आप केवल नोड में रुचि रखते हैं, इसलिए बाकी kwargs में एकत्र हुए हैं। ui ऑब्जेक्ट का उपयोग स्थिति और त्रुटि संदेशों को देने के लिए किया जाता है।

check_comment.py की सामग्री:,

[hooks] 
pretxncommit.check_comment = python:/path/to/check_comment.py:check_comment 

.suffix_namepretxncommit पर किसी भी विश्व स्तर पर परिभाषित हुक अधिभावी से बचने के लिए है:

#!/usr/bin/env python 

import re 

def check_comment(ui, repo, hooktype, node=None, **kwargs): 
    ui.status('Checking comment...\n') 
    comment = repo[node].description() 
    pattern = '^((Issue \d+:)|(No Issue:)).+' 
    if not re.match(pattern, comment, flags=re.IGNORECASE): 
     ui.warn('Comment does not match pattern. You must start it with "Issue 12323:" or "No Issue:"\n') 
     return True 

hgrc में, हुक python:/path/to/file.py:function_name साथ, इस तरह परिभाषित किया जाएगा विशेष रूप से यदि इसे वैश्विक के बजाय रिपोजिटरी के hgrc में परिभाषित किया गया है। प्रत्यय यह है कि कैसे एक ही हुक को कई प्रतिक्रियाओं की अनुमति देता है।

0

यदि हुक एक भंडार पर चलता है जो कि उदाहरण के माध्यम से किया जाता है hgserve:
मैं एक ही उत्पादन

  • सर्वर लॉग TortoiseHg कार्यक्षेत्र लॉग पैन में
  • या cmd लाइन

में दिखाने के लिए एक pretxnchangegroup लिपि में इस छोटे से अजगर समारोह का उपयोग करें:

def log(ui, string): 
    print(string) # will show up as "remote:" on the client 
    ui.status("{0}\n".format(string)) # will show up in the same hg process: hgserve ; append newline 
    return 
संबंधित मुद्दे