Skip to content

Commit e254e7e

Browse files
committed
Fix regression when fetching repo with a ref
Also note that depth and ref cannot be used in combination.
1 parent d3c0b38 commit e254e7e

File tree

3 files changed

+19
-53
lines changed

3 files changed

+19
-53
lines changed

Diff for: lib/mix/lib/mix/scm/git.ex

+17-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Mix.SCM.Git do
99

1010
@impl true
1111
def format(opts) do
12-
if rev = get_opts_rev(opts) do
12+
if rev = opts[:ref] || opts[:branch] || opts[:tag] do
1313
"#{redact_uri(opts[:git])} - #{rev}"
1414
else
1515
redact_uri(opts[:git])
@@ -22,7 +22,7 @@ defmodule Mix.SCM.Git do
2222
{:git, _, lock_rev, lock_opts} ->
2323
lock = String.slice(lock_rev, 0, 7)
2424

25-
case Enum.find_value([:branch, :ref, :tag], &List.keyfind(lock_opts, &1, 0)) do
25+
case Enum.find_value([:ref, :branch, :tag], &List.keyfind(lock_opts, &1, 0)) do
2626
{:ref, _} -> lock <> " (ref)"
2727
{key, val} -> lock <> " (#{key}: #{val})"
2828
nil -> lock
@@ -125,18 +125,18 @@ defmodule Mix.SCM.Git do
125125
sparse_toggle(opts)
126126
update_origin(opts[:git])
127127

128-
rev = get_lock_rev(opts[:lock], opts) || get_opts_rev(opts)
129-
130128
# Fetch external data
129+
branch_or_tag = opts[:branch] || opts[:tag]
130+
131131
["--git-dir=.git", "fetch", "--force", "--quiet"]
132132
|> Kernel.++(progress_switch(git_version()))
133133
|> Kernel.++(tags_switch(opts[:tag]))
134134
|> Kernel.++(depth_switch(opts[:depth]))
135-
|> Kernel.++(if rev, do: ["origin", rev], else: [])
135+
|> Kernel.++(if branch_or_tag, do: ["origin", branch_or_tag], else: [])
136136
|> git!()
137137

138138
# Migrate the Git repo
139-
rev = rev || default_branch()
139+
rev = get_lock_rev(opts[:lock], opts) || opts[:ref] || branch_or_tag || default_branch()
140140
git!(["--git-dir=.git", "checkout", "--quiet", rev])
141141

142142
if opts[:submodules] do
@@ -250,18 +250,24 @@ defmodule Mix.SCM.Git do
250250
end
251251

252252
defp validate_depth(opts) do
253-
case Keyword.take(opts, [:depth]) do
254-
[] ->
255-
opts
253+
case Keyword.take(opts, [:depth, :ref]) do
254+
[_, _] ->
255+
Mix.raise(
256+
"Cannot specify :depth and :ref at the same time. " <>
257+
"Error on Git dependency: #{redact_uri(opts[:git])}"
258+
)
256259

257-
[{:depth, depth}] when is_integer(depth) and depth > 0 ->
260+
[depth: depth] when is_integer(depth) and depth > 0 ->
258261
opts
259262

260-
invalid_depth ->
263+
[depth: invalid_depth] ->
261264
Mix.raise(
262265
"The depth must be a positive integer, and be specified only once, got: #{inspect(invalid_depth)}. " <>
263266
"Error on Git dependency: #{redact_uri(opts[:git])}"
264267
)
268+
269+
_ ->
270+
opts
265271
end
266272
end
267273

@@ -290,10 +296,6 @@ defmodule Mix.SCM.Git do
290296
end
291297
end
292298

293-
defp get_opts_rev(opts) do
294-
opts[:branch] || opts[:ref] || opts[:tag]
295-
end
296-
297299
defp redact_uri(git) do
298300
case URI.parse(git) do
299301
%{userinfo: nil} -> git

Diff for: lib/mix/lib/mix/tasks/deps.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ defmodule Mix.Tasks.Deps do
125125
* `:depth` *(since v1.17.0)* - creates a shallow clone of the Git repository,
126126
limiting the history to the specified number of commits. This can significantly
127127
improve clone speed for large repositories when full history is not needed.
128-
The value must be a positive integer, typically `1`.
128+
The value must be a positive integer, typically `1`. Cannot be used with the
129+
`:ref` option.
129130
130131
If your Git repository requires authentication, such as basic username:password
131132
HTTP authentication via URLs, it can be achieved via Git configuration, keeping

Diff for: lib/mix/test/mix/tasks/deps.git_test.exs

-37
Original file line numberDiff line numberDiff line change
@@ -530,43 +530,6 @@ defmodule Mix.Tasks.DepsGitTest do
530530
end)
531531
end
532532

533-
test "with ref" do
534-
[last, _ | _] = get_git_repo_revs("git_repo")
535-
536-
Process.put(:git_repo_opts, depth: 1, ref: last)
537-
538-
in_fixture("no_mixfile", fn ->
539-
Mix.Project.push(GitApp)
540-
541-
Mix.Tasks.Deps.Get.run([])
542-
message = "* Getting git_repo (#{fixture_path("git_repo")} - #{last})"
543-
assert_received {:mix_shell, :info, [^message]}
544-
assert_shallow("deps/git_repo", 1)
545-
end)
546-
end
547-
548-
test "changing refspec updates retaining depth" do
549-
[last, first | _] = get_git_repo_revs("git_repo")
550-
551-
Process.put(:git_repo_opts, ref: first, depth: 1)
552-
553-
in_fixture("no_mixfile", fn ->
554-
Mix.Project.push(GitApp)
555-
556-
Mix.Tasks.Deps.Get.run([])
557-
message = "* Getting git_repo (#{fixture_path("git_repo")} - #{first})"
558-
assert_received {:mix_shell, :info, [^message]}
559-
assert_shallow("deps/git_repo", 1)
560-
assert File.read!("mix.lock") =~ first
561-
562-
# Change refspec
563-
update_dep(ref: last, depth: 1)
564-
Mix.Tasks.Deps.Get.run([])
565-
assert_shallow("deps/git_repo", 1)
566-
assert File.read!("mix.lock") =~ last
567-
end)
568-
end
569-
570533
test "removing depth retains shallow repository" do
571534
# For compatibility and simplicity, we follow Git's behavior and do not
572535
# attempt to unshallow an existing repository. This should not be a

0 commit comments

Comments
 (0)