Skip to content

Commit

Permalink
[JBIDE-17879] removed dupl code to check project changes, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adietish committed Jul 15, 2014
1 parent e778836 commit 87310fb
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 173 deletions.
Expand Up @@ -21,7 +21,9 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -1031,22 +1033,22 @@ private static IStatus createStatus(Exception e, String message,
}


public static boolean isDirty(IProject project, boolean includeUntracked)
public static boolean isDirty(IProject project, boolean includeUntracked, IProgressMonitor monitor)
throws NoWorkTreeException, IOException, GitAPIException {
return isDirty(getRepository(project), includeUntracked);
return isDirty(getRepository(project), includeUntracked, monitor);
}

public static boolean isDirty(IProject project)
public static boolean isDirty(IProject project, IProgressMonitor monitor)
throws NoWorkTreeException, IOException, GitAPIException {
return isDirty(getRepository(project));
return isDirty(getRepository(project), monitor);
}

/**
* Returns <code>true</code> if the given repository has uncommitted and
* non-tracked resources.
*/
public static boolean isDirty(Repository repository) throws NoWorkTreeException, IOException, GitAPIException {
return isDirty(repository, true);
public static boolean isDirty(Repository repository, IProgressMonitor monitor) throws IOException {
return isDirty(repository, true, monitor);
}

/**
Expand All @@ -1070,47 +1072,39 @@ public static boolean isDirty(Repository repository) throws NoWorkTreeException,
* they wont.
* @return
* @throws IOException
* @throws NoWorkTreeException
* @throws GitAPIException
*/
public static boolean isDirty(Repository repository, boolean includeUntracked)
throws NoWorkTreeException, IOException, GitAPIException {
Assert.isLegal(repository != null);

org.eclipse.jgit.api.Status repoStatus =
new Git(repository).status().call();
boolean isDirty = false;
isDirty |= !repoStatus.getUncommittedChanges().isEmpty();
if (includeUntracked) {
isDirty |= !repoStatus.getUntracked().isEmpty();
isDirty |= !repoStatus.getUntrackedFolders().isEmpty();
}
return isDirty;
public static boolean isDirty(Repository repository, boolean includeUntracked, IProgressMonitor monitor) throws IOException {
return countChanges(repository, includeUntracked, monitor) > 0;
}

/**
* Returns the changes in the index of the HEAD branch in the given
* repository. Returns the index diff if there are changes,
* <code>null</code> otherwise.
* Returns the number of changes for a given repository. If
* <strong>includeUntracked</strong> is set to <code>true</code> then
* non-tracked changes are taken into account.
*
* @param repo
* the repository to get index changes for
* @param repository
* the repository to check changes for
* @param includeUntracked
* whether to take untracked changes into account
* @param monitor
* the monitor to report progress to
* @return the changes in the index or null;
* @return the number of changes
* @throws IOException
*/
public static IndexDiff getIndexChanges(Repository repo, IProgressMonitor monitor) throws IOException {
EclipseGitProgressTransformer jgitMonitor = new EclipseGitProgressTransformer(monitor);

IndexDiff indexDiff = new IndexDiff(repo, Constants.HEAD, IteratorService.createInitialIterator(repo));
if (!indexDiff.diff(
jgitMonitor, 0, 0, NLS.bind("Repository: {0}", repo.getDirectory().getPath()))) {
return null;
}
return indexDiff;
public static int countChanges(Repository repository, boolean includeUntracked, IProgressMonitor monitor)
throws IOException {
Assert.isLegal(repository != null);

Collection<String> uncommittedChanges = new GitIndexDiffBuilder(repository)
.changed(true)
.removed(true)
.missing(true)
.conflicting(true)
.untracked(includeUntracked)
.build(monitor);
return uncommittedChanges.size();
}

/**
* Returns <code>true</code> if the given branch in the given repository has
* local commits that were not pushed to its remote yet.
Expand Down Expand Up @@ -1317,4 +1311,95 @@ private static final int getEgitTimeout() {
return Platform.getPreferencesService().
getInt(EGIT_UI_PLUGIN_ID, REMOTE_CONNECTION_TIMEOUT, DEFAULT_TIMEOUT, null);
}

public static class GitIndexDiffBuilder {

private Repository repository;

private boolean added = true;
private boolean changed= true;
private boolean conflicting = true;
private boolean missing = true;
private boolean modified = true;
private boolean removed = true;
private boolean untracked = true;

GitIndexDiffBuilder(Repository repository) {
this.repository = repository;
}

public GitIndexDiffBuilder added(boolean include) {
this.added = include;
return this;
}

public GitIndexDiffBuilder changed(boolean include) {
this.changed = include;
return this;
}

public GitIndexDiffBuilder conflicting(boolean include) {
this.conflicting = include;
return this;
}

public GitIndexDiffBuilder missing(boolean include) {
this.missing = include;
return this;
}

public GitIndexDiffBuilder modified(boolean include) {
this.modified = include;
return this;
}

public GitIndexDiffBuilder removed(boolean include) {
this.removed = include;
return this;
}

public GitIndexDiffBuilder untracked(boolean include) {
this.untracked = include;
return this;
}

public Collection<String> build(IProgressMonitor monitor) throws IOException {
Set<String> resources = new HashSet<String>();
IndexDiff diff = getIndexChanges(repository, monitor);
if (diff != null) {
if (added) {
resources.addAll(diff.getAdded());
}
if (changed) {
resources.addAll(diff.getChanged());
}
if (conflicting) {
resources.addAll(diff.getConflicting());
}
if (missing) {
resources.addAll(diff.getMissing());
}
if (modified) {
resources.addAll(diff.getModified());
}
if (removed) {
resources.addAll(diff.getRemoved());
}
if (untracked) {
resources.addAll(diff.getUntracked());
}
}
return resources;
}

private static IndexDiff getIndexChanges(Repository repo, IProgressMonitor monitor) throws IOException {
IndexDiff indexDiff = new IndexDiff(repo, Constants.HEAD, IteratorService.createInitialIterator(repo));
if (!indexDiff.diff(
new EclipseGitProgressTransformer(monitor), 0, 0,
NLS.bind("Repository: {0}", repo.getDirectory().getPath()))) {
return null;
}
return indexDiff;
}
}
}
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.egit.core.op.AddToIndexOperation;
import org.eclipse.egit.core.op.PushOperationResult;
Expand Down Expand Up @@ -213,8 +212,8 @@ protected boolean isInDestProjectTree(String magicProject, IModule[] module) {

protected PushOperationResult publish(final IProject project, final IServer server, final IProgressMonitor monitor)
throws CoreException {
int uncommittedChanges = OpenShiftServerUtils.countCommitableChanges(project, server, monitor);
try {
int uncommittedChanges = EGitUtils.countChanges(EGitUtils.getRepository(project), true, new NullProgressMonitor());
if (uncommittedChanges > 0) {
OpenshiftCoreUIIntegration.openCommitDialog(project,new JobChangeAdapter() {
@Override
Expand Down
Expand Up @@ -10,12 +10,9 @@
*******************************************************************************/
package org.jboss.tools.openshift.express.internal.core.server;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
Expand All @@ -26,14 +23,10 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.jgit.lib.IndexDiff;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.osgi.util.NLS;
Expand All @@ -49,7 +42,6 @@
import org.jboss.ide.eclipse.as.core.util.RegExUtils;
import org.jboss.ide.eclipse.as.core.util.ServerUtil;
import org.jboss.tools.openshift.egit.core.EGitUtils;
import org.jboss.tools.openshift.egit.core.internal.EGitCoreActivator;
import org.jboss.tools.openshift.express.internal.core.OpenShiftCoreActivator;
import org.jboss.tools.openshift.express.internal.core.connection.Connection;
import org.jboss.tools.openshift.express.internal.core.connection.ConnectionURL;
Expand Down Expand Up @@ -639,102 +631,6 @@ public static IServer setExpressUsername(IServer server, String val) throws Core
wc.setAttribute(ATTRIBUTE_USERNAME, val);
return wc.save(false, new NullProgressMonitor());
}

public static int countCommitableChanges(IProject project, IServer server, IProgressMonitor monitor)
throws CoreException {
Collection<String> commitableChanges = new GitIndexDiffBuilder(EGitUtils.checkedGetRepository(project))
.conflicting(false)
.missing(false)
// .untracked(false)
.build(monitor);
return commitableChanges.size();
}

public static class GitIndexDiffBuilder {

private Repository repository;

private boolean added = true;
private boolean changed= true;
private boolean conflicting = true;
private boolean missing = true;
private boolean modified = true;
private boolean removed = true;
private boolean untracked = true;

GitIndexDiffBuilder(Repository repository) {
this.repository = repository;
}

public GitIndexDiffBuilder added(boolean filter) {
this.added = filter;
return this;
}

public GitIndexDiffBuilder changed(boolean filter) {
this.changed = filter;
return this;
}

public GitIndexDiffBuilder conflicting(boolean filter) {
this.conflicting = filter;
return this;
}

public GitIndexDiffBuilder missing(boolean filter) {
this.missing = filter;
return this;
}

public GitIndexDiffBuilder modified(boolean filter) {
this.modified = filter;
return this;
}

public GitIndexDiffBuilder removed(boolean filter) {
this.removed = filter;
return this;
}

public GitIndexDiffBuilder untracked(boolean filter) {
this.untracked = filter;
return this;
}

public Collection<String> build(IProgressMonitor monitor) throws CoreException {
try {
HashSet<String> resources = new HashSet<String>();
IndexDiff diff = EGitUtils.getIndexChanges(repository, monitor);
if (diff != null) {
if (added) {
resources.addAll(diff.getAdded());
}
if (changed) {
resources.addAll(diff.getChanged());
}
if (conflicting) {
resources.addAll(diff.getConflicting());
}
if (missing) {
resources.addAll(diff.getMissing());
}
if (modified) {
resources.addAll(diff.getModified());
}
if (removed) {
resources.addAll(diff.getRemoved());
}
if (untracked) {
resources.addAll(diff.getUntracked());
}
}
return resources;
} catch (IOException ioe) {
throw new CoreException(new Status(IStatus.ERROR, EGitCoreActivator.PLUGIN_ID,
"Unable to count commitable resources", ioe));
}
}
}

public static String[] toNames(final IProject[] projects) {
if (projects == null) {
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
Expand Down Expand Up @@ -353,7 +354,7 @@ private IStatus getGitDirtyStatus(IProject project) {
"The git repository for project {0} looks corrupt. Please fix it before using it.",
project.getName()));
try {
if (EGitUtils.isDirty(project, false)) {
if (EGitUtils.isDirty(project, false, new NullProgressMonitor())) {
return ValidationStatus.error(NLS.bind(
"The project {0} has uncommitted changes. Please commit those changes first.",
project.getName()));
Expand Down
Expand Up @@ -197,7 +197,7 @@ protected void addAndCommitModifiedResource(IProject project, IProgressMonitor m
OpenShiftException, NoWorkTreeException, IOException, GitAPIException {
EGitUtils.checkedGetRepository(project);
new AddToIndexOperation(modifiedResources).execute(monitor);
if (EGitUtils.isDirty(project)) {
if (EGitUtils.isDirty(project, monitor)) {
EGitUtils.commit(project, monitor);
}
}
Expand Down
Expand Up @@ -93,7 +93,7 @@ public IProject execute(IProgressMonitor monitor)
IProject project = getProject();
Assert.isTrue(EGitUtils.isSharedWithGit(project));

if (EGitUtils.isDirty(project, false)) {
if (EGitUtils.isDirty(project, false, monitor)) {
throw new UnCommittedChangesException(
"The project {0} has uncommitted changes. Please commit those changes first.", project.getName());
}
Expand Down

0 comments on commit 87310fb

Please sign in to comment.