Skip to content

Commit

Permalink
git has changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Esser committed Dec 21, 2024
1 parent e14fe50 commit 8edebcf
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.archicontribs.modelrepository.grafico;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -14,7 +16,11 @@ public class GitExecutorTest {

private static final Logger LOGGER = Logger.getLogger(GitExecutorTest.class.getName());

private GitExecutor underTest = new GitExecutor(TestData.GIT_PATH, TestData.GIT_REPO);
private GitExecutor underTest;

public GitExecutorTest() throws GitExecutionException {
underTest = new GitExecutor(TestData.GIT_PATH, TestData.GIT_REPO);
}

@Test
public void canFindGitOnPath() throws IOException, InterruptedException {
Expand Down Expand Up @@ -100,5 +106,17 @@ public void canGitRebaseAbort() throws GitExecutionException, IOException {
GitExecutionResult abortResult = underTest.rebaseAbort();
assertEquals(0, abortResult.exitCode());
}

@Test
public void canDetectUncommittedFile() throws GitExecutionException, IOException {
canGitReset(); // arrange

assertFalse(underTest.hasChanges());

Files.move(TestData.GIT_FILE.toPath(), Paths.get(TestData.GIT_FOLDER.getPath(), TestData.GIT_FILE.getName()),
StandardCopyOption.REPLACE_EXISTING);

assertTrue(underTest.hasChanges());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
<booleanAttribute key="org.eclipse.debug.core.ATTR_FORCE_SYSTEM_CONSOLE_ENCODING" value="false"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/org.archicontribs.modelrepository.tests/src/org/archicontribs/modelrepository/grafico/GitExecutorTest.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit5"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.archicontribs.modelrepository.grafico.GitExecutorTest"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.archicontribs.modelrepository.tests"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea"/>
</launchConfiguration>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected AbstractModelAction(IWorkbenchWindow window) {
public void setRepository(IArchiRepository repository) {
fRepository = repository;
setEnabled(shouldBeEnabled());
setShellRepository(new ShellArchiRepository(repository.getLocalRepositoryFolder()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.archicontribs.modelrepository.grafico.GraficoModelLoader;
import org.archicontribs.modelrepository.grafico.GraficoUtils;
import org.archicontribs.modelrepository.grafico.IRepositoryListener;
import org.archicontribs.modelrepository.grafico.ShellArchiRepository;
import org.archicontribs.modelrepository.merge.MergeConflictHandler;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
Expand Down Expand Up @@ -71,18 +70,12 @@ public RefreshModelAction(IWorkbenchWindow window) {
setText(Messages.RefreshModelAction_0);
setToolTipText(Messages.RefreshModelAction_0);
}

public RefreshModelAction(IWorkbenchWindow window, IArchimateModel model) {
this(window, model, false);
}

public RefreshModelAction(IWorkbenchWindow window, IArchimateModel model, boolean enableShellMode) {
this(window);

if(model != null) {
setRepository(new ArchiRepository(GraficoUtils.getLocalRepositoryFolderForModel(model)));
if (enableShellMode)
setShellRepository(new ShellArchiRepository(GraficoUtils.getLocalRepositoryFolderForModel(model)));
}
}

Expand Down Expand Up @@ -181,7 +174,8 @@ protected int init() throws IOException, GitAPIException {
getRepository().exportModelToGraficoFiles();

// Then offer to Commit
if(getRepository().hasChangesToCommit()) {
if((super.isShellModeAvailable() && super.getShellRepository().hasChanges())
|| getRepository().hasChangesToCommit()) {
if(!offerToCommitChanges()) {
return USER_CANCEL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ private static final String[] from(String command, String... commandArgs) {

public GitExecutor(File localRepoFolder) throws GitExecutionException {
this(new File("git"), localRepoFolder);

version(); // assure git is operational
}
public GitExecutor(File gitPath, File gitRepo) {

public GitExecutor(File gitPath, File gitRepo) throws GitExecutionException {
this.gitPath = gitPath;
this.gitRepo = gitRepo;

version(); // assure git is operational
}

private GitExecutionResult gitExec(File gitPath, File workingDir, String... gitCommands)
Expand All @@ -56,7 +56,7 @@ private GitExecutionResult gitExec(File gitPath, File workingDir, String... gitC
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
// TODO add supplier -> consumer her to also process structured/multi-line
String line = br.readLine();
String line = br.readLine();
return new GitExecutionResult(p.waitFor(), line);
}

Expand Down Expand Up @@ -121,4 +121,17 @@ public GitExecutionResult rebase(String onto) throws GitExecutionException {
public GitExecutionResult rebaseAbort() throws GitExecutionException {
return gitExec("rebase", "--abort");
}

public boolean hasChanges() throws GitExecutionException {
// https://stackoverflow.com/a/3879077
GitExecutionResult refreshIndexResult = gitExec("update-index", "--refresh");
if (refreshIndexResult.exitCode() != 0)
return true;

GitExecutionResult diffIndexResult = gitExec("diff-index", "--quiet", "HEAD");
if (diffIndexResult.exitCode() != 0)
return true;

return false; // otherwise
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@

import org.archicontribs.modelrepository.authentication.UsernamePassword;
import org.archicontribs.modelrepository.grafico.GitExecutor.PullMode;
import org.eclipse.jgit.api.errors.GitAPIException;

public class ShellArchiRepository {

private final GitExecutor executor;

public ShellArchiRepository(File localRepoFolder) {
try {
this.executor = new GitExecutor(localRepoFolder);
this.executor = new GitExecutor(new File("/snap/eclipse-pde/current/usr/bin/git"), localRepoFolder);
} catch (GitExecutionException e) {
throw new RuntimeException(e);
}
}

public GitExecutionResult pullFromRemote(UsernamePassword npw)
throws IOException, GitAPIException {
throws IOException {
try {
GitExecutionResult result = executor.pull(PullMode.REBASE_MERGE);

Expand All @@ -29,6 +28,14 @@ public GitExecutionResult pullFromRemote(UsernamePassword npw)
throw new IOException(e);
}
}

public boolean hasChanges() throws IOException {
try {
return executor.hasChanges();
} catch (GitExecutionException e) {
throw new IOException(e);
}
}



Expand Down

0 comments on commit 8edebcf

Please sign in to comment.