Class: MiniGit

Inherits:
Object
  • Object
show all
Defined in:
lib/minigit.rb,
lib/minigit/version.rb

Direct Known Subclasses

Capturing

Defined Under Namespace

Classes: Capturing, GitError

Constant Summary collapse

VERSION =
"0.0.4"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(where = nil, opts = {}) ⇒ MiniGit

Returns a new instance of MiniGit.



67
68
69
70
71
72
73
74
75
76
# File 'lib/minigit.rb', line 67

def initialize(where=nil, opts={})
  where, opts = nil, where if where.is_a?(Hash)
  @git_command = opts[:git_command] if opts[:git_command]
  if where
    @git_dir, @git_work_tree = find_git_dir(where)
  else
    @git_dir = opts[:git_dir] if opts[:git_dir]
    @git_work_tree = opts[:git_work_tree] if opts[:git_work_tree]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



88
89
90
# File 'lib/minigit.rb', line 88

def method_missing(meth, *args, &block)
  self.git(meth, *args)
end

Class Attribute Details

.debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/minigit.rb', line 8

def debug
  @debug
end

.git_commandObject



11
12
13
# File 'lib/minigit.rb', line 11

def git_command
  @git_command || ( (self==::MiniGit) ? 'git' : ::MiniGit.git_command )
end

Instance Attribute Details

#git_commandObject



51
52
53
# File 'lib/minigit.rb', line 51

def git_command
  @git_command || self.class.git_command
end

#git_dirObject (readonly)

Returns the value of attribute git_dir.



49
50
51
# File 'lib/minigit.rb', line 49

def git_dir
  @git_dir
end

#git_work_treeObject (readonly)

Returns the value of attribute git_work_tree.



49
50
51
# File 'lib/minigit.rb', line 49

def git_work_tree
  @git_work_tree
end

Class Method Details

.[](arg) ⇒ Object



23
24
25
# File 'lib/minigit.rb', line 23

def [](arg)
_myself[arg]
end

.[]=(key, value) ⇒ Object



27
28
29
# File 'lib/minigit.rb', line 27

def []=(key, value)
  _myself[key] = value
end

.git(*args) ⇒ Object



19
20
21
# File 'lib/minigit.rb', line 19

def git(*args)
  _myself.git(*args)
end

.method_missing(meth, *args, &block) ⇒ Object



15
16
17
# File 'lib/minigit.rb', line 15

def method_missing(meth, *args, &block)
  _myself.git(meth, *args)
end

Instance Method Details

#[](arg) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/minigit.rb', line 154

def [](arg)
  begin
  self.capturing.config(arg).strip
  rescue MiniGit::GitError
    nil
  end
end

#[]=(key, value) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/minigit.rb', line 162

def []=(key, value)
  begin
    self.noncapturing.config(key, value)
  rescue MiniGit::GitError
    nil
  end
end

#capturingObject



124
125
126
127
128
129
# File 'lib/minigit.rb', line 124

def capturing
  @capturing ||= Capturing.new(
    :git_command => @git_command,
    :git_dir => @git_dir,
    :git_work_tree => @git_work_tree)
end

#find_git_dir(where) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/minigit.rb', line 55

def find_git_dir(where)
  path = Pathname.new(where)
  raise ArgumentError, "#{where} does not seem to exist" unless path.exist?
  path = path.dirname unless path.directory?
  Dir.chdir(path.to_s) do
    out = `#{git_command} rev-parse --git-dir --show-toplevel`
    $stderr.puts "+ [#{Dir.pwd}] #{git_command} rev-parse --git-dir --show-toplevel # => #{out.inspect}" if MiniGit.debug
    raise ArgumentError, "Invalid repository path #{where}" unless $?.success?
    out
  end.lines.map { |ln| path.join(Pathname.new(ln.strip)).realpath.to_s }
end

#git(*args) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/minigit.rb', line 78

def git(*args)
  argv = switches_for(*args)
  with_git_env do
    $stderr.puts "+ #{git_command} #{Shellwords.join(argv)}" if MiniGit.debug
    rv = system(git_command, *argv)
    raise GitError.new(argv, $?) unless $?.success?
    rv
  end
end

#noncapturingObject



131
132
133
# File 'lib/minigit.rb', line 131

def noncapturing
  self
end

#switches_for(*args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/minigit.rb', line 92

def switches_for(*args)
  rv = []
  args.each do |arg|
    case arg
    when Hash
      arg.keys.sort_by(&:to_s).each do |k|
        short = (k.to_s.length == 1)
        switch = short ? "-#{k}" : "--#{k}".gsub('_', '-')
        Array(arg[k]).each do |value|
          if value == true
            rv << switch
          elsif short
            rv << switch
            rv << value.to_s
          else
            rv << "#{switch}=#{value}"
          end
        end
      end
    when String
      rv << arg
    when Enumerable
      rv += switches_for(*arg)
    when Symbol
      rv << arg.to_s.gsub('_', '-')
    else
      rv << arg.to_s
    end
  end
  rv
end