Skip to main content



      Home
Home » Eclipse Projects » EGit / JGit » Initializing New GitHub Repository with API
Initializing New GitHub Repository with API [message #871291] Fri, 11 May 2012 15:17 Go to next message
Eclipse UserFriend
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 15:18] by Moderator

Re: Initializing New GitHub Repository with API [message #871293 is a reply to message #871291] Fri, 11 May 2012 15:19 Go to previous messageGo to next message
Eclipse UserFriend
What exact error are you getting when it is failing?

Could you post the code snippet you are using for this as well?
Re: Initializing New GitHub Repository with API [message #871294 is a reply to message #871293] Fri, 11 May 2012 15:23 Go to previous messageGo to next message
Eclipse UserFriend
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 15:24] by Moderator

Re: Initializing New GitHub Repository with API [message #871303 is a reply to message #871294] Fri, 11 May 2012 16:27 Go to previous messageGo to next message
Eclipse UserFriend
Which call is throwing that exception?
Re: Initializing New GitHub Repository with API [message #871304 is a reply to message #871303] Fri, 11 May 2012 16:30 Go to previous message
Eclipse UserFriend
That exception is thrown on the below line (when it's not commented out)

String blobSha = service.createBlob(repo,new Blob().setContent(README_CONTENTS).setEncoding(Blob.ENCODING_UTF8));


Previous Topic:pushing to remote repo fails, authenticity error
Next Topic:project decorations still broken with local tracking
Goto Forum:
  


Current Time: Thu Jun 12 15:47:12 EDT 2025

Powered by FUDForum. Page generated in 0.03646 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top