Class: Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/bub_bot/repo.rb

Constant Summary collapse

PUSH_REMOTE_NAME =

When we create a temproary remote to push to, use this name.

'push_remote'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, origin_remote, server) ⇒ Repo

Returns a new instance of Repo.



7
8
9
10
11
12
13
14
# File 'lib/bub_bot/repo.rb', line 7

def initialize(target, origin_remote, server)
  puts "target = #{target}"
  puts "remote = #{origin_remote}"
  puts "server = #{server}"
  @target = target
  @origin_remote = origin_remote
  @server = server
end

Class Method Details

.branches(origin_remote) ⇒ Object

Gets a list of branch names without actually cloning the repo.



17
18
19
20
21
22
23
# File 'lib/bub_bot/repo.rb', line 17

def self.branches(origin_remote)
  puts 'repo.branches'
  # git ls-remote returns a list of lines like this:
  #   ea656e141760b0d8ba49d92506427322120ce945	refs/heads/some-branch-name
  ls_remote_output = `git ls-remote --heads #{origin_remote}`
  ls_remote_output.split("\n").map {|line| line.split('/').last }
end

Instance Method Details

#checkout(branch_name) ⇒ Object



73
74
75
76
77
78
# File 'lib/bub_bot/repo.rb', line 73

def checkout(branch_name)
  clean
  cmd("remote set-branches origin '*'")
  fetch
  git.checkout(branch_name)
end

#cleanObject



63
64
65
66
67
# File 'lib/bub_bot/repo.rb', line 63

def clean
  # TODO: make sure there's no weird changes on the git repo
  git.clean(force: true)
  git.reset_hard
end

#dirObject



102
103
104
# File 'lib/bub_bot/repo.rb', line 102

def dir
  "/tmp/bub_repos"
end

#fetchObject



69
70
71
# File 'lib/bub_bot/repo.rb', line 69

def fetch
  git.fetch
end

#gitObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bub_bot/repo.rb', line 25

def git
  return @_git if @_git

  if Dir.exists?(repo_dir) && Dir.exists?(repo_dir + '/.git')
    puts "Repo exists!"
    @_git = Git.open(repo_dir)
    # TODO: handle other errors beside "dir doesn't exist"
  else
    puts "Cloning repo"
    @_git = Git.clone(@origin_remote, repo_dir_name, path: dir, depth: 1, :log => Logger.new(STDOUT))
    puts 'here'
  end
  @_git
end

#pullObject



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

def pull
  git.pull
end

#push(branch, remote, &on_complete) ⇒ Object

Push is async



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bub_bot/repo.rb', line 41

def push(branch, remote, &on_complete)
  # TODO: handle people pushing while another push is still going, since we
  # have some shared state in the form of the filesystem

  puts 'Pushing'
  git.remotes.find{ |remote| remote.name == 'push_remote' }&.remove
  puts 'Maybe removed old remote'

  git.add_remote('push_remote', remote)
  puts 'added remote'

  Kernel.system("cd #{repo_dir}; git remote set-branches --add origin '#{branch}'")
  puts "Pushing #{branch} to #{remote}"

  puts "about to git fetch origin for branch #{branch}"
  git.fetch('origin', branch: branch)
  puts 'about to finally push'
  git.push(remote, "+origin/#{branch}:master")
  puts 'Finished final push'
  on_complete.call if on_complete
end

#repo_dirObject

We name repo dirs after server + name (eg ‘burrito__core`). This lets multiple deploys to the same server (eg deploying both core and web to burrito at the same time) to work, as well as multiple deploys to the same target (eg deploying core on both burrito and gyro) to work.

Note that simultanious deploys to the same target and server (eg two deploys at once to burrito__core) won’t work. Both deploys would use the same git working directory and step on eachother’s toes. That’s fine, though, because they’d also step on eachother’s toes in the actual target environment. We should prevent this case in the UI.



94
95
96
# File 'lib/bub_bot/repo.rb', line 94

def repo_dir
  "#{dir}/#{repo_dir_name}"
end

#repo_dir_nameObject



98
99
100
# File 'lib/bub_bot/repo.rb', line 98

def repo_dir_name
  "#{@server}__#{@target}"
end