Skip to content

Commit

Permalink
Merge pull request #63 from square/tsutton/dont-clear-cache-if-auth-e…
Browse files Browse the repository at this point in the history
…rror

Don't clear cache if an error was authentication-related
  • Loading branch information
timsutton authored Nov 15, 2023
2 parents 3d50ec4 + d734927 commit 04014b9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby-version: ["2.7.4", "3.1.3"]
ruby-version: ["2.7.4", "3.0.6", "3.1.4", "3.2.2", "3.3.0-preview3"]
experimental: [false]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
Expand Down
47 changes: 26 additions & 21 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
PATH
remote: .
specs:
git-fastclone (1.4.3)
git-fastclone (1.4.4)
colorize

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
colorize (0.8.1)
colorize (1.1.0)
diff-lcs (1.5.0)
json (2.6.3)
parallel (1.22.1)
parser (3.1.3.0)
language_server-protocol (3.17.0.3)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
racc (1.7.3)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.6.1)
rexml (3.2.5)
rake (13.1.0)
regexp_parser (2.8.2)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.39.0)
rspec-support (3.12.1)
rubocop (1.57.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.1.2.1)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.23.0, < 2.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.24.0)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
unicode-display_width (2.3.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)

PLATFORMS
ruby
arm64-darwin-23
x86_64-linux

DEPENDENCIES
bundler
Expand All @@ -57,4 +62,4 @@ DEPENDENCIES
rubocop

BUNDLED WITH
2.3.26
2.4.20
14 changes: 11 additions & 3 deletions lib/git-fastclone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def initialize
def run
url, path, options = parse_inputs

require_relative './git-fastclone/version'
require_relative 'git-fastclone/version'
msg = "git-fastclone #{GitFastCloneVersion::VERSION}"
if color
puts msg.yellow
Expand Down Expand Up @@ -354,10 +354,17 @@ def store_updated_repo(url, mirror, repo_name, fail_hard)
# To avoid corruption of the cache, if we failed to update or check out we remove
# the cache directory entirely. This may cause the current clone to fail, but if the
# underlying error from git is transient it will not affect future clones.
clear_cache(mirror, url)
#
# The only exception to this is authentication failures, because they are transient,
# usually due to either a remote server outage or a local credentials config problem.
clear_cache(mirror, url) unless auth_error?(e.output)
raise e if fail_hard
end

def auth_error?(error)
error.to_s =~ /.*^fatal: Authentication failed/m
end

def retriable_error?(error)
error_strings = [
/^fatal: missing blob object/,
Expand All @@ -366,7 +373,8 @@ def retriable_error?(error)
/^fatal: pack has \d+ unresolved delta/,
/^error: unable to read sha1 file of /,
/^fatal: did not receive expected object/,
/^fatal: unable to read tree [a-z0-9]+\n^warning: Clone succeeded, but checkout failed/
/^fatal: unable to read tree [a-z0-9]+\n^warning: Clone succeeded, but checkout failed/,
/^fatal: Authentication failed/
]
error.to_s =~ /.*#{Regexp.union(error_strings)}/m
end
Expand Down
2 changes: 1 addition & 1 deletion lib/git-fastclone/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# Version string for git-fastclone
module GitFastCloneVersion
VERSION = '1.4.3'
VERSION = '1.4.4'
end
14 changes: 13 additions & 1 deletion spec/git_fastclone_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def create_lockfile_double

describe '.store_updated_repo' do
context 'when fail_hard is true' do
it 'should raise a Runtime error and clear cache' do
it 'should raise a Runtime error and clear cache if there were no authentication errors' do
status = double('status')
allow(status).to receive(:exitstatus).and_return(1)
ex = RunnerExecution::RunnerExecutionRuntimeError.new(status, 'cmd')
Expand All @@ -318,6 +318,18 @@ def create_lockfile_double
subject.store_updated_repo(placeholder_arg, placeholder_arg, placeholder_arg, true)
end.to raise_error(ex)
end

it 'should raise a Runtime error and skip clearing the cache if there were authentication errors' do
status = double('status')
allow(status).to receive(:exitstatus).and_return(1)
ex = RunnerExecution::RunnerExecutionRuntimeError.new(status, 'cmd')
allow(ex).to receive(:output).and_return('fatal: Authentication failed')
allow(subject).to receive(:fail_on_error) { raise ex }
expect(FileUtils).to_not receive(:remove_entry_secure).with(placeholder_arg, force: true)
expect do
subject.store_updated_repo(placeholder_arg, placeholder_arg, placeholder_arg, true)
end.to raise_error(ex)
end
end

context 'when fail_hard is false' do
Expand Down

0 comments on commit 04014b9

Please sign in to comment.