This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-publish-branch
executable file
·70 lines (62 loc) · 2.55 KB
/
git-publish-branch
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
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
## git-publish-branch: a simple script to ease the unnecessarily complex
## task of "publishing" a branch, i.e., taking a local branch, creating a
## reference to it on a remote repo, and setting up the local branch to
## track the remote one, all in one go. you can even delete that remote
## reference.
##
## Usage: git publish-branch [-d] <branch> [repository]
##
## '-d' signifies deletion. <branch> is the branch to publish, and
## [repository] defaults to "origin". The remote branch name will be the
## same as the local branch name. Don't make life unnecessarily complex
## for yourself.
##
## Note that unpublishing a branch doesn't delete the local branch.
## Safety first!
##
## git-publish-branch Copyright 2008 William Morgan <[email protected]>.
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You can find the GNU General Public License at:
## http://www.gnu.org/licenses/
def exec cmd
puts cmd
system cmd or die unless $fake
end
def die s=nil
$stderr.puts s if s
exit(-1)
end
head = `git symbolic-ref HEAD`.chomp.gsub(/refs\/heads\//, "")
delete = ARGV.delete "-d"
$fake = ARGV.delete "-n"
branch = (ARGV.shift || head).gsub(/refs\/heads\//, "")
remote = ARGV.shift || "origin"
local_ref = `git show-ref heads/#{branch}`
remote_ref = `git show-ref remotes/#{remote}/#{branch}`
remote_config = `git config branch.#{branch}.merge`
if delete
## we don't do any checking here because the remote branch might actually
## exist, whether we actually know about it or not.
exec "git push #{remote} :refs/heads/#{branch}"
unless local_ref.empty?
exec "git config --unset branch.#{branch}.remote"
exec "git config --unset branch.#{branch}.merge"
end
else
die "No local branch #{branch} exists!" if local_ref.empty?
die "A remote branch #{branch} on #{remote} already exists!" unless remote_ref.empty?
die "Local branch #{branch} is already a tracking branch!" unless remote_config.empty?
exec "git push #{remote} #{branch}:refs/heads/#{branch}"
exec "git config branch.#{branch}.remote #{remote}"
exec "git config branch.#{branch}.merge refs/heads/#{branch}"
end