1

I am trying to create a Git repository using objective-git. The documentation is somewhat lacking on how the GT classes work together to create a repository so I'm kind of stuck.

This is what I have working so far:

GTRepository *newRepo = [GTRepository initializeEmptyRepositoryAtFileURL:repoURL error:&error]

I then copy a bunch of files into that directory. I can open then the repo in a Git client and see those files in the working directory. Everything looks good so far. But this is where I'm stuck, what is my next move to actually create a branch and commit those files?

EDIT: I have now gotten the GTIndex from my GTRepository (though, the fileURL property of the index is still nil, so I'm not sure how to generate that file). I iterate through my files, calling addFile:error: which creates GTIndexEntries in my index. I then call writeTree: and get back a GTTree that I pass into [repository createCommitWithTree:newTree message:@"Initial Commit" parents:nil updatingReferenceNamed:nil error:&error];. This returns a valid GTCommit object and no NSError, but I would expect that [repository headReferenceWithError:&error]; would now return a reference to the newly created commit, but it still returns nil. Is there a step I am missing to finalize this commit?

Patrick Goley
  • 5,397
  • 22
  • 42
  • 1
    Do you really want to create an index or load the repository's existing index with something like `[repository indexWithError:NULL]` ? – Edward Thomson Nov 21 '13 at 21:54
  • I didn't see the index file so I assumed I had to create my own, but I see that indexWithError does return a valid GTIndex. Now I will attempt to use addFile: and writeTree: using the index, instead of using GTTreeBuilder – Patrick Goley Nov 21 '13 at 22:02

1 Answers1

3

Looks like there's a [GTReference referenceByCreatingReferenceNamed:name] static method. Here's an example of how you use it.

Ben Straub
  • 5,675
  • 3
  • 28
  • 43
  • both of these methods eventually call the function git_reference_lookup() that searches for an existing reference with that name. I have no existing references – Patrick Goley Nov 21 '13 at 21:42
  • Rats. Well, I guess you could always just call `git_reference_create`: http://libgit2.github.com/libgit2/#HEAD/group/reference/git_reference_create – Ben Straub Nov 21 '13 at 22:51
  • that's true, I'd just like to stick to Objective-Git if possible – Patrick Goley Nov 21 '13 at 22:54
  • 1
    Edited: check out `[GTReference referenceByCreatingReferenceNamed:name]`! – Ben Straub Nov 22 '13 at 17:47
  • 1
    Yes!! That did it, you pass in the @"refs/heads/branchName" as name and the SHA of some commit as the reference target parameter. Then passing that ref to GTBranch initWithReference:repository: and then check out the ref with the repo and boom, you've got an initial commit and the master branch checked out. – Patrick Goley Nov 22 '13 at 20:20