Skip to content

Commit

Permalink
Merge pr #79 "enable line ending normalization" - fixes #73
Browse files Browse the repository at this point in the history
  • Loading branch information
WtfJoke committed Nov 15, 2015
2 parents 8fa1355 + 607de7d commit 9b9f7a0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 21 deletions.
6 changes: 6 additions & 0 deletions config.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ MaxChangeSetsToAcceptTogether = 10
# On bitbucket/github: #
CommitMessageWorkItemPrefix=

# Optional: Specifies the line(s) which are added to .gitattributes
# Define a semicolon-separated list of lines
# Example:
# Gitattributes = # handle text files; * text=auto; *.sql text
Gitattributes =

[Miscellaneous]
# Set to true if you want to see which commands are sent to command line
LogShellCommands = False
Expand Down
38 changes: 23 additions & 15 deletions configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def read(configname=None):
parsedconfig = configparser.ConfigParser()
parsedconfig.read(configname)
generalsection = parsedconfig['General']
migrationsection = parsedconfig['Migration']
migrationsectionname = 'Migration'
migrationsection = parsedconfig[migrationsectionname]
miscsectionname = 'Miscellaneous'
global user
if not user:
Expand All @@ -45,9 +46,11 @@ def read(configname=None):
previousstreamname = migrationsection.get('PreviousStream', '').strip()
baselines = getinitialcomponentbaselines(migrationsection.get('InitialBaseLines'))
ignorefileextensionsproperty = parsedconfig.get(miscsectionname, 'IgnoreFileExtensions', fallback='')
ignorefileextensions = parseignorefileextensionsproperty(ignorefileextensionsproperty)
ignorefileextensions = parsesplittedproperty(ignorefileextensionsproperty)
includecomponentroots = parsedconfig.get(miscsectionname, 'IncludeComponentRoots', fallback="False")
commitmessageprefix = migrationsection.get('CommitMessageWorkItemPrefix', "")
gitattributesproperty = parsedconfig.get(migrationsectionname, 'Gitattributes', fallback='')
gitattributes = parsesplittedproperty(gitattributesproperty)

configbuilder = Builder().setuser(user).setpassword(password).setrepourl(repositoryurl).setscmcommand(scmcommand)
configbuilder.setworkspace(workspace).setgitreponame(gitreponame).setrootfolder(os.getcwd())
Expand All @@ -58,6 +61,7 @@ def read(configname=None):
configbuilder.setpreviousstreamname(previousstreamname)
configbuilder.setignorefileextensions(ignorefileextensions)
configbuilder.setincludecomponentroots(includecomponentroots).setcommitmessageprefix(commitmessageprefix)
configbuilder.setgitattributes(gitattributes)
global config
config = configbuilder.build()
return config
Expand Down Expand Up @@ -96,18 +100,16 @@ def getinitialcomponentbaselines(definedbaselines):
return initialcomponentbaselines


def parseignorefileextensionsproperty(ignorefileextensionsproperty):
def parsesplittedproperty(property, separator=';'):
"""
:param ignorefileextensionsproperty
:return: a list of file extensions to be ignored, possibly empty
:param property
:return: a list single properties, possibly empty
"""
splittedextensions = []
if ignorefileextensionsproperty and len(ignorefileextensionsproperty) > 0:
splittedextensions = ignorefileextensionsproperty.split(';')
ignorefileextensions = []
for splittedextension in splittedextensions:
ignorefileextensions.append(splittedextension.strip())
return ignorefileextensions
properties = []
if property and len(property) > 0:
for splittedproperty in property.split(separator):
properties.append(splittedproperty.strip())
return properties


class Builder:
Expand All @@ -133,6 +135,7 @@ def __init__(self):
self.ignorefileextensions = ""
self.includecomponentroots = ""
self.commitmessageprefix = ""
self.gitattributes = ""

def setuser(self, user):
self.user = user
Expand Down Expand Up @@ -211,6 +214,10 @@ def setcommitmessageprefix(self, commitmessageprefix):
self.commitmessageprefix = commitmessageprefix
return self

def setgitattributes(self, gitattributes):
self.gitattributes = gitattributes
return self

@staticmethod
def isenabled(stringwithbooleanexpression):
return stringwithbooleanexpression == "True"
Expand All @@ -221,14 +228,14 @@ def build(self):
self.streamname, self.gitreponame, self.useprovidedhistory,
self.useautomaticconflictresolution, self.maxchangesetstoaccepttogether, self.clonedgitreponame, self.rootFolder,
self.previousstreamname, self.ignorefileextensions, self.includecomponentroots,
self.commitmessageprefix)
self.commitmessageprefix, self.gitattributes)


class ConfigObject:
def __init__(self, user, password, repourl, scmcommand, workspace, useexistingworkspace, workdirectory,
initialcomponentbaselines, streamname, gitreponame, useprovidedhistory,
useautomaticconflictresolution, maxchangesetstoaccepttogether, clonedgitreponame, rootfolder, previousstreamname,
ignorefileextensionsproperty, includecomponentroots, commitmessageprefix):
ignorefileextensions, includecomponentroots, commitmessageprefix, gitattributes):
self.user = user
self.password = password
self.repo = repourl
Expand All @@ -249,9 +256,10 @@ def __init__(self, user, password, repourl, scmcommand, workspace, useexistingwo
self.streamuuid = ""
self.previousstreamname = previousstreamname
self.previousstreamuuid = ""
self.ignorefileextensions = ignorefileextensionsproperty
self.ignorefileextensions = ignorefileextensions
self.includecomponentroots = includecomponentroots
self.commitmessageprefix = commitmessageprefix
self.gitattributes = gitattributes

def getlogpath(self, filename):
if not self.hasCreatedLogFolder:
Expand Down
21 changes: 19 additions & 2 deletions gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self):

@staticmethod
def createignore():
newline = "\n"
newline = os.linesep
git_ignore = ".gitignore"

if not os.path.exists(git_ignore):
Expand All @@ -27,6 +27,22 @@ def createignore():
shell.execute("git add " + git_ignore)
shell.execute("git commit -m %s -q" % shell.quote("Add .gitignore"))

@staticmethod
def createattributes():
"""
create a .gitattributes file (if so specified and not yet present)
"""
config = configuration.get()
if len(config.gitattributes) > 0:
newline = os.linesep
gitattribues = ".gitattributes"
if not os.path.exists(gitattribues):
with open(gitattribues, "w") as attributes:
for line in config.gitattributes:
attributes.write(line + newline)
shell.execute("git add " + gitattribues)
shell.execute("git commit -m %s -q" % shell.quote("Add .gitattributes"))

def initalize(self):
self.createrepo()
self.preparerepo()
Expand All @@ -35,6 +51,7 @@ def initalize(self):
def preparerepo():
Initializer.setgitconfigs()
Initializer.createignore()
Initializer.createattributes()

def createrepo(self):
shell.execute("git init --bare " + self.repoName)
Expand Down Expand Up @@ -299,5 +316,5 @@ def match(repositoryfiles, extensions):
if len(repositoryfile) >= extlen:
if repositoryfile[-extlen:] == extension:
# escape a backslash with a backslash, and append a newline
repositoryfilestoignore.append(repositoryfile.replace('\\', '\\\\') + '\n')
repositoryfilestoignore.append(repositoryfile.replace('\\', '\\\\') + os.linesep)
return repositoryfilestoignore
1 change: 1 addition & 0 deletions tests/resources/test_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ UseProvidedHistory = True
UseAutomaticConflictResolution = True
MaxChangeSetsToAcceptTogether = 100
CommitMessageWorkItemPrefix = UP-
Gitattributes = # Handle line endings automatically for text files; # and leave binary files untouched; * text=auto; *.sql text

[Miscellaneous]
LogShellCommands = True
Expand Down
33 changes: 29 additions & 4 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,42 @@ def test_getSampleConfig_ExpectInitializedConfigWithDefaultValues(self):
self.assertEqual(config, configuration.get())

def test_fileExtensionsToBeIgnored_ShouldBeEmpty_FromNone(self):
config = Builder().setignorefileextensions(configuration.parseignorefileextensionsproperty(None)).build()
config = Builder().setignorefileextensions(configuration.parsesplittedproperty(None)).build()
self.assertEqual(0, len(config.ignorefileextensions))

def test_fileExtensionsToBeIgnored_ShouldBeEmpty_FromEmpty(self):
config = Builder().setignorefileextensions("").build()
self.assertEqual(0, len(config.ignorefileextensions))

def test_fileExtensionsToBeIgnored_SingleExtensions(self):
config = Builder().setignorefileextensions(configuration.parseignorefileextensionsproperty(" .zip ")).build()
def test_fileExtensionsToBeIgnored_SingleExtension(self):
config = Builder().setignorefileextensions(configuration.parsesplittedproperty(" .zip ")).build()
self.assertEqual(1, len(config.ignorefileextensions))
self.assertEqual(['.zip'], config.ignorefileextensions)

def test_fileExtensionsToBeIgnored_MultipleExtensions(self):
config = Builder().setignorefileextensions(configuration.parseignorefileextensionsproperty(".zip; .jar; .exe")) \
config = Builder().setignorefileextensions(configuration.parsesplittedproperty(".zip; .jar; .exe")) \
.build()
self.assertEqual(3, len(config.ignorefileextensions))
self.assertEqual(['.zip', '.jar', '.exe'], config.ignorefileextensions)

def test_gitattributes_ShouldBeEmpty_FromNone(self):
config = Builder().setgitattributes(configuration.parsesplittedproperty(None)).build()
self.assertEqual(0, len(config.gitattributes))

def test_gitattributes_ShouldBeEmpty_FromEmpty(self):
config = Builder().setgitattributes(configuration.parsesplittedproperty("")).build()
self.assertEqual(0, len(config.gitattributes))

def test_gitattributes__SingleProperty(self):
config = Builder().setgitattributes(configuration.parsesplittedproperty(" * text=auto ")).build()
self.assertEqual(1, len(config.gitattributes))
self.assertEqual(['* text=auto'], config.gitattributes)

def test_gitattributes__MultipleProperties(self):
config = Builder().setgitattributes(configuration.parsesplittedproperty(" # some comment ; * text=auto ; *.sql text ")).build()
self.assertEqual(3, len(config.gitattributes))
self.assertEqual(['# some comment', '* text=auto', '*.sql text'], config.gitattributes)

def test_read_passedin_configfile(self):
self._assertTestConfig(configuration.read(testhelper.getrelativefilename('resources/test_config.ini')))

Expand Down Expand Up @@ -115,6 +133,12 @@ def _assertTestConfig(self, config, user=None, password=None):
self.assertTrue(config.useautomaticconflictresolution)
self.assertEqual(100, config.maxchangesetstoaccepttogether)
self.assertEqual("UP-", config.commitmessageprefix)
gitattributes = config.gitattributes
self.assertEqual(4, len(gitattributes))
self.assertEqual('# Handle line endings automatically for text files', gitattributes[0])
self.assertEqual('# and leave binary files untouched', gitattributes[1])
self.assertEqual('* text=auto', gitattributes[2])
self.assertEqual('*.sql text', gitattributes[3])
# [Miscellaneous]
self.assertTrue(shell.logcommands) # directly deviated to shell
ignorefileextensions = config.ignorefileextensions
Expand Down Expand Up @@ -142,6 +166,7 @@ def _assertDefaultConfig(self, config):
self.assertFalse(config.useautomaticconflictresolution)
self.assertEqual(10, config.maxchangesetstoaccepttogether)
self.assertEqual("", config.commitmessageprefix)
self.assertEqual(0, len(config.gitattributes))
# [Miscellaneous]
self.assertFalse(shell.logcommands) # directly deviated to shell
self.assertEqual(0, len(config.ignorefileextensions))
Expand Down
27 changes: 27 additions & 0 deletions tests/test_gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ def test_CreationOfGitIgnore_DoesntExist_ShouldGetCreated(self):
gitignorepath = os.path.join(os.getcwd(), ignore)
self.assertTrue(os.path.exists(gitignorepath))

def test_CreationOfGitattributes_ExistAlready_ShouldntGetCreated(self):
with testhelper.mkchdir("aFolder") as folder:
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").setgitattributes(["# comment", "*.sql text"]).build()
attributes = '.gitattributes'
existing_git_attribute_entry = "* text=auto"
Initializer().createrepo()
with open(attributes, 'w') as gitattributes:
gitattributes.write(existing_git_attribute_entry)
Initializer.createattributes()
with open(attributes, 'r') as gitattributes:
for line in gitattributes.readlines():
self.assertEqual(existing_git_attribute_entry, line)

def test_CreationOfGitattributes_DoesntExist_ShouldGetCreated(self):
with testhelper.mkchdir("aFolder") as folder:
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").setgitattributes(["# comment", "* text=auto"]).build()
attributes = '.gitattributes'
Initializer().createrepo()
Initializer.createattributes()
gitattributespath = os.path.join(os.getcwd(), attributes)
self.assertTrue(os.path.exists(gitattributespath))
with open(gitattributespath, 'r') as gitattributes:
lines = gitattributes.readlines()
self.assertEqual(2, len(lines))
self.assertEquals('# comment\n', lines[0])
self.assertEquals('* text=auto\n', lines[1])

def test_BranchRenaming_TargetBranchDoesntExist(self):
with testhelper.createrepo(folderprefix="gitfunctionstestcase_"):
branchname = "hello"
Expand Down

0 comments on commit 9b9f7a0

Please sign in to comment.