Class: Braid::Operations::Git

Inherits:
Proxy
  • Object
show all
Defined in:
lib/braid/operations.rb

Instance Method Summary collapse

Methods inherited from Proxy

command, #require_version, #require_version!, #version

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Braid::Operations::Proxy

Instance Method Details

#apply(diff, *args) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/braid/operations.rb', line 222

def apply(diff, *args)
  err = nil
  status = Open4.popen4("git apply --index --whitespace=nowarn #{args.join(' ')} -") do |pid, stdin, stdout, stderr|
    stdin.puts(diff)
    stdin.close

    err = stderr.read
  end.exitstatus
  raise ShellExecutionError, err unless status == 0
  true
end

#branchObject



217
218
219
220
# File 'lib/braid/operations.rb', line 217

def branch
  status, out, err = exec!("git branch | grep '*'")
  out[2..-1]
end

#checkout(treeish) ⇒ Object



132
133
134
135
136
137
# File 'lib/braid/operations.rb', line 132

def checkout(treeish)
  # TODO debug
  msg "Checking out '#{treeish}'."
  invoke(:checkout, treeish)
  true
end

#commit(message, *args) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/braid/operations.rb', line 113

def commit(message, *args)
  status, out, err = exec("git commit -m #{message.inspect} --no-verify #{args.join(' ')}")

  if status == 0
    true
  elsif out.match(/nothing.* to commit/)
    false
  else
    raise ShellExecutionError, err
  end
end

#diff_tree(src_tree, dst_tree, prefix = nil) ⇒ Object



197
198
199
200
201
202
# File 'lib/braid/operations.rb', line 197

def diff_tree(src_tree, dst_tree, prefix = nil)
  cmd = "git diff-tree -p --binary #{src_tree} #{dst_tree}"
  cmd << " --src-prefix=a/#{prefix}/ --dst-prefix=b/#{prefix}/" if prefix
  status, out, err = exec!(cmd)
  out
end

#ensure_clean!Object



209
210
211
# File 'lib/braid/operations.rb', line 209

def ensure_clean!
  status_clean? || raise(LocalChangesPresent)
end

#fetch(remote) ⇒ Object



125
126
127
128
129
130
# File 'lib/braid/operations.rb', line 125

def fetch(remote)
  # open4 messes with the pipes of index-pack
  system("git fetch -n #{remote} &> /dev/null")
  raise ShellExecutionError, "could not fetch" unless $? == 0
  true
end

#headObject



213
214
215
# File 'lib/braid/operations.rb', line 213

def head
  rev_parse("HEAD")
end

#merge_base(target, source) ⇒ Object

Returns the base commit or nil.



140
141
142
143
144
# File 'lib/braid/operations.rb', line 140

def merge_base(target, source)
  invoke(:merge_base, target, source)
rescue ShellExecutionError
  nil
end

#merge_ours(opt) ⇒ Object

Implies no commit.



170
171
172
173
# File 'lib/braid/operations.rb', line 170

def merge_ours(opt)
  invoke(:merge, "-s ours --no-commit", opt)
  true
end

#merge_subtree(opt) ⇒ Object

Implies no commit.



176
177
178
179
180
# File 'lib/braid/operations.rb', line 176

def merge_subtree(opt)
  # TODO which options are needed?
  invoke(:merge, "-s subtree --no-commit --no-ff", opt)
  true
end

#read_tree(treeish, prefix) ⇒ Object



182
183
184
185
# File 'lib/braid/operations.rb', line 182

def read_tree(treeish, prefix)
  invoke(:read_tree, "--prefix=#{prefix}/ -u", treeish)
  true
end

#remote_add(remote, path, branch) ⇒ Object

Implies tracking.



153
154
155
156
# File 'lib/braid/operations.rb', line 153

def remote_add(remote, path, branch)
  invoke(:remote, "add", "-t #{branch} -m #{branch}", remote, path)
  true
end

#remote_exists?(remote) ⇒ Boolean

Checks git and svn remotes.

Returns:

  • (Boolean)


159
160
161
162
# File 'lib/braid/operations.rb', line 159

def remote_exists?(remote)
  # TODO clean up and maybe return more information
  !!File.readlines(".git/config").find { |line| line =~ /^\[(svn-)?remote "#{Regexp.escape(remote)}"\]/ }
end

#reset_hard(target) ⇒ Object



164
165
166
167
# File 'lib/braid/operations.rb', line 164

def reset_hard(target)
  invoke(:reset, "--hard", target)
  true
end

#rev_parse(opt) ⇒ Object



146
147
148
149
150
# File 'lib/braid/operations.rb', line 146

def rev_parse(opt)
  invoke(:rev_parse, opt)
rescue ShellExecutionError
  raise UnknownRevision, opt
end

#rm_r(path) ⇒ Object



187
188
189
190
# File 'lib/braid/operations.rb', line 187

def rm_r(path)
  invoke(:rm, "-r", path)
  true
end

#status_clean?Boolean

Returns:

  • (Boolean)


204
205
206
207
# File 'lib/braid/operations.rb', line 204

def status_clean?
  status, out, err = exec("git status")
  !out.split("\n").grep(/nothing to commit/).empty?
end

#tree_hash(path, treeish = "HEAD") ⇒ Object



192
193
194
195
# File 'lib/braid/operations.rb', line 192

def tree_hash(path, treeish = "HEAD")
  out = invoke(:ls_tree, treeish, "-d", path)
  out.split[2]
end