From a9669da41be5c5faf207fc6af46e8490897a58f2 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Tue, 24 Apr 2012 12:25:14 +0400 Subject: [PATCH] GitFileUtils#add(VirtualFile...) and (FilePath...): DRY * Move updating the UntrackedFilesHolder to a separate method. * Safe (with nullity check) convert from FilePath to VirtualFile when needed. * @Nullable, @NotNull * Update javadocs. --- .../src/git4idea/util/GitFileUtils.java | 77 ++++++++----------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/plugins/git4idea/src/git4idea/util/GitFileUtils.java b/plugins/git4idea/src/git4idea/util/GitFileUtils.java index e4a8e615aa45..2686efff4d31 100644 --- a/plugins/git4idea/src/git4idea/util/GitFileUtils.java +++ b/plugins/git4idea/src/git4idea/util/GitFileUtils.java @@ -27,9 +27,9 @@ import git4idea.commands.GitCommand; import git4idea.commands.GitSimpleHandler; import git4idea.repo.GitRepository; import git4idea.repo.GitRepositoryManager; -import git4idea.repo.GitUntrackedFilesHolder; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -101,77 +101,62 @@ public class GitFileUtils { } /** - * Add/index files - * - * @param project the project - * @param root a vcs root - * @param files files to add - * @return a result of operation - * @throws VcsException in case of git problem + * Add files to the Git index. */ - public static void addFiles(Project project, VirtualFile root, Collection files) throws VcsException { - for (List paths : VcsFileUtil.chunkFiles(root, files)) { - GitSimpleHandler handler = new GitSimpleHandler(project, root, GitCommand.ADD); - handler.endOptions(); - handler.addParameters(paths); - handler.setNoSSH(true); - handler.run(); - } + public static void addFiles(@NotNull Project project, @NotNull VirtualFile root, + @NotNull Collection files) throws VcsException { + addPaths(project, root, VcsFileUtil.chunkFiles(root, files)); + updateUntrackedFilesHolderOnFileAdd(project, root, files); + } + + private static void updateUntrackedFilesHolderOnFileAdd(@NotNull Project project, @NotNull VirtualFile root, + @NotNull Collection addedFiles) { GitRepositoryManager manager = GitUtil.getRepositoryManager(project); if (manager == null) { return; } final GitRepository repository = manager.getRepositoryForRoot(root); if (repository != null) { - repository.getUntrackedFilesHolder().remove(files); + repository.getUntrackedFilesHolder().remove(addedFiles); } } /** - * Add/index files - * - * @param project the project - * @param root a vcs root - * @param files files to add - * @return a result of operation - * @throws VcsException in case of git problem + * Add files to the Git index. */ public static void addFiles(Project project, VirtualFile root, VirtualFile... files) throws VcsException { addFiles(project, root, Arrays.asList(files)); } /** - * Add/index files - * - * @param project the project - * @param root a vcs root - * @param files files to add - * @return a result of operation - * @throws VcsException in case of git problem + * Add files to the Git index. */ - public static void addPaths(Project project, VirtualFile root, Collection files) throws VcsException { - GitRepositoryManager manager = GitUtil.getRepositoryManager(project); - if (manager == null) { - return; + public static void addPaths(@NotNull Project project, @NotNull VirtualFile root, + @NotNull Collection files) throws VcsException { + addPaths(project, root, VcsFileUtil.chunkPaths(root, files)); + updateUntrackedFilesHolderOnFileAdd(project, root, getVirtualFilesFromFilePaths(files)); + } + + @NotNull + private static Collection getVirtualFilesFromFilePaths(@NotNull Collection paths) { + Collection files = new ArrayList(paths.size()); + for (FilePath path : paths) { + VirtualFile file = path.getVirtualFile(); + if (file != null) { + files.add(file); + } } + return files; + } - final GitRepository repository = manager.getRepositoryForRoot(root); - final GitUntrackedFilesHolder untrackedFilesHolder = (repository == null ? null : repository.getUntrackedFilesHolder()); - - for (List paths : VcsFileUtil.chunkPaths(root, files)) { + private static void addPaths(Project project, VirtualFile root, List> chunkedPaths) throws VcsException { + for (List paths : chunkedPaths) { GitSimpleHandler handler = new GitSimpleHandler(project, root, GitCommand.ADD); handler.endOptions(); handler.addParameters(paths); handler.setNoSSH(true); handler.run(); } - - for (FilePath path : files) { - VirtualFile vf = path.getVirtualFile(); - if (untrackedFilesHolder != null && vf != null) { - untrackedFilesHolder.remove(vf); - } - } } /**