gitstats: We no longer require running from the root of the repository
[git-stats.git] / src / git_stats / index.py
blob52a09180987ebce320c6594a41804cf16de37e47
1 #!/usr/bin/env python
3 from git_python import Git
5 import commit
7 def stagedChanges(ignoreAddedFiles=False):
8 """Returns the paths to all files that have changes staged.
9 When calling this function the current directory should be the root
10 of the git directory that should be acted on.
12 Params:
13 ignoreAddedFiles: When True files that added newly are ignored.
15 Returns:
16 A list of paths that were changed.
17 """
19 git = Git(".")
20 result = git.diff_index("--cached", "--name-status", "HEAD")
22 log = result.split('\n')
24 changed = []
26 for line in log:
27 # Skip the last empty line
28 if len(line.lstrip()) == 0:
29 continue
31 splitline = line.split('\t')
33 # Skip files that were freshly added
34 if splitline[0] == 'A' and ignoreAddedFiles:
35 continue
37 changed.append(splitline[1])
39 return changed
41 def touched():
42 """Shows what commits touched the same files as the currently staged changes.
43 """
45 staged = stagedChanges(True)
46 if not staged:
47 print("No changes staged (or only new files added)")
48 return
50 touched = commit.commitsThatTouched(staged)
51 commit.prettyPrint(touched)
53 def dispatch(*args):
54 """Dispatches index related commands
55 """
57 pass
59 if __name__ == '__main__':
60 staged = stagedChanges(True)
62 if not staged:
63 print("No changes staged (or only new files added)")
64 else:
65 for path in staged:
66 print(path)