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.



12
13
14
15
# File 'lib/envirobly/git/commit.rb', line 12

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.



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

def working_dir
  @working_dir
end

Instance Method Details

#dir_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/envirobly/git/commit.rb', line 41

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

#exists?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/envirobly/git/commit.rb', line 17

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

#file_content(path) ⇒ Object



46
47
48
# File 'lib/envirobly/git/commit.rb', line 46

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

#file_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/envirobly/git/commit.rb', line 37

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

#messageObject



29
30
31
# File 'lib/envirobly/git/commit.rb', line 29

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

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



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

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



71
72
73
74
# File 'lib/envirobly/git/commit.rb', line 71

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.


77
78
79
80
# File 'lib/envirobly/git/commit.rb', line 77

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



21
22
23
# File 'lib/envirobly/git/commit.rb', line 21

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

#short_refObject



25
26
27
# File 'lib/envirobly/git/commit.rb', line 25

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

#timeObject



33
34
35
# File 'lib/envirobly/git/commit.rb', line 33

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