Skip to content

Commit 6effbce

Browse files
authored
Don't use chdir (exercism#7908)
1 parent ffbc85b commit 6effbce

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

app/commands/user/github_solution_syncer/create_pull_request.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,18 @@ def base_sha
3636
client.branch(repo_full_name, base_branch).commit.sha
3737
rescue Octokit::NotFound
3838
# If it doesn't, then this is a naked repo, so create it.
39-
Dir.mktmpdir do |dir|
40-
@path = dir
39+
WithGitContext.() do |git|
4140
# No existing branch so create it
42-
git "init", "-b", base_branch
41+
git.("init", "-b", base_branch)
4342

4443
# Make sure we have the right user set
45-
git "config", "user.name", "Exercism's Solution Syncer Bot"
46-
git "config", "user.email", "211797793+exercism-solutions-syncer[bot]@users.noreply.github.com"
44+
git.("config", "user.name", "Exercism's Solution Syncer Bot")
45+
git.("config", "user.email", "211797793+exercism-solutions-syncer[bot]@users.noreply.github.com")
4746

4847
# Make an empty commit and push it
49-
git "commit", "--allow-empty", "-m", "Initial empty commit"
50-
git "remote", "add", "origin", repo_url
51-
git "push", "origin", base_branch
48+
git.("commit", "--allow-empty", "-m", "Initial empty commit")
49+
git.("remote", "add", "origin", repo_url)
50+
git.("push", "origin", base_branch)
5251
end
5352

5453
client.branch(repo_full_name, base_branch).commit.sha
@@ -57,12 +56,6 @@ def base_sha
5756
memoize
5857
def base_branch = client.repository(repo_full_name).default_branch
5958

60-
def git(*args)
61-
Dir.chdir(@path) do
62-
system("git", *args, exception: true)
63-
end
64-
end
65-
6659
def repo_url = "https://x-access-token:#{token}@github.com/#{repo_full_name}.git"
6760

6861
memoize

app/commands/user/github_solution_syncer/local_git_repo.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def update
1414

1515
Dir.mktmpdir do |dir|
1616
@path = dir
17+
1718
clone_repo
1819
create_branch
1920
yield self if block_given?
@@ -51,7 +52,8 @@ def base_branch_name
5152
def repo = syncer.repo_full_name
5253

5354
def clone_repo
54-
git("clone", "--depth=1", repo_url, ".")
55+
# Manually do this as we don't want the working dir logic
56+
system("git", "clone", "--depth=1", repo_url, @path, exception: true)
5557

5658
# Make sure we have the right user set
5759
git "config", "user.name", "Exercism's Solution Syncer Bot"
@@ -80,9 +82,9 @@ def push_branch
8082
end
8183

8284
def git(*args)
83-
Dir.chdir(@path) do
84-
system("git", *args, exception: true)
85-
end
85+
git_dir = File.join(@path, '.git')
86+
work_tree = @path
87+
system("git", "--git-dir=#{git_dir}", "--work-tree=#{work_tree}", *args, exception: true)
8688
end
8789

8890
def repo_url
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class User::GithubSolutionSyncer
2+
class WithGitContext
3+
include Mandate
4+
5+
def initialize(&block)
6+
@block = block
7+
end
8+
9+
def call
10+
# If it doesn't, then this is a naked repo, so create it.
11+
Dir.mktmpdir do |dir|
12+
git_dir = File.join(dir, '.git')
13+
work_tree = dir
14+
15+
git = lambda do |*args|
16+
system("git", "--git-dir=#{git_dir}", "--work-tree=#{work_tree}", *args, exception: true)
17+
end
18+
19+
block.(git)
20+
end
21+
end
22+
23+
private
24+
attr_reader :block
25+
end
26+
end

0 commit comments

Comments
 (0)