mortar 0.13.2 → 0.13.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -132,12 +132,64 @@ class Mortar::Command::Base
132
132
  error("Project missing required directories: #{missing_dirs.to_s}")
133
133
  end
134
134
  end
135
+ # Register logic
136
+ #
137
+ # if project id is not created, just pass in nil
138
+ def register_do(name, is_public, is_embedded, project_id)
139
+ if is_public
140
+ unless confirm("Public projects allow anyone to view and fork the code in this project\'s repository. Are you sure? (y/n)")
141
+ error("Mortar project was not registered")
142
+ end
143
+ end
144
+
145
+ if is_embedded
146
+ validate_project_structure()
147
+
148
+ register_project(name, is_public, project_id) do |project_result|
149
+ initialize_embedded_project(project_result)
150
+ end
151
+ else
152
+ unless git.has_dot_git?
153
+ # check if we're in the parent directory
154
+ if File.exists? name
155
+ error("mortar projects:register must be run from within the project directory.\nPlease \"cd #{name}\" and rerun this command.")
156
+ else
157
+ error("No git repository found in the current directory.\nTo register a project that is not its own git repository, use the --embedded option.\nIf you do want this project to be its own git repository, please initialize git in this directory, and then rerun the register command.\nTo initialize your project in git, use:\n\ngit init\ngit add .\ngit commit -a -m \"first commit\"")
158
+ end
159
+ end
160
+
135
161
 
136
- def register_project(name, is_private)
162
+ unless git.remotes(git_organization).empty?
163
+ begin
164
+ error("Currently in project: #{project.name}. You can not register a new project inside of an existing mortar project.")
165
+ rescue Mortar::Command::CommandFailed => cf
166
+ error("Currently in an existing Mortar project. You can not register a new project inside of an existing mortar project.")
167
+ end
168
+ end
169
+
170
+ register_project(name, is_public, project_id) do |project_result|
171
+ git.remote_add("mortar", project_result['git_url'])
172
+ git.push_master
173
+ display "Your project is ready for use. Type 'mortar help' to see the commands you can perform on the project.\n\n"
174
+ end
175
+ end
176
+ end
177
+
178
+ def register_api_call(name, is_public)
137
179
  project_id = nil
180
+ is_private = !is_public # is private required by restful api
181
+ validate_project_name(name)
138
182
  action("Sending request to register project: #{name}") do
139
183
  project_id = api.post_project(name, is_private).body["project_id"]
140
184
  end
185
+ return project_id
186
+ end
187
+
188
+ def register_project(name, is_public, project_id)
189
+ 'registering project....\n'
190
+ if project_id == nil
191
+ project_id = register_api_call(name, is_public)
192
+ end
141
193
 
142
194
  project_result = nil
143
195
  project_status = nil
@@ -88,23 +88,27 @@ class Mortar::Command::Projects < Mortar::Command::Base
88
88
  error("Usage: mortar projects:create PROJECTNAME\nMust specify PROJECTNAME")
89
89
  end
90
90
 
91
- Mortar::Command::run("generate:project", [name])
92
91
 
93
- FileUtils.cd(name)
94
92
 
95
93
  args = [name,]
94
+ is_public = false
96
95
  if options[:public]
97
- args.push('--public')
96
+ is_public= true
98
97
  end
99
-
98
+ validate_project_name(name)
99
+ project_id = register_api_call(name,is_public)
100
+ # is_public is created for clarity in other sections of code
101
+ Mortar::Command::run("generate:project", [name])
102
+ FileUtils.cd(name)
103
+ is_embedded = false
100
104
  if options[:embedded]
101
- args.push("--embedded")
102
- Mortar::Command::run("projects:register", args)
105
+ is_embedded = true
106
+ register_do(name, is_public, is_embedded, project_id)
103
107
  else
104
108
  git.git_init
105
109
  git.git("add .")
106
- git.git("commit -m \"Mortar project scaffolding\"")
107
- Mortar::Command::run("projects:register", args)
110
+ git.git("commit -m \"Mortar project scaffolding\"")
111
+ register_do(name, is_public, is_embedded, project_id)
108
112
  display "NOTE: You'll need to change to the new directory to use your project:\n cd #{name}\n\n"
109
113
  end
110
114
  end
@@ -123,52 +127,13 @@ class Mortar::Command::Projects < Mortar::Command::Base
123
127
  error("Usage: mortar projects:register PROJECT\nMust specify PROJECT.")
124
128
  end
125
129
  validate_arguments!
126
-
127
- if options[:public]
128
- unless confirm("Public projects allow anyone to view and fork the code in this project\'s repository. Are you sure? (y/n)")
129
- error("Mortar project was not registered")
130
- end
131
- is_private = false
132
- else
133
- is_private = true
134
- end
135
-
136
- if options[:embedded]
137
- validate_project_name(name)
138
- validate_project_structure()
139
-
140
- register_project(name, is_private) do |project_result|
141
- initialize_embedded_project(project_result)
142
- end
143
- else
144
- unless git.has_dot_git?
145
- # check if we're in the parent directory
146
- if File.exists? name
147
- error("mortar projects:register must be run from within the project directory.\nPlease \"cd #{name}\" and rerun this command.")
148
- else
149
- error("No git repository found in the current directory.\nTo register a project that is not its own git repository, use the --embedded option.\nIf you do want this project to be its own git repository, please initialize git in this directory, and then rerun the register command.\nTo initialize your project in git, use:\n\ngit init\ngit add .\ngit commit -a -m \"first commit\"")
150
- end
151
- end
152
-
153
- validate_project_name(name)
154
-
155
- unless git.remotes(git_organization).empty?
156
- begin
157
- error("Currently in project: #{project.name}. You can not register a new project inside of an existing mortar project.")
158
- rescue Mortar::Command::CommandFailed => cf
159
- error("Currently in an existing Mortar project. You can not register a new project inside of an existing mortar project.")
160
- end
161
- end
162
-
163
- register_project(name, is_private) do |project_result|
164
- git.remote_add("mortar", project_result['git_url'])
165
- git.push_master
166
- display "Your project is ready for use. Type 'mortar help' to see the commands you can perform on the project.\n\n"
167
- end
168
- end
130
+ # nil is non existant project_id because it hasn't been posted yet
131
+ register_do(name, options[:public], options[:embedded], nil)
132
+
169
133
  end
170
134
  alias_command "register", "projects:register"
171
135
 
136
+
172
137
  # projects:set_remote PROJECTNAME
173
138
  #
174
139
  # Used after you checkout code for an existing Mortar project from a non-Mortar git repository.
@@ -239,4 +204,52 @@ class Mortar::Command::Projects < Mortar::Command::Base
239
204
 
240
205
  display "\nYour project is ready for use. Type 'mortar help' to see the commands you can perform on the project.\n\n"
241
206
  end
207
+
208
+
209
+ # projects:fork GIT_URL PROJECT_NAME
210
+ #
211
+ # Used when you want to fork an existing Github repository into your own Mortar project.
212
+ #
213
+ # --public # Register a public project, which can be viewed and forked by anyone.
214
+ #
215
+ def fork
216
+ git_url = shift_argument
217
+ name = shift_argument
218
+ unless git_url and name
219
+ error("Usage: mortar projects:fork GIT_URL PROJECT\nMust specify GIT_URL and PROJECT.")
220
+ end
221
+ validate_arguments!
222
+ validate_project_name(name)
223
+
224
+ if git.has_dot_git?
225
+ begin
226
+ error("Currently in git repo. You can not fork a new project inside of an existing git repository.")
227
+ rescue Mortar::Command::CommandFailed => cf
228
+ error("Currently in git repo. You can not fork a new project inside of an existing git repository.")
229
+ end
230
+ end
231
+
232
+ if options[:public]
233
+ unless confirm("Public projects allow anyone to view and fork the code in this project\'s repository. Are you sure? (y/n)")
234
+ error("Mortar project was not registered")
235
+ end
236
+ is_public = true
237
+ else
238
+ is_public = false
239
+ end
240
+
241
+ git.clone(git_url, name, "base")
242
+ Dir.chdir(name)
243
+ # register a nil project id because it hasn't been created yet
244
+ register_project(name, is_public, nil) do |project_result|
245
+ git.remote_add("mortar", project_result['git_url'])
246
+ git.push_master
247
+ # We want the default remote to be the Mortar managed repo.
248
+ git.git("fetch --all")
249
+ git.git("branch --set-upstream-to mortar/master")
250
+ display "Your project is ready for use. Type 'mortar help' to see the commands you can perform on the project.\n\n"
251
+ end
252
+ end
253
+ alias_command "fork", "projects:fork"
254
+
242
255
  end
data/lib/mortar/git.rb CHANGED
@@ -522,8 +522,8 @@ module Mortar
522
522
  #
523
523
  # clone
524
524
  #
525
- def clone(git_url, path="")
526
- git("clone %s \"%s\"" % [git_url, path], true, false)
525
+ def clone(git_url, path="", remote_name="origin")
526
+ git("clone -o %s %s \"%s\"" % [remote_name, git_url, path], true, false)
527
527
  end
528
528
  end
529
529
  end
@@ -16,5 +16,5 @@
16
16
 
17
17
  module Mortar
18
18
  # see http://semver.org/
19
- VERSION = "0.13.2"
19
+ VERSION = "0.13.3"
20
20
  end
@@ -93,7 +93,8 @@ STDOUT
93
93
 
94
94
  context("create") do
95
95
 
96
- it "generates and registers a project" do
96
+ it "registers and generates a project" do
97
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
97
98
  mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
98
99
  project_id = "1234abcd1234abcd1234"
99
100
  project_name = "some_new_project"
@@ -125,6 +126,7 @@ STDOUT
125
126
  File.read("pigscripts/some_new_project.pig").each_line { |line| line.match(/<%.*%>/).should be_nil }
126
127
 
127
128
  stdout.should == <<-STDOUT
129
+ Sending request to register project: some_new_project... done
128
130
  \e[1;32m create\e[0m
129
131
  \e[1;32m create\e[0m README.md
130
132
  \e[1;32m create\e[0m .gitignore
@@ -159,7 +161,7 @@ STDOUT
159
161
  \e[1;32m create\e[0m vendor/udfs/jython/.gitkeep
160
162
  \e[1;32m create\e[0m vendor/udfs/java
161
163
  \e[1;32m create\e[0m vendor/udfs/java/.gitkeep
162
- Sending request to register project: some_new_project... done\n\n\r\e[0KStatus: ACTIVE \n\nYour project is ready for use. Type 'mortar help' to see the commands you can perform on the project.\n
164
+ \n\r\e[0KStatus: ACTIVE \n\nYour project is ready for use. Type 'mortar help' to see the commands you can perform on the project.\n
163
165
  NOTE: You'll need to change to the new directory to use your project:
164
166
  cd some_new_project
165
167
 
@@ -168,12 +170,14 @@ STDOUT
168
170
 
169
171
  it "generates and registers an embedded project" do
170
172
  mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
173
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
174
+
171
175
  project_id = "1234abcd1234abcd1234"
172
176
  project_name = "some_new_project"
173
177
  project_git_url = "git@github.com:mortarcode-dev/#{project_name}"
174
178
  mock(Mortar::Auth.api).post_project("some_new_project", true) {Excon::Response.new(:body => {"project_id" => project_id})}
175
179
  mock(Mortar::Auth.api).get_project(project_id).returns(Excon::Response.new(:body => {"status" => Mortar::API::Projects::STATUS_ACTIVE,
176
- "git_url" => project_git_url})).ordered
180
+ "git_url" => project_git_url})).ordered
177
181
 
178
182
  # test that sync_embedded_project is called. the method itself is tested in git_spec.
179
183
  mock(@git).sync_embedded_project.with_any_args.times(1) { true }
@@ -239,7 +243,7 @@ STDERR
239
243
  it "errors when a project already exists with the name requested" do
240
244
  mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
241
245
  with_git_initialized_project do |p|
242
- stderr, stdout = execute("projects:register Project1", nil, @git)
246
+ stderr, stdout = execute("projects:register Project1 --embedded", nil, @git)
243
247
  stderr.should == <<-STDERR
244
248
  ! Your account already contains a project named Project1.
245
249
  ! Please choose a different name for your new project, or clone the existing Project1 code using:
@@ -250,7 +254,6 @@ STDERR
250
254
  end
251
255
 
252
256
  it "show appropriate error message when user tries to register a project inside of an existing project" do
253
- mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
254
257
  with_git_initialized_project do |p|
255
258
  stderr, stdout = execute("projects:register some_new_project", nil, @git)
256
259
  stderr.should == <<-STDERR
@@ -261,6 +264,10 @@ STDERR
261
264
 
262
265
  it "Confirms if user wants to create a public project, exits if not" do
263
266
  project_name = "some_new_project"
267
+ project_id = "1234"
268
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
269
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
270
+ mock(Mortar::Auth.api).post_project("some_new_project", false) {Excon::Response.new(:body => {"project_id" => project_id})}
264
271
  any_instance_of(Mortar::Command::Projects) do |base|
265
272
  mock(base).ask.with_any_args.times(1) { 'n' }
266
273
  end
@@ -464,5 +471,72 @@ STDERR
464
471
  end
465
472
 
466
473
  end
474
+
475
+ context("fork") do
476
+ it "shows correct error message when user doesn't give git_url" do
477
+ stderr, stdout = execute("projects:fork")
478
+ stderr.should == <<-STDERR
479
+ ! Usage: mortar projects:fork GIT_URL PROJECT
480
+ ! Must specify GIT_URL and PROJECT.
481
+ STDERR
482
+ end
483
+
484
+ it "errors when a project already exists with the name requested" do
485
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
486
+ with_git_initialized_project do |p|
487
+ stderr, stdout = execute("projects:fork GIT_URL Project1", nil, @git)
488
+ stderr.should == <<-STDERR
489
+ ! Your account already contains a project named Project1.
490
+ ! Please choose a different name for your new project, or clone the existing Project1 code using:
491
+ !
492
+ ! mortar projects:clone Project1
493
+ STDERR
494
+ end
495
+ end
496
+
497
+ it "shows error when in git repo already" do
498
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
499
+
500
+ with_git_initialized_project do |p|
501
+ stderr, stdout = execute("projects:fork GIT_URL Project3", nil, @git)
502
+ stderr.should == <<-STDERR
503
+ ! Currently in git repo. You can not fork a new project inside of an existing git repository.
504
+ STDERR
505
+ end
506
+ end
507
+
508
+ it "calls correct git commands on success" do
509
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
510
+ mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
511
+ project_id = "1234abcd1234abcd1234"
512
+ project_name = "some_new_project"
513
+ project_git_url = "git@github.com:mortarcode-dev/#{project_name}"
514
+ original_git_url = "git@github.com:mortardata/mortar-recsys-fake"
515
+
516
+ mock(Mortar::Auth.api).post_project("some_new_project", false) {Excon::Response.new(:body => {"project_id" => project_id})}
517
+ status = Mortar::API::Projects::STATUS_ACTIVE
518
+ response = Excon::Response.new(:body => {"status" => status, "git_url" => project_git_url})
519
+ mock(Mortar::Auth.api).get_project(project_id).returns(response).ordered
520
+
521
+ mock(@git).has_dot_git?().returns(false)
522
+ mock(@git).clone(original_git_url, project_name, "base")
523
+ stub(Dir).chdir
524
+
525
+ mock(@git).remote_add("mortar", project_git_url)
526
+ mock(@git).push_master
527
+ mock(@git).git("fetch --all")
528
+ mock(@git).git("branch --set-upstream-to mortar/master")
529
+ any_instance_of(Mortar::Command::Projects) do |base|
530
+ mock(base).ask.with_any_args.times(1) { 'y' }
531
+ end
532
+
533
+ stderr, stdout = execute("projects:fork #{original_git_url} #{project_name} --public", nil, @git)
534
+ stdout.should == <<-STDOUT
535
+ Public projects allow anyone to view and fork the code in this project's repository. Are you sure? (y/n) Sending request to register project: some_new_project... done\n\n\r\e[0KStatus: ACTIVE \n\nYour project is ready for use. Type 'mortar help' to see the commands you can perform on the project.\n
536
+ STDOUT
537
+ end
538
+
539
+ end
540
+
467
541
  end
468
542
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mortar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.2
4
+ version: 0.13.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-21 00:00:00.000000000 Z
12
+ date: 2014-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
@@ -337,7 +337,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
337
337
  version: '0'
338
338
  requirements: []
339
339
  rubyforge_project:
340
- rubygems_version: 1.8.24
340
+ rubygems_version: 1.8.23
341
341
  signing_key:
342
342
  specification_version: 3
343
343
  summary: Client library and CLI to interact with the Mortar service.