2016-09-08 09:05:00 +01:00
#: @hide_from_man_page
2016-10-30 15:40:11 -04:00
#: * `boneyard-formula-pr` [`--dry-run`] [`--local`] [`--reason=<reason>`] <formula-name> :
2016-09-08 09:05:00 +01:00
#: Creates a pull request to boneyard a formula.
#:
#: If `--dry-run` is passed, print what would be done rather than doing it.
#:
#: If `--local` is passed, perform only local operations (i.e. don't push or create PR).
2016-10-30 15:40:11 -04:00
#:
#: If `--reason=<reason>` is passed, append this to the commit/PR message.
2016-06-08 12:21:11 -07:00
require " formula "
2016-11-20 13:00:01 -05:00
require " json "
2016-06-08 12:21:11 -07:00
require " fileutils "
begin
require " json "
rescue LoadError
puts " Homebrew does not provide Ruby dependencies; install with: "
puts " gem install json "
odie " Dependency json is not installed. "
end
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2016-06-08 12:21:11 -07:00
def boneyard_formula_pr
2016-06-20 17:32:12 +02:00
local_only = ARGV . include? ( " --local " )
2016-06-08 12:21:11 -07:00
formula = ARGV . formulae . first
2016-10-30 15:40:11 -04:00
reason = ARGV . value ( " reason " )
2016-06-08 12:21:11 -07:00
odie " No formula found! " unless formula
formula_relpath = formula . path . relative_path_from ( formula . tap . path )
formula_file = " #{ formula . name } .rb "
bottle_block = File . read ( formula . path ) . include? " bottle do "
boneyard_tap = Tap . fetch ( " homebrew " , " boneyard " )
tap_migrations_path = formula . tap . path / " tap_migrations.json "
if ARGV . dry_run?
2016-12-06 15:14:12 -05:00
ohai " brew update "
ohai " brew tap #{ boneyard_tap . name } "
ohai " cd #{ formula . tap . path } "
2016-06-08 12:21:11 -07:00
cd formula . tap . path
2016-12-06 15:14:12 -05:00
ohai " cp #{ formula_relpath } #{ boneyard_tap . path } "
ohai " git rm #{ formula_relpath } "
2016-06-08 12:21:11 -07:00
unless File . exist? tap_migrations_path
2016-12-06 15:14:12 -05:00
ohai " Creating tap_migrations.json for #{ formula . tap . name } "
ohai " git add #{ tap_migrations_path } "
2016-06-08 12:21:11 -07:00
end
2016-12-06 15:14:12 -05:00
ohai " Loading tap_migrations.json "
ohai " Adding #{ formula . name } to tap_migrations.json "
2016-06-08 12:21:11 -07:00
else
safe_system HOMEBREW_BREW_FILE , " update "
safe_system HOMEBREW_BREW_FILE , " tap " , boneyard_tap . name
cd formula . tap . path
cp formula_relpath , boneyard_tap . formula_dir
safe_system " git " , " rm " , formula_relpath
unless File . exist? tap_migrations_path
tap_migrations_path . write <<-EOS.undent
{
}
EOS
safe_system " git " , " add " , tap_migrations_path
end
2016-11-20 13:00:01 -05:00
tap_migrations = JSON . parse ( File . read ( tap_migrations_path ) )
2016-06-08 12:21:11 -07:00
tap_migrations [ formula . name ] = boneyard_tap . name
2016-11-13 23:36:04 +01:00
tap_migrations = tap_migrations . sort . inject ( { } ) { | acc , elem | acc . merge! ( elem [ 0 ] = > elem [ 1 ] ) }
2016-06-08 12:21:11 -07:00
tap_migrations_path . atomic_write ( JSON . pretty_generate ( tap_migrations ) + " \n " )
end
2016-06-20 19:58:20 +02:00
unless which ( " hub " ) || local_only
2016-06-08 12:21:11 -07:00
if ARGV . dry_run?
2016-12-06 15:14:12 -05:00
ohai " brew install hub "
2016-06-08 12:21:11 -07:00
else
safe_system HOMEBREW_BREW_FILE , " install " , " hub "
end
end
branch = " #{ formula . name } -boneyard "
2016-10-30 15:40:11 -04:00
reason = " because #{ reason } " if reason
2016-06-08 12:21:11 -07:00
if ARGV . dry_run?
2016-12-06 15:14:12 -05:00
ohai " cd #{ formula . tap . path } "
ohai " git checkout --no-track -b #{ branch } origin/master "
ohai " git commit --no-edit --verbose --message= \" #{ formula . name } : migrate to boneyard \" -- #{ formula_relpath } #{ tap_migrations_path . basename } "
2016-06-20 17:32:12 +02:00
unless local_only
2016-12-06 15:14:12 -05:00
ohai " hub fork --no-remote "
ohai " hub fork "
ohai " hub fork (to read $HUB_REMOTE) "
ohai " git push $HUB_REMOTE #{ branch } : #{ branch } "
ohai " hub pull-request -m $' #{ formula . name } : migrate to boneyard \\ n \\ nCreated with `brew boneyard-formula-pr` #{ reason } .' "
2016-06-20 17:32:12 +02:00
end
2016-12-06 15:18:53 -05:00
ohai " git checkout - "
2016-06-08 12:21:11 -07:00
else
cd formula . tap . path
2016-06-20 17:40:54 +02:00
safe_system " git " , " checkout " , " --no-track " , " -b " , branch , " origin/master "
2016-06-08 12:21:11 -07:00
safe_system " git " , " commit " , " --no-edit " , " --verbose " ,
" --message= #{ formula . name } : migrate to boneyard " ,
" -- " , formula_relpath , tap_migrations_path . basename
2016-06-20 17:32:12 +02:00
unless local_only
safe_system " hub " , " fork " , " --no-remote "
quiet_system " hub " , " fork "
2016-07-13 16:19:51 +08:00
remote = Utils . popen_read ( " hub fork 2>&1 " ) [ / fatal: remote (.+) already exists \ . / , 1 ]
2016-06-20 17:32:12 +02:00
odie " cannot get remote from 'hub'! " unless remote
safe_system " git " , " push " , remote , " #{ branch } : #{ branch } "
pr_message = <<-EOS.undent
#{formula.name}: migrate to boneyard
2016-10-30 15:40:11 -04:00
Created with ` brew boneyard-formula-pr ` #{reason}.
2016-06-20 17:32:12 +02:00
EOS
pr_url = Utils . popen_read ( " hub " , " pull-request " , " -m " , pr_message ) . chomp
end
2016-12-06 15:18:53 -05:00
safe_system " git " , " checkout " , " - "
2016-06-08 12:21:11 -07:00
end
if ARGV . dry_run?
2016-12-06 15:14:12 -05:00
ohai " cd #{ boneyard_tap . path } "
ohai " git checkout --no-track -b #{ branch } origin/master "
2016-06-08 12:21:11 -07:00
if bottle_block
2016-12-06 15:14:12 -05:00
ohai " Removing bottle block "
2016-06-08 12:21:11 -07:00
else
2016-12-06 15:14:12 -05:00
ohai " No bottle block to remove "
2016-06-08 12:21:11 -07:00
end
2016-12-06 15:14:12 -05:00
ohai " git add #{ formula_file } "
ohai " git commit --no-edit --verbose --message= \" #{ formula . name } : migrate from #{ formula . tap . repo } \" -- #{ formula_file } "
2016-06-20 17:32:12 +02:00
unless local_only
2016-12-06 15:14:12 -05:00
ohai " hub fork --no-remote "
ohai " hub fork "
ohai " hub fork (to read $HUB_REMOTE) "
ohai " git push $HUB_REMOTE #{ branch } : #{ branch } "
ohai " hub pull-request --browse -m $' #{ formula . name } : migrate from #{ formula . tap . repo } \\ n \\ nGoes together with $PR_URL \\ n \\ nCreated with `brew boneyard-formula-pr` #{ reason } .' "
2016-06-20 17:32:12 +02:00
end
2016-12-06 15:18:53 -05:00
ohai " git checkout - "
2016-06-08 12:21:11 -07:00
else
cd boneyard_tap . formula_dir
2016-06-20 17:40:54 +02:00
safe_system " git " , " checkout " , " --no-track " , " -b " , branch , " origin/master "
2016-06-08 12:21:11 -07:00
if bottle_block
Utils :: Inreplace . inreplace formula_file , / bottle do . + ? end \ n \ n / m , " "
end
safe_system " git " , " add " , formula_file
safe_system " git " , " commit " , " --no-edit " , " --verbose " ,
" --message= #{ formula . name } : migrate from #{ formula . tap . repo } " ,
" -- " , formula_file
2016-06-20 17:32:12 +02:00
unless local_only
safe_system " hub " , " fork " , " --no-remote "
quiet_system " hub " , " fork "
2016-07-13 16:19:51 +08:00
remote = Utils . popen_read ( " hub fork 2>&1 " ) [ / fatal: remote (.+) already exists \ . / , 1 ]
2016-06-20 17:32:12 +02:00
odie " cannot get remote from 'hub'! " unless remote
safe_system " git " , " push " , remote , " #{ branch } : #{ branch } "
safe_system " hub " , " pull-request " , " --browse " , " -m " , <<-EOS.undent
#{formula.name}: migrate from #{formula.tap.repo}
Goes together with #{pr_url}.
2016-06-08 12:21:11 -07:00
2016-10-30 15:40:11 -04:00
Created with ` brew boneyard-formula-pr ` #{reason}.
2016-06-20 17:32:12 +02:00
EOS
end
2016-12-06 15:18:53 -05:00
safe_system " git " , " checkout " , " - "
2016-06-08 12:21:11 -07:00
end
end
end