summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/repo/GitRepositoryFiles.java
blob: cb0bd9d3dd7134112e0cf02e55229547be7103cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.repo;

import com.intellij.openapi.vfs.VirtualFile;
import git4idea.util.GitFileUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collection;

import static git4idea.GitUtil.DOT_GIT;

/**
 * Stores paths to Git service files (from .git/ directory) that are used by IDEA, and provides test-methods to check if a file
 * matches once of them.
 *
 * @author Kirill Likhodedov
 */
public class GitRepositoryFiles {

  public static final String COMMIT_EDITMSG = "COMMIT_EDITMSG";
  public static final String CONFIG = "config";
  public static final String HEAD = "HEAD";
  public static final String INDEX = "index";
  public static final String INFO = "info";
  public static final String INFO_EXCLUDE = INFO + "/exclude";
  public static final String MERGE_HEAD = "MERGE_HEAD";
  public static final String MERGE_MSG = "MERGE_MSG";
  public static final String REBASE_APPLY = "rebase-apply";
  public static final String REBASE_MERGE = "rebase-merge";
  public static final String PACKED_REFS = "packed-refs";
  public static final String REFS_HEADS = "refs/heads";
  public static final String REFS_REMOTES = "refs/remotes";
  public static final String REFS_TAGS = "refs/tags";
  public static final String SQUASH_MSG = "SQUASH_MSG";

  public static final String GIT_HEAD  = DOT_GIT + slash(HEAD);
  public static final String GIT_REFS_REMOTES = DOT_GIT + slash(REFS_REMOTES);
  public static final String GIT_PACKED_REFS = DOT_GIT + slash(PACKED_REFS);
  public static final String GIT_MERGE_HEAD = DOT_GIT + slash(MERGE_HEAD);
  public static final String GIT_MERGE_MSG = DOT_GIT + slash(MERGE_MSG);
  public static final String GIT_SQUASH_MSG = DOT_GIT + slash(SQUASH_MSG);
  public static final String GIT_COMMIT_EDITMSG = DOT_GIT + slash(COMMIT_EDITMSG);

  private final String myConfigFilePath;
  private final String myHeadFilePath;
  private final String myIndexFilePath;
  private final String myMergeHeadPath;
  private final String myRebaseApplyPath;
  private final String myRebaseMergePath;
  private final String myPackedRefsPath;
  private final String myRefsHeadsDirPath;
  private final String myRefsRemotesDirPath;
  private final String myRefsTagsPath;
  private final String myCommitMessagePath;
  private final String myExcludePath;

  public static GitRepositoryFiles getInstance(@NotNull VirtualFile gitDir) {
    // maybe will be cached later to store a single GitRepositoryFiles for a root. 
    return new GitRepositoryFiles(gitDir);
  }

  private GitRepositoryFiles(@NotNull VirtualFile gitDir) {
    // add .git/ and .git/refs/heads to the VFS
    // save paths of the files, that we will watch
    String gitDirPath = GitFileUtils.stripFileProtocolPrefix(gitDir.getPath());
    myConfigFilePath = gitDirPath + slash(CONFIG);
    myHeadFilePath = gitDirPath + slash(HEAD);
    myIndexFilePath = gitDirPath + slash(INDEX);
    myMergeHeadPath = gitDirPath + slash(MERGE_HEAD);
    myCommitMessagePath = gitDirPath + slash(COMMIT_EDITMSG);
    myRebaseApplyPath = gitDirPath + slash(REBASE_APPLY);
    myRebaseMergePath = gitDirPath + slash(REBASE_MERGE);
    myPackedRefsPath = gitDirPath + slash(PACKED_REFS);
    myRefsHeadsDirPath = gitDirPath + slash(REFS_HEADS);
    myRefsTagsPath = gitDirPath + slash(REFS_TAGS);
    myRefsRemotesDirPath = gitDirPath + slash(REFS_REMOTES);
    myExcludePath = gitDirPath + slash(INFO_EXCLUDE);
  }

  @NotNull
  private static String slash(@NotNull String s) {
    return "/" + s;
  }

  /**
   * Returns subdirectories of .git which we are interested in - they should be watched by VFS.
   */
  @NotNull
  static Collection<String> getSubDirRelativePaths() {
    return Arrays.asList(slash(REFS_HEADS), slash(REFS_REMOTES), slash(REFS_TAGS), slash(INFO));
  }
  
  @NotNull
  String getRefsHeadsPath() {
    return myRefsHeadsDirPath;
  }

  @NotNull
  String getRefsRemotesPath() {
    return myRefsRemotesDirPath;
  }

  @NotNull
  String getRefsTagsPath() {
    return myRefsTagsPath;
  }

  /**
   * {@code .git/config}
   */
  public boolean isConfigFile(String filePath) {
    return filePath.equals(myConfigFilePath);
  }

  /**
   * .git/index
   */
  public boolean isIndexFile(String filePath) {
    return filePath.equals(myIndexFilePath);
  }

  /**
   * .git/HEAD
   */
  public boolean isHeadFile(String file) {
    return file.equals(myHeadFilePath);
  }

  /**
   * Any file in .git/refs/heads, i.e. a branch reference file.
   */
  public boolean isBranchFile(String filePath) {
    return filePath.startsWith(myRefsHeadsDirPath);
  }

  /**
   * Any file in .git/refs/remotes, i.e. a remote branch reference file.
   */
  public boolean isRemoteBranchFile(String filePath) {
    return filePath.startsWith(myRefsRemotesDirPath);
  }

  /**
   * .git/refs/tags/*
   */
  public boolean isTagFile(@NotNull String path) {
    return path.startsWith(myRefsTagsPath);
  }

  /**
   * .git/rebase-merge or .git/rebase-apply
   */
  public boolean isRebaseFile(String path) {
    return path.equals(myRebaseApplyPath) || path.equals(myRebaseMergePath);
  }

  /**
   * .git/MERGE_HEAD
   */
  public boolean isMergeFile(String file) {
    return file.equals(myMergeHeadPath);
  }

  /**
   * .git/packed-refs
   */
  public boolean isPackedRefs(String file) {
    return file.equals(myPackedRefsPath);
  }

  public boolean isCommitMessageFile(@NotNull String file) {
    return file.equals(myCommitMessagePath);
  }

  /**
   * {@code $GIT_DIR/info/exclude}
   */
  public boolean isExclude(@NotNull String path) {
    return path.equals(myExcludePath);
  }

}