-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Bring back support for Git :ref and :depth used together #13713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ defmodule Mix.SCM.Git do | |
|
||
@impl true | ||
def format(opts) do | ||
if rev = opts[:ref] || opts[:branch] || opts[:tag] do | ||
if rev = get_opts_rev(opts) do | ||
"#{redact_uri(opts[:git])} - #{rev}" | ||
else | ||
redact_uri(opts[:git]) | ||
|
@@ -126,17 +126,17 @@ defmodule Mix.SCM.Git do | |
update_origin(opts[:git]) | ||
|
||
# Fetch external data | ||
branch_or_tag = opts[:branch] || opts[:tag] | ||
rev = get_lock_rev(opts[:lock], opts) || get_opts_rev(opts) | ||
|
||
["--git-dir=.git", "fetch", "--force", "--quiet"] | ||
|> Kernel.++(progress_switch(git_version())) | ||
|> Kernel.++(tags_switch(opts[:tag])) | ||
|> Kernel.++(depth_switch(opts[:depth])) | ||
|> Kernel.++(if branch_or_tag, do: ["origin", branch_or_tag], else: []) | ||
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|> Kernel.++(refspec_switch(opts, rev)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would merge refspec_switch and depth_switch logic into a single function. There is too much dependency between these two and unifying it in a single place would probably be cleaner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other than moving the call to defp refspec_switch(_opts, nil), do: []
defp refspec_switch(opts, rev) do
depth_switch(opts[:depth]) ++
case Keyword.take(opts, [:depth, :branch, :tag]) do
[_ | _] -> ["origin", rev]
_ -> []
end
end
defp depth_switch(nil), do: []
defp depth_switch(n) when is_integer(n) and n > 0 do
check_depth_support(git_version())
["--depth=#{n}"]
end We only want to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm this misses the case of depth with no rev (tip of default branch, when neither of :ref, :branch, or :tag are specified)? (I think a test case would go red) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn’t that rev equals to nil, handled in the first clause? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, my code above also falls into the same mistake. The current commit handles that case with the two independent functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So which arguments would reproduce the failure? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran the tests locally for code in #13713 (comment) and #13713 (comment) (with a small fix, Both fail 5 tests (5 out of 10 in the The failure is the same, the produced repos are not shallow:
That confirms what I said in #13713 (comment) last night (I was on mobile and "ran the tests" on my head). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I got it. We need to run depth even if there is no rev. :) |
||
|> git!() | ||
|
||
# Migrate the Git repo | ||
rev = get_lock_rev(opts[:lock], opts) || opts[:ref] || branch_or_tag || default_branch() | ||
rev = rev || default_branch() | ||
git!(["--git-dir=.git", "checkout", "--quiet", rev]) | ||
|
||
if opts[:submodules] do | ||
|
@@ -219,6 +219,15 @@ defmodule Mix.SCM.Git do | |
["--depth=#{n}"] | ||
end | ||
|
||
defp refspec_switch(_opts, nil), do: [] | ||
|
||
defp refspec_switch(opts, rev) do | ||
case Keyword.take(opts, [:depth, :branch, :tag]) do | ||
[_ | _] -> ["origin", rev] | ||
_ -> [] | ||
end | ||
end | ||
|
||
## Helpers | ||
|
||
defp validate_git_options(opts) do | ||
|
@@ -249,25 +258,30 @@ defmodule Mix.SCM.Git do | |
end | ||
end | ||
|
||
@sha1_size 40 | ||
|
||
defp validate_depth(opts) do | ||
case Keyword.take(opts, [:depth, :ref]) do | ||
[_, _] -> | ||
Mix.raise( | ||
"Cannot specify :depth and :ref at the same time. " <> | ||
"Error on Git dependency: #{redact_uri(opts[:git])}" | ||
) | ||
case Keyword.take(opts, [:depth]) do | ||
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[] -> | ||
opts | ||
|
||
[{:depth, depth}] when is_integer(depth) and depth > 0 -> | ||
ref = opts[:ref] | ||
|
||
if ref && byte_size(ref) < @sha1_size do | ||
Mix.raise( | ||
"When :depth is used with :ref, a full commit hash is required. " <> | ||
"Error on Git dependency: #{redact_uri(opts[:git])}" | ||
) | ||
end | ||
|
||
[depth: depth] when is_integer(depth) and depth > 0 -> | ||
opts | ||
|
||
[depth: invalid_depth] -> | ||
invalid_depth -> | ||
Mix.raise( | ||
"The depth must be a positive integer, and be specified only once, got: #{inspect(invalid_depth)}. " <> | ||
"Error on Git dependency: #{redact_uri(opts[:git])}" | ||
) | ||
|
||
_ -> | ||
opts | ||
end | ||
end | ||
|
||
|
@@ -296,6 +310,10 @@ defmodule Mix.SCM.Git do | |
end | ||
end | ||
|
||
defp get_opts_rev(opts) do | ||
opts[:branch] || opts[:ref] || opts[:tag] | ||
end | ||
|
||
defp redact_uri(git) do | ||
case URI.parse(git) do | ||
%{userinfo: nil} -> git | ||
|
Uh oh!
There was an error while loading. Please reload this page.