Initial hacking to start handling explicit file/contents

Change-Id: I359dd280ce9edef56789c086cc377238238700e4
This commit is contained in:
Darragh Bailey 2018-03-10 11:18:17 +00:00 committed by Darragh Bailey
parent 545037556d
commit 56bc35c7e7
1 changed files with 24 additions and 16 deletions

View File

@ -240,29 +240,37 @@ class GitFixture(fixtures.Fixture):
if self._graph:
self.gittree = GitTree(self, self._graph, self._branches)
def _create_file(self):
contents = "\n\n".join(self._faker.paragraphs(3))
def _create_file(self, filename=None, contents=None):
if not contents:
contents = "\n\n".join(self._faker.paragraphs(3))
# always want to ensure the files added to the repo are unique no
# matter which branch they are added to, as otherwise there may
# be conflicts caused by replaying local changes and performing
# merges
while True:
tmpfile = tempfile.NamedTemporaryFile(
dir=self.repo.working_dir, delete=False)
if tmpfile.name not in self._file_list:
self._file_list.add(tmpfile.name)
break
# case where same filename in use on a different branch
tmpfile.close()
os.remove(tmpfile.name)
if filename is None
while True:
tmpfile = tempfile.NamedTemporaryFile(
dir=self.repo.working_dir, delete=False)
# close immediately to ensure code at the end of this
# be reopened for writing
tmpfile.close()
if tmpfile.name not in self._file_list:
self._file_list.add(tmpfile.name)
filename = tmpfile.name
break
# remove file as name in use elsewhere in the git repo
# on a different branch
os.remove(tmpfile.name)
tmpfile.write(contents.encode('utf-8'))
tmpfile.close()
return tmpfile.name
with open(filename, 'w') as f:
f.write(contents.encode('utf-8'))
def _create_file_commit(self, change_id=None, message_prefix=None):
filename = self._create_file()
return filename
def _create_file_commit(self, message=None, contents=None,
change_id=None, message_prefix=None):
filename = self._create_file(contents=contents)
self.repo.git.add(filename)
message = "Adding %s" % os.path.basename(filename)
if message_prefix: