Class: Homebrew::PatchPuller

Inherits:
Object
  • Object
show all
Defined in:
brew/Library/Homebrew/dev-cmd/pull.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, args, description = nil) ⇒ PatchPuller

Returns a new instance of PatchPuller



314
315
316
317
318
319
320
321
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 314

def initialize(url, args, description = nil)
  @base_url = url
  # GitHub provides commits/pull-requests raw patches using this URL.
  @patch_url = url + ".patch"
  @patchpath = HOMEBREW_CACHE + File.basename(patch_url)
  @description = description
  @args = args
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url



310
311
312
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 310

def base_url
  @base_url
end

#patch_urlObject (readonly)

Returns the value of attribute patch_url



311
312
313
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 311

def patch_url
  @patch_url
end

#patchpathObject (readonly)

Returns the value of attribute patchpath



312
313
314
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 312

def patchpath
  @patchpath
end

Instance Method Details

#apply_patchObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 350

def apply_patch
  # Applies a patch previously downloaded with fetch_patch()
  # Deletes the patch file as a side effect, regardless of success

  ohai "Applying patch"
  patch_args = []
  # Normally we don't want whitespace errors, but squashing them can break
  # patches so an option is provided to skip this step.
  if @args.ignore_whitespace? || @args.clean?
    patch_args << "--whitespace=nowarn"
  else
    patch_args << "--whitespace=fix"
  end

  # Fall back to three-way merge if patch does not apply cleanly
  patch_args << "-3"
  patch_args << patchpath

  begin
    safe_system "git", "am", *patch_args
  rescue ErrorDuringExecution
    if @args.resolve?
      odie "Patch failed to apply: try to resolve it."
    else
      system "git", "am", "--abort"
      odie "Patch failed to apply: aborted."
    end
  ensure
    patchpath.unlink
  end
end

#fetch_patchObject



343
344
345
346
347
348
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 343

def fetch_patch
  extra_msg = @description ? "(#{@description})" : nil
  ohai "Fetching patch #{extra_msg}"
  puts "Patch: #{patch_url}"
  curl_download patch_url, to: patchpath
end

#pull_merge_commit(issue) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 328

def pull_merge_commit(issue)
  # Used by forks of homebrew-core that use merge-commits (for example linuxbrew)
  ohai "Fast-forwarding to the merge commit"
  test_bot_origin = patch_url[%r{(https://github\.com/[\w-]+/[\w-]+)/compare/}, 1]
  safe_system "git", "fetch", "--quiet", test_bot_origin, "pr-#{issue}" if test_bot_origin
  safe_system "git", "merge", "--quiet", "--ff-only", "--no-edit", "FETCH_HEAD"
  return if $CHILD_STATUS.success?

  safe_system "git", "reset", "--hard", "FETCH_HEAD"
  odie <<~EOS
    Not possible to fast-forward.
    Maybe somebody pushed commits to origin/master between the merge commit creation and now.
  EOS
end

#pull_patchObject



323
324
325
326
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 323

def pull_patch
  fetch_patch
  apply_patch
end