diff options
Diffstat (limited to 'Library/Homebrew/utils')
| -rw-r--r-- | Library/Homebrew/utils/formatter.rb | 10 | ||||
| -rw-r--r-- | Library/Homebrew/utils/git.rb | 28 | ||||
| -rw-r--r-- | Library/Homebrew/utils/github.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/utils/lock.sh | 9 | ||||
| -rw-r--r-- | Library/Homebrew/utils/popen.rb | 4 | ||||
| -rw-r--r-- | Library/Homebrew/utils/tty.rb | 2 |
6 files changed, 42 insertions, 13 deletions
diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb index 099b1c6d3..a29b43c8d 100644 --- a/Library/Homebrew/utils/formatter.rb +++ b/Library/Homebrew/utils/formatter.rb @@ -91,17 +91,17 @@ module Formatter output end - def pluralize(count, singular, plural = nil) - return "#{count} #{singular}" if count == 1 + def pluralize(count, singular, plural = nil, show_count: true) + return (show_count ? "#{count} #{singular}" : singular.to_s) if count == 1 - *adjectives, noun = singular.split(" ") + *adjectives, noun = singular.to_s.split(" ") plural ||= { "formula" => "formulae", }.fetch(noun, "#{noun}s") - words = adjectives << plural + words = adjectives.push(plural).join(" ") - "#{count} #{words.join(" ")}" + show_count ? "#{count} #{words}" : words end end diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb index 1b4d24894..43d93b64e 100644 --- a/Library/Homebrew/utils/git.rb +++ b/Library/Homebrew/utils/git.rb @@ -1,3 +1,31 @@ +require "open3" + +module Git + module_function + + def last_revision_commit_of_file(repo, file, before_commit: nil) + args = [before_commit.nil? ? "--skip=1" : before_commit.split("..").first] + + out, = Open3.capture3( + HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo, + "log", "--oneline", "--max-count=1", *args, "--", file + ) + out.split(" ").first + end + + def last_revision_of_file(repo, file, before_commit: nil) + relative_file = Pathname(file).relative_path_from(repo) + + commit_hash = last_revision_commit_of_file(repo, file, before_commit: before_commit) + + out, = Open3.capture3( + HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo, + "show", "#{commit_hash}:#{relative_file}" + ) + out + end +end + module Utils def self.git_available? return @git if instance_variable_defined?(:@git) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 88c5199c2..5ba381529 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -164,7 +164,7 @@ module GitHub args += ["--data", "@#{data_tmpfile.path}"] end - args += ["--dump-header", headers_tmpfile.path.to_s] + args += ["--dump-header", headers_tmpfile.path] output, errors, status = curl_output(url.to_s, *args) output, _, http_code = output.rpartition("\n") diff --git a/Library/Homebrew/utils/lock.sh b/Library/Homebrew/utils/lock.sh index 7f0638c1a..7f2bb8b73 100644 --- a/Library/Homebrew/utils/lock.sh +++ b/Library/Homebrew/utils/lock.sh @@ -38,14 +38,19 @@ _create_lock() { local lock_fd="$1" local name="$2" local ruby="/usr/bin/ruby" + local python="/usr/bin/python" [[ -x "$ruby" ]] || ruby="$(which ruby 2>/dev/null)" + [[ -x "$python" ]] || python="$(which python 2>/dev/null)" - if [[ -n "$ruby" ]] + if [[ -x "$ruby" ]] && "$ruby" -e "exit(RUBY_VERSION >= '1.8.7')" then "$ruby" -e "File.new($lock_fd).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)" - elif [[ -n "$(which flock)" ]] + elif [[ -x "$(which flock)" ]] then flock -n "$lock_fd" + elif [[ -x "$python" ]] + then + "$python" -c "import fcntl; fcntl.flock($lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)" else onoe "Cannot create $name lock, please avoid running Homebrew in parallel." fi diff --git a/Library/Homebrew/utils/popen.rb b/Library/Homebrew/utils/popen.rb index 350d9a09f..4e03711a1 100644 --- a/Library/Homebrew/utils/popen.rb +++ b/Library/Homebrew/utils/popen.rb @@ -3,10 +3,6 @@ module Utils popen(args, "rb", &block) end - def self.popen_read_text(*args, &block) - popen(args, "r", &block) - end - def self.popen_write(*args, &block) popen(args, "wb", &block) end diff --git a/Library/Homebrew/utils/tty.rb b/Library/Homebrew/utils/tty.rb index 505165dc5..642a33b91 100644 --- a/Library/Homebrew/utils/tty.rb +++ b/Library/Homebrew/utils/tty.rb @@ -6,7 +6,7 @@ module Tty end def width - `/usr/bin/tput cols`.strip.to_i + (`/bin/stty size`.split[1] || 80).to_i end def truncate(string) |
