Created a script that handles index related activities.
[git-stats.git] / src / git_stats / index.py
blob32023c153ef171345f93f4d7c03e885d433b3504
1 #!/usr/bin/env python
3 from git_python import Git
5 def stagedChanges(ignoreAddedFiles):
6 """Returns the paths to all files that have changes staged.
7 When calling this function the current directory should be the root
8 of the git directory that should be acted on.
10 Params:
11 ignoreAddedFiles: When True files that added newly are ignored.
13 Returns:
14 A list of paths that were changed.
15 """
17 git = Git(".")
18 result = git.diff_index("--cached", "--name-status", "HEAD")
20 log = result.split('\n')
22 changed = []
24 for line in log:
25 # Skip the last empty line
26 if len(line.lstrip()) == 0:
27 continue
29 splitline = line.split('\t')
31 # Skip files that were freshly added
32 if splitline[0] == 'A' and ignoreAddedFiles:
33 continue
35 changed.append(splitline[1])
37 return changed
39 if __name__ == '__main__':
40 import os
41 os.chdir("../..")
43 staged = stagedChanges(True)
45 if not staged:
46 print("No changes staged (or only new files added)")
47 else:
48 for path in staged:
49 print(path)