From 7518d37b586023be3c5f7f5eeb7a53584da7ae70 Mon Sep 17 00:00:00 2001 From: Matt Solomon Date: Sat, 31 Dec 2022 09:56:49 -0800 Subject: [PATCH 1/3] fix: remove forge init template history --- cli/src/cmd/forge/init.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cli/src/cmd/forge/init.rs b/cli/src/cmd/forge/init.rs index 47fbd0f0bcb1d..9d482b1e5ea17 100644 --- a/cli/src/cmd/forge/init.rs +++ b/cli/src/cmd/forge/init.rs @@ -70,8 +70,9 @@ impl Cmd for InitArgs { } let root = dunce::canonicalize(root)?; - // if a template is provided, then this command is just an alias to `git clone - // ` + // if a template is provided, then this command clones the template repo, removes the .git + // folder, and initializes a new git repo—-this ensures there is no history from the + // template and the template is not set as a remote. if let Some(template) = template { let template = if template.starts_with("https://") { template @@ -79,9 +80,23 @@ impl Cmd for InitArgs { "https://github.com/".to_string() + &template }; p_println!(!quiet => "Initializing {} from {}...", root.display(), template); + Command::new("git") .args(["clone", "--recursive", &template, &root.display().to_string()]) .exec()?; + + let git_output = Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .current_dir(&root) + .output()? + .stdout; + let commit_hash = String::from_utf8(git_output)?; + Command::new("rm").args(["-rf", ".git"]).exec()?; + Command::new("git").args(["init"]).exec()?; + Command::new("git").args(["add", "--all"]).exec()?; + + let commit_msg = format!("chore: init from {template} at {commit_hash}"); + Command::new("git").args(["commit", "-m", &commit_msg]).exec()?; } else { // check if target is empty if !force && root.read_dir().map(|mut i| i.next().is_some()).unwrap_or(false) { From b8c05bf7f894b69c13ae4a55716ae1c85d53aada Mon Sep 17 00:00:00 2001 From: Matt Solomon Date: Sat, 31 Dec 2022 11:08:24 -0800 Subject: [PATCH 2/3] refactor: change how .git dir is deleted, remove unneeded .current_dir usage --- cli/src/cmd/forge/init.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cli/src/cmd/forge/init.rs b/cli/src/cmd/forge/init.rs index 9d482b1e5ea17..660bccaa21ecd 100644 --- a/cli/src/cmd/forge/init.rs +++ b/cli/src/cmd/forge/init.rs @@ -85,13 +85,10 @@ impl Cmd for InitArgs { .args(["clone", "--recursive", &template, &root.display().to_string()]) .exec()?; - let git_output = Command::new("git") - .args(["rev-parse", "--short", "HEAD"]) - .current_dir(&root) - .output()? - .stdout; + let git_output = + Command::new("git").args(["rev-parse", "--short", "HEAD"]).output()?.stdout; let commit_hash = String::from_utf8(git_output)?; - Command::new("rm").args(["-rf", ".git"]).exec()?; + std::fs::remove_dir_all(".git")?; Command::new("git").args(["init"]).exec()?; Command::new("git").args(["add", "--all"]).exec()?; From 75f918a102f85a06029691e6063ffa9531074b5d Mon Sep 17 00:00:00 2001 From: Matt Solomon Date: Sat, 31 Dec 2022 11:14:36 -0800 Subject: [PATCH 3/3] fix: handle root option --- cli/src/cmd/forge/init.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cli/src/cmd/forge/init.rs b/cli/src/cmd/forge/init.rs index 660bccaa21ecd..74425da78f3fa 100644 --- a/cli/src/cmd/forge/init.rs +++ b/cli/src/cmd/forge/init.rs @@ -85,6 +85,11 @@ impl Cmd for InitArgs { .args(["clone", "--recursive", &template, &root.display().to_string()]) .exec()?; + // Navigate to the newly cloned repo. + let initial_dir = std::env::current_dir()?; + std::env::set_current_dir(&root)?; + + // Modify the git history. let git_output = Command::new("git").args(["rev-parse", "--short", "HEAD"]).output()?.stdout; let commit_hash = String::from_utf8(git_output)?; @@ -94,6 +99,9 @@ impl Cmd for InitArgs { let commit_msg = format!("chore: init from {template} at {commit_hash}"); Command::new("git").args(["commit", "-m", &commit_msg]).exec()?; + + // Navigate back. + std::env::set_current_dir(initial_dir)?; } else { // check if target is empty if !force && root.read_dir().map(|mut i| i.next().is_some()).unwrap_or(false) {