private void logStatus(final Git git) throws GitAPIException { final Status status = git.status().call(); if (!status.getAdded().isEmpty()) { LOG.info("Added files {}", status.getAdded()); } if (!status.getChanged().isEmpty()) { LOG.info("Changed files {}", status.getChanged()); } if (!status.getRemoved().isEmpty()) { LOG.info("Removed files {}", status.getRemoved()); } if (!status.getModified().isEmpty()) { LOG.info("Modified files {}", status.getModified()); } if (!status.getMissing().isEmpty()) { LOG.info("Missing files {}", status.getMissing()); } }
/** * Returns <code>true</code> if the given repository has uncommitted changes. Uncommitted changes * taken into account are * * <ul> * <li>freshly added (but uncommitted resources) * <li>changed (but uncommitted) * <li>modified (but uncommitted resources) * <li>removed (but uncommitted resources) * </ul> * * @param repository the repository to check for uncommitted changes * @return * @throws IOException * @throws NoWorkTreeException * @throws GitAPIException */ public static boolean isDirty(Repository repository) throws NoWorkTreeException, IOException, GitAPIException { boolean hasChanges = false; org.eclipse.jgit.api.Status repoStatus = new Git(repository).status().call(); hasChanges |= !repoStatus.getAdded().isEmpty(); hasChanges |= !repoStatus.getChanged().isEmpty(); hasChanges |= !repoStatus.getModified().isEmpty(); hasChanges |= !repoStatus.getRemoved().isEmpty(); hasChanges |= !repoStatus.getConflicting().isEmpty(); hasChanges |= !repoStatus.getMissing().isEmpty(); return hasChanges; }
/** * @param gitConfigFolder e.g. /your/project/root/.git * @return Returns true if 'git status' has modified files inside the 'Changes to be committed' * section */ public static boolean isCommitNecessary(String gitConfigFolder) throws MojoExecutionException { try { Repository repo = new FileRepository(gitConfigFolder); Git git = new Git(repo); Status status = git.status().call(); Set<String> modified = status.getModified(); return (modified.size() != 0); } catch (Exception e) { throw new MojoExecutionException("Error trying to find out if git commit is needed", e); } }
@Test public void markAsMerged() throws Exception { GitSynchronizeData gsd = new GitSynchronizeData(repo, HEAD, HEAD, false); GitSynchronizeDataSet gsds = new GitSynchronizeDataSet(gsd); GitResourceVariantTreeSubscriber subscriber = new GitResourceVariantTreeSubscriber(gsds); String fileName = "src/Main.java"; File file = testRepo.createFile(iProject, fileName); testRepo.appendContentAndCommit(iProject, file, "class Main {}", "some file"); testRepo.addToIndex(iProject.getFile(".classpath")); testRepo.addToIndex(iProject.getFile(".project")); testRepo.commit("project files"); IFile workspaceFile = testRepo.getIFile(iProject, file); ResourceMapping mapping = AdapterUtils.adapt(workspaceFile, ResourceMapping.class); ResourceMapping[] inputMappings = new ResourceMapping[] {mapping}; SubscriberScopeManager manager = new SubscriberScopeManager("Scope", inputMappings, subscriber, true); testRepo.appendFileContent(file, "some changes"); Status status = new Git(repo).status().call(); assertEquals(0, status.getAdded().size()); assertEquals(1, status.getModified().size()); String repoRelativePath = testRepo.getRepoRelativePath(workspaceFile.getLocation().toPortableString()); assertTrue(status.getModified().contains(repoRelativePath)); GitSubscriberMergeContext mergeContext = new GitSubscriberMergeContext(subscriber, manager, gsds); IDiff node = new ResourceDiff(iProject.getFolder("src"), IDiff.CHANGE); mergeContext.markAsMerged(node, true, null); status = new Git(repo).status().call(); assertEquals(1, status.getChanged().size()); assertEquals(0, status.getModified().size()); assertTrue(status.getChanged().contains(repoRelativePath)); }
@Override protected void executeCustom(Git git) throws Exception { StatusCommand sc = git.status(); Status status = sc.call(); if (isclean != null && status.isClean()) { PropertyHelper.getPropertyHelper(getProject()).setNewProperty(isclean, "true"); } if (isdirty != null && !status.isClean()) { PropertyHelper.getPropertyHelper(getProject()).setNewProperty(isdirty, "true"); } if (untracked != null) { ResourceCollection resources = buildResourcesCollection(status.getUntracked()); getProject().addReference(untracked, resources); } if (added != null) { ResourceCollection resources = buildResourcesCollection(status.getAdded()); getProject().addReference(added, resources); } if (modified != null) { ResourceCollection resources = buildResourcesCollection(status.getModified()); getProject().addReference(modified, resources); } if (changed != null) { ResourceCollection resources = buildResourcesCollection(status.getChanged()); getProject().addReference(changed, resources); } if (missing != null) { ResourceCollection resources = buildResourcesCollection(status.getMissing()); getProject().addReference(missing, resources); } if (removed != null) { ResourceCollection resources = buildResourcesCollection(status.getRemoved()); getProject().addReference(removed, resources); } }
public void commitAllChanges(final String message) { final Repository repository = getRepository(); try { final Git git = new Git(repository); git.add().addFilepattern(".").call(); final Status status = git.status().call(); if (status.getChanged().size() > 0 || status.getAdded().size() > 0 || status.getModified().size() > 0 || status.getRemoved().size() > 0) { final RevCommit rev = git.commit() .setAll(true) .setCommitter(person) .setAuthor(person) .setMessage(message) .call(); LOGGER.info("Git commit " + rev.getName() + " [" + message + "]"); } } catch (final Exception e) { throw new IllegalStateException("Could not commit changes to local Git repository", e); } }
/** * Gets the status of this file. * * @return status of this file. */ public TextArea getStatus() { TextArea area = null; try { Path repoPath = this.getHost().repository; Git git = Git.open(repoPath.toFile()); // // I would like to use absolutePath, but that seems to barf when // we try to relativize this if a full path is not given. // Path relativePath = repoPath.relativize(Paths.get(this.file.getPath())); Status status = git.status().addPath(relativePath.toString()).call(); if (logger.isDebugEnabled()) { logger.debug(this.file.getAbsolutePath()); logger.debug("Added: " + status.getAdded()); logger.debug("Changed: " + status.getChanged()); logger.debug("Conflicting: " + status.getConflicting()); logger.debug("Missing: " + status.getMissing()); logger.debug("Modified: " + status.getModified()); logger.debug("Removed: " + status.getRemoved()); logger.debug("Uncommitted: " + status.getUncommittedChanges()); logger.debug("Untracked: " + status.getUntracked()); logger.debug("Untracked folders; " + status.getUntrackedFolders()); } // // Are we a file or directory? // StringBuffer buffer = new StringBuffer(); int length = 0; if (this.file.isFile()) { if (status.getAdded().contains(relativePath.toString())) { buffer.append("Added" + "\n"); length++; } if (status.getChanged().contains(relativePath.toString())) { buffer.append("Changed" + "\n"); length++; } if (status.getConflicting().contains(relativePath.toString())) { buffer.append("Conflicting" + "\n"); length++; } if (status.getMissing().contains(relativePath.toString())) { buffer.append("Missing" + "\n"); length++; } if (status.getModified().contains(relativePath.toString())) { buffer.append("Modified" + "\n"); length++; } if (status.getRemoved().contains(relativePath.toString())) { buffer.append("Removed" + "\n"); length++; } if (status.getUncommittedChanges().contains(relativePath.toString())) { buffer.append("Uncommitted" + "\n"); length++; } if (status.getUntracked().contains(relativePath.toString())) { buffer.append("Untracked (New)" + "\n"); length++; } if (status.getUntrackedFolders().contains(relativePath.toString())) { buffer.append("Untracked Folders (New)" + "\n"); length++; } } else if (this.file.isDirectory()) { if (status.getUntracked().size() > 0) { buffer.append("Untracked (New)" + "\n"); length++; } if (status.getUntrackedFolders().size() > 0) { buffer.append("Untracked Folders (New)" + "\n"); length++; } } if (length > 0) { area = new TextArea(); area.setValue(buffer.toString().trim()); area.setWidth("100.0%"); area.setRows(length); area.setReadOnly(true); } } catch (IOException | NoWorkTreeException | GitAPIException e) { logger.error(e); } return area; }