2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package git4idea.test;
18 import com.intellij.openapi.util.text.StringUtil;
19 import com.intellij.openapi.vfs.VirtualFile;
20 import com.intellij.vcs.log.Hash;
21 import com.intellij.vcs.log.VcsLogObjectsFactory;
22 import com.intellij.vcs.log.VcsRef;
23 import com.intellij.vcs.log.VcsRefType;
24 import com.intellij.vcs.log.impl.HashImpl;
25 import git4idea.branch.GitBranchUtil;
26 import git4idea.log.GitRefManager;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
36 private final VcsLogObjectsFactory myFactory;
38 public RefParser(@NotNull VcsLogObjectsFactory factory) {
42 // e25b7d8f (HEAD, refs/remotes/origin/master, refs/remotes/origin/HEAD, refs/heads/master)
43 public List<VcsRef> parseCommitRefs(@NotNull String input, @NotNull VirtualFile root) {
44 int firstSpaceIndex = input.indexOf(' ');
45 if (firstSpaceIndex < 0) {
46 return Collections.emptyList();
48 String strHash = input.substring(0, firstSpaceIndex);
49 Hash hash = HashImpl.build(strHash);
50 String refPaths = input.substring(firstSpaceIndex + 2, input.length() - 1);
51 String[] longRefPaths = refPaths.split(", ");
52 List<VcsRef> refs = new ArrayList<>();
53 for (String longRefPatch : longRefPaths) {
54 VcsRef ref = createRef(hash, longRefPatch, root);
63 private static String getRefName(@NotNull String longRefPath) {
64 String tagPrefix = "tag: ";
65 longRefPath = StringUtil.trimStart(longRefPath, tagPrefix);
69 // example input: fb29c80 refs/tags/92.29
71 private VcsRef createRef(@NotNull Hash hash, @NotNull String longRefPath, @NotNull VirtualFile root) {
72 String name = getRefName(longRefPath);
73 VcsRefType type = GitRefManager.getRefType(name);
75 return myFactory.createRef(hash, GitBranchUtil.stripRefsPrefix(name), type, root);