提交 96c0e229 authored 作者: Josh Bleecher Snyder's avatar Josh Bleecher Snyder

Make whitespace commit hook better at finding the hg executable

Fixes compatibility issues with MacHG and probably GUIs. (Note that the released version of MacHG actually doesn't work at the moment, but new ones should soon -- cf. http://bitbucket.org/jfh/machg/issue/141/p2d2-environment-variables-not-set)
上级 221b58bd
...@@ -81,8 +81,11 @@ class MercurialRuntimeError(Exception): ...@@ -81,8 +81,11 @@ class MercurialRuntimeError(Exception):
pass pass
def run_mercurial_command(hg_command): def run_mercurial_command(hg_command):
hg_executable = os.environ.get("HG", None) or "hg"
hg_command_tuple = hg_command.split()
hg_command_tuple.insert(0, hg_executable)
try: try:
hg_subprocess = Popen(hg_command.split(), stdout=PIPE, stderr=PIPE) hg_subprocess = Popen(hg_command_tuple, stdout=PIPE, stderr=PIPE)
except OSError: except OSError:
print >> sys.stderr, "Can't find the hg executable!" print >> sys.stderr, "Can't find the hg executable!"
sys.exit(1) sys.exit(1)
...@@ -99,22 +102,22 @@ def parse_stdout_filelist(hg_out_filelist): ...@@ -99,22 +102,22 @@ def parse_stdout_filelist(hg_out_filelist):
return files return files
def changed_files(): def changed_files():
hg_out = run_mercurial_command("hg tip --template '{file_mods}'") hg_out = run_mercurial_command("tip --template '{file_mods}'")
return parse_stdout_filelist(hg_out) return parse_stdout_filelist(hg_out)
def added_files(): def added_files():
hg_out = run_mercurial_command("hg tip --template '{file_adds}'") hg_out = run_mercurial_command("tip --template '{file_adds}'")
return parse_stdout_filelist(hg_out) return parse_stdout_filelist(hg_out)
def is_python_file(filename): def is_python_file(filename):
return filename.endswith(".py") return filename.endswith(".py")
def get_file_contents(filename, revision="tip"): def get_file_contents(filename, revision="tip"):
hg_out = run_mercurial_command("hg cat -r %s %s" % (revision, filename)) hg_out = run_mercurial_command("cat -r %s %s" % (revision, filename))
return hg_out return hg_out
def save_commit_message(filename): def save_commit_message(filename):
commit_message = run_mercurial_command("hg tip --template '{desc}'") commit_message = run_mercurial_command("tip --template '{desc}'")
save_file = open(filename, "w") save_file = open(filename, "w")
save_file.write(commit_message) save_file.write(commit_message)
save_file.close() save_file.close()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论