Class: GitHubGitDownloadStrategy

Inherits:
GitDownloadStrategy show all
Defined in:
download_strategy.rb

Overview

Strategy for downloading a Git repository from GitHub.

Constant Summary

Constants inherited from VCSDownloadStrategy

VCSDownloadStrategy::REF_TYPES

Instance Attribute Summary

Attributes inherited from VCSDownloadStrategy

#last_commit

Attributes inherited from AbstractDownloadStrategy

#cache, #cached_location, #source_modified_time, #url

Instance Method Summary collapse

Methods inherited from GitDownloadStrategy

#last_commit, #source_modified_time

Methods inherited from VCSDownloadStrategy

#fetch, #fetch_last_commit, #head?

Methods inherited from AbstractDownloadStrategy

#basename, #clear_cache, #fetch, #quiet?, #shutup!, #stage

Methods included from Context

current, current=, #debug?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(url, name, version, **meta) ⇒ GitHubGitDownloadStrategy

Returns a new instance of GitHubGitDownloadStrategy.



1025
1026
1027
1028
1029
1030
1031
1032
# File 'download_strategy.rb', line 1025

def initialize(url, name, version, **meta)
  super

  return unless %r{^https?://github\.com/(?<user>[^/]+)/(?<repo>[^/]+)\.git$} =~ @url

  @user = user
  @repo = repo
end

Instance Method Details

#commit_outdated?(commit) ⇒ Boolean

Returns:

  • (Boolean)


1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
# File 'download_strategy.rb', line 1062

def commit_outdated?(commit)
  @last_commit ||= github_last_commit
  if @last_commit
    return true unless commit
    return true unless @last_commit.start_with?(commit)

    if multiple_short_commits_exist?(commit)
      true
    else
      version.update_commit(commit)
      false
    end
  else
    super
  end
end

#default_branchString

Returns:



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'download_strategy.rb', line 1089

def default_branch
  return @default_branch if defined?(@default_branch)

  command! "git",
           args:  ["remote", "set-head", "origin", "--auto"],
           chdir: cached_location

  result = command! "git",
                    args:  ["symbolic-ref", "refs/remotes/origin/HEAD"],
                    chdir: cached_location

  @default_branch = result.stdout[%r{^refs/remotes/origin/(.*)$}, 1]
end

#default_refspecString

Returns:



1080
1081
1082
1083
1084
1085
1086
# File 'download_strategy.rb', line 1080

def default_refspec
  if default_branch
    "+refs/heads/#{default_branch}:refs/remotes/origin/#{default_branch}"
  else
    super
  end
end

#github_last_commitObject



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'download_strategy.rb', line 1034

def github_last_commit
  return if Homebrew::EnvConfig.no_github_api?

  output, _, status = curl_output(
    "--silent", "--head", "--location",
    "-H", "Accept: application/vnd.github.v3.sha",
    "https://api.github.com/repos/#{@user}/#{@repo}/commits/#{@ref}"
  )

  return unless status.success?

  commit = output[/^ETag: "(\h+)"/, 1]
  version.update_commit(commit) if commit
  commit
end

#multiple_short_commits_exist?(commit) ⇒ Boolean

Returns:

  • (Boolean)


1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'download_strategy.rb', line 1050

def multiple_short_commits_exist?(commit)
  return if Homebrew::EnvConfig.no_github_api?

  output, _, status = curl_output(
    "--silent", "--head", "--location",
    "-H", "Accept: application/vnd.github.v3.sha",
    "https://api.github.com/repos/#{@user}/#{@repo}/commits/#{commit}"
  )

  !(status.success? && output && output[/^Status: (200)/, 1] == "200")
end