use of org.eclipse.jgit.api.CheckoutCommand in project egit by eclipse.
the class DiscardChangesOperation method discardChanges.
private void discardChanges(Repository repository, Collection<String> paths) throws GitAPIException {
ResourceUtil.saveLocalHistory(repository);
try (Git git = new Git(repository)) {
CheckoutCommand checkoutCommand = git.checkout();
if (revision != null) {
checkoutCommand.setStartPoint(revision);
}
if (stage != null) {
checkoutCommand.setStage(stage.checkoutStage);
}
if (paths.isEmpty() || paths.contains("")) {
// $NON-NLS-1$
checkoutCommand.setAllPaths(true);
} else {
for (String path : paths) {
checkoutCommand.addPath(path);
}
}
checkoutCommand.call();
}
}
use of org.eclipse.jgit.api.CheckoutCommand in project bndtools by bndtools.
the class GitCloneTemplate method generateOutputs.
@Override
public ResourceMap generateOutputs(Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
File workingDir = null;
File gitDir = null;
// Get existing checkout if available
synchronized (this) {
if (checkedOut != null) {
workingDir = checkedOut.getWorkTree();
gitDir = new File(workingDir, ".git");
}
}
if (workingDir == null) {
// Need to do a new checkout
workingDir = Files.createTempDirectory("checkout").toFile();
gitDir = new File(workingDir, ".git");
try {
CloneCommand cloneCmd = Git.cloneRepository().setURI(params.cloneUrl).setDirectory(workingDir).setNoCheckout(true);
cloneCmd.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
Git git = cloneCmd.call();
CheckoutCommand checkout = git.checkout().setCreateBranch(true).setName("_tmp");
if (params.branch == null) {
checkout.setStartPoint(GitCloneTemplateParams.DEFAULT_BRANCH);
} else {
String startPoint = null;
if (params.branch.startsWith(Constants.DEFAULT_REMOTE_NAME + "/")) {
startPoint = params.branch;
} else {
// Check for a matching tag
for (Ref ref : git.tagList().call()) {
if (ref.getName().endsWith("/" + params.branch)) {
startPoint = params.branch;
break;
}
}
if (startPoint == null) {
// Check remote branches
for (Ref ref : git.branchList().setListMode(ListMode.REMOTE).call()) {
if (ref.getName().endsWith("/" + params.branch)) {
startPoint = Constants.DEFAULT_REMOTE_NAME + "/" + params.branch;
break;
}
}
if (startPoint == null) {
if (SHA1_PATTERN.matcher(params.branch).matches()) {
startPoint = params.branch;
if (startPoint == null) {
throw new Exception("Unable to find requested ref \"" + params.branch + "\"");
}
}
}
}
}
checkout.setStartPoint(startPoint);
}
checkout.call();
checkedOut = git.getRepository();
} catch (JGitInternalException e) {
Throwable cause = e.getCause();
if (cause instanceof Exception)
throw (Exception) cause;
throw e;
}
}
final File exclude = gitDir;
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File path) {
return !path.equals(exclude);
}
};
return toResourceMap(workingDir, filter);
}
use of org.eclipse.jgit.api.CheckoutCommand in project bndtools by bndtools.
the class GitUtils method getRepository.
public static synchronized Repository getRepository(File gitRoot, String branch, String gitUrl, String gitPushUrl) throws IOException, ConfigInvalidException, JGitInternalException, GitAPIException {
File dotGit;
if (gitRoot.getName().equals(Constants.DOT_GIT)) {
dotGit = gitRoot;
} else {
dotGit = new File(gitRoot, Constants.DOT_GIT);
}
Repository repository = localRepos.get(dotGit);
if (repository != null && dotGit.exists()) {
return repository;
}
if (!dotGit.exists()) {
Git.cloneRepository().setDirectory(gitRoot).setCloneAllBranches(true).setURI(gitUrl).call();
FileBasedConfig config = new FileBasedConfig(new File(dotGit, "config"), FS.DETECTED);
config.load();
if (gitPushUrl != null) {
config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "pushurl", gitPushUrl);
}
config.save();
}
FileRepositoryBuilder builder = new FileRepositoryBuilder();
repository = builder.setGitDir(dotGit).readEnvironment().findGitDir().build();
localRepos.put(dotGit, repository);
try {
repository.incrementOpen();
Git git = Git.wrap(repository);
// Check branch
boolean pull = true;
String currentBranch = repository.getBranch();
if (branch != null && !branch.equals(currentBranch)) {
CheckoutCommand checkout = git.checkout();
if (!branchExists(git, branch)) {
checkout.setCreateBranch(true);
pull = false;
}
checkout.setName(branch);
checkout.call();
}
if (pull) {
git.pull().call();
} else {
git.fetch().call();
}
} catch (Exception e) {
if (!(e.getCause() instanceof TransportException)) {
throw new RuntimeException(e);
}
} finally {
if (repository != null) {
repository.close();
}
}
return repository;
}
use of org.eclipse.jgit.api.CheckoutCommand in project blueocean-plugin by jenkinsci.
the class GitUtils method merge.
public static void merge(final Repository repo, final String localRef, final String remoteRef) {
try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
CheckoutCommand checkoutCommand = git.checkout();
checkoutCommand.setCreateBranch(false);
checkoutCommand.setName(localRef);
checkoutCommand.call();
Ref mergeBranchRef = repo.exactRef(remoteRef);
MergeResult merge = git.merge().include(mergeBranchRef).setFastForward(MergeCommand.FastForwardMode.FF_ONLY).call();
if (merge.getConflicts() != null) {
throw new RuntimeException("Merge has conflicts");
}
} catch (Exception e) {
throw new ServiceException.UnexpectedErrorException("Unable to merge: " + remoteRef + " to: " + localRef, e);
}
}
Aggregations