aharrison24-git-external 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/git-external +68 -15
  2. metadata +3 -3
data/bin/git-external CHANGED
@@ -6,9 +6,10 @@ $ignore_file = "#{$root_dir}/.gitignore"
6
6
  $configurations = {}
7
7
 
8
8
  def usage
9
- puts "Usage: git external add <repository-url> <path>"
9
+ puts "Usage: git external add <repository-url> <path> [<branch>]"
10
10
  puts " or: git external init [--] [<path>...]"
11
11
  puts " or: git external update [--] [<path>...]"
12
+ puts " or: git external cmd '<command>'"
12
13
  puts " or: git external list"
13
14
  end
14
15
 
@@ -38,8 +39,10 @@ def normalize_url(url)
38
39
  if url =~ /^\./
39
40
  origin_url = `git config --get remote.origin.url`.chomp
40
41
 
42
+ # If url has no protocol...
41
43
  unless origin_url =~ /^\w+:\/\//
42
44
  if origin_url =~ /^([^:\/]+):([^:]+)/
45
+ # ... then assume it's ssh
43
46
  origin_url = "ssh://#{$1}/#{$2}"
44
47
  end
45
48
  end
@@ -53,48 +56,96 @@ def normalize_url(url)
53
56
  end
54
57
  end
55
58
 
56
- def init_external(url, path)
59
+ # Check if a branch exists
60
+ def branch_exists(path, branch)
61
+ branches = `cd #{path}; git branch`
62
+ regex = Regexp.new('[\\n\\s\\*]+' + Regexp.escape(branch.to_s) + '\\n')
63
+ result = ((branches =~ regex) ? true : false)
64
+ return result
65
+ end
66
+
67
+ def init_external(url, path, branch='master')
57
68
  require 'fileutils'
58
- unless File.directory? "#{path}/.git"
69
+ if File.directory? "#{path}/.git"
70
+ puts "- Repository already exists"
71
+ else
59
72
  FileUtils.makedirs File.dirname(path)
60
73
  url = normalize_url url
61
74
  system "git clone #{url} #{path}"
75
+
76
+ # Create a local tracking branch if it doesn't exist already
77
+ unless branch_exists(path, branch)
78
+ puts "- Creating local tracking branch: #{branch} -> origin/#{branch}"
79
+ system "cd #{path}; git branch checkout --track #{branch} origin/#{branch}"
80
+ end
81
+
82
+ # Check out the local branch
83
+ puts "- Checkout local branch: #{branch}"
84
+ system "cd #{path}; git checkout #{branch}"
62
85
  end
63
86
  end
64
87
 
65
- def update_external(url, path)
88
+ def update_external(url, path, branch='master')
66
89
  require 'fileutils'
67
90
  if File.directory? "#{path}/.git"
68
- `cd #{path}; git pull origin master`
91
+ `cd #{path}; git pull origin #{branch}`
92
+ end
93
+ end
94
+
95
+ def get_default_head(path)
96
+ origin_head = `cd #{path}; git symbolic-ref refs/remotes/origin/HEAD`
97
+ if origin_head =~ /refs\/remotes\/origin\/(.*)/
98
+ $1
99
+ else
100
+ raise "#{path}: Can't determine default HEAD for origin remote"
69
101
  end
70
102
  end
71
103
 
72
- def command_add(url, path)
104
+ def command_add(url, path, branch='master')
105
+ command_rm(path)
73
106
  `git config -f #{$externals_file} --add external.#{path}.path #{path}`
74
107
  `git config -f #{$externals_file} --add external.#{path}.url #{url}`
108
+ `git config -f #{$externals_file} --add external.#{path}.branch #{branch}`
75
109
  `echo "#{path}" >> #{$ignore_file}`
76
110
  end
77
111
 
78
112
  def command_rm(path)
79
- `git config -f #{$externals_file} --unset external.#{path}.path`
80
- `git config -f #{$externals_file} --unset external.#{path}.url`
81
- `git config -f #{$externals_file} --remove-section external.#{path}`
113
+ if File.file? $externals_file
114
+ `git config -f #{$externals_file} --unset external.#{path}.path`
115
+ `git config -f #{$externals_file} --unset external.#{path}.url`
116
+ `git config -f #{$externals_file} --unset external.#{path}.branch`
117
+ `git config -f #{$externals_file} --remove-section external.#{path} > /dev/null 2>&1`
118
+ File.delete $externals_file if `wc -l #{$externals_file}`.chomp.to_i == 0
119
+ end
120
+ if File.file? $ignore_file
121
+ # Remove entry from .gitignore file
122
+ `perl -pi -e 's/\\Q#{path.gsub(/\//, '\/')}\\E\n//g' #{$ignore_file}`
123
+ end
124
+ end
82
125
 
83
- # Remove entry from .gitignore file
84
- `perl -pi -e 's/\\Q#{path.gsub(/\//, '\/')}\\E\n//g' #{$ignore_file}`
85
- File.delete $externals_file if `wc -l #{$externals_file}`.chomp.to_i == 0
126
+ def command_list_default_heads
127
+ $configurations.each do |name, config|
128
+ puts "#{name}: #{get_default_head(config["path"])}"
129
+ end
86
130
  end
87
131
 
88
132
  def command_init
89
133
  $configurations.each do |name, config|
90
134
  puts name
91
- init_external config["url"], config["path"]
135
+ init_external config["url"], config["path"], config["branch"]
92
136
  end
93
137
  end
94
138
 
95
139
  def command_update
96
140
  $configurations.each do |name, config|
97
- update_external config["url"], config["path"]
141
+ update_external config["url"], config["path"], config["branch"]
142
+ end
143
+ end
144
+
145
+ def command_cmd(cmd)
146
+ $configurations.each do |name, config|
147
+ path = config['path']
148
+ system("echo #{path}; cd #{path}; #{cmd}")
98
149
  end
99
150
  end
100
151
 
@@ -107,11 +158,13 @@ load_configuration
107
158
  command=ARGV[0]
108
159
 
109
160
  case command
110
- when "add" then command_add ARGV[1], ARGV[2]
161
+ when "add" then command_add ARGV[1], ARGV[2], ARGV[3] || "master"
111
162
  when "rm" then command_rm ARGV[1]
112
163
  when "init" then command_init
113
164
  when "update" then command_update
165
+ when "cmd" then command_cmd ARGV[1]
114
166
  when "list" then command_list
167
+ when "heads" then command_list_default_heads
115
168
  else usage
116
169
  end
117
170
 
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aharrison24-git-external
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Daniel Cestari
8
+ - Daniel Cestari, Alastair Harrison
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-01 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Extension for git which adds a command providing similar functionality
15
15
  to git submodules but without attaching each module to a single version