Class: Braid::Commands::Push

Inherits:
Braid::Command show all
Defined in:
lib/braid/commands/push.rb

Instance Method Summary collapse

Methods inherited from Braid::Command

#config, #force?, msg, #msg, run, #verbose?

Methods included from Operations::VersionControl

#git, #git_cache

Instance Method Details

#run(path, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/braid/commands/push.rb', line 7

def run(path, options = {})
  mirror = config.get!(path)

  branch = options['branch'] || mirror.branch

  setup_remote(mirror)
  mirror.fetch

  base_revision = git.rev_parse(mirror.remote)
  unless mirror.merged?(base_revision)
    msg 'Mirror is not up to date. Stopping.'
    clear_remote(mirror, options)
    return
  end

  diff = mirror.diff
  if diff.empty?
    msg 'No local changes found. Stopping.'
    clear_remote(mirror, options)
    return
  end

  clone_dir = Dir.tmpdir + "/braid_push.#{$$}"
  Dir.mkdir(clone_dir)
  remote_url = git.remote_url(mirror.remote)
  if remote_url == mirror.cached_url
    remote_url = mirror.url
  elsif File.directory?(remote_url)
    remote_url = File.expand_path(remote_url)
  end
  Dir.chdir(clone_dir) do
    msg 'Cloning mirror with local changes.'
    git.init
    git.fetch(mirror.cached_url) if File.exist?(mirror.cached_url)
    git.fetch(remote_url, "+refs/heads/#{mirror.branch}")
    git.checkout(base_revision)
    git.apply(diff)
    system('git commit -v')
    msg "Pushing changes to remote branch #{branch}."
    git.push(remote_url, "HEAD:refs/heads/#{branch}")
  end
  FileUtils.rm_r(clone_dir)

  clear_remote(mirror, options)
end