6 from optparse
import OptionParser
9 from git_stats
import commit
11 def stagedChanges(ignore_added_files
=False):
12 """Returns the paths to all files that have changes staged.
13 When calling this function the current directory should be the root
14 of the git directory that should be acted on.
17 ignore_added_files: When True files that added newly are ignored.
20 A list of paths that were changed.
24 result
= git
.diff_index("--cached", "--name-status", "HEAD")
26 log
= result
.split('\n')
31 # Skip the last empty line
32 if len(line
.lstrip()) == 0:
35 splitline
= line
.split('\t')
37 # Skip files that were freshly added
38 if splitline
[0] == 'A' and ignore_added_files
:
41 changed
.append(splitline
[1])
45 def touched(show_all
):
46 """Returns which commits touched the same files as the staged changes.
49 show_all: When true, all files, even new ones, are searched.
51 Returns: If there were no staged changes, None is returned.
52 Otherwise the list of matching commits is returned.
55 staged
= stagedChanges(not show_all
)
57 # There were no starged changes, return 'None' explicitly
61 # Find all commits that touched the same files and return them
62 touched
= commit
.commitsThatTouched(staged
)
66 """Dispatches index related commands
69 progname
= os
.path
.basename(sys
.argv
[0]) + " index"
71 parser
= OptionParser(prog
=progname
)
76 help="search even new files, which usually have no matching commits")
81 help="shows all commits that touch the same paths as the staged changes")
83 # Default to True for now, until there is another option
84 parser
.set_default("touched", True)
85 parser
.set_default("all", False)
87 (options
, args
) = parser
.parse_args(list(args
))
89 result
= touched(options
.all
)
92 msg
= "No changes staged"
94 msg
+= " or no matching commits"
96 msg
+= ", no matching commits, or only new files added"
101 commit
.prettyPrint(result
)