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

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.



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

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



77
78
79
# File 'lib/minigit.rb', line 77

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

Class Attribute Details

.git_commandObject



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

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

Instance Attribute Details

#git_commandObject



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

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

#git_dirObject (readonly)

Returns the value of attribute git_dir.



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

def git_dir
  @git_dir
end

#git_work_treeObject (readonly)

Returns the value of attribute git_work_tree.



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

def git_work_tree
  @git_work_tree
end

Class Method Details

.git(*args) ⇒ Object



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

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

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



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

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

Instance Method Details

#capturingObject



113
114
115
116
117
# File 'lib/minigit.rb', line 113

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)


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

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`
    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



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

def git(*args)
  argv = switches_for(*args)
  with_git_env do
    rv = system(git_command, *argv)
    raise GitError.new(argv, $?) unless $?.success?
    rv
  end
end

#noncapturingObject



119
120
121
# File 'lib/minigit.rb', line 119

def noncapturing
  self
end

#switches_for(*args) ⇒ Object



81
82
83
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
# File 'lib/minigit.rb', line 81

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