Skip to content

Commit d27313d

Browse files
authored
Merge pull request #258 from rspec/git-tools
Git tools
2 parents 4197653 + ebc5d6f commit d27313d

File tree

1 file changed

+61
-20
lines changed

1 file changed

+61
-20
lines changed

Rakefile

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Projects = ['rspec', 'rspec-core', 'rspec-expectations', 'rspec-mocks', 'rspec-r
1010
UnDocumentedProjects = %w[ rspec rspec-support ]
1111
BaseRspecPath = Pathname.new(Dir.pwd)
1212
ReposPath = BaseRspecPath.join('repos')
13+
MAX_PROJECT_NAME_LENGTH = Projects.map(&:length).max
1314

1415
def run_command(command, opts={})
1516
projects = if opts[:except]
@@ -35,17 +36,25 @@ def run_command(command, opts={})
3536
end
3637
end
3738

39+
def announce(project)
40+
puts "="*50
41+
puts "# #{project}"
42+
puts "-"*40
43+
end
44+
3845
def each_project(options = {})
39-
projects = Projects
46+
projects = options.fetch(:only, Projects)
4047
projects -= Array(options[:except])
4148

4249
projects.each do |project|
4350
Dir.chdir("repos/#{project}") do
44-
puts "="*50
45-
puts "# #{project}"
46-
puts "-"*40
47-
yield project
48-
puts
51+
if options[:silent]
52+
yield project
53+
else
54+
announce(project)
55+
yield project
56+
puts
57+
end
4958
end
5059
end
5160
end
@@ -63,14 +72,30 @@ desc "Updates the rspec.github.io docs"
6372
task :update_docs, [:version, :branch, :website_path] do |t, args|
6473
abort "You must have ag installed to generate docs" if `which ag` == ""
6574
args.with_defaults(:website_path => "../rspec.github.io")
66-
each_project :except => (UnDocumentedProjects) do |project|
67-
latest_release = `git fetch --tags && git tag | grep '^v\\\d.\\\d.\\\d$' | grep v#{args[:version]} | tail -1`
75+
76+
projects = {}
77+
skipped = []
78+
79+
$stdout.write "Checking versions..."
80+
81+
each_project :silent => true, :except => (UnDocumentedProjects) do |project|
82+
$stdout.write "\rChecking versions... #{project}"
83+
latest_release = `git fetch --tags && git tag -l "v#{args[:version]}*" | grep v#{args[:version]} | tail -1`
6884

6985
if latest_release.empty?
70-
next "No release found for #{args[:version]} in #{`pwd`}"
86+
skipped << project
87+
else
88+
projects[project] = latest_release
7189
end
90+
$stdout.write "\rChecking versions... " + (" " * MAX_PROJECT_NAME_LENGTH)
91+
end
7292

73-
`git checkout #{latest_release}`
93+
$stdout.write "\r\n"
94+
95+
abort "No projects matched #{args[:version]}" if projects.empty?
96+
97+
each_project(:only => projects.keys) do |project|
98+
`git checkout #{projects[project]}`
7499
doc_destination_path = "#{args[:website_path]}/source/documentation/#{args[:version]}/#{project}/"
75100
cmd = "bundle update && \
76101
RUBYOPT='-I#{args[:website_path]}/lib' bundle exec yard \
@@ -89,6 +114,8 @@ task :update_docs, [:version, :branch, :website_path] do |t, args|
89114
Bundler.unbundled_system %Q{ag -l href=\\"\\\(?:..\/\\\)*css #{doc_destination_path} | xargs -I{} sed #{in_place} 's/href="\\\(..\\\/\\\)*css/href="\\\/documentation\\\/#{args[:version]}\\\/#{project}\\\/css/' {}}
90115
Bundler.unbundled_system %Q{ag --html -l . #{doc_destination_path} | xargs -I{} sed #{in_place} /^[[:space:]]*$/d {}}
91116
end
117+
118+
puts "Skipped projects: (#{skipped.join(", ")}) due to no matching version." unless skipped.empty?
92119
end
93120

94121
namespace :gem do
@@ -176,16 +203,26 @@ namespace :git do
176203
sh "git add ."
177204
sh "git ci -m 'Update version to #{version}'"
178205
end
179-
force_update(branch, nil)
206+
force_update(branch, nil, false)
180207
end
181208

182-
{ :status => nil, :push => nil, :reset => '--hard', :diff => nil }.each do |command, options|
209+
{ :show => nil, :status => nil, :reset => '--hard', :diff => nil }.each do |command, options|
183210
desc "git #{command} on all the repos"
184211
task command => :clone do
185212
run_command "git #{command} #{options}".strip
186213
end
187214
end
188215

216+
desc 'git push on all the repos'
217+
task :push, :force do |t, args|
218+
branch = `git rev-parse --abbrev-ref HEAD`
219+
if args[:force]
220+
run_command "git push origin #{branch} --force-with-lease"
221+
else
222+
run_command "git push origin #{branch}"
223+
end
224+
end
225+
189226
desc 'git pull on all the repos'
190227
task :pull => [:clone] do
191228
run_command "git pull --rebase"
@@ -350,9 +387,9 @@ namespace :ci do
350387
end
351388

352389
desc "Updates the CI files and creates a PR"
353-
task :create_pr_with_updates, :custom_pr_comment do |t, args|
390+
task :create_pr_with_updates, :custom_pr_comment, :force do |t, args|
354391
opts = { except: %w[ rspec-rails ] }
355-
force_update(update_ci_files_in_repos(opts), args[:custom_pr_comment], opts)
392+
force_update(update_ci_files_in_repos(opts), args[:custom_pr_comment], args[:force] == "force", opts)
356393
end
357394
end
358395

@@ -429,8 +466,8 @@ namespace :common_plaintext_files do
429466
end
430467

431468
desc "Updates the common plaintext files files and creates a PR"
432-
task :create_pr_with_updates, :custom_pr_comment do |_t, args|
433-
force_update(update_common_plaintext_files_in_repos, args[:custom_pr_comment])
469+
task :create_pr_with_updates, :custom_pr_comment, :force do |_t, args|
470+
force_update(update_common_plaintext_files_in_repos, args[:custom_pr_comment], args[:force] == "force")
434471
end
435472
end
436473

@@ -521,12 +558,16 @@ def each_project_with_common_build(opts={}, &b)
521558
each_project(:except => except, &b)
522559
end
523560

524-
def force_update(branch, custom_pr_comment, opts={})
561+
def force_update(branch, custom_pr_comment, skip_confirmation=false, opts={})
525562
each_project_with_common_build(opts) do |name|
526563
unless system("git push origin #{branch}")
527-
puts "Push failed, force? (y/n)"
528-
if STDIN.gets.downcase =~ /^y/
529-
sh "git push origin +#{branch}"
564+
if skip_confirmation
565+
sh "git push origin #{branch} --force-with-lease"
566+
else
567+
puts "Push failed, force? (y/n)"
568+
if STDIN.gets.downcase =~ /^y/
569+
sh "git push origin +#{branch}"
570+
end
530571
end
531572
create_pull_request(name, branch, custom_pr_comment) rescue nil
532573
else

0 commit comments

Comments
 (0)