Skip to content

Commit

Permalink
block invalid branch names
Browse files Browse the repository at this point in the history
If a stream name would lead to invalid git branch names, abort the migration and suggest renaming the stream.
Fixes rtcTo#51.
  • Loading branch information
ohumbel committed Aug 31, 2015
1 parent 5640afc commit 1698b34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def replaceauthor(author, email):
shell.execute("git config --replace-all user.name " + shell.quote(author))
shell.execute("git config --replace-all user.email " + email)

@staticmethod
def checkbranchname(branchname):
exitcode = shell.execute("git check-ref-format --normalize refs/heads/" + branchname)
if exitcode is 0:
return True
else:
return False

@staticmethod
def branch(branchname):
branchexist = shell.execute("git show-ref --verify --quiet refs/heads/" + branchname)
Expand Down
17 changes: 17 additions & 0 deletions migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ def parsecommandline():
configuration.setconfigfile(arguments.configfile)


def validate():
config = configuration.get()
streamname = config.streamname
branchname = streamname + "_branchpoint"
previousstreamname = config.previousstreamname
offendingbranchname = None
if not Commiter.checkbranchname(streamname):
offendingbranchname = streamname
elif not Commiter.checkbranchname(branchname):
offendingbranchname = branchname
elif not Commiter.checkbranchname(previousstreamname):
offendingbranchname = previousstreamname
if offendingbranchname:
sys.exit(offendingbranchname + " is not a valid git branch name - consider renaming the stream")


if __name__ == "__main__":
parsecommandline()
validate()
migrate()
12 changes: 12 additions & 0 deletions tests/test_gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ def test_filterignore(self):
self.assertEqual(jar, lines[0].strip())
self.assertEqual(zip, lines[1].strip())

def test_checkbranchname_expect_valid(self):
with testhelper.createrepo(folderprefix="gitfunctionstestcase_"):
self.assertEqual(True, Commiter.checkbranchname("master"), "master should be a valid branch name")

def test_checkbranchname_quoted_expect_invalid(self):
with testhelper.createrepo(folderprefix="gitfunctionstestcase_"):
self.assertEqual(False, Commiter.checkbranchname("'master pflaster'"), "'master pflaster' should not be a valid branch name")

def test_checkbranchname_unquoted_expect_invalid(self):
with testhelper.createrepo(folderprefix="gitfunctionstestcase_"):
self.assertEqual(False, Commiter.checkbranchname("master pflaster"), "master pflaster should not be a valid branch name")

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

0 comments on commit 1698b34

Please sign in to comment.