Skip to content

Commit

Permalink
allow filtering the output of 'git status -z' with a prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ohumbel committed Aug 24, 2015
1 parent 16a6fc0 commit f9fcbc7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
46 changes: 23 additions & 23 deletions gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@ def addandcommit(changeentry):
def handle_captitalization_filename_changes():
sandbox = os.path.join(configuration.get().workDirectory, configuration.get().clonedGitRepoName)
lines = shell.getoutput("git status -z")
for entry in Commiter.splitoutputofgitstatusz(lines):
if entry.startswith("A "):
newfilerelativepath = entry[3:] # cut A and following space and NUL at the end
directoryofnewfile = os.path.dirname(os.path.join(sandbox, newfilerelativepath))
newfilename = os.path.basename(newfilerelativepath)
cwd = os.getcwd()
os.chdir(directoryofnewfile)
files = shell.getoutput("git ls-files")
for previousFileName in files:
was_same_file_name = newfilename.lower() == previousFileName.lower()
file_was_renamed = newfilename != previousFileName

if was_same_file_name and file_was_renamed:
shell.execute("git rm --cached %s" % previousFileName)
os.chdir(cwd)
for newfilerelativepath in Commiter.splitoutputofgitstatusz(lines, "A "):
directoryofnewfile = os.path.dirname(os.path.join(sandbox, newfilerelativepath))
newfilename = os.path.basename(newfilerelativepath)
cwd = os.getcwd()
os.chdir(directoryofnewfile)
files = shell.getoutput("git ls-files")
for previousFileName in files:
was_same_file_name = newfilename.lower() == previousFileName.lower()
file_was_renamed = newfilename != previousFileName

if was_same_file_name and file_was_renamed:
shell.execute("git rm --cached %s" % previousFileName)
os.chdir(cwd)

@staticmethod
def getcommitcommand(changeentry):
Expand Down Expand Up @@ -186,25 +184,27 @@ def ignore(filelines):
ignore.writelines(filelines)

@staticmethod
def splitoutputofgitstatusz(lines):
def splitoutputofgitstatusz(lines, filterprefix=None):
"""
Split the output of 'git status -z' into single files
:param lines: the output line(s) from the command
:return: a list of all repository files with status changes
:param filterprefix: if given, only the files of those entries matching the prefix will be returned
:return: a list of repository files with status changes
"""
repositoryfiles = []
for line in lines: # expect exactly one line
entries = line.split(sep='\x00') # ascii 0 is the delimiter
for entry in entries:
entry = entry.strip()
if len(entry) > 0:
start = entry.find(' ')
if 1 <= start <= 2:
repositoryfile = entry[3:] # output is formatted
else:
repositoryfile = entry # file on a single line (e.g. rename continuation)
repositoryfiles.append(repositoryfile)
if not filterprefix or entry.startswith(filterprefix):
start = entry.find(' ')
if 1 <= start <= 2:
repositoryfile = entry[3:] # output is formatted
else:
repositoryfile = entry # file on a single line (e.g. rename continuation)
repositoryfiles.append(repositoryfile)
return repositoryfiles


Expand Down
18 changes: 18 additions & 0 deletions tests/test_gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ def test_splitoutputofgitstatusz(self):
self.assertEqual('project1/src/sub/kling |and| klong.zip', repositoryfiles[10])
self.assertEqual('project1/src/sub/klingklong.zip', repositoryfiles[11])

def test_splitoutputofgitstatusz_filterprefix_A(self):
with open('./resources/test_ignore_git_status_z.txt', 'r') as file:
repositoryfiles = Commiter.splitoutputofgitstatusz(file.readlines(), 'A ')
self.assertEqual(1, len(repositoryfiles))
self.assertEqual('project1/src/tobedeleted.txt', repositoryfiles[0])

def test_splitoutputofgitstatusz_filterprefix_double_question(self):
with open('./resources/test_ignore_git_status_z.txt', 'r') as file:
repositoryfiles = Commiter.splitoutputofgitstatusz(file.readlines(), '?? ')
self.assertEqual(7, len(repositoryfiles))
self.assertEqual('project1/src/sub/kling -- klong.zip', repositoryfiles[0])
self.assertEqual('project1/src/sub/kling :and: klong.zip', repositoryfiles[1])
self.assertEqual('project1/src/sub/kling ;and; klong.zip', repositoryfiles[2])
self.assertEqual('project1/src/sub/kling >and< klong.zip', repositoryfiles[3])
self.assertEqual('project1/src/sub/kling \\and\\ klong.zip', repositoryfiles[4])
self.assertEqual('project1/src/sub/kling |and| klong.zip', repositoryfiles[5])
self.assertEqual('project1/src/sub/klingklong.zip', repositoryfiles[6])

def test_filterignore(self):
with testhelper.mkchdir("aFolder") as folder:
# create test repo
Expand Down

0 comments on commit f9fcbc7

Please sign in to comment.