Skip to content

Commit 924a724

Browse files
authored
improvement: igniter.new Don't run git init if already in git repo (#328)
1 parent ff45e81 commit 924a724

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

installer/lib/mix/tasks/igniter.new.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ defmodule Mix.Tasks.Igniter.New do
393393
Igniter.Installer.Loading.with_spinner(
394394
"Initializing local git repository, staging all files, and committing",
395395
fn ->
396-
case System.cmd("git", ["init"]) do
396+
case maybe_init_git() do
397397
{_, 0} ->
398398
case System.cmd("git", ["add", "."]) do
399399
{_, 0} ->
@@ -416,6 +416,19 @@ defmodule Mix.Tasks.Igniter.New do
416416
)
417417
end
418418

419+
defp maybe_init_git() do
420+
skip_git_check? = System.get_env("IGNITER_SKIP_GIT_CHECK") == "true"
421+
422+
if skip_git_check? do
423+
System.cmd("git", ["init"])
424+
else
425+
case System.cmd("git", ["rev-parse", "--is-inside-work-tree"]) do
426+
{_, 0} -> {true, 0}
427+
{_, _} -> System.cmd("git", ["init"])
428+
end
429+
end
430+
end
431+
419432
defp maybe_warn_outdated(latest_version, opts) do
420433
if Version.compare(@installer_version, latest_version) == :lt do
421434
if opts[:yes] do

installer/test/mix/tasks/igniter.new_test.exs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule Mix.Tasks.Igniter.NewTest do
2424
"mix",
2525
["igniter.new", project_name, "--git", "--yes", "--no-installer-version-check"],
2626
stderr_to_stdout: true,
27-
env: [{"MIX_ENV", "test"}]
27+
env: [{"MIX_ENV", "test"}, {"IGNITER_SKIP_GIT_CHECK", "true"}]
2828
)
2929

3030
if exit_code != 0 do
@@ -112,6 +112,42 @@ defmodule Mix.Tasks.Igniter.NewTest do
112112
end
113113
end
114114

115+
@tag :integration
116+
test "does not run git init when already in git project", %{tmp_dir: tmp_dir} do
117+
if !git_available?() do
118+
:ok
119+
else
120+
project_parent = "parent_folder"
121+
project_name = "child_project_with_git_parent"
122+
123+
original_cwd = File.cwd!()
124+
125+
try do
126+
File.cd!(tmp_dir)
127+
File.mkdir!(project_parent)
128+
File.cd!(project_parent)
129+
System.cmd("git", ["init", "."])
130+
assert File.exists?(".git"), "Git was not successfully set up in the parent folder"
131+
132+
# Run igniter.new with --git flag using System.cmd
133+
{_output, exit_code} =
134+
System.cmd(
135+
"mix",
136+
["igniter.new", project_name, "--git", "--yes", "--no-installer-version-check"],
137+
stderr_to_stdout: true,
138+
env: [{"MIX_ENV", "test"}]
139+
)
140+
141+
assert exit_code == 0
142+
143+
File.cd!(project_name)
144+
refute File.exists?(".git"), "git should not be initialized when already in git project"
145+
after
146+
File.cd!(original_cwd)
147+
end
148+
end
149+
end
150+
115151
@tag :integration
116152
test "git functionality works with other flags", %{tmp_dir: tmp_dir} do
117153
unless git_available?() do
@@ -137,7 +173,7 @@ defmodule Mix.Tasks.Igniter.NewTest do
137173
"--no-installer-version-check"
138174
],
139175
stderr_to_stdout: true,
140-
env: [{"MIX_ENV", "test"}]
176+
env: [{"MIX_ENV", "test"}, {"IGNITER_SKIP_GIT_CHECK", "true"}]
141177
)
142178

143179
assert exit_code == 0,

0 commit comments

Comments
 (0)