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.3"

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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/minigit.rb', line 59

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



80
81
82
# File 'lib/minigit.rb', line 80

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



43
44
45
# File 'lib/minigit.rb', line 43

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

#git_dirObject (readonly)

Returns the value of attribute git_dir.



41
42
43
# File 'lib/minigit.rb', line 41

def git_dir
  @git_dir
end

#git_work_treeObject (readonly)

Returns the value of attribute git_work_tree.



41
42
43
# File 'lib/minigit.rb', line 41

def git_work_tree
  @git_work_tree
end

Class Method Details

.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

#capturingObject



116
117
118
119
120
# File 'lib/minigit.rb', line 116

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)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/minigit.rb', line 47

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



70
71
72
73
74
75
76
77
78
# File 'lib/minigit.rb', line 70

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



122
123
124
# File 'lib/minigit.rb', line 122

def noncapturing
  self
end

#switches_for(*args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/minigit.rb', line 84

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