Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2014-2014 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl2.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.kuali.common.core.scm.git;
 
 import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.ImmutableList.copyOf;
 import static com.google.common.collect.Lists.newArrayList;
 import static java.lang.Long.parseLong;
 import static java.lang.String.format;
 import static java.util.Arrays.asList;
 import static org.apache.commons.lang3.StringUtils.isBlank;
 import static org.apache.commons.lang3.StringUtils.remove;
 import static org.apache.commons.lang3.StringUtils.trim;
 import static org.apache.commons.lang3.StringUtils.trimToNull;
 import static org.kuali.common.util.Encodings.encodeUTF8;
 import static org.kuali.common.util.base.Exceptions.illegalState;
 import static org.kuali.common.util.base.Precondition.checkNotBlank;
 import static org.kuali.common.util.base.Precondition.checkNotNull;
 import static org.kuali.common.util.file.Files.currentWorkingDirectory;
 import static org.kuali.common.util.file.Files.getCanonicalFile;
 
 import java.io.File;
 import java.util.List;
 import java.util.Set;
 
 
 
 public final class GitNativeClient extends AbstractNativeClient {
 
 	private final GitNativeContext context;
 
 	private final File workingDirectory;
 
 	public File getTopLevelDirectory() {
 		checkState(dir.isDirectory(), "%s is not a directory"dir);
 		return dir;
 	}
 
 	public boolean isDetached() {
 		return checkNotBlankTrimmedOutput(revParse("--symbolic-full-name""--abbrev-ref".getHead())).equals(.getHead());
 	}
 
 	public ExecRequest.Builder builder(List<Stringargs) {
 		ExecRequest.Builder builder = ExecRequest.builder(.getExecutable());
 		builder.withArgs(args);
 		return builder;
 	}
 
 	protected String getEncoding() {
 		return .getEncoding();
 	}
 
 	public Optional<StringgetTagName() {
 		try {
 			return Optional.of(checkNotBlankTrimmedOutput("describe""--tags""--exact-match"));
 		} catch (IllegalStateException e) {
 			return absent();
 		}
 	}
 
 	public String getVersion() {
 		return trim(remove(checkNotBlankTrimmedOutput("--version"), "git version"));
 	}
 
 	public String getUrl() {
		return getOriginUrl();
	}
		String originUrl = getOriginUrl();
		checkState(originUrl.indexOf('?') == -1, "[%s] cannot contain the question mark character"originUrl);
		if (tag.isPresent()) {
			return buildUrl(originUrlnew Param(.getTag(), tag.get()));
		}
		GitCommit commit = lastCommit();
		if (isDetached()) {
			return buildUrl(originUrlnew Param(.getRevision(), commit.getRevision()));
else {
			Param branch = new Param(.getBranch(), getCurrentBranch());
			Param revision = new Param(.getRevision(), commit.getRevision());
			return buildUrl(originUrlbranchrevision);
		}
	}
	private String buildUrl(String urlParam... params) {
		return (params == null) ? checkNotBlank(url"url") : buildUrl(urlcopyOf(params));
	}
	private String buildUrl(String urlList<Paramparams) {
		checkNotBlank(url"url");
		if (checkNotNull(params"params").isEmpty()) {
			return url;
		}
		sb.append(url);
		sb.append("?");
		for (int i = 0; i < params.size(); i++) {
			if (i != 0) {
				sb.append("&");
			}
			Param param = params.get(i);
			sb.append(encodeUTF8(param.getName()) + "=" + encodeUTF8(param.getValue()));
		}
		return sb.toString();
	}
	private static class Param {
		public Param(String nameString value) {
			this. = checkNotBlank(name"name");
			this. = checkNotBlank(value"value");
		}
		private final String name;
		private final String value;
		public String getName() {
			return ;
		}
		public String getValue() {
			return ;
		}
	}
	public String getRevision() {
	}
	public String getOriginUrl() {
	}
	public String getRemoteUrl(String remote) {
		String name = format("remote.%s.url"validateRemote(remote));
		return checkNotBlankTrimmedOutput("config""--get"name);
	}
	public void init() {
		execute("init");
	}
	public void init(File file) {
		execute("init"getCanonicalFile(file).getPath());
	}
	public Set<Stringremotes() {
		Optional<Stringoutput = getStringOutput("remote");
		String trimmed = trimToNull(output.orNull());
		if (isBlank(trimmed)) {
			return ImmutableSet.of();
else {
			return ImmutableSet.copyOf(Splitter.on('\n').trimResults().splitToList(trimmed));
		}
	}
	public void gc() {
		execute("gc""--quiet");
	}

Return an estimate of the total repository size in bytes. If gc is true, you'll get a better estimate but it takes longer. (few seconds for small repo's, few minutes for large repo's) first.
	public long estimateRepositorySize(boolean gc) {
		if (gc) {
			gc();
		}
		List<Stringlines = getRequiredLines(asList("count-objects""-v"), 1);
		String startsWithToken = "size-pack:";
		for (String line : lines) {
			if (line.startsWith(startsWithToken)) {
				String kiloBytes = trim(remove(linestartsWithToken));
				return parseLong(kiloBytes) * 1024L;
			}
		}
		throw illegalState("unable to estimate size. no output line started with %s"startsWithToken);
	}

Return an estimate of the total repository size in bytes
	public long estimateRepositorySize() {
		return estimateRepositorySize(false);
	}
	public void remoteUpdate(String remote) {
		execute("remote""update"validateRemote(remote));
	}
	public GitCommit lastOriginCommit(String branch) {
		return lastCommit(Optional.of(.getOrigin()), Optional.of(branch));
	}
	public GitCommit lastCommitRemote(String remote) {
		return lastCommit(Optional.of(remote), Optional.<Stringabsent());
	}
	public GitCommit lastCommit(String remoteString branch) {
		return lastCommit(Optional.of(remote), Optional.of(branch));
	}
	public boolean inSyncWithOrigin() {
	}
	public boolean inSyncWithOrigin(String branch) {
		return inSyncWithRemote(.getOrigin(), branch);
	}
	public boolean inSyncWithRemote(String remote) {
	}
	public boolean inSyncWithRemote(String remoteString branch) {
		GitCommit remoteCommit = lastCommit(remotebranch);
		GitCommit localCommit = lastCommit(branch);
		return remoteCommit.getRevision().equals(localCommit.getRevision());
	}
	}
	public GitCommit lastCommit(String branch) {
		return lastCommit(Optional.<Stringabsent(), Optional.of(branch));
	}
	public GitCommit lastCommit() {
		return lastCommit(Optional.<Stringabsent(), Optional.<Stringabsent());
	}

Clone the url into the current working directory
	public void clone(String url) {
		clone(ImmutableList.<Stringof(), url, Optional.<Fileabsent());
	}

Clone the url into the indicated directory
	public void clone(String urlFile directory) {
		clone(ImmutableList.<Stringof(), url, Optional.of(directory));
	}
	public void clone(List<StringoptionsString url) {
		clone(optionsurl, Optional.<Fileabsent());
	}
	public void clone(List<StringoptionsString urlFile directory) {
		clone(optionsurl, Optional.of(directory));
	}

Returns true if the current working directory is the .git directory, or any sub-directory underneath
	public boolean isInsideGitDir() {
		return isTrue(revParse("--is-inside-git-dir"));
	}

Returns true if the current working directory is inside the work tree of the repository, false otherwise
	public boolean isInsideWorkTree() {
		return isTrue(revParse("--is-inside-work-tree"));
	}

Returns the name of the branch checked out in the current working directory
		checkState(!isDetached(), "local checkout at [%s] is detached");
		return checkNotBlankTrimmedOutput(revParse("--abbrev-ref""HEAD"));
	}
		if (remote.isPresent()) {
		}
		return remote;
	}
	private String validateRemote(String remote) {
		checkNotBlank(remote"remote");
		checkState(remotes().contains(remote), "%s is not a valid remote"remote);
		return remote;
	}
	private Optional<StringrevParse(String... args) {
		return revParse(ImmutableList.copyOf(args));
	}
	private Optional<StringrevParse(List<Stringargs) {
		return getStringOutput(combinedList("rev-parse"args));
	}
	private GitCommit lastCommit(Optional<StringremoteOptional<Stringbranch) {
		checkNotBlank(checkNotNull(branch"branch"), "branch");
		if (remote.isPresent()) {
			remoteUpdate(remote.get());
		}
		List<Stringargs = getLastCommitArgs(remotebranch);
		List<Stringlines = getRequiredLines(args, 3);
		String revision = trim(remove(lines.get(0), "commit"));
		String author = trim(remove(lines.get(1), "Author:"));
		String date = trim(remove(lines.get(2), "Date:"));
		long timestamp = getTime(date.getCommitDateFormat());
		return GitCommit.builder().withRevision(revision).withAuthor(author).withTimestamp(timestamp).build();
	}
	private List<StringgetLastCommitArgs(Optional<StringremoteOptional<Stringbranch) {
		List<Stringargs = newArrayList(asList("--no-pager""log""--max-count=1"));
		Optional<StringbranchReference = getBranchReference(remotebranch);
		if (branchReference.isPresent()) {
			args.add(branchReference.get());
		}
		return ImmutableList.copyOf(args);
	}
	private Optional<StringgetBranchReference(Optional<StringremoteOptional<Stringbranch) {
		List<Stringlines = newArrayList();
		if (remote.isPresent()) {
			lines.add(remote.get());
		}
		if (branch.isPresent()) {
			lines.add(branch.get());
		}
		if (lines.isEmpty()) {
			return absent();
else {
			return Optional.of(Joiner.on('/').join(lines));
		}
	}
	private void clone(List<StringoptionsString urlOptional<Filedirectory) {
		checkNotNull(options"options");
		checkNotBlank(url"url");
		Optional<Filecanonical = checkNotExistingFile(directory);
		List<Stringargs = newArrayList();
		args.add("clone");
		args.addAll(options);
		args.add(url);
		if (canonical.isPresent()) {
			args.add(canonical.get().getPath());
		}
		execute(args);
	}
	public ExecResult execute(ExecRequest request) {
		return .getCli().execute(request);
	}
	private GitNativeClient(Builder builder) {
		this. = builder.context;
		this. = builder.workingDirectory;
	}
	public static GitNativeClient build() {
		return builder().build();
	}
	public static Builder builder() {
		return new Builder();
	}
	public GitNativeClient cd(File directory) {
		return changeDirectory(directory);
	}
	public GitNativeClient changeDirectory(File directory) {
		return GitNativeClient.builder().withContext().withWorkingDirectory(directory).build();
	}
	public static class Builder extends ValidatingBuilder<GitNativeClient> {
		private GitNativeContext context = GitNativeContext.build();
		public Builder withWorkingDirectory(File workingDirectory) {
			this. = workingDirectory;
			return this;
		}
		public Builder withContext(GitNativeContext context) {
			this. = context;
			return this;
		}
		public GitNativeClient build() {
			return validate(new GitNativeClient(this));
		}
	}
		return ;
	}
	}
New to GrepCode? Check out our FAQ X