Skip to content

Commit a44159a

Browse files
authored
fix: remove forge init template history (#4001)
* fix: remove forge init template history * refactor: change how .git dir is deleted, remove unneeded .current_dir usage * fix: handle root option
1 parent 0398a95 commit a44159a

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

cli/src/cmd/forge/init.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,38 @@ impl Cmd for InitArgs {
7070
}
7171
let root = dunce::canonicalize(root)?;
7272

73-
// if a template is provided, then this command is just an alias to `git clone <url>
74-
// <path>`
73+
// if a template is provided, then this command clones the template repo, removes the .git
74+
// folder, and initializes a new git repo—-this ensures there is no history from the
75+
// template and the template is not set as a remote.
7576
if let Some(template) = template {
7677
let template = if template.starts_with("https://") {
7778
template
7879
} else {
7980
"https://github.com/".to_string() + &template
8081
};
8182
p_println!(!quiet => "Initializing {} from {}...", root.display(), template);
83+
8284
Command::new("git")
8385
.args(["clone", "--recursive", &template, &root.display().to_string()])
8486
.exec()?;
87+
88+
// Navigate to the newly cloned repo.
89+
let initial_dir = std::env::current_dir()?;
90+
std::env::set_current_dir(&root)?;
91+
92+
// Modify the git history.
93+
let git_output =
94+
Command::new("git").args(["rev-parse", "--short", "HEAD"]).output()?.stdout;
95+
let commit_hash = String::from_utf8(git_output)?;
96+
std::fs::remove_dir_all(".git")?;
97+
Command::new("git").args(["init"]).exec()?;
98+
Command::new("git").args(["add", "--all"]).exec()?;
99+
100+
let commit_msg = format!("chore: init from {template} at {commit_hash}");
101+
Command::new("git").args(["commit", "-m", &commit_msg]).exec()?;
102+
103+
// Navigate back.
104+
std::env::set_current_dir(initial_dir)?;
85105
} else {
86106
// check if target is empty
87107
if !force && root.read_dir().map(|mut i| i.next().is_some()).unwrap_or(false) {

0 commit comments

Comments
 (0)