Initializing New GitHub Repository with API [message #871291] |
Fri, 11 May 2012 19:17 |
Owen Parrish Messages: 3 Registered: May 2012 |
Junior Member |
|
|
I want to use the GitHub API to initialize a newly created GitHub repository. I want to mimic the new functionality that's apart of the web UI, where a README will automatically be created and committed so that the fresh repo can just be cloned.
I'm having trouble though because every call to the GitHub API responds with a repository empty error.
I've tried following the procedure here: swanson.github.com/blog/2011/07/23/digging-around-the-github-api-take-2.html
That didn't work obviously because there are no existing commits.
So, then I thought I could:
1. create a blob
2. create a tree from that blob
3. commit that tree
4. create a reference pointing to that tree
That failed on step 1 because the repo is empty.
I've tried permutations of the above steps (creating the tree first, creating the ref first, creating the commit first) and each time - I get a repo empty error.
Is it even possible to do what I'm trying to do from starting from an empty repo?
[Updated on: Fri, 11 May 2012 19:18] Report message to a moderator
|
|
|
|
Re: Initializing New GitHub Repository with API [message #871294 is a reply to message #871293] |
Fri, 11 May 2012 19:23 |
Owen Parrish Messages: 3 Registered: May 2012 |
Junior Member |
|
|
Exact error is "Git Repository is empty. (409)" REALLY rough code is below. I added a content field to TreeEntry.
public void initializeRepo() throws SmartServiceException {
DataService service = createDataService();
RepositoryId repo = new RepositoryId(repositoryOwner, repositoryName);
try {
// if (LOG.isDebugEnabled()) {
// LOG.debug("Getting reference " + repo.toString() + "/"
// + MASTER_REFERENCE);
// }
//Create a blob for the file with its contents
// String blobSha = service
// .createBlob(
// repo,
// new Blob()
// .setContent(
// README_CONTENTS)
// .setEncoding(Blob.ENCODING_UTF8));
//create a new tree with our blob
TreeEntry treeEntry = createTreeEntryWithContent(readmeFileName, README_CONTENTS);
List<TreeEntry> treeEntries = new ArrayList<TreeEntry>();
treeEntries.add(treeEntry);
Tree newTree = service.createTree(repo, treeEntries);
//Commit our blob
Commit newCommit = new Commit().setTree(newTree);
Commit createdCommit = service.createCommit(repo, newCommit);
//Create a reference to the new tree
Reference newRef = new Reference()
.setObject((TypedResource) new TypedResource()
.setType(TypedResource.TYPE_COMMIT)
.setUrl(createdCommit.getUrl())
.setSha(createdCommit.getSha())).setRef(MASTER_REFERENCE);
Reference createdRef = service.createReference(repo, newRef);
} catch (IOException e) {
e.printStackTrace();
}
}
private TreeEntry createBlobTreeEntry(String path, String sha) {
TreeEntry treeEntry = new TreeEntry();
treeEntry.setPath(path);
treeEntry.setMode(TreeEntry.MODE_BLOB);
treeEntry.setSha(sha);
treeEntry.setType(TreeEntry.TYPE_BLOB);
return treeEntry;
}
private TreeEntry createTreeEntryWithContent(String path, String content) {
TreeEntry treeEntry = new TreeEntry();
treeEntry.setPath(path);
treeEntry.setMode(TreeEntry.MODE_BLOB);
treeEntry.setType(TreeEntry.TYPE_BLOB);
treeEntry.setContent(content);
return treeEntry;
}
[Updated on: Fri, 11 May 2012 19:24] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04233 seconds