Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 GitHub Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Kevin Sawicki (GitHub Inc.) - initial API and implementation /
 
 package org.eclipse.egit.github.core.service;
 
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_BLOBS;
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_COMMITS;
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_GIT;
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REFS;
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REPOS;
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_TAGS;
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_TREES;
 
 
 import java.util.List;
 import java.util.Map;
 
Data service class for low-level access to Git repository data.

See also:
GitHub data API documentation
 
 public class DataService extends GitHubService {

Create data service
 
 	public DataService() {
 		super();
 	}

Create data service

Parameters:
client
 
 	public DataService(GitHubClient client) {
 		super(client);
 	}

Get blob for given SHA-1

Parameters:
repository
sha
Returns:
blob
Throws:
java.io.IOException
 
 	public Blob getBlob(IRepositoryIdProvider repositoryString sha)
 			throws IOException {
 		final String id = getId(repository);
 		if (sha == null)
 			throw new IllegalArgumentException("SHA-1 cannot be null"); //$NON-NLS-1$
 		if (sha.length() == 0)
 			throw new IllegalArgumentException("SHA-1 cannot be empty"); //$NON-NLS-1$
 
 		StringBuilder uri = new StringBuilder();
 		uri.append('/').append(id);
 		uri.append('/').append(sha);
 		GitHubRequest request = createRequest();
 		request.setType(Blob.class);
 		request.setUri(uri);
 		return (Blob.get(request).getBody();
 	}

Create blob with given content

Parameters:
repository
blob
Returns:
SHA-1 of created blob
Throws:
java.io.IOException
	public String createBlob(IRepositoryIdProvider repositoryBlob blob)
			throws IOException {
		final String id = getId(repository);
		if (blob == null)
			throw new IllegalArgumentException("Blob cannot be null"); //$NON-NLS-1$
		uri.append('/').append(id);
		ShaResource created = .post(uri.toString(), blob,
		return created != null ? created.getSha() : null;
	}

Get tree with given SHA-1

Parameters:
repository
sha
Returns:
tree
Throws:
java.io.IOException
	public Tree getTree(IRepositoryIdProvider repositoryString sha)
			throws IOException {
		return getTree(repositoryshafalse);
	}

Get tree with given SHA-1

Parameters:
repository
sha
recursive
Returns:
tree
Throws:
java.io.IOException
	public Tree getTree(IRepositoryIdProvider repositoryString sha,
			boolean recursivethrows IOException {
		final String id = getId(repository);
		if (sha == null)
			throw new IllegalArgumentException("SHA-1 cannot be null"); //$NON-NLS-1$
		if (sha.length() == 0)
			throw new IllegalArgumentException("SHA-1 cannot be empty"); //$NON-NLS-1$
		uri.append('/').append(id);
		uri.append('/').append(sha);
		request.setType(Tree.class);
		request.setUri(uri);
		if (recursive)
			request.setParams(Collections.singletonMap("recursive""1")); //$NON-NLS-1$ //$NON-NLS-2$
		return (Tree.get(request).getBody();
	}

Create tree

Parameters:
repository
entries
Returns:
created tree
Throws:
java.io.IOException
			Collection<TreeEntryentriesthrows IOException {
		return createTree(repositoryentriesnull);
	}

Create tree

Parameters:
repository
entries
baseTree
Returns:
created tree
Throws:
java.io.IOException
			Collection<TreeEntryentriesString baseTreethrows IOException {
		final String id = getId(repository);
		uri.append('/').append(id);
		request.setType(Tree.class);
		request.setUri(uri);
		Map<StringObjectparams = new HashMap<StringObject>();
		if (entries != null)
			params.put("tree"entries.toArray()); //$NON-NLS-1$
		if (baseTree != null)
			params.put("base_tree"baseTree); //$NON-NLS-1$
		return .post(uri.toString(), paramsTree.class);
	}

Get reference with given name

Parameters:
repository
name
Returns:
reference
Throws:
java.io.IOException
	public Reference getReference(IRepositoryIdProvider repositoryString name)
			throws IOException {
		final String id = getId(repository);
		if (name == null)
			throw new IllegalArgumentException("Name cannot be null"); //$NON-NLS-1$
		if (name.length() == 0)
			throw new IllegalArgumentException("Name cannot be empty"); //$NON-NLS-1$
		uri.append('/').append(id);
		if (!name.startsWith("refs/")) //$NON-NLS-1$
		uri.append('/').append(name);
		request.setType(Reference.class);
		request.setUri(uri);
		return (Reference.get(request).getBody();
	}

Get references for given repository

Parameters:
repository
Returns:
non-null but possibly empty list of references
Throws:
java.io.IOException
			throws IOException {
		final String id = getId(repository);
		uri.append('/').append(id);
		request.setType(new TypeToken<List<Reference>>() {
		}.getType());
		request.setUri(uri);
		return getAll(request);
	}

Create reference

Parameters:
repository
reference
Returns:
created reference
Throws:
java.io.IOException
			Reference referencethrows IOException {
		final String id = getId(repository);
		if (reference == null)
			throw new IllegalArgumentException("Reference cannot be null"); //$NON-NLS-1$
		TypedResource object = reference.getObject();
		if (object == null)
					"Reference object cannot be null"); //$NON-NLS-1$
		uri.append('/').append(id);
		Map<StringStringparams = new HashMap<StringString>();
		params.put("sha"object.getSha()); //$NON-NLS-1$
		params.put("ref"reference.getRef()); //$NON-NLS-1$
		return .post(uri.toString(), paramsReference.class);
	}

Edit reference

Parameters:
repository
reference
Returns:
updated reference
Throws:
java.io.IOException
			Reference referencethrows IOException {
		return editReference(repositoryreferencefalse);
	}

Edit reference

Parameters:
repository
reference
force
Returns:
updated reference
Throws:
java.io.IOException
			Reference referenceboolean forcethrows IOException {
		final String id = getId(repository);
		if (reference == null)
			throw new IllegalArgumentException("Reference cannot be null"); //$NON-NLS-1$
		TypedResource object = reference.getObject();
		if (object == null)
			throw new IllegalArgumentException("Object cannot be null"); //$NON-NLS-1$
		String ref = reference.getRef();
		if (ref == null)
			throw new IllegalArgumentException("Ref cannot be null"); //$NON-NLS-1$
		if (ref.length() == 0)
			throw new IllegalArgumentException("Ref cannot be empty"); //$NON-NLS-1$
		uri.append('/').append(id);
		if (!ref.startsWith("refs/")) //$NON-NLS-1$
		uri.append('/').append(ref);
		Map<StringObjectparams = new HashMap<StringObject>();
		params.put("sha"object.getSha()); //$NON-NLS-1$
		if (force)
			params.put("force"true); //$NON-NLS-1$
		return .post(uri.toString(), paramsReference.class);
	}

Get commit for given SHA-1

Parameters:
repository
sha
Returns:
commit
Throws:
java.io.IOException
	public Commit getCommit(IRepositoryIdProvider repositoryString sha)
			throws IOException {
		final String id = getId(repository);
		if (sha == null)
			throw new IllegalArgumentException("SHA-1 cannot be null"); //$NON-NLS-1$
		if (sha.length() == 0)
			throw new IllegalArgumentException("SHA-1 cannot be empty"); //$NON-NLS-1$
		uri.append('/').append(id);
		uri.append('/').append(sha);
		request.setType(Commit.class);
		request.setUri(uri);
		return (Commit.get(request).getBody();
	}

Create commit in given repository

Parameters:
repository
commit
Returns:
created commit
Throws:
java.io.IOException
	public Commit createCommit(IRepositoryIdProvider repositoryCommit commit)
			throws IOException {
		final String id = getId(repository);
		if (commit == null)
			throw new IllegalArgumentException("Commit cannot be null"); //$NON-NLS-1$
		uri.append('/').append(id);
		Map<StringObjectparams = new HashMap<StringObject>();
		params.put("author"commit.getAuthor()); //$NON-NLS-1$
		params.put("committer"commit.getCommitter()); //$NON-NLS-1$
		params.put("message"commit.getMessage()); //$NON-NLS-1$
		List<Commitparents = commit.getParents();
		if (parents != null && parents.size() > 0) {
			List<StringparentIds = new ArrayList<String>();
			for (Commit parent : parents)
				parentIds.add(parent.getSha());
			params.put("parents"parentIds); //$NON-NLS-1$
		}
		Tree tree = commit.getTree();
		if (tree != null)
			params.put("tree"tree.getSha()); //$NON-NLS-1$
		return .post(uri.toString(), paramsCommit.class);
	}

Get tag for given SHA-1

Parameters:
repository
sha
Returns:
tag
Throws:
java.io.IOException
	public Tag getTag(IRepositoryIdProvider repositoryString sha)
			throws IOException {
		final String id = getId(repository);
		if (sha == null)
			throw new IllegalArgumentException("SHA-1 cannot be null"); //$NON-NLS-1$
		if (sha.length() == 0)
			throw new IllegalArgumentException("SHA-1 cannot be empty"); //$NON-NLS-1$
		uri.append('/').append(id);
		uri.append('/').append(sha);
		request.setType(Tag.class);
		request.setUri(uri);
		return (Tag.get(request).getBody();
	}

Create tag object in given repository

Parameters:
repository
tag
Returns:
created tag
Throws:
java.io.IOException
	public Tag createTag(IRepositoryIdProvider repositoryTag tag)
			throws IOException {
		final String id = getId(repository);
		if (tag == null)
			throw new IllegalArgumentException("Tag cannot be null"); //$NON-NLS-1$
		uri.append('/').append(id);
		Map<StringObjectparams = new HashMap<StringObject>();
		params.put("tag"tag.getTag()); //$NON-NLS-1$
		params.put("message"tag.getMessage()); //$NON-NLS-1$
		TypedResource object = tag.getObject();
		if (object != null) {
			params.put("object"object.getSha()); //$NON-NLS-1$
			params.put("type"object.getType()); //$NON-NLS-1$
		}
		params.put("tagger"tag.getTagger()); //$NON-NLS-1$
		return .post(uri.toString(), paramsTag.class);
	}
New to GrepCode? Check out our FAQ X