Class: Dapp::Dimg::GitRepo::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/dimg/git_repo/base.rb

Overview

Base class for any Git repo (remote, gitkeeper, etc)

Direct Known Subclasses

Own, Remote

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimg, name) ⇒ Base

Returns a new instance of Base.



9
10
11
12
# File 'lib/dapp/dimg/git_repo/base.rb', line 9

def initialize(dimg, name)
  @dimg = dimg
  @name = name
end

Instance Attribute Details

#dimgObject (readonly)

Returns the value of attribute dimg.



6
7
8
# File 'lib/dapp/dimg/git_repo/base.rb', line 6

def dimg
  @dimg
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/dapp/dimg/git_repo/base.rb', line 7

def name
  @name
end

Instance Method Details

#branchObject



70
71
72
73
74
# File 'lib/dapp/dimg/git_repo/base.rb', line 70

def branch
  git.head.name.sub(/^refs\/heads\//, '')
rescue Rugged::ReferenceError => e
  raise Error::Rugged, code: :git_repository_reference_error, data: { name: name, message: e.message.downcase }
end

#commit_at(commit) ⇒ Object



76
77
78
# File 'lib/dapp/dimg/git_repo/base.rb', line 76

def commit_at(commit)
  lookup_commit(commit).time.to_i
end

#commit_exists?(commit) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/dapp/dimg/git_repo/base.rb', line 62

def commit_exists?(commit)
  git.exists?(commit)
end

#diff(from, to, **kwargs) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/dapp/dimg/git_repo/base.rb', line 52

def diff(from, to, **kwargs)
  if to.nil?
    raise "Workdir diff not supported for #{self.class}"
  elsif from.nil?
    Rugged::Tree.diff(git, nil, to, **kwargs)
  else
    lookup_commit(from).diff(lookup_commit(to), **kwargs)
  end
end

#entries(commit, paths: [], exclude_paths: []) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dapp/dimg/git_repo/base.rb', line 34

def entries(commit, paths: [], exclude_paths: [])
  [].tap do |entries|
    lookup_commit(commit).tree.walk(:preorder) do |root, entry|
      fullpath = File.join(root, entry[:name]).reverse.chomp('/').reverse

      is_exclude_path = exclude_paths.any? { |p| check_path?(fullpath, p) }
      is_include_path = begin
        paths.empty? ||
          paths.any? { |p| check_path?(fullpath, p) || check_subpath?(fullpath, p) }
      end

      next false if is_exclude_path || !is_include_path

      entries << [root, entry]
    end
  end
end

#exclude_pathsObject



14
15
16
# File 'lib/dapp/dimg/git_repo/base.rb', line 14

def exclude_paths
  []
end

#find_commit_id_by_message(regex) ⇒ Object



80
81
82
83
84
85
# File 'lib/dapp/dimg/git_repo/base.rb', line 80

def find_commit_id_by_message(regex)
  walker.each do |commit|
    msg = commit.message.encode('UTF-8', invalid: :replace, undef: :replace)
    return commit.oid if msg =~ regex
  end
end

#latest_commit(_branch) ⇒ Object



66
67
68
# File 'lib/dapp/dimg/git_repo/base.rb', line 66

def latest_commit(_branch)
  raise
end

#lookup_commit(commit) ⇒ Object



97
98
99
# File 'lib/dapp/dimg/git_repo/base.rb', line 97

def lookup_commit(commit)
  git.lookup(commit)
end

#lookup_object(oid) ⇒ Object



93
94
95
# File 'lib/dapp/dimg/git_repo/base.rb', line 93

def lookup_object(oid)
  git.lookup(oid)
end

#patches(from, to, exclude_paths: [], **kwargs) ⇒ Object

FIXME: Убрать логику исключения путей exclude_paths из данного класса, FIXME: т.к. большинство методов не поддерживают инвариант FIXME “всегда выдавать данные с исключенными путями”. FIXME: Например, метод diff выдает данные без учета exclude_paths. FIXME: Лучше перенести фильтрацию в GitArtifact::diff_patches. FIXME: ИЛИ обеспечить этот инвариант, но это ограничит в возможностях FIXME: использование Rugged извне этого класса и это более сложный путь. FIXME: Лучше сейчас убрать фильтрацию, а добавить ее когда наберется достаточно FIXME: примеров использования.



28
29
30
31
32
# File 'lib/dapp/dimg/git_repo/base.rb', line 28

def patches(from, to, exclude_paths: [], **kwargs)
  diff(from, to, **kwargs).patches.select do |patch|
    !exclude_paths.any? { |p| check_path?(patch.delta.new_file[:path], p) }
  end
end

#walkerObject



87
88
89
90
91
# File 'lib/dapp/dimg/git_repo/base.rb', line 87

def walker
  walker = Rugged::Walker.new(git)
  walker.push(git.head.target_id)
  walker
end