I've been working on some Python scripts which make use of the gitpython library to automate the creation of local branches based on issues in our redmine instance. To get started I'm trying to create some local repo's to simulate the system as I expect it to work but I'm falling at the first hurdle.
This is the setup for my test case which should create a 'remote' repo, then clone it to make a local one and finally, create a local feature branch:
def setUp(self):
# Create a remote git repo to simulate the one gitlab maintains
baseDir = os.path.join('C:\\', 'test-repos')
if os.path.exists(baseDir):
shutil.rmtree(baseDir)
gitlabRepoLocation = os.path.join(baseDir, 'gitlab')
gitlabRepo = git.Repo.init(gitlabRepoLocation)
# Clone to a local repo
localRepoLocation = os.path.join(baseDir, 'local')
localRepo = git.Repo.clone_from("file://"+gitlabRepoLocation, localRepoLocation)
localRepo.create_head('some-feature') # <-- This fails
But when I run it I get:
Traceback (most recent call last):
File "C:\Projects\PyTools\Gitted\test_Helpers.py", line 70, in setUp
gitlabRepo.create_head('some-feature')
File "C:\Python34\lib\site-packages\git\repo\base.py", line 330, in create_head
return Head.create(self, path, commit, force, logmsg)
File "C:\Python34\lib\site-packages\git\refs\symbolic.py", line 527, in create
return cls._create(repo, path, cls._resolve_ref_on_create, reference, force, logmsg)
File "C:\Python34\lib\site-packages\git\refs\symbolic.py", line 479, in _create
target = repo.rev_parse(str(reference))
File "C:\Python34\lib\site-packages\git\repo\fun.py", line 311, in rev_parse
obj = name_to_object(repo, rev)
File "C:\Python34\lib\site-packages\git\repo\fun.py", line 124, in name_to_object
raise BadName(name)
gitdb.exc.BadName: Ref 'HEAD' did not resolve to an object
Turns out @torek was correct. Adding and comitting an empty file solved the issue:
filename = 'readme.txt'
open(filename, 'wb').close()
gitlabRepo.index.add([filename])
gitlabRepo.index.commit("Adding "+filename+ "to repo")