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

[#442] Replace CommandRunner with JGit for GitBranch #1454

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation group: 'org.apache.commons', name: 'commons-csv', version: '1.6'
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.9'
implementation group: 'org.fusesource.jansi', name: 'jansi', version: '1.18'
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.9.0.202009080501-r'

testImplementation group: 'junit', name: 'junit', version: '4.12'
}
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/reposense/git/GitBranch.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
package reposense.git;

import static reposense.system.CommandRunner.runCommand;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Ref;

import reposense.git.exception.GitBranchException;
import reposense.util.StringsUtil;
import reposense.system.LogsManager;

/**
* Contains git branch related functionalities.
* Git branch is responsible for list, create, or delete branches.
*/
public class GitBranch {
private static final Logger logger = LogsManager.getLogger(GitBranch.class);
private static final String MESSAGE_MISSING_REPOSITORY =
"The directory %s does not exist or is not a Git repository.";

/**
* Returns the current working branch of the repository at {@code root}.
*/
public static String getCurrentBranch(String root) throws GitBranchException {
Path rootPath = Paths.get(root);
String gitBranchCommand = "git branch";
String branch;

try {
return StringsUtil.filterText(runCommand(rootPath, gitBranchCommand), "\\* (.*)").split("\\*")[1].trim();
} catch (RuntimeException rte) {
throw new GitBranchException(rte);
Git git = Git.open(new File(root));
List<Ref> gitRefs = git.getRepository().getRefDatabase().getRefs();

if (gitRefs.size() == 0) {
// An empty repository does not have any refs
return null;
}

return git.getRepository().getBranch();
} catch (IOException ioe) {
logger.log(Level.SEVERE, String.format(MESSAGE_MISSING_REPOSITORY, root), ioe);
throw new GitBranchException(ioe);
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/reposense/report/RepoCloner.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ public RepoLocation getClonedRepoLocation() {
String bareRepoPath = FileUtil.getBareRepoPath(configs[currentIndex]).toString();
currentRepoDefaultBranch = GitBranch.getCurrentBranch(bareRepoPath);
} catch (GitBranchException gbe) {
// GitBranch will throw this exception when repository is empty
logger.log(Level.WARNING, String.format(MESSAGE_ERROR_GETTING_BRANCH,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this given you have added code for this already? Or did you forget to remove it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't remove this exception handling as I think that there might be some other errors that may occur. For instance, GitBranchException is thrown because an IOException occurred, which probably happened if the directory given is not a Git repository (which shouldn't happen).

configs[currentIndex].getLocation(), configs[currentIndex].getBranch()), gbe);
return null;
}

if (currentRepoDefaultBranch == null) {
// Current repository is empty
logger.log(Level.WARNING, String.format(MESSAGE_ERROR_GETTING_BRANCH,
configs[currentIndex].getLocation(), configs[currentIndex].getBranch()));
return null;
}

cleanupPrevRepoFolder();

previousIndex = currentIndex;
Expand Down