Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 66 additions & 7 deletions gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ def initialcommit():
class Commiter:
commitcounter = 0
isattachedtoaworkitemregex = re.compile("^\d*:.*-")
findignorepatternregex = re.compile("\{([^\{\}]*)\}")

@staticmethod
def addandcommit(changeentry):
Commiter.filterignore()
Commiter.handleignore()
Commiter.replaceauthor(changeentry.author, changeentry.email)
shell.execute("git add -A")

Expand Down Expand Up @@ -168,16 +169,23 @@ def promotebranchtomaster(branchname):
return 1 # branch couldnt get renamed

@staticmethod
def filterignore():
def handleignore():
"""
add files with extensions to be ignored to .gitignore
check untracked files and handle both global and local ignores
"""
# make sure we see all untracked files:
strippedlines = shell.getoutput("git status --untracked-files=all -z")
repositoryfiles = Commiter.splitoutputofgitstatusz(strippedlines)
Commiter.ignoreextensions(repositoryfiles)
Commiter.ignorejazzignore(repositoryfiles)

@staticmethod
def ignoreextensions(repositoryfiles):
"""
add files with extensions to be ignored to the global .gitignore
"""
# there is only work to to if there are extensions configured at all
ignorefileextensions = configuration.get().ignorefileextensions
if len(ignorefileextensions) > 0:
# make sure we see all untracked files:
strippedlines = shell.getoutput('git status --untracked-files=all -z')
repositoryfiles = Commiter.splitoutputofgitstatusz(strippedlines)
Commiter.ignore(ExtensionFilter.match(repositoryfiles, ignorefileextensions))

@staticmethod
Expand Down Expand Up @@ -214,6 +222,57 @@ def splitoutputofgitstatusz(lines, filterprefix=None):
repositoryfiles.append(repositoryfile)
return repositoryfiles

@staticmethod
def translatejazzignore(jazzignorelines):
"""
translate the lines of a local .jazzignore file into the lines of a local .gitignore file

:param jazzignorelines: the input lines
:return: the .gitignore lines
"""
recursive = False
gitignorelines = []
for line in jazzignorelines:
if not line.startswith("#"):
line = line.strip()
if line.startswith("core.ignore"):
gitignorelines.append(os.linesep)
recursive = line.startswith("core.ignore.recursive")
for foundpattern in Commiter.findignorepatternregex.findall(line):
gitignoreline = foundpattern + os.linesep
if not recursive:
gitignoreline = '/' + gitignoreline # forward, not os.sep
gitignorelines.append(gitignoreline)
return gitignorelines

@staticmethod
def ignorejazzignore(repositoryfiles):
"""
If a .jazzignore file is modified or added, translate it to .gitignore,
if a .jazzignore file is deleted, delete the corresponding .gitignore file as well.

:param repositoryfiles: the modified files
"""
jazzignore = ".jazzignore"
jazzignorelen = len(jazzignore)
for repositoryfile in repositoryfiles:
if repositoryfile[-jazzignorelen:] == jazzignore:
path = repositoryfile[0:len(repositoryfile)-jazzignorelen]
gitignore = path + ".gitignore"
if os.path.exists(repositoryfile):
# update (or create) .gitignore
jazzignorelines = []
with open(repositoryfile, 'r') as jazzignorefile:
jazzignorelines = jazzignorefile.readlines()
if len(jazzignorelines) > 0:
# overwrite in any case
with open(gitignore, 'w') as gitignorefile:
gitignorefile.writelines(Commiter.translatejazzignore(jazzignorelines))
else:
# delete .gitignore
if os.path.exists(gitignore):
os.remove(gitignore)


class Differ:
@staticmethod
Expand Down
5 changes: 2 additions & 3 deletions migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ def migrate():
git.promotebranchtomaster(streamname)

RTCLogin.logout()
shouter.shout("\nAll changes accepted - Migration of stream '%s' is completed. \n"
"You should adjust your .gitignore to ignore the same files as defined in your .jazzignore \n"
"Afterwards you can distribute the git-repo '%s'" % (streamname, config.gitRepoName))
shouter.shout("\nAll changes accepted - Migration of stream '%s' is completed.\n"
"You can distribute the git-repo '%s'." % (streamname, config.gitRepoName))


def prepare():
Expand Down
13 changes: 13 additions & 0 deletions tests/resources/test_.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

*.class
*.so
*.pyc

/*.suo
/.classpath
/.idea
/.project
/.settings
/bin
/dist
/a?c
37 changes: 37 additions & 0 deletions tests/resources/test_.jazzignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### Jazz Ignore 0
# Ignored files and folders will not be committed, but may be modified during
# accept or update.
# - Ignore properties should contain a space separated list of filename patterns.
# - Each pattern is case sensitive and surrounded by braces ('{' and '}').
# - "*" matches zero or more characters.
# - "?" matches a single character.
# - The pattern list may be split across lines by ending the line with a
# backslash and starting the next line with a tab.
# - Patterns in core.ignore prevent matching resources in the same
# directory from being committed.
# - Patterns in core.ignore.recursive matching resources in the current
# directory and all subdirectories from being committed.
# - The default value of core.ignore.recursive is *.class
# - The default value for core.ignore is bin
#
# To ignore shell scripts and hidden files in this subtree:
# e.g: core.ignore.recursive = {*.sh} {\.*}
#
# To ignore resources named 'bin' in the current directory (but allow
# them in any sub directorybelow):
# e.g: core.ignore.recursive = {*.sh} {\.*}
#
# NOTE: modifying ignore files will not change the ignore status of
# Eclipse derived resources.

core.ignore.recursive = {*.class} {*.so} \
{*.pyc}

core.ignore = {*.suo} {.classpath} \
{.idea} \
{.project} \
{.settings} \
{bin} \
{dist} \
{a?c}

105 changes: 97 additions & 8 deletions tests/test_gitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,21 @@ def test_splitoutputofgitstatusz_filterprefix_double_question(self):
self.assertEqual('project1/src/sub/kling |and| klong.zip', repositoryfiles[5])
self.assertEqual('project1/src/sub/klingklong.zip', repositoryfiles[6])

def test_filterignore(self):
def test_handleignore_global_extensions(self):
with testhelper.mkchdir("aFolder") as folder:
# create test repo
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").setignorefileextensions('.zip; .jar').build()
ignore = '.gitignore'
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").setignorefileextensions(".zip; .jar").build()
ignore = ".gitignore"
Initializer().createrepo()
# simulate addition of .zip and .jar files
zip = 'test.zip'
zip = "test.zip"
with open(zip, 'w') as testzip:
testzip.write('test zip content')
jar = 'test.jar'
testzip.write("test zip content")
jar = "test.jar"
with open(jar, 'w') as testjar:
testjar.write('test jar content')
testjar.write("test jar content")
# do the filtering
Commiter.filterignore()
Commiter.handleignore()
# check output of .gitignore
with open(ignore, 'r') as gitIgnore:
lines = gitIgnore.readlines()
Expand All @@ -173,6 +173,87 @@ def test_filterignore(self):
self.assertEqual(jar, lines[0].strip())
self.assertEqual(zip, lines[1].strip())

def test_handleignore_local_jazzignore_expect_new_gitignore(self):
with testhelper.mkchdir("aFolder") as folder:
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").build()
Initializer().createrepo()
subfolder = "aSubFolder"
os.mkdir(subfolder)
jazzignore = subfolder + os.sep + ".jazzignore"
with open(jazzignore, 'w') as testjazzignore:
testjazzignore.write("# my ignores\n")
testjazzignore.write("core.ignore = {*.pyc}")
expectedlines = ["\n","/*.pyc\n"]
gitignore = subfolder + os.sep + ".gitignore"
self.assertFalse(os.path.exists(gitignore))
Commiter.handleignore()
self.assertTrue(os.path.exists(gitignore))
gitignorelines = []
with open(gitignore, 'r') as localgitignore:
gitignorelines = localgitignore.readlines()
self.assertEqual(expectedlines, gitignorelines)

def test_handleignore_local_jazzignore_expect_overwrite_gitignore(self):
with testhelper.mkchdir("aFolder") as folder:
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").build()
Initializer().createrepo()
subfolder = "aSubFolder"
os.mkdir(subfolder)
gitignore = subfolder + os.sep + ".gitignore"
with open(gitignore, 'w') as localgitignore:
localgitignore.write('\n')
localgitignore.write("/*.pyc")
jazzignore = subfolder + os.sep + ".jazzignore"
with open(jazzignore, 'w') as testjazzignore:
testjazzignore.write("# my ignores\n")
testjazzignore.write("core.ignore = {*.class} {bin}")
expectedlines = ["\n","/*.class\n","/bin\n"]
Commiter.handleignore()
self.assertTrue(os.path.exists(gitignore))
gitignorelines = []
with open(gitignore, 'r') as localgitignore:
gitignorelines = localgitignore.readlines()
self.assertEqual(expectedlines, gitignorelines)

def test_handleignore_local_jazzignore_expect_empty_gitignore(self):
with testhelper.mkchdir("aFolder") as folder:
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").build()
Initializer().createrepo()
subfolder = "aSubFolder"
os.mkdir(subfolder)
gitignore = subfolder + os.sep + ".gitignore"
with open(gitignore, 'w') as localgitignore:
localgitignore.write('\n')
localgitignore.write("/*.pyc")
jazzignore = subfolder + os.sep + ".jazzignore"
with open(jazzignore, 'w') as testjazzignore:
testjazzignore.write("# my ignores are empty\n")
Commiter.handleignore()
self.assertTrue(os.path.exists(gitignore))
gitignorelines = []
with open(gitignore, 'r') as localgitignore:
gitignorelines = localgitignore.readlines()
self.assertEqual(0, len(gitignorelines))

def test_handleignore_local_jazzignore_expect_delete_gitignore(self):
with testhelper.mkchdir("aFolder") as folder:
# create a repository with a .jazzignore and .gitignore file
configuration.config = Builder().setworkdirectory(folder).setgitreponame("test.git").build()
Initializer().createrepo()
subfolder = "aSubFolder"
os.mkdir(subfolder)
jazzignore = subfolder + os.sep + ".jazzignore"
with open(jazzignore, 'w') as testjazzignore:
testjazzignore.write("# my ignores\n")
testjazzignore.write("core.ignore = {*.pyc}")
Commiter.addandcommit(testhelper.createchangeentry(comment="Initial .jazzignore"))
gitignore = subfolder + os.sep + ".gitignore"
self.assertTrue(os.path.exists(gitignore))
# now remove .jazzignore
os.remove(jazzignore)
Commiter.handleignore()
self.assertFalse(os.path.exists(gitignore))

def test_checkbranchname_expect_valid(self):
with testhelper.createrepo(folderprefix="gitfunctionstestcase_"):
self.assertEqual(True, Commiter.checkbranchname("master"), "master should be a valid branch name")
Expand Down Expand Up @@ -231,6 +312,14 @@ def test_IllegalGitCharsShouldntCreateFile_SpecialCaseAlreadyQuoted(self):
Commiter.addandcommit(testhelper.createchangeentry(comment="Check out \"" + ">" + "\"US3333\""))
self.assertEqual(0, len(shell.getoutput("git status -z")), "No file should be created by commit message")

def test_translatejazzignore(self):
with open(testhelper.getrelativefilename('./resources/test_.jazzignore'), 'r') as jazzignore:
inputlines = jazzignore.readlines()
with open(testhelper.getrelativefilename('./resources/test_.gitignore'), 'r') as gitignore:
expectedlines = gitignore.readlines()
self.assertEqual(expectedlines, Commiter.translatejazzignore(inputlines))


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