Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AF-1636 - Imported projects have wrong config on Git repository config #532

Merged
merged 1 commit into from Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,13 +17,11 @@
package org.uberfire.java.nio.fs.jgit.util.commands;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Optional;

import org.eclipse.jgit.internal.ketch.KetchLeaderCache;
import org.eclipse.jgit.internal.storage.file.WindowCache;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.storage.file.WindowCacheConfig;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
Expand All @@ -40,6 +38,7 @@

public class Clone {

public static final String REFS_MIRRORED = "+refs/heads/*:refs/remotes/origin/*";
private final File repoDir;
private final String origin;
private final CredentialsProvider credentialsProvider;
Expand Down Expand Up @@ -82,7 +81,7 @@ public Optional<Git> execute() {

final Collection<RefSpec> refSpecList;
if (isMirror) {
refSpecList = singletonList(new RefSpec("+refs/*:refs/*"));
refSpecList = singletonList(new RefSpec(REFS_MIRRORED));
} else {
refSpecList = emptyList();
}
Expand All @@ -92,19 +91,6 @@ public Optional<Git> execute() {
remote,
refSpecList);

if (isMirror) {
final StoredConfig config = git.getRepository().getConfig();
config.setBoolean("remote",
"origin",
"mirror",
true);
try {
config.save();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

git.syncRemote(remote);

if (git.isKetchEnabled()) {
Expand Down
Expand Up @@ -19,14 +19,20 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.hooks.PostCommitHook;
import org.eclipse.jgit.hooks.PreCommitHook;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.junit.Test;
import org.uberfire.java.nio.fs.jgit.util.Git;
Expand Down Expand Up @@ -103,22 +109,22 @@ public void cloneShouldOnlyWorksWithEmptyRepos() throws IOException {
null).execute().get())
.isInstanceOf(Clone.CloneException.class);
}

@Test
public void testCloneWithHookDir() throws IOException, GitAPIException {
final File hooksDir = createTempDirectory();

writeMockHook(hooksDir, PostCommitHook.NAME);
writeMockHook(hooksDir, PreCommitHook.NAME);

final File parentFolder = createTempDirectory();

final File gitSource = new File(parentFolder,
SOURCE_GIT + ".git");
SOURCE_GIT + ".git");

final File gitTarget = new File(parentFolder,
TARGET_GIT + ".git");


final Git origin = setupGitRepo(gitSource, hooksDir);

Expand All @@ -137,7 +143,7 @@ public void testCloneWithHookDir() throws IOException, GitAPIException {

assertThat(new ListRefs(cloned.getRepository()).execute().get(0).getName()).isEqualTo("refs/heads/master");
assertThat(new ListRefs(cloned.getRepository()).execute().get(1).getName()).isEqualTo("refs/heads/user_branch");

boolean foundPreCommitHook = false;
boolean foundPostCommitHook = false;
File[] hooks = new File(cloned.getRepository().getDirectory(), "hooks").listFiles();
Expand Down Expand Up @@ -183,4 +189,63 @@ private Git setupGitRepo(File gitSource, File hooksDir) throws IOException {
}}).execute();
return origin;
}

@Test
public void cloneNotMirrorRepoConfigTest() throws IOException {
final File parentFolder = createTempDirectory();

final File gitSource = new File(parentFolder,
SOURCE_GIT + ".git");

final File gitTarget = new File(parentFolder,
TARGET_GIT + ".git");

final Git origin = setupGitRepo(gitSource, null);

boolean isMirror = false;
final Git clonedNotMirror = new Clone(gitTarget,
gitSource.getAbsolutePath(),
isMirror,
CredentialsProvider.getDefault(),
null,
null).execute().get();

assertThat(clonedNotMirror).isNotNull();

StoredConfig config = clonedNotMirror.getRepository().getConfig();

assertNotEquals(Clone.REFS_MIRRORED, config.getString("remote", "origin", "fetch"));
assertNull(config.getString("remote", "origin", "mirror"));
assertEquals(gitSource.getAbsolutePath(), config.getString("remote", "origin", "url"));
}

@Test
public void cloneMirrorRepoConfigTest() throws IOException {
final File parentFolder = createTempDirectory();

final File gitSource = new File(parentFolder,
SOURCE_GIT + ".git");

final File gitTarget = new File(parentFolder,
TARGET_GIT + ".git");

final Git origin = setupGitRepo(gitSource, null);

boolean isMirror = true;
final Git clonedMirror = new Clone(gitTarget,
gitSource.getAbsolutePath(),
isMirror,
CredentialsProvider.getDefault(),
null,
null).execute().get();

assertThat(clonedMirror).isNotNull();

StoredConfig config = clonedMirror.getRepository().getConfig();

assertEquals(Clone.REFS_MIRRORED, config.getString("remote", "origin", "fetch"));
assertNull(config.getString("remote", "origin", "mirror"));
assertEquals(gitSource.getAbsolutePath(), config.getString("remote", "origin", "url"));
}

}