Module: GitRemoteBranch

Defined in:
lib/git_remote_branch.rb

Constant Summary collapse

VERSION =
'0.2.1'
CMD_ALIASES =
{
  :create => %w{create new},
  :delete => %w{delete destroy kill remove},
  :track  => %w{track follow grab fetch},
}

Instance Method Summary collapse

Instance Method Details

#create_branch(branch_name, origin, current_branch) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/git_remote_branch.rb', line 47

def create_branch(branch_name, origin, current_branch)
  cmd = []
  cmd << "git push origin #{current_branch}:refs/heads/#{branch_name}"
  cmd << "git fetch #{origin}"
  cmd << "git branch --track #{branch_name} #{origin}/#{branch_name}"
  cmd << "git checkout #{branch_name}"
  execute_cmds(cmd)
end

#delete_branch(branch_name, origin, current_branch) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/git_remote_branch.rb', line 56

def delete_branch(branch_name, origin, current_branch)
  cmd = []
  cmd << "git push #{origin} :refs/heads/#{branch_name}"
  cmd << "git checkout master" if current_branch == branch_name
  cmd << "git branch -d #{branch_name}"
  execute_cmds(cmd)
end

#execute_cmd(cmd) ⇒ Object



32
33
34
35
36
37
# File 'lib/git_remote_branch.rb', line 32

def execute_cmd(cmd)
  res, out, err = steal_pipes do
                                `#{cmd}`
                              end
  return res, out, err
end

#execute_cmds(*cmds) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/git_remote_branch.rb', line 39

def execute_cmds(*cmds)
  cmds.flatten.each do |c|
    puts "Executing: #{c}"
    `#{c}`
    puts ""
  end
end

#get_actionObject



79
80
81
82
83
84
85
# File 'lib/git_remote_branch.rb', line 79

def get_action
  a = ARGV[0].downcase
  return :create if CMD_ALIASES[:create].include?(a)
  return :delete if CMD_ALIASES[:delete].include?(a)
  return :track  if CMD_ALIASES[:track].include?(a)
  return nil
end

#get_branchObject



87
88
89
# File 'lib/git_remote_branch.rb', line 87

def get_branch
  ARGV[1].downcase
end

#get_current_branchObject



69
70
71
72
73
74
75
76
77
# File 'lib/git_remote_branch.rb', line 69

def get_current_branch
  x = `git branch -l`
  x.each_line do |l|
    return l.sub("*","").strip if l[0] == 42
  end

  puts "Couldn't identify the current local branch."
  return nil
end

#get_originObject



91
92
93
94
# File 'lib/git_remote_branch.rb', line 91

def get_origin
  return ARGV[2] if ARGV.size > 2 
  return "origin"
end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/git_remote_branch.rb', line 14

def print_usage
  puts <<-HELP
Usage:

git_remote_branch create branch_name [origin_server]
-or-
git_remote_branch delete branch_name [origin_server]
-or-
git_remote_branch track branch_name [origin_server]

If origin_server is not specified, the name 'origin' is assumed

All commands also have aliases:
#{ CMD_ALIASES.keys.map{|k| k.to_s}.sort.map {|k| 
  "#{k}: #{CMD_ALIASES[k.to_sym].join(', ')}" }.join("\n  ") }
HELP
end


10
11
12
# File 'lib/git_remote_branch.rb', line 10

def print_welcome
  puts "git_remote_branch version #{VERSION}", '-' * 70, ''
end

#read_paramsObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/git_remote_branch.rb', line 96

def read_params
  p={}
  begin
    p[:action] = get_action
    p[:branch] = get_branch
    p[:origin] = get_origin
    p[:current_branch] = get_current_branch
    p
  rescue
    {:action=>:help}
  end
end

#track_branch(branch_name, origin) ⇒ Object



64
65
66
67
# File 'lib/git_remote_branch.rb', line 64

def track_branch(branch_name, origin)
  cmd = ["git branch --track #{branch_name} #{origin}/#{branch_name}"]
  execute_cmds(cmd)
end