diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-27 11:59:44 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-27 11:59:44 +0100 |
commit | 8456422732182d9d3f231b71a573013dda4dcf73 (patch) | |
tree | fbc9ac7b49644e70d44fc3e719c643d0bfec50ef /plugins | |
parent | 737e18758cc001a5a98159c3781c6bbbcb19f447 (diff) | |
download | sonarqube-8456422732182d9d3f231b71a573013dda4dcf73.tar.gz sonarqube-8456422732182d9d3f231b71a573013dda4dcf73.zip |
Fix some quality flaws
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java index 4e7f15e3e07..6bf04da30ec 100644 --- a/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java +++ b/plugins/sonar-git-plugin/src/main/java/org/sonar/plugins/scm/git/JGitBlameCommand.java @@ -53,36 +53,50 @@ public class JGitBlameCommand extends BlameCommand { @Override public void blame(BlameInput input, BlameOutput output) { - Git git = null; File basedir = input.fileSystem().baseDir(); + Repository repo = buildRepository(basedir); try { - Repository repo = new RepositoryBuilder() - .findGitDir(basedir) - .setMustExist(true) - .build(); - git = Git.wrap(repo); + Git git = Git.wrap(repo); File gitBaseDir = repo.getWorkTree(); ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1); - List<Future<Void>> tasks = new ArrayList<Future<Void>>(); - for (InputFile inputFile : input.filesToBlame()) { - tasks.add(submitTask(output, git, gitBaseDir, inputFile, executorService)); + List<Future<Void>> tasks = submitTasks(input, output, git, gitBaseDir, executorService); + waitForTaskToComplete(tasks); + } finally { + if (repo != null) { + repo.close(); } - for (Future<Void> task : tasks) { - try { - task.get(); - } catch (ExecutionException e) { - // Unwrap ExecutionException - throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause()); - } catch (InterruptedException e) { - throw new IllegalStateException(e); - } + } + } + + private void waitForTaskToComplete(List<Future<Void>> tasks) { + for (Future<Void> task : tasks) { + try { + task.get(); + } catch (ExecutionException e) { + // Unwrap ExecutionException + throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause()); + } catch (InterruptedException e) { + throw new IllegalStateException(e); } + } + } + + private List<Future<Void>> submitTasks(BlameInput input, BlameOutput output, Git git, File gitBaseDir, ExecutorService executorService) { + List<Future<Void>> tasks = new ArrayList<Future<Void>>(); + for (InputFile inputFile : input.filesToBlame()) { + tasks.add(submitTask(output, git, gitBaseDir, inputFile, executorService)); + } + return tasks; + } + + private Repository buildRepository(File basedir) { + try { + return new RepositoryBuilder() + .findGitDir(basedir) + .setMustExist(true) + .build(); } catch (IOException e) { throw new IllegalStateException("Unable to open Git repository", e); - } finally { - if (git != null && git.getRepository() != null) { - git.getRepository().close(); - } } } |