Class: Envirobly::Git::Commit

Inherits:
Envirobly::Git show all
Defined in:
lib/envirobly/git/commit.rb

Direct Known Subclasses

Unstaged

Constant Summary collapse

EXECUTABLE_FILE_MODE =
"100755"
"120000"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Envirobly::Git

#current_branch, #git

Constructor Details

#initialize(ref, working_dir: Dir.getwd) ⇒ Commit

Returns a new instance of Commit.



10
11
12
13
# File 'lib/envirobly/git/commit.rb', line 10

def initialize(ref, working_dir: Dir.getwd)
  @ref = ref
  super working_dir
end

Instance Attribute Details

#working_dirObject (readonly)

Returns the value of attribute working_dir.



8
9
10
# File 'lib/envirobly/git/commit.rb', line 8

def working_dir
  @working_dir
end

Instance Method Details

#dir_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/envirobly/git/commit.rb', line 39

def dir_exists?(path)
  suffix = path.end_with?("/") ? nil : "/"
  git(%(cat-file -t #{@ref}:#{path}#{suffix})).stdout.strip == "tree"
end

#exists?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/envirobly/git/commit.rb', line 15

def exists?
  git(%(cat-file -t #{@ref})).stdout.strip == "commit"
end

#file_content(path) ⇒ Object



44
45
46
# File 'lib/envirobly/git/commit.rb', line 44

def file_content(path)
  git(%(show #{@ref}:#{path})).stdout
end

#file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/envirobly/git/commit.rb', line 35

def file_exists?(path)
  git(%(cat-file -t #{@ref}:#{path})).stdout.strip == "blob"
end

#messageObject



27
28
29
# File 'lib/envirobly/git/commit.rb', line 27

def message
  git(%(log #{@ref} -n1 --pretty=%B)).stdout.strip
end

#object_tree(ref: @ref, chdir: @working_dir) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/envirobly/git/commit.rb', line 48

def object_tree(ref: @ref, chdir: @working_dir)
  @object_tree ||= begin
    objects = {}
    objects[chdir] = []

    git(%(ls-tree -r #{ref}), chdir:).stdout.lines.each do |line|
      mode, type, object_hash, path = line.split(/\s+/)

      next if path.start_with?("#{Envirobly::Config::DIR}/")

      if type == "commit"
        objects.merge! object_tree(ref: object_hash, chdir: File.join(chdir, path))
      else
        objects[chdir] << [ mode, type, object_hash, path ]
      end
    end

    objects
  end
end

#object_tree_checksumObject



69
70
71
72
# File 'lib/envirobly/git/commit.rb', line 69

def object_tree_checksum
  digestable = object_tree.values.flatten.to_json
  @object_tree_checksum ||= Digest::SHA256.hexdigest(digestable)
end

#objects_with_checksum_at(path) ⇒ Object

Deprecated.


75
76
77
78
# File 'lib/envirobly/git/commit.rb', line 75

def objects_with_checksum_at(path)
  git(%{ls-tree #{@ref} --format='%(objectname) %(path)' #{path}}).stdout.lines.map(&:chomp).
    reject { _1.split(" ").last == Envirobly::Config::DIR }
end

#refObject



19
20
21
# File 'lib/envirobly/git/commit.rb', line 19

def ref
  @normalized_ref ||= git(%(rev-parse #{@ref})).stdout.strip
end

#short_refObject



23
24
25
# File 'lib/envirobly/git/commit.rb', line 23

def short_ref
  @short_ref ||= ref[0..6]
end

#timeObject



31
32
33
# File 'lib/envirobly/git/commit.rb', line 31

def time
  Time.parse git(%(log #{@ref} -n1 --date=iso --pretty=format:"%ad")).stdout
end