Skip to content

Search parent directories for .git dir #128

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/pl/project13/maven/git/GitDirLocator.java
Original file line number Diff line number Diff line change
@@ -77,6 +77,25 @@ public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) {
*/
@Nullable
private File findProjectGitDirectory() {

try {
// First, search up the file system hierarchy, then the project hierarchy...
File baseDir = this.mavenProject.getBasedir().getCanonicalFile();

while(baseDir != null) {
File gitDir = new File(baseDir, Constants.DOT_GIT);

if (isExistingDirectory(gitDir)) {
return gitDir;
}

baseDir = baseDir.getParentFile();
}
} catch(IOException e) {
e.printStackTrace();
// pass
}

MavenProject currentProject = this.mavenProject;

while (currentProject != null) {
11 changes: 10 additions & 1 deletion src/test/java/pl/project13/maven/git/GitIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -18,9 +18,13 @@
package pl.project13.maven.git;

import com.google.common.base.Optional;
import com.google.common.io.Files;

import org.apache.commons.io.FileUtils;
import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.api.Git;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;

import java.io.File;
@@ -32,7 +36,7 @@

public abstract class GitIntegrationTest {

final String SANDBOX_DIR = "target/sandbox";
final String SANDBOX_DIR = Files.createTempDir().getAbsolutePath();

protected GitCommitIdMojo mojo;
protected FileSystemMavenSandbox mavenSandbox;
@@ -44,6 +48,11 @@ public void setUp() throws Exception {
initializeMojoWithDefaults(mojo);
}

@After
public void tearDown() throws Exception {
FileUtils.deleteDirectory(new File(SANDBOX_DIR));
}

protected Git git() throws IOException, InterruptedException {
return Git.open(dotGitDir(projectDir()));
}