Class: SpinelKit::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/spinel_kit/git.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sha, branch) ⇒ Git

Returns a new instance of Git.



27
28
29
30
# File 'lib/spinel_kit/git.rb', line 27

def initialize(sha, branch)
  @sha    = sha
  @branch = branch
end

Class Method Details

.readObject

Read provenance from ./.git/HEAD. Returns a SpinelKit::Git (sha/branch).



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
# File 'lib/spinel_kit/git.rb', line 42

def self.read
  s = "unknown"
  b = "unknown"
  if File.exist?(".git/HEAD")
    head = File.read(".git/HEAD")
    if head.length > 0 && head[head.length - 1...head.length] == "\n"
      head = head[0...head.length - 1]
    end
    if head.length > 5 && head[0...5] == "ref: "
      ref_rel = head[5...head.length]
      pp = ref_rel.split("/")
      if pp.length >= 3
        b = pp[pp.length - 1]
      end
      ref_path = ".git/" + ref_rel
      if File.exist?(ref_path)
        sha_raw = File.read(ref_path)
        if sha_raw.length >= 40
          s = sha_raw[0...40]
        end
      end
    else
      if head.length >= 40
        s = head[0...40]
        b = "HEAD"
      end
    end
  end
  SpinelKit::Git.new(s, b)
end

Instance Method Details

#branchObject



36
37
38
# File 'lib/spinel_kit/git.rb', line 36

def branch
  @branch
end

#shaObject



32
33
34
# File 'lib/spinel_kit/git.rb', line 32

def sha
  @sha
end