/
git-update
executable file
·60 lines (50 loc) · 1.98 KB
/
git-update
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/ruby
#---------------------------------------------------------------------------------------------------
# TODO:
# make it optional --rebase or merge (not --rebase)
#---------------------------------------------------------------------------------------------------
def usage
#puts "Usage: #{$0}"
puts <<End
Switches to the master branch (or whichever one you specify), does a git pull, (assumes the merge will be successful because you are doing all your changes in a topic branch and *not* in master),
and then switches back to the topic branch you were on.
Useful if you want to do a rebase: issue this command first to make sure the 'master' branch you are rebasing against is actually up-to-date!
Alternatively, you could do a git fetch and then rebase against FETCH_HEAD.
git rebase FETCH_HEAD master
End
exit 1
end
#---------------------------------------------------------------------------------------------------
require 'getoptlong'
opts = GetoptLong.new(
[ '--ask', GetoptLong::NO_ARGUMENT ],
[ '--help', GetoptLong::NO_ARGUMENT ]
)
opts.each do | opt, arg |
case opt
when '--ask'
@ask = true
when '--help'
usage
end
end
@branch = ARGV.shift || 'master'
@original_branch = `git current-branch`
#---------------------------------------------------------------------------------------------------
require File.dirname(__FILE__) + '/../lib/getch'
if @ask
exit unless (print "Do you want to do a git pull on #{@branch} first (y/N)? "; STDIN.getch(:case_insensitive => true) == 'y')
end
at_exit {
system "git checkout #{@original_branch}"
system %(git rebase "#{@branch}") # TODO: make this optional?
}
system %(git checkout "#{@branch}")
(puts "Aborting"; exit 1) if !$?.success?
system %(git pull --rebase)
#---------------------------------------------------------------------------------------------------
# Original sh version:
#original_branch=`git current-branch`
#git checkout master
#git pull
#git checkout $original_branch