forked from vphilomin/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_latest
39 lines (27 loc) · 1.19 KB
/
fetch_latest
1
2
3
4
5
6
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
#!/usr/bin/env ruby
require_relative 'git_utils.rb'
def get_latest_remote_branch_name(remote_name)
branch_pattern = /^\d*$/
branches = run_git_command("fetch #{remote_name}")
latest_branch = Dir.entries(File.join('.git','refs','remotes',remote_name)).select{|folder| branch_pattern =~ folder}.sort{|first,second| second <=> first}.first
end
def get_all_available_non_origin_remotes
run_git_command("remote",true).split("\n").select{|remote| /origin/ !~ remote}
end
def choose_remote(remotes)
pick_item_from(remotes,"Choose remote:")
end
def update_to_latest_branch_on(remote_name)
run_git_command("add -A")
run_git_command('commit -m "Updated"')
run_git_command("checkout clean")
latest_branch = get_latest_remote_branch_name(remote_name)
new_branch = "pull_#{remote_name}_#{latest_branch}"
run_git_command("checkout -b #{new_branch}")
run_git_command("checkout #{new_branch}")
run_git_command("pull #{remote_name} #{latest_branch}")
end
%w[master clean].each{|branch| exit_if_on_the_branch branch}
remote_name = ARGV.shift
remote_name = choose_remote(get_all_available_non_origin_remotes) if remote_name == nil
update_to_latest_branch_on(remote_name) unless remote_name.empty?