Python Mercurial如何使用hg库获取差异
我使用Python编写了一个工具来获取Mercurial存储库的状态和差异。获取状态很容易,但当我尝试获取差异时,会出现以下错误:AttributeError:'module'对象没有属性'configbool'。 这是我的代码和输出: 代码 输出Python Mercurial如何使用hg库获取差异,python,mercurial,Python,Mercurial,我使用Python编写了一个工具来获取Mercurial存储库的状态和差异。获取状态很容易,但当我尝试获取差异时,会出现以下错误:AttributeError:'module'对象没有属性'configbool'。 这是我的代码和输出: 代码 输出 ====状态===== (['app/file/file_modified.py']、[]、[]、[]、[]、[]、[]、[]、[]、[]) 回溯(最近一次呼叫最后一次): 文件“test.py”,第19行,在 diff=commands.diff(
====状态=====
(['app/file/file_modified.py']、[]、[]、[]、[]、[]、[]、[]、[]、[])
回溯(最近一次呼叫最后一次):
文件“test.py”,第19行,在
diff=commands.diff(ui、repo)
文件“/usr/lib/python2.7/dist packages/mercurial/commands.py”,第2940行,格式不同
diffopts=patch.diffopts(用户界面,选项)
文件“/usr/lib/python2.7/dist packages/mercurial/patch.py”,第1557行,格式为diffopts
def get(key,name=None,getter=ui.configbool):
AttributeError:“模块”对象没有属性“configbool”
zsh:退出1 python test.py
你有没有办法通过Python获得回购的差异?多亏了ngoldbaum的评论,我安装了Python hglib:
apt-get install python-hglib
然后,我编写此函数以获取对象中的差异线:
#!/usr/bin/python
# coding:utf-8
import re
import hglib
def get_diff(path, include_empty_lines=False):
client = hglib.open(path) # get client of repo
diff = client.diff() # get plain text diff
added_lines = []
removed_lines = []
current_file = ''
for line in diff.split("\n")[1:]:
if line.startswith('+++'):
current_file = re.split('\s',line)[1][2:]
continue
if not line.startswith('+++') and not line.startswith('---'):
if line.startswith('+'):
if include_empty_lines or line[1:]:
added_lines.append({
"file": current_file,
"line_content": line[1:]
})
if line.startswith('-'):
if include_empty_lines or line[1:]:
removed_lines.append({
"file": current_file,
"line_content": line[1:]
})
return {"added_lines":added_lines, "removed_lines":removed_lines}
diff = get_diff('/path/to/repo')
print(diff)
# gives
# {
# 'added_lines':
# [
# {
# 'line_content': 'import re',
# 'file': 'bestfile.py'
# },
# {
# 'line_content': ' re.split("\s", name)',
# 'file': 'parsing_file.py'
# }
# ],
# 'removed_lines':
# [
# {
# 'line_content': ' name.split(" ")',
# 'file': 'parsing_file.py'
# }
# ]
# }
我希望这段代码会有帮助 多亏了ngoldbaum的评论,我安装了python hglib:
apt-get install python-hglib
然后,我编写此函数以获取对象中的差异线:
#!/usr/bin/python
# coding:utf-8
import re
import hglib
def get_diff(path, include_empty_lines=False):
client = hglib.open(path) # get client of repo
diff = client.diff() # get plain text diff
added_lines = []
removed_lines = []
current_file = ''
for line in diff.split("\n")[1:]:
if line.startswith('+++'):
current_file = re.split('\s',line)[1][2:]
continue
if not line.startswith('+++') and not line.startswith('---'):
if line.startswith('+'):
if include_empty_lines or line[1:]:
added_lines.append({
"file": current_file,
"line_content": line[1:]
})
if line.startswith('-'):
if include_empty_lines or line[1:]:
removed_lines.append({
"file": current_file,
"line_content": line[1:]
})
return {"added_lines":added_lines, "removed_lines":removed_lines}
diff = get_diff('/path/to/repo')
print(diff)
# gives
# {
# 'added_lines':
# [
# {
# 'line_content': 'import re',
# 'file': 'bestfile.py'
# },
# {
# 'line_content': ' re.split("\s", name)',
# 'file': 'parsing_file.py'
# }
# ],
# 'removed_lines':
# [
# {
# 'line_content': ' name.split(" ")',
# 'file': 'parsing_file.py'
# }
# ]
# }
我希望这段代码会有帮助 您不应该将mercurial用作库,因为它没有稳定的API。您可能想改用python hglib:非常感谢。我可以得到纯文本的差异。你不应该使用mercurial作为库,因为它没有一个稳定的API。您可能想改用python hglib:非常感谢。我能得到纯文本的差异。