Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()?;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we also have to "cd" into it (std::env::set_current_dir)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes it seems root can be set and is not always the current dir, will fix

let root = root.unwrap_or_else(|| std::env::current_dir().unwrap());

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed in 75f918a

Copy link
Member

@DaniPopes DaniPopes Dec 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that .current_dir on the clone command was fine, i meant that when we're deleting the .git folder and initializing, we have to move into in the newly cloned repo directory

sorry didn't see the last reply, ty

// 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