本文整理匯總了Python中unidiff.PatchSet方法的典型用法代碼示例。如果您正苦於以下問題:Python unidiff.PatchSet方法的具體用法?Python unidiff.PatchSet怎麽用?Python unidiff.PatchSet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類unidiff
的用法示例。
在下文中一共展示了unidiff.PatchSet方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_added
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def _get_added(cls, diff):
patches = PatchSet(StringIO(diff))
diff_contents = []
for p in patches:
if p.added > 0:
contents = []
for h in p:
added = []
for i, line in enumerate(h):
if line.is_added:
added_line = Line(line.target_line_no, line.value, i + 1)
added.append(added_line)
contents += added
diff_contents.append(
DiffContent(p.path, contents)
)
return diff_contents
示例2: _load_all_patches
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def _load_all_patches(series_iter, patches_dir):
"""
Returns a tuple of the following:
- boolean indicating success or failure of reading files
- dict of relative UNIX path strings to unidiff.PatchSet
"""
had_failure = False
unidiff_dict = dict()
for relative_path in series_iter:
if relative_path in unidiff_dict:
continue
unidiff_dict[relative_path] = unidiff.PatchSet.from_filename(
str(patches_dir / relative_path), encoding=ENCODING)
if not (patches_dir / relative_path).read_text(encoding=ENCODING).endswith('\n'):
had_failure = True
get_logger().warning('Patch file does not end with newline: %s',
str(patches_dir / relative_path))
return had_failure, unidiff_dict
示例3: fetch_diff
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def fetch_diff(self):
SyncGit.logger.info("Fetching diff from remote origin")
try:
firehol_repo = git.cmd.Git(self.repo_path)
firehol_repo.checkout("master")
firehol_repo.fetch("origin")
diff_stdout = firehol_repo.execute(["git", "diff", "master", "origin/master"], True).split("\n")
try:
udiff = unidiff.PatchSet(diff_stdout)
firehol_repo.execute(["git", "reset", "--hard", "origin/master"])
firehol_repo.merge()
self.logger.info("Successfully fetched diff from remote origin")
return udiff
except unidiff.UnidiffParseError:
self.logger.exception("UnidiffParseError occurred")
except git.GitCommandError:
self.logger.exception("GitCommandError occurred")
示例4: get_candidates_from_diff
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def get_candidates_from_diff(difftext):
try:
import unidiff
except ImportError as e:
raise SystemExit("Could not import unidiff library: %s", e.message)
patch = unidiff.PatchSet(difftext, encoding='utf-8')
candidates = []
for patchedfile in [patchfile for patchfile in
patch.added_files + patch.modified_files]:
if patchedfile.source_file == '/dev/null':
candidates.append(patchedfile.path)
else:
lines = ",".join(["%s-%s" % (hunk.target_start, hunk.target_start + hunk.target_length)
for hunk in patchedfile])
candidates.append("%s:%s" % (patchedfile.path, lines))
return candidates
示例5: test_no_changed_files_ignore
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def test_no_changed_files_ignore(app, pr_context, caplog):
diff = """diff --git a/removed_file b/removed_file
deleted file mode 100644
index 1f38447..0000000
--- a/removed_file
+++ /dev/null
@@ -1,3 +0,0 @@
-This content shouldn't be here.
-
-This file will be removed.
"""
spec = Specification()
spec.linters.append(ObjectDict(name='flake8', pattern=None))
lint = LintProcessor(pr_context, spec, '/tmp')
patch = PatchSet(diff.split('\n'))
with mock.patch.object(lint, 'load_changes') as load_changes:
load_changes.return_value = patch
lint.process()
assert load_changes.called
assert 'No changed files found' in caplog.text
示例6: test_patch_set
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def test_patch_set(self):
reporter = git.GitDiffReporter('07ac1490a')
assert isinstance(reporter.patch_set, PatchSet)
示例7: patch_set
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def patch_set(self):
return PatchSet(self._diff)
示例8: git_diff
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def git_diff(ref=None):
cmd = [
'git',
'-c', 'diff.mnemonicprefix=no',
'diff',
ref,
'--no-color',
'--no-ext-diff'
]
data = execute(list(filter(None, cmd)))
return PatchSet(data.splitlines())
示例9: get_changed_files
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def get_changed_files(patch_str: str = None) -> Set[str]:
"""get list of changed files from the patch from STDIN."""
if patch_str is None:
patch_str = sys.stdin
patch = PatchSet(patch_str)
changed_files = set({f.path for f in patch.modified_files + patch.added_files + patch.removed_files})
logging.info('Files modified by this patch:\n ' + '\n '.join(sorted(changed_files)))
return changed_files
示例10: diff
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def diff(self, id, raw=False):
from unidiff import PatchSet
endpoint = '2.0/repositories/{repo}/pullrequests/{id}/diff'.format(
repo=self.repo,
id=id
)
res = self.client.get(endpoint, raw=True)
res.encoding = 'utf-8'
if raw:
return res.text
patch = PatchSet(res.text.split('\n'))
return patch
示例11: test_flake8_lint_a_py
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def test_flake8_lint_a_py(app, pr_context):
diff = """diff --git a/a.py b/a.py
new file mode 100644
index 0000000..fdeea15
--- /dev/null
+++ b/a.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+
+
+def add(a, b):
+ return a+ b
"""
spec = Specification()
spec.linters.append(ObjectDict(name='flake8', pattern=None))
lint = LintProcessor(pr_context, spec, os.path.join(FIXTURES_PATH, 'flake8'))
patch = PatchSet(diff.split('\n'))
with mock.patch.object(lint, 'load_changes') as load_changes,\
mock.patch.object(lint, 'update_build_status') as build_status,\
mock.patch.object(lint, '_report') as report:
load_changes.return_value = patch
build_status.return_value = None
report.return_value = (1, 2)
lint.problems.set_changes(patch)
lint.process()
assert load_changes.called
assert len(lint.problems) == 1
problem = lint.problems[0]
assert problem.filename == 'a.py'
assert problem.line == 6
示例12: test_eslint_lint_a_js
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def test_eslint_lint_a_js(app, pr_context):
diff = """diff --git a/.eslintrc b/.eslintrc
new file mode 100644
index 0000000..45e5d69
--- /dev/null
+++ b/.eslintrc
@@ -0,0 +1,5 @@
+{
+ "rules": {
+ "quotes": [2, "single"]
+ }
+}
diff --git a/a.js b/a.js
new file mode 100644
index 0000000..f119a7f
--- /dev/null
+++ b/a.js
@@ -0,0 +1 @@
+console.log("bar")
"""
spec = Specification()
spec.linters.append(ObjectDict(name='eslint', pattern=None))
lint = LintProcessor(pr_context, spec, os.path.join(FIXTURES_PATH, 'eslint'))
patch = PatchSet(diff.split('\n'))
with mock.patch.object(lint, 'load_changes') as load_changes,\
mock.patch.object(lint, 'update_build_status') as build_status,\
mock.patch.object(lint, '_report') as report:
load_changes.return_value = patch
build_status.return_value = None
report.return_value = (1, 2)
lint.problems.set_changes(patch)
lint.process()
assert load_changes.called
assert len(lint.problems) == 1
problem = lint.problems[0]
assert problem.filename == 'a.js'
assert problem.line == 1
示例13: test_pycodestyle_lint_a_py
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def test_pycodestyle_lint_a_py(app, pr_context):
diff = """diff --git a/a.py b/a.py
new file mode 100644
index 0000000..fdeea15
--- /dev/null
+++ b/a.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+
+
+def add(a, b):
+ return a+ b
"""
spec = Specification()
spec.linters.append(ObjectDict(name='pycodestyle', pattern=None))
lint = LintProcessor(pr_context, spec, os.path.join(FIXTURES_PATH, 'pycodestyle'))
patch = PatchSet(diff.split('\n'))
with mock.patch.object(lint, 'load_changes') as load_changes,\
mock.patch.object(lint, 'update_build_status') as build_status,\
mock.patch.object(lint, '_report') as report:
load_changes.return_value = patch
build_status.return_value = None
report.return_value = (1, 2)
lint.problems.set_changes(patch)
lint.process()
assert load_changes.called
assert len(lint.problems) == 1
problem = lint.problems[0]
assert problem.filename == 'a.py'
assert problem.line == 6
示例14: test_jsonlint_a_json_changes_in_range
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def test_jsonlint_a_json_changes_in_range(app, pr_context):
diff = """diff --git a/b.json b/b.json
index 6ebebfe..6be8d74 100644
--- a/b.json
+++ b/b.json
@@ -1,3 +1,4 @@
{
"a": 1
+ "b": 2
}
"""
spec = Specification()
spec.linters.append(ObjectDict(name='jsonlint', pattern=None))
lint = LintProcessor(pr_context, spec, os.path.join(FIXTURES_PATH, 'jsonlint'))
patch = PatchSet(diff.split('\n'))
with mock.patch.object(lint, 'load_changes') as load_changes,\
mock.patch.object(lint, 'update_build_status') as build_status,\
mock.patch.object(lint, '_report') as report:
load_changes.return_value = patch
build_status.return_value = None
report.return_value = (1, 2)
lint.problems.set_changes(patch)
lint.process()
assert load_changes.called
assert len(lint.problems) == 1
problem = lint.problems[0]
assert problem.filename == 'b.json'
assert problem.line == 2
示例15: test_jsonlint_a_json_changes_out_of_range
# 需要導入模塊: import unidiff [as 別名]
# 或者: from unidiff import PatchSet [as 別名]
def test_jsonlint_a_json_changes_out_of_range(app, pr_context):
diff = """diff --git a/c.json b/c.json
index 9b90002..c36a2a4 100644
--- a/c.json
+++ b/c.json
@@ -3,4 +3,5 @@
"b": 2,
c: 3,
d: 4
+ e: 5
}
"""
spec = Specification()
spec.linters.append(ObjectDict(name='jsonlint', pattern=None))
lint = LintProcessor(pr_context, spec, os.path.join(FIXTURES_PATH, 'jsonlint'))
patch = PatchSet(diff.split('\n'))
with mock.patch.object(lint, 'load_changes') as load_changes,\
mock.patch.object(lint, 'update_build_status') as build_status,\
mock.patch.object(lint, '_report') as report:
load_changes.return_value = patch
build_status.return_value = None
report.return_value = (1, 2)
lint.problems.set_changes(patch)
lint.process()
assert load_changes.called
assert len(lint.problems) == 0