Skip to content

Commit 6b3cf56

Browse files
authored
feat(forge): don't commit installations by default (foundry-rs#9884)
* feat(`forge`)!: flip `no_commit` to `commit` + don't commit installations by default * fix tests + always commit when initialized via template * fix * nit
1 parent e5ec47b commit 6b3cf56

File tree

8 files changed

+212
-205
lines changed

8 files changed

+212
-205
lines changed

Cargo.lock

Lines changed: 158 additions & 139 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cast/tests/cli/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ type 2
818818
blobGasPrice
819819
blobGasUsed
820820
to 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45
821-
revertReason Transaction too old, data: "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000135472616e73616374696f6e20746f6f206f6c6400000000000000000000000000"
821+
revertReason [..]Transaction too old, data: "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000135472616e73616374696f6e20746f6f206f6c6400000000000000000000000000"
822822
823823
"#]]);
824824
});

crates/cli/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ and it requires clean working and staging areas, including no untracked files.
460460
461461
Check the current git repository's status with `git status`.
462462
Then, you can track files with `git add ...` and then commit them with `git commit`,
463-
ignore them in the `.gitignore` file, or run this command again with the `--no-commit` flag."
463+
ignore them in the `.gitignore` file."
464464
))
465465
}
466466
}

crates/forge/bin/cmd/clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl CloneArgs {
130130
Self::collect_compilation_metadata(&meta, chain, address, &root, &client).await?;
131131

132132
// step 5. git add and commit the changes if needed
133-
if !install.no_commit {
133+
if install.commit {
134134
let git = Git::new(&root);
135135
git.add(Some("--all"))?;
136136
let msg = format!("chore: forge clone {address}");

crates/forge/bin/cmd/init.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct InitArgs {
4444
impl InitArgs {
4545
pub fn run(self) -> Result<()> {
4646
let Self { root, template, branch, install, offline, force, vscode } = self;
47-
let DependencyInstallOpts { shallow, no_git, no_commit } = install;
47+
let DependencyInstallOpts { shallow, no_git, commit } = install;
4848

4949
// create the root dir if it does not exist
5050
if !root.exists() {
@@ -70,17 +70,15 @@ impl InitArgs {
7070
// collapsed. gitmodules will be initialized after the template is fetched
7171
git.fetch(true, &template, branch)?;
7272

73-
if !no_commit {
74-
// reset git history to the head of the template
75-
// first get the commit hash that was fetched
76-
let commit_hash = git.commit_hash(true, "FETCH_HEAD")?;
77-
// format a commit message for the new repo
78-
let commit_msg = format!("chore: init from {template} at {commit_hash}");
79-
// get the hash of the FETCH_HEAD with the new commit message
80-
let new_commit_hash = git.commit_tree("FETCH_HEAD^{tree}", Some(commit_msg))?;
81-
// reset head of this repo to be the head of the template repo
82-
git.reset(true, new_commit_hash)?;
83-
}
73+
// reset git history to the head of the template
74+
// first get the commit hash that was fetched
75+
let commit_hash = git.commit_hash(true, "FETCH_HEAD")?;
76+
// format a commit message for the new repo
77+
let commit_msg = format!("chore: init from {template} at {commit_hash}");
78+
// get the hash of the FETCH_HEAD with the new commit message
79+
let new_commit_hash = git.commit_tree("FETCH_HEAD^{tree}", Some(commit_msg))?;
80+
// reset head of this repo to be the head of the template repo
81+
git.reset(true, new_commit_hash)?;
8482

8583
// if shallow, just initialize submodules
8684
if shallow {
@@ -102,7 +100,7 @@ impl InitArgs {
102100
}
103101

104102
// ensure git status is clean before generating anything
105-
if !no_git && !no_commit && !force && git.is_in_repo()? {
103+
if !no_git && commit && !force && git.is_in_repo()? {
106104
git.ensure_clean()?;
107105
}
108106

@@ -141,7 +139,7 @@ impl InitArgs {
141139

142140
// set up the repo
143141
if !no_git {
144-
init_git_repo(git, no_commit)?;
142+
init_git_repo(git, commit)?;
145143
}
146144

147145
// install forge-std
@@ -170,8 +168,8 @@ impl InitArgs {
170168
///
171169
/// Creates `.gitignore` and `.github/workflows/test.yml`, if they don't exist already.
172170
///
173-
/// Commits everything in `root` if `no_commit` is false.
174-
fn init_git_repo(git: Git<'_>, no_commit: bool) -> Result<()> {
171+
/// Commits everything in `root` if `commit` is true.
172+
fn init_git_repo(git: Git<'_>, commit: bool) -> Result<()> {
175173
// git init
176174
if !git.is_in_repo()? {
177175
git.init()?;
@@ -191,7 +189,7 @@ fn init_git_repo(git: Git<'_>, no_commit: bool) -> Result<()> {
191189
}
192190

193191
// commit everything
194-
if !no_commit {
192+
if commit {
195193
git.add(Some("--all"))?;
196194
git.commit("chore: forge init")?;
197195
}

crates/forge/bin/cmd/install.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ pub struct DependencyInstallOpts {
7676
#[arg(long)]
7777
pub no_git: bool,
7878

79-
/// Do not create a commit.
79+
/// Create a commit after installing the dependencies.
8080
#[arg(long)]
81-
pub no_commit: bool,
81+
pub commit: bool,
8282
}
8383

8484
impl DependencyInstallOpts {
@@ -91,12 +91,11 @@ impl DependencyInstallOpts {
9191
/// See also [`Self::install`].
9292
///
9393
/// Returns true if any dependency was installed.
94-
pub fn install_missing_dependencies(mut self, config: &mut Config) -> bool {
94+
pub fn install_missing_dependencies(self, config: &mut Config) -> bool {
9595
let lib = config.install_lib_dir();
9696
if self.git(config).has_missing_dependencies(Some(lib)).unwrap_or(false) {
9797
// The extra newline is needed, otherwise the compiler output will overwrite the message
9898
let _ = sh_println!("Missing dependencies found. Installing now...\n");
99-
self.no_commit = true;
10099
if self.install(config, Vec::new()).is_err() {
101100
let _ =
102101
sh_warn!("Your project has missing dependencies that could not be installed.");
@@ -109,7 +108,7 @@ impl DependencyInstallOpts {
109108

110109
/// Installs all dependencies
111110
pub fn install(self, config: &mut Config, dependencies: Vec<Dependency>) -> Result<()> {
112-
let Self { no_git, no_commit, .. } = self;
111+
let Self { no_git, commit, .. } = self;
113112

114113
let git = self.git(config);
115114

@@ -138,7 +137,7 @@ impl DependencyInstallOpts {
138137

139138
fs::create_dir_all(&libs)?;
140139

141-
let installer = Installer { git, no_commit };
140+
let installer = Installer { git, commit };
142141
for dep in dependencies {
143142
let path = libs.join(dep.name());
144143
let rel_path = path
@@ -157,7 +156,7 @@ impl DependencyInstallOpts {
157156
if no_git {
158157
installed_tag = installer.install_as_folder(&dep, &path)?;
159158
} else {
160-
if !no_commit {
159+
if commit {
161160
git.ensure_clean()?;
162161
}
163162
installed_tag = installer.install_as_submodule(&dep, &path)?;
@@ -173,14 +172,16 @@ impl DependencyInstallOpts {
173172
.exec()?;
174173
}
175174

176-
// update .gitmodules which is at the root of the repo,
177-
// not necessarily at the root of the current Foundry project
178-
let root = Git::root_of(git.root)?;
179-
git.root(&root).add(Some(".gitmodules"))?;
175+
if commit {
176+
// update .gitmodules which is at the root of the repo,
177+
// not necessarily at the root of the current Foundry project
178+
let root = Git::root_of(git.root)?;
179+
git.root(&root).add(Some(".gitmodules"))?;
180+
}
180181
}
181182

182183
// commit the installation
183-
if !no_commit {
184+
if commit {
184185
let mut msg = String::with_capacity(128);
185186
msg.push_str("forge install: ");
186187
msg.push_str(dep.name());
@@ -216,7 +217,7 @@ pub fn install_missing_dependencies(config: &mut Config) -> bool {
216217
#[derive(Clone, Copy, Debug)]
217218
struct Installer<'a> {
218219
git: Git<'a>,
219-
no_commit: bool,
220+
commit: bool,
220221
}
221222

222223
impl Installer<'_> {
@@ -275,7 +276,7 @@ impl Installer<'_> {
275276
std::iter::empty::<PathBuf>(),
276277
)?;
277278

278-
if !self.no_commit {
279+
if self.commit {
279280
self.git.add(Some(path))?;
280281
}
281282

@@ -522,7 +523,7 @@ mod tests {
522523
fn get_oz_tags() {
523524
let tmp = tempdir().unwrap();
524525
let git = Git::new(tmp.path());
525-
let installer = Installer { git, no_commit: true };
526+
let installer = Installer { git, commit: false };
526527

527528
git.init().unwrap();
528529

crates/forge/tests/cli/cmd.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@ forgetest!(can_detect_dirty_git_status_on_init, |prj, cmd| {
295295
fs::create_dir_all(&nested).unwrap();
296296

297297
cmd.current_dir(&nested);
298-
cmd.arg("init").assert_failure().stderr_eq(str![[r#"
298+
cmd.args(["init", "--commit"]).assert_failure().stderr_eq(str![[r#"
299299
Error: The target directory is a part of or on its own an already initialized git repository,
300300
and it requires clean working and staging areas, including no untracked files.
301301
302302
Check the current git repository's status with `git status`.
303303
Then, you can track files with `git add ...` and then commit them with `git commit`,
304-
ignore them in the `.gitignore` file, or run this command again with the `--no-commit` flag.
304+
ignore them in the `.gitignore` file.
305305
306306
"#]]);
307307

@@ -584,10 +584,10 @@ Error: git fetch exited with code 128
584584
"#]]);
585585
});
586586

587-
// checks that `forge init --template [template] work with --no-commit
587+
// checks that `forge init --template [template] works by default i.e without committing
588588
forgetest!(can_init_template_with_no_commit, |prj, cmd| {
589589
prj.wipe();
590-
cmd.args(["init", "--template", "foundry-rs/forge-template", "--no-commit"])
590+
cmd.args(["init", "--template", "foundry-rs/forge-template"])
591591
.arg(prj.root())
592592
.assert_success()
593593
.stdout_eq(str![[r#"
@@ -1308,14 +1308,13 @@ forgetest!(can_install_and_remove, |prj, cmd| {
13081308
let forge_std_mod = git_mod.join("forge-std");
13091309

13101310
let install = |cmd: &mut TestCommand| {
1311-
cmd.forge_fuse()
1312-
.args(["install", "foundry-rs/forge-std", "--no-commit"])
1313-
.assert_success()
1314-
.stdout_eq(str![[r#"
1311+
cmd.forge_fuse().args(["install", "foundry-rs/forge-std"]).assert_success().stdout_eq(
1312+
str![[r#"
13151313
Installing forge-std in [..] (url: Some("https://github.com/foundry-rs/forge-std"), tag: None)
13161314
Installed forge-std[..]
13171315
1318-
"#]]);
1316+
"#]],
1317+
);
13191318

13201319
assert!(forge_std.exists());
13211320
assert!(forge_std_mod.exists());
@@ -1376,14 +1375,13 @@ forgetest!(can_reinstall_after_manual_remove, |prj, cmd| {
13761375
let forge_std_mod = git_mod.join("forge-std");
13771376

13781377
let install = |cmd: &mut TestCommand| {
1379-
cmd.forge_fuse()
1380-
.args(["install", "foundry-rs/forge-std", "--no-commit"])
1381-
.assert_success()
1382-
.stdout_eq(str![[r#"
1378+
cmd.forge_fuse().args(["install", "foundry-rs/forge-std"]).assert_success().stdout_eq(
1379+
str![[r#"
13831380
Installing forge-std in [..] (url: Some("https://github.com/foundry-rs/forge-std"), tag: None)
13841381
Installed forge-std[..]
13851382
1386-
"#]]);
1383+
"#]],
1384+
);
13871385

13881386
assert!(forge_std.exists());
13891387
assert!(forge_std_mod.exists());
@@ -1446,7 +1444,7 @@ forgetest!(
14461444

14471445
// install main dependency
14481446
cmd.forge_fuse()
1449-
.args(["install", "evalir/forge-5980-test", "--no-commit"])
1447+
.args(["install", "evalir/forge-5980-test"])
14501448
.assert_success()
14511449
.stdout_eq(str![[r#"
14521450
Installing forge-5980-test in [..] (url: Some("https://github.com/evalir/forge-5980-test"), tag: None)

crates/forge/tests/cli/config.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,11 @@ forgetest_init!(can_parse_remappings_correctly, |prj, cmd| {
268268
assert_eq!(expected, output);
269269

270270
let install = |cmd: &mut TestCommand, dep: &str| {
271-
cmd.forge_fuse().args(["install", dep, "--no-commit"]).assert_success().stdout_eq(str![[
272-
r#"
271+
cmd.forge_fuse().args(["install", dep]).assert_success().stdout_eq(str![[r#"
273272
Installing solmate in [..] (url: Some("https://github.com/transmissions11/solmate"), tag: None)
274273
Installed solmate[..]
275274
276-
"#
277-
]]);
275+
"#]]);
278276
};
279277

280278
install(&mut cmd, "transmissions11/solmate");
@@ -694,24 +692,19 @@ forgetest!(can_update_libs_section, |prj, cmd| {
694692
// explicitly set gas_price
695693
prj.update_config(|config| config.libs = vec!["node_modules".into()]);
696694

697-
cmd.args(["install", "foundry-rs/forge-std", "--no-commit"]).assert_success().stdout_eq(str![
698-
[r#"
695+
cmd.args(["install", "foundry-rs/forge-std"]).assert_success().stdout_eq(str![[r#"
699696
Installing forge-std in [..] (url: Some("https://github.com/foundry-rs/forge-std"), tag: None)
700697
Installed forge-std[..]
701698
702-
"#]
703-
]);
699+
"#]]);
704700

705701
let config = cmd.forge_fuse().config();
706702
// `lib` was added automatically
707703
let expected = vec![PathBuf::from("node_modules"), PathBuf::from("lib")];
708704
assert_eq!(config.libs, expected);
709705

710706
// additional install don't edit `libs`
711-
cmd.forge_fuse()
712-
.args(["install", "dapphub/ds-test", "--no-commit"])
713-
.assert_success()
714-
.stdout_eq(str![[r#"
707+
cmd.forge_fuse().args(["install", "dapphub/ds-test"]).assert_success().stdout_eq(str![[r#"
715708
Installing ds-test in [..] (url: Some("https://github.com/dapphub/ds-test"), tag: None)
716709
Installed ds-test
717710
@@ -726,13 +719,11 @@ Installing ds-test in [..] (url: Some("https://github.com/dapphub/ds-test"), tag
726719
forgetest!(config_emit_warnings, |prj, cmd| {
727720
cmd.git_init();
728721

729-
cmd.args(["install", "foundry-rs/forge-std", "--no-commit"]).assert_success().stdout_eq(str![
730-
[r#"
722+
cmd.args(["install", "foundry-rs/forge-std"]).assert_success().stdout_eq(str![[r#"
731723
Installing forge-std in [..] (url: Some("https://github.com/foundry-rs/forge-std"), tag: None)
732724
Installed forge-std[..]
733725
734-
"#]
735-
]);
726+
"#]]);
736727

737728
let faulty_toml = r"[default]
738729
src = 'src'

0 commit comments

Comments
 (0)