Skip to content

Commit

Permalink
fix: remove forge init template history (#4001)
Browse files Browse the repository at this point in the history
* fix: remove forge init template history

* refactor: change how .git dir is deleted, remove unneeded .current_dir usage

* fix: handle root option
  • Loading branch information
mds1 authored Jan 1, 2023
1 parent 0398a95 commit a44159a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions cli/src/cmd/forge/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,38 @@ impl Cmd for InitArgs {
}
let root = dunce::canonicalize(root)?;

// if a template is provided, then this command is just an alias to `git clone <url>
// <path>`
// 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
} else {
"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()?;

// 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)?;
std::fs::remove_dir_all(".git")?;
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()?;

// 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) {
Expand Down

0 comments on commit a44159a

Please sign in to comment.