Skip to content

Commit ebd644e

Browse files
committed
Adding cookbook site vendor support
1 parent 5f45ea2 commit ebd644e

1 file changed

Lines changed: 41 additions & 17 deletions

File tree

chef/lib/chef/knife/cookbook_site_vendor.rb

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,56 @@ def run
2828
vendor_path = File.join(Chef::Config[:cookbook_path].first)
2929
cookbook_path = File.join(vendor_path, name_args[0])
3030
upstream_file = File.join(vendor_path, "#{name_args[0]}.tar.gz")
31-
branch_name = "#{name_args[0]}-chef-upstream"
31+
branch_name = "chef-vendor-#{name_args[0]}"
3232

3333
download = Chef::Knife::CookbookSiteDownload.new
3434
download.config[:file] = upstream_file
3535
download.name_args = name_args
3636
download.run
3737

38-
Dir.chdir(vendor_path) do
39-
Chef::Log.info("Checking out the master branch")
40-
system("git checkout master")
41-
branch_output = `git branch --no-color | grep #{branch_name}`
42-
if branch_output =~ /#{branch_name}$/m
43-
system("git checkout #{branch_name}")
38+
Chef::Log.info("Checking out the master branch.")
39+
Chef::Mixin::Command.run_command(:command => "git checkout master", :cwd => vendor_path)
40+
Chef::Log.info("Checking the status of the vendor branch.")
41+
status, branch_output, branch_error = Chef::Mixin::Command.output_of_command("git branch --no-color | grep #{branch_name}", :cwd => vendor_path)
42+
if branch_output =~ /#{branch_name}$/m
43+
Chef::Log.info("Vendor branch found.")
44+
Chef::Mixin::Command.run_command(:command => "git checkout #{branch_name}", :cwd => vendor_path)
45+
else
46+
Chef::Log.info("Creating vendor branch.")
47+
Chef::Mixin::Command.run_command(:command => "git checkout -b #{branch_name}", :cwd => vendor_path)
48+
end
49+
Chef::Log.info("Removing pre-existing version.")
50+
Chef::Mixin::Command.run_command(:command => "rm -r #{cookbook_path}", :cwd => vendor_path) if File.directory?(cookbook_path)
51+
Chef::Log.info("Uncompressing #{name_args[0]} version #{download.version}.")
52+
Chef::Mixin::Command.run_command(:command => "tar zxvf #{upstream_file}", :cwd => vendor_path)
53+
Chef::Mixin::Command.run_command(:command => "rm #{upstream_file}", :cwd => vendor_path)
54+
Chef::Log.info("Adding changes.")
55+
Chef::Mixin::Command.run_command(:command => "git add #{name_args[0]}", :cwd => vendor_path)
56+
Chef::Log.info("Committing changes.")
57+
begin
58+
Chef::Mixin::Command.run_command(:command => "git commit -a -m 'Import #{name_args[0]} version #{download.version}'", :cwd => vendor_path)
59+
rescue Chef::Exceptions::Exec => e
60+
Chef::Log.warn("Checking out the master branch.")
61+
Chef::Log.warn("No changes from current vendor #{name_args[0]}, aborting!")
62+
Chef::Mixin::Command.run_command(:command => "git checkout master", :cwd => vendor_path)
63+
exit 1
64+
end
65+
Chef::Log.info("Creating tag chef-vendor-#{name_args[0]}-#{download.version}.")
66+
Chef::Mixin::Command.run_command(:command => "git tag -f #{name_args[0]}-#{download.version}", :cwd => vendor_path)
67+
Chef::Log.info("Checking out the master branch.")
68+
Chef::Mixin::Command.run_command(:command => "git checkout master", :cwd => vendor_path)
69+
Chef::Log.info("Merging changes from #{name_args[0]} version #{download.version}.")
70+
71+
Dir.chdir(vendor_path) do
72+
if system("git merge #{branch_name}")
73+
Chef::Log.info("Cookbook #{name_args[0]} version #{download.version} successfully vendored!")
74+
exit 0
4475
else
45-
system("git checkout -b #{branch_name}")
76+
Chef::Log.error("You have merge conflicts - please resolve manually!")
77+
Chef::Log.error("(Hint: cd #{vendor_path}; git status)")
78+
exit 1
4679
end
47-
system("rm -r #{cookbook_path}")
48-
system("tar zxvf #{upstream_file}")
49-
system("rm #{upstream_file}")
50-
system("git add #{name_args[0]}")
51-
system("git commit -a -m 'Import #{name_args[0]} version #{download.version}'")
52-
system("git tag -f #{name_args[0]}-#{download.version}")
53-
system("git checkout master")
54-
system("git merge #{branch_name}")
5580
end
56-
Chef::Log.info("Cookbook #{name_args[0]} version #{download.version} successfully vendored")
5781
end
5882

5983
end

0 commit comments

Comments
 (0)