Skip to content

Commit

Permalink
Merge pull request Homebrew#18909 from Homebrew/tap-new-multiuser
Browse files Browse the repository at this point in the history
dev-cmd/tap-new: improve handling of multi-user setups
  • Loading branch information
MikeMcQuaid authored Dec 10, 2024
2 parents ad356d3 + ccdf39f commit 03d29a5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
25 changes: 21 additions & 4 deletions Library/Homebrew/dev-cmd/tap-new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "abstract_command"
require "fileutils"
require "tap"
require "utils/uid"

module Homebrew
module DevCmd
Expand Down Expand Up @@ -172,16 +173,32 @@ def run
write_path(tap, ".github/workflows/publish.yml", actions_publish)

unless args.no_git?
cd tap.path do
cd tap.path do |path|
Utils::Git.set_name_email!
Utils::Git.setup_gpg!

# Would be nice to use --initial-branch here but it's not available in
# older versions of Git that we support.
safe_system "git", "-c", "init.defaultBranch=#{branch}", "init"
safe_system "git", "add", "--all"
safe_system "git", "commit", "-m", "Create #{tap} tap"
safe_system "git", "branch", "-m", branch

args = []
git_owner = File.stat(File.join(path, ".git")).uid
if git_owner != Process.uid && git_owner == Process.euid
# Under Homebrew user model, EUID is permitted to execute commands under the UID.
# Root users are never allowed (see brew.sh).
args << "-c" << "safe.directory=#{path}"
end

# Use the configuration of the original user, which will have author information and signing keys.
Utils::UID.drop_euid do
env = { HOME: Utils::UID.uid_home }.compact
env[:TMPDIR] = nil if (tmpdir = ENV.fetch("TMPDIR", nil)) && !File.writable?(tmpdir)
with_env(env) do
safe_system "git", *args, "add", "--all"
safe_system "git", *args, "commit", "-m", "Create #{tap} tap"
safe_system "git", *args, "branch", "-m", branch
end
end
end
end

Expand Down
13 changes: 2 additions & 11 deletions Library/Homebrew/utils/github/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,6 @@ def initialize(github_message, errors)
JSON::ParserError,
].freeze

sig { returns(T.nilable(String)) }
private_class_method def self.uid_home
require "etc"
Etc.getpwuid(Process.uid)&.dir
rescue ArgumentError
# Cover for misconfigured NSS setups
nil
end

# Gets the token from the GitHub CLI for github.com.
sig { returns(T.nilable(String)) }
def self.github_cli_token
Expand All @@ -152,7 +143,7 @@ def self.github_cli_token
# Avoid `Formula["gh"].opt_bin` so this method works even with `HOMEBREW_DISABLE_LOAD_FORMULA`.
env = {
"PATH" => PATH.new(HOMEBREW_PREFIX/"opt/gh/bin", ENV.fetch("PATH")),
"HOME" => uid_home,
"HOME" => Utils::UID.uid_home,
}.compact
gh_out, _, result = system_command "gh",
args: ["auth", "token", "--hostname", "github.com"],
Expand All @@ -173,7 +164,7 @@ def self.keychain_username_password
git_credential_out, _, result = system_command "git",
args: ["credential-osxkeychain", "get"],
input: ["protocol=https\n", "host=github.com\n"],
env: { "HOME" => uid_home }.compact,
env: { "HOME" => Utils::UID.uid_home }.compact,
print_stderr: false
return unless result.success?

Expand Down
9 changes: 9 additions & 0 deletions Library/Homebrew/utils/uid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@ def self.drop_euid(&_block)
Process::Sys.seteuid(original_euid)
end
end

sig { returns(T.nilable(String)) }
def self.uid_home
require "etc"
Etc.getpwuid(Process.uid)&.dir
rescue ArgumentError
# Cover for misconfigured NSS setups
nil
end
end
end

0 comments on commit 03d29a5

Please sign in to comment.