Added pathsTouched to the history module.
[git-stats.git] / src / git_stats / history.py
blobbe2982a1d40d42025f63b1d87885f8d452d98abc
1 #!/usr/bin/env python
3 from git_python import Git
5 def commitsOfTouchedChanges(pathsChanged):
6 """Returns a list of commits that touch the specified paths.
8 Params:
9 pathsChanged: A list of changed path relative to the current working dir.
11 Returns:
12 A list of 40-character SHA's of the touched commits.
13 """
15 if not pathsChanged:
16 raise ValueError("No changed paths specified")
18 git = Git(".")
19 result = git.rev_list("HEAD", *pathsChanged)
21 touched = result.split('\n')
23 # Don't return the trailing empty one
24 return touched[:-1]
26 def pathsTouched(commit, ignoreAddedFiles):
27 """Returns a list of paths touched by a specific commit.
29 Params:
30 commit: A commit identifier as accepted by git-log.
31 ignoreAddedFiles: When True newly added files are ignored.
33 Returns:
34 A list of paths touched by the specified commit.
35 """
36 git = Git(".")
37 result = git.log("--name-status", "-1", "--pretty=format:", commit)
39 log = result.split('\n')
41 paths = []
43 for line in log:
44 if len(line.lstrip()) == 0:
45 continue
47 splitline = line.split('\t')
48 if splitline[0] == 'A' and ignoreAddedFiles:
49 continue
51 paths.append(splitline[1])
53 return paths
55 if __name__ == '__main__':
56 import os
57 os.chdir("../..")
59 touched = commitsOfTouchedChanges(["README"])
61 for line in touched:
62 print(line)
65 touched = pathsTouched("HEAD", True)
67 for line in touched:
68 print(line)