List of usage examples for org.eclipse.jgit.api Status isClean
public boolean isClean()
From source file:ch.sourcepond.maven.release.scm.git.GitRepository.java
License:Apache License
@Override public void errorIfNotClean() throws SCMException { final Status status = currentStatus(); final boolean isClean = status.isClean(); if (!isClean) { final SCMException exception = new SCMException( "Cannot release with uncommitted changes. Please check the following files:"); final Set<String> uncommittedChanges = status.getUncommittedChanges(); if (uncommittedChanges.size() > 0) { exception.add("Uncommitted:"); for (final String path : uncommittedChanges) { exception.add(" * %s", path); }/* ww w .ja v a 2 s.c o m*/ } final Set<String> untracked = status.getUntracked(); if (untracked.size() > 0) { exception.add("Untracked:"); for (final String path : untracked) { exception.add(" * %s", path); } } throw exception.add("Please commit or revert these changes before releasing."); } }
From source file:com.barchart.jenkins.cascade.PluginScm.java
License:BSD License
/** * Copy from remote into local./*from w w w. j av a2 s. c o m*/ */ public static void scmCheckout(final BuildContext<CascadeBuild> context, final MavenModuleSet project) throws IOException, InterruptedException { final String message = checkScm(project); if (message != null) { throw new IllegalStateException(message); } final GitSCM gitScm = (GitSCM) project.getScm(); final FilePath workspace = workspace(context, project); /** Remote objects. */ final BuildLogger logger = context.logger(); final String localBranch = localBranchName(gitScm); final String remoteURI = remoteURI(gitScm); final String remoteName = remoteName(gitScm); final String remoteBranch = remoteBranchName(gitScm); /** Remote operation. */ final FileCallable<String> callable = new FileCallable<String>() { private static final long serialVersionUID = 1L; public String invoke(final File basedir, final VirtualChannel channel) throws IOException, InterruptedException { final boolean hasRepo = PluginScmGit.doRepoTest(basedir); if (hasRepo) { logger.logTab("repository present"); final Status status = PluginScmGit.doStatus(basedir); if (!status.isClean()) { logger.logTab("repository needs cleanup"); PluginScmGit.doReset(basedir); } /** Spec for the fetch mapping. */ final RefSpec fetchSpec = PluginScmGit.refFetch(remoteBranch, remoteName, remoteBranch); final FetchResult fetchResult = PluginScmGit.doFetch(basedir, remoteName, fetchSpec); logger.logTab("fetch result: " + fetchResult.getTrackingRefUpdates().size()); /** Spec of the head of the remote branch. */ final String refHead = PluginScmGit.refHeads(remoteBranch); /** Reference to head of the remote branch. */ final Ref remoteHead = fetchResult.getAdvertisedRef(refHead); if (remoteHead == null) { logger.logErr("remote branch not found: " + refHead); throw new IllegalStateException("Unexpected"); } final ObjectId commit = remoteHead.getObjectId(); final MergeResult mergeResult = PluginScmGit.doMerge(basedir, commit); final MergeStatus mergeStatus = mergeResult.getMergeStatus(); logger.logTab("merge result: " + mergeStatus); if (!mergeStatus.isSuccessful()) { logger.logTab("repository needs clone"); PluginScmGit.doClone(basedir, remoteURI, remoteName); } } else { logger.logTab("repository needs clone"); PluginScmGit.doClone(basedir, remoteURI, remoteName); } final CheckoutResult checkoutResult = PluginScmGit.doCheckout(basedir, localBranch, remoteName, remoteBranch); final CheckoutResult.Status checkoutStatus = checkoutResult.getStatus(); logger.logTab("checkout status: " + checkoutStatus); if (!PluginScmGit.isSuccess(checkoutStatus)) { throw new IllegalStateException("Unexpected"); } /** FIXME checkout does not work */ PluginScmGit.doReset(basedir); final Ref ref = PluginScmGit.findRef(basedir, localBranch); logger.logTab(localBranch + ": " + ref.getObjectId().name()); /** TODO delete local tags */ return null; } }; workspace.act(callable); }
From source file:com.bb.extensions.plugin.unittests.internal.git.GitWrapper.java
License:Open Source License
/** * @return true if the git repo is clean according to "git status". *//*w ww . j a v a 2 s.c o m*/ public boolean isRepoClean() { try { org.eclipse.jgit.api.Status status = git.status().call(); return status.isClean(); } catch (NoWorkTreeException e) { e.printStackTrace(); } catch (GitAPIException e) { e.printStackTrace(); } return false; }
From source file:com.microsoft.gittf.core.util.RepositoryUtil.java
License:Open Source License
/** * Determines if there are uncommitted changes in the working directory or * not.// www . j a v a 2 s.c om * * @param repository * @return * @throws GitAPIException * @throws NoWorkTreeException */ public static boolean hasUncommittedChanges(Repository repository) throws NoWorkTreeException, GitAPIException { Status currentStatus = new Git(repository).status().call(); if (!currentStatus.isClean()) { return true; } return false; }
From source file:com.mortardata.project.TestEmbeddedMortarProject.java
License:Apache License
protected void assertCleanTree() throws GitAPIException { Status status = this.git.status().call(); Assert.assertTrue("Expected no files in the status; got: " + status.toString(), status.isClean()); }
From source file:com.mycila.maven.plugin.license.git.GitLookup.java
License:Apache License
/** * Returns the year of the last change of the given {@code file} based on the history of the present git branch. The * year is taken either from the committer date or from the author identity depending on how {@link #dateSource} was * initialized./*from ww w . j ava2 s . c om*/ * <p> * See also the note on time zones in {@link #GitLookup(File, DateSource, TimeZone, int)}. * * @param file * @return * @throws NoHeadException * @throws GitAPIException * @throws IOException */ public int getYearOfLastChange(File file) throws NoHeadException, GitAPIException, IOException { String repoRelativePath = pathResolver.relativize(file); Status status = new Git(repository).status().addPath(repoRelativePath).call(); if (!status.isClean()) { /* Return the current year for modified and unstaged files */ return toYear(System.currentTimeMillis(), timeZone != null ? timeZone : DEFAULT_ZONE); } RevWalk walk = new RevWalk(repository); walk.markStart(walk.parseCommit(repository.resolve(Constants.HEAD))); walk.setTreeFilter(AndTreeFilter.create(PathFilter.create(repoRelativePath), TreeFilter.ANY_DIFF)); walk.setRevFilter(MaxCountRevFilter.create(checkCommitsCount)); walk.setRetainBody(false); int commitYear = 0; for (RevCommit commit : walk) { int y; switch (dateSource) { case COMMITER: int epochSeconds = commit.getCommitTime(); y = toYear(epochSeconds * 1000L, timeZone); break; case AUTHOR: PersonIdent id = commit.getAuthorIdent(); Date date = id.getWhen(); y = toYear(date.getTime(), id.getTimeZone()); break; default: throw new IllegalStateException("Unexpected " + DateSource.class.getName() + " " + dateSource); } if (y > commitYear) { commitYear = y; } } walk.dispose(); return commitYear; }
From source file:com.peergreen.configuration.git.GitWrite.java
License:Apache License
protected void commit(String filePattern, String message) throws RepositoryException { // check if there are changes ? Status status = null; try {/*w w w . j a va 2s .c om*/ status = getGitManager().git().status().call(); } catch (GitAPIException e) { throw new RepositoryException( "Cannot commit the file pattern '" + filePattern + "' with message '" + message + ".", e); } // no changes, so do not commit if (status.isClean()) { return; } // Commit it RevCommit revCommit = null; try { revCommit = getGitManager().git().commit() .setCommitter("Peergreen Config Repository", "configrepository@peergreen.com") .setMessage(message).setOnly(filePattern).call(); } catch (GitAPIException e) { throw new RepositoryException( "Cannot commit the file pattern '" + filePattern + "' with message '" + message + ".", e); } // Update the writeId update(revCommit.getId()); }
From source file:com.rimerosolutions.ant.git.tasks.UpToDateTask.java
License:Apache License
@Override protected void doExecute() throws BuildException { Repository repo = git.getRepository(); FileTreeIterator workingTreeIterator = new FileTreeIterator(repo); try {/* w ww .j a v a2s . c o m*/ IndexDiff diff = new IndexDiff(repo, Constants.HEAD, workingTreeIterator); diff.diff(); Status status = new Status(diff); if (!status.isClean()) { if (modificationExistProperty != null) { getProject().setProperty(modificationExistProperty, "true"); } if (isFailOnError()) { StringBuilder msg = new StringBuilder(); msg.append("The Git tree was modified."); msg.append("\n").append("Changed:").append(status.getChanged()); msg.append("\n").append("Added:").append(status.getAdded()); msg.append("\n").append("Modified:").append(status.getModified()); msg.append("\n").append("Missing:").append(status.getMissing()); msg.append("\n").append("Removed:").append(status.getRemoved()); msg.append("\n").append("Untracked:").append(status.getUntracked()); throw new GitBuildException(String.format(STATUS_NOT_CLEAN_TEMPLATE, msg.toString())); } } else { log(MESSAGE_UPTODATE_SUCCESS); } } catch (IOException ioe) { throw new GitBuildException(MESSAGE_UPTODATE_FAILED, ioe); } }
From source file:com.tenxdev.ovcs.command.AbstractSyncCommand.java
License:Open Source License
/** * Commit all changes and push to remote repository * * @throws OvcsException//from w ww.j a v a2 s . co m * if changes could not be committed or pushed */ protected void commitAndPush() throws OvcsException { try { final FileRepository fileRepository = getRepoForCurrentDir(); final Git git = new Git(fileRepository); final Status status = git.status().setProgressMonitor(new TextProgressMonitor()).call(); if (!status.isClean()) { git.add().addFilepattern(".").call(); git.commit().setMessage("initial synchronization").setAll(true).call(); doPush(git); } } catch (final GitAPIException e) { throw new OvcsException("Unable to commit to git repo: " + e.getMessage(), e); } }
From source file:com.tenxdev.ovcs.command.StatusCommand.java
License:Open Source License
private void displayChanges(final FileRepository repository) throws OvcsException { try {/*from ww w.j a va2 s . c o m*/ final Status status = new Git(repository).status().call(); if (status.isClean()) { System.out.println("No changes."); } else { displayChanges("Added", status.getUntracked()); displayChanges("Modified", status.getModified()); displayChanges("Removed", status.getMissing()); } } catch (NoWorkTreeException | GitAPIException e) { throw new OvcsException("Unable to query git status: " + e.getMessage(), e); } }