5 from git_stats
import commit
6 from git_stats
import diff
8 def calculateDiffSize(difference
):
9 """Calculates the true diff size
11 All lines that start with '+\t', '-\t' are counted.
12 Lines that are of size 1 are also counted.
17 # Take each line and only count if it is part of the diff
18 for line
in difference
:
19 if len(line
) == 1 or line
[1] == '\t' and \
20 (line
[0] == '+' or line
[0] == '-'):
25 def findMatch(left
, right
):
26 """Tries to find a match between left and right
28 If it is plausible that there is a match the difference is returend.
29 Otherwise False is returned.
32 # Get the diff and convert it to a usable format
33 res
= difflib
.unified_diff(left
, right
, n
=0, lineterm
="")
36 # Get some sizes for easy calculation
37 ressize
= calculateDiffSize(res
)
39 rightsize
= len(right
)
41 # The difference is larger than either side
42 if ressize
> leftsize
or ressize
> rightsize
:
45 # The difference is larger than the average
46 if ressize
> (leftsize
+ rightsize
)/2:
49 # This is probably a match, return the difference
53 """Tries to find a match between added and removed hunks
55 The diff of the specified commit is retreived and it is
56 split into hunks. The hunks that were added are compared
57 with the hunks that were deleted. If they are similar the
58 pair is deemed a match.
62 result
= commit
.getDiff(target
)
63 targetDiff
= result
.split('\n')
65 # And have it parsed, but don't add line numbering to the hunks
66 parsedDiffs
= diff
.parseCommitDiff(targetDiff
, number
=False)
68 # To store the matches in
71 # Iterate over all the diffs, e.g., take all pairs
72 for left
in parsedDiffs
:
73 for right
in parsedDiffs
:
74 # Don't compare with self, that'd always match
78 # A removal hunk, not interesting as a left side
79 # Only interesting when comparing with addition
80 if not left
.linesAdded
:
83 # An add hunk, not interesting as a right side
84 # We are interested in this as a left side
85 if not right
.linesDeleted
:
88 # Try to find a match for this pair
89 res
= findMatch(left
.linesAdded
, right
.linesDeleted
)
95 result
.append(left
.linesAdded
, right
.linesAdded
, res
)
100 """Dispatches matching related commands
103 # We accept exactly one comment as argument
105 print("Please specify a commit to check")
109 result
= match(args
[0])