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(manager, name) ⇒ Base

Returns a new instance of Base.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#branchObject



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

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



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

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

#commit_exists?(commit) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/dapp/dimg/git_repo/base.rb', line 73

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

#dappObject



13
14
15
16
17
18
19
20
21
# File 'lib/dapp/dimg/git_repo/base.rb', line 13

def dapp
  @dapp ||= begin
    if manager.is_a? ::Dapp::Dapp
      manager
    else
      dimg.dapp
    end
  end
end

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



63
64
65
66
67
68
69
70
71
# File 'lib/dapp/dimg/git_repo/base.rb', line 63

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

#dimgObject



23
24
25
26
27
28
29
30
31
# File 'lib/dapp/dimg/git_repo/base.rb', line 23

def dimg
  @dimg ||= begin
    if manager.is_a? ::Dapp::Dimg::Dimg
      manager
    else
      raise
    end
  end
end

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



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

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
      next if entry[:type] == :tree || ignore_path?(fullpath, paths: paths, exclude_paths: exclude_paths)
      entries << [root, entry]
    end
  end
end

#exclude_pathsObject



33
34
35
# File 'lib/dapp/dimg/git_repo/base.rb', line 33

def exclude_paths
  []
end

#find_commit_id_by_message(regex) ⇒ Object



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

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



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

def latest_commit(_branch)
  raise
end

#lookup_commit(commit) ⇒ Object



108
109
110
# File 'lib/dapp/dimg/git_repo/base.rb', line 108

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

#lookup_object(oid) ⇒ Object



104
105
106
# File 'lib/dapp/dimg/git_repo/base.rb', line 104

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

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

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



47
48
49
50
51
# File 'lib/dapp/dimg/git_repo/base.rb', line 47

def patches(from, to, paths: [], exclude_paths: [], **kwargs)
  diff(from, to, **kwargs).patches.select do |patch|
    !ignore_path?(patch.delta.new_file[:path], paths: paths, exclude_paths: exclude_paths)
  end
end

#walkerObject



98
99
100
101
102
# File 'lib/dapp/dimg/git_repo/base.rb', line 98

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