-
Notifications
You must be signed in to change notification settings - Fork 157
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
dcshzj
wants to merge
1
commit into
reposense:master
Choose a base branch
from
dcshzj:442-deprecate-commandrunner
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).