summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/log/RefParser.java
blob: 619cfcd5d4dad75b044ab30f33a07aaabf4a54c9 (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
package git4idea.log;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcs.log.Hash;
import com.intellij.vcs.log.VcsLogObjectsFactory;
import com.intellij.vcs.log.VcsRef;
import com.intellij.vcs.log.impl.HashImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * TODO: remove when tags are supported by the {@link git4idea.repo.GitRepositoryReader}.
 *
 * @author erokhins
 */
class RefParser {

  private final VcsLogObjectsFactory myFactory;

  public RefParser(VcsLogObjectsFactory factory) {
    myFactory = factory;
  }

  // e25b7d8f (HEAD, refs/remotes/origin/master, refs/remotes/origin/HEAD, refs/heads/master)
  public List<VcsRef> parseCommitRefs(@NotNull String input, @NotNull VirtualFile root) {
    int firstSpaceIndex = input.indexOf(' ');
    if (firstSpaceIndex < 0) {
      return Collections.emptyList();
    }
    String strHash = input.substring(0, firstSpaceIndex);
    Hash hash = HashImpl.build(strHash);
    String refPaths = input.substring(firstSpaceIndex + 2, input.length() - 1);
    String[] longRefPaths = refPaths.split(", ");
    List<VcsRef> refs = new ArrayList<VcsRef>();
    for (String longRefPatch : longRefPaths) {
      VcsRef ref = createRef(hash, longRefPatch, root);
      if (ref != null) {
        refs.add(ref);
      }
    }
    return refs;
  }

  @Nullable
  private static String getRefName(@NotNull String longRefPath, @NotNull String startPatch) {
    String tagPrefix = "tag: ";
    if (longRefPath.startsWith(tagPrefix)) {
      longRefPath = longRefPath.substring(tagPrefix.length());
    }
    if (longRefPath.startsWith(startPatch)) {
      return longRefPath.substring(startPatch.length());
    }
    else {
      return null;
    }
  }

  // example input: fb29c80 refs/tags/92.29
  @Nullable
  private VcsRef createRef(@NotNull Hash hash, @NotNull String longRefPath, @NotNull VirtualFile root) {
    String name = getRefName(longRefPath, "refs/tags/");
    if (name != null) {
      return myFactory.createRef(hash, name, GitRefManager.TAG, root);
    }

    return null;
  }
}