From e497b16a704bc429f2fde0466a06772c6d2826c9 Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Thu, 24 Nov 2016 04:44:49 +0300 Subject: [PATCH] [git] check if repository is a shallow clone --- plugins/git4idea/src/git4idea/repo/GitRepoInfo.java | 12 ++++++++++-- .../src/git4idea/repo/GitRepositoryFiles.java | 8 ++++++++ .../src/git4idea/repo/GitRepositoryImpl.java | 3 ++- .../src/git4idea/repo/GitRepositoryReader.java | 9 +++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/plugins/git4idea/src/git4idea/repo/GitRepoInfo.java b/plugins/git4idea/src/git4idea/repo/GitRepoInfo.java index a8a62cf452b5..445a78560dab 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepoInfo.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepoInfo.java @@ -38,6 +38,7 @@ public class GitRepoInfo { @NotNull private final Set myBranchTrackInfos; @NotNull private final Collection mySubmodules; @NotNull private final GitHooksInfo myHooksInfo; + private final boolean myIsShallow; public GitRepoInfo(@Nullable GitLocalBranch currentBranch, @Nullable String currentRevision, @@ -47,7 +48,8 @@ public class GitRepoInfo { @NotNull Map remoteBranches, @NotNull Collection branchTrackInfos, @NotNull Collection submodules, - @NotNull GitHooksInfo hooksInfo) { + @NotNull GitHooksInfo hooksInfo, + boolean isShallow) { myCurrentBranch = currentBranch; myCurrentRevision = currentRevision; myState = state; @@ -57,6 +59,7 @@ public class GitRepoInfo { myBranchTrackInfos = new LinkedHashSet<>(branchTrackInfos); mySubmodules = submodules; myHooksInfo = hooksInfo; + myIsShallow = isShallow; } @Nullable @@ -110,6 +113,10 @@ public class GitRepoInfo { return myHooksInfo; } + public boolean isShallow() { + return myIsShallow; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -126,6 +133,7 @@ public class GitRepoInfo { if (!areEqual(myRemoteBranches, info.myRemoteBranches)) return false; if (!mySubmodules.equals(info.mySubmodules)) return false; if (!myHooksInfo.equals(info.myHooksInfo)) return false; + if (myIsShallow != info.myIsShallow) return false; return true; } @@ -141,6 +149,7 @@ public class GitRepoInfo { result = 31 * result + myBranchTrackInfos.hashCode(); result = 31 * result + mySubmodules.hashCode(); result = 31 * result + myHooksInfo.hashCode(); + result = 31 * result + (myIsShallow ? 1 : 0); return result; } @@ -178,5 +187,4 @@ public class GitRepoInfo { return b1.getKey().getName().equals(b2.getKey().getName()) && b1.getValue().equals(b2.getValue()); } } - } diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryFiles.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryFiles.java index 92b2d3deb7c2..ad6075dd4440 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryFiles.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryFiles.java @@ -59,6 +59,7 @@ public class GitRepositoryFiles { private static final String HOOKS = "hooks"; private static final String PRE_COMMIT_HOOK = "pre-commit"; private static final String PRE_PUSH_HOOK = "pre-push"; + private static final String SHALLOW = "shallow"; private final VirtualFile myMainDir; private final VirtualFile myWorktreeDir; @@ -81,6 +82,7 @@ public class GitRepositoryFiles { private final String myInfoDirPath; private final String myExcludePath; private final String myHooksDirPath; + private final String myShallow; private GitRepositoryFiles(@NotNull VirtualFile mainDir, @NotNull VirtualFile worktreeDir) { myMainDir = mainDir; @@ -96,6 +98,7 @@ public class GitRepositoryFiles { myInfoDirPath = mainPath + slash(INFO); myExcludePath = mainPath + slash(INFO_EXCLUDE); myHooksDirPath = mainPath + slash(HOOKS); + myShallow = mainPath + slash(SHALLOW); String worktreePath = myWorktreeDir.getPath(); myHeadFilePath = worktreePath + slash(HEAD); @@ -226,6 +229,11 @@ public class GitRepositoryFiles { return file(myHooksDirPath + slash(PRE_PUSH_HOOK)); } + @NotNull + public File getShallowFile() { + return file(myShallow); + } + @NotNull private static File file(@NotNull String filePath) { return new File(FileUtil.toSystemDependentName(filePath)); diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryImpl.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryImpl.java index 05f580664411..62a78566aa51 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryImpl.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryImpl.java @@ -210,12 +210,13 @@ public class GitRepositoryImpl extends RepositoryImpl implements GitRepository { GitConfig config = GitConfig.read(configFile); Collection remotes = config.parseRemotes(); GitBranchState state = myReader.readState(remotes); + boolean isShallow = myReader.hasShallowCommits(); Collection trackInfos = config.parseTrackInfos(state.getLocalBranches().keySet(), state.getRemoteBranches().keySet()); GitHooksInfo hooksInfo = myReader.readHooksInfo(); Collection submodules = new GitModulesFileReader().read(getSubmoduleFile()); sw.report(); return new GitRepoInfo(state.getCurrentBranch(), state.getCurrentRevision(), state.getState(), remotes, - state.getLocalBranches(), state.getRemoteBranches(), trackInfos, submodules, hooksInfo); + state.getLocalBranches(), state.getRemoteBranches(), trackInfos, submodules, hooksInfo, isShallow); } @NotNull diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java index 161f08a1c123..7e309c6f4d78 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java @@ -103,6 +103,15 @@ class GitRepositoryReader { return file.exists() && file.canExecute(); } + boolean hasShallowCommits() { + File shallowFile = myGitFiles.getShallowFile(); + if (!shallowFile.exists()) { + return false; + } + + return shallowFile.length() > 0; + } + @Nullable private static String getCurrentRevision(@NotNull HeadInfo headInfo, @Nullable Hash currentBranchHash) { String currentRevision;