Skip to content

Commit

Permalink
unify parsing of 'git status -z' output
Browse files Browse the repository at this point in the history
  • Loading branch information
ohumbel committed Aug 24, 2015
1 parent 926780f commit 16a6fc0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
63 changes: 30 additions & 33 deletions gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,21 @@ 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 line in lines:
for entry in line.split(sep='\x00'): # ascii 0 is the delimiter
entry = entry.strip()
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 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)

@staticmethod
def getcommitcommand(changeentry):
Expand Down Expand Up @@ -174,10 +172,8 @@ def filterignore():
if len(ignorefileextensions) > 0:
# make sure we see all untracked files:
strippedlines = shell.getoutput('git status --untracked-files=all --porcelain -z')
# expect exactly one line:
for strippedline in strippedlines:
repositoryfiles = Commiter.splitoutputofgitstatusz(strippedline)
Commiter.ignore(ExtensionFilter.match(repositoryfiles, ignorefileextensions))
repositoryfiles = Commiter.splitoutputofgitstatusz(strippedlines)
Commiter.ignore(ExtensionFilter.match(repositoryfiles, ignorefileextensions))

@staticmethod
def ignore(filelines):
Expand All @@ -190,24 +186,25 @@ def ignore(filelines):
ignore.writelines(filelines)

@staticmethod
def splitoutputofgitstatusz(line):
def splitoutputofgitstatusz(lines):
"""
Split the output of 'git status -z' into single files
:param line: the output line from the command
:param lines: the output line(s) from the command
:return: a list of all repository files with status changes
"""
repositoryfiles = []
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)
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)
return repositoryfiles


Expand Down
6 changes: 3 additions & 3 deletions tests/test_gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_CopyBranch_TargetAlreadyExist_ShouldFail(self):

def test_splitoutputofgitstatusz(self):
with open('./resources/test_ignore_git_status_z.txt', 'r') as file:
repositoryfiles = Commiter.splitoutputofgitstatusz(file.readline())
repositoryfiles = Commiter.splitoutputofgitstatusz(file.readlines())
self.assertEqual(12, len(repositoryfiles))
self.assertEqual('project1/src/tobedeleted.txt', repositoryfiles[0])
self.assertEqual('project2/src/taka.txt', repositoryfiles[1])
Expand Down Expand Up @@ -153,8 +153,8 @@ def test_filterignore(self):
lines = gitIgnore.readlines()
self.assertEqual(2, len(lines))
lines.sort()
self.assertEqual('test.jar', lines[0].strip())
self.assertEqual('test.zip', lines[1].strip())
self.assertEqual(jar, lines[0].strip())
self.assertEqual(zip, lines[1].strip())

def simulateCreationAndRenameInGitRepo(self, originalfilename, newfilename):
open(originalfilename, 'a').close() # create file
Expand Down

0 comments on commit 16a6fc0

Please sign in to comment.