Skip to content

Commit a951eb8

Browse files
committed
fix(init): install post-applypatch hook
1 parent 2f0d57e commit a951eb8

File tree

7 files changed

+96
-20
lines changed

7 files changed

+96
-20
lines changed

git-branchless-opts/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ pub enum Command {
231231
#[clap(hide = true)]
232232
HookPreAutoGc,
233233

234+
/// Internal use.
235+
#[clap(hide = true)]
236+
HookPostApplypatch,
237+
234238
/// Internal use.
235239
#[clap(hide = true)]
236240
HookPostCheckout {

git-branchless/src/commands/hooks.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ pub fn hook_post_merge(effects: &Effects, _is_squash_merge: isize) -> eyre::Resu
134134
hook_post_commit_common(effects, "post-merge")
135135
}
136136

137+
/// Handle Git's `post-applypatch` hook.
138+
///
139+
/// See the man-page for `githooks(5)`.
140+
#[instrument]
141+
pub fn hook_post_applypatch(effects: &Effects) -> eyre::Result<()> {
142+
hook_post_commit_common(effects, "post-applypatch")
143+
}
144+
137145
mod reference_transaction {
138146
use std::collections::HashMap;
139147
use std::fs::File;

git-branchless/src/commands/init.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ use lib::core::repo_ext::RepoExt;
1919
use lib::git::{BranchType, Config, ConfigRead, ConfigWrite, GitRunInfo, GitVersion, Repo};
2020

2121
pub const ALL_HOOKS: &[(&str, &str)] = &[
22+
(
23+
"post-applypatch",
24+
r#"
25+
git branchless hook-post-applypatch "$@"
26+
"#,
27+
),
28+
(
29+
"post-checkout",
30+
r#"
31+
git branchless hook-post-checkout "$@"
32+
"#,
33+
),
2234
(
2335
"post-commit",
2436
r#"
@@ -35,12 +47,6 @@ git branchless hook-post-merge "$@"
3547
"post-rewrite",
3648
r#"
3749
git branchless hook-post-rewrite "$@"
38-
"#,
39-
),
40-
(
41-
"post-checkout",
42-
r#"
43-
git branchless hook-post-checkout "$@"
4450
"#,
4551
),
4652
(

git-branchless/src/commands/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ fn do_main_and_drop_locals() -> eyre::Result<i32> {
126126
ExitCode(0)
127127
}
128128

129+
Command::HookPostApplypatch => {
130+
hooks::hook_post_applypatch(&effects)?;
131+
ExitCode(0)
132+
}
133+
129134
Command::HookPostCheckout {
130135
previous_commit,
131136
current_commit,

git-branchless/tests/test_bug_report.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,55 @@ fn test_bug_report() -> eyre::Result<()> {
3737

3838
insta::assert_snapshot!(stdout, @r###"
3939
<details>
40-
<summary>Show 6 hooks</summary>
40+
<summary>Show 7 hooks</summary>
4141
42-
##### Hook `post-commit`
42+
##### Hook `post-applypatch`
4343
4444
```
4545
#!/bin/sh
4646
## START BRANCHLESS CONFIG
4747
48-
git branchless hook-post-commit "$@"
48+
git branchless hook-post-applypatch "$@"
4949
5050
## END BRANCHLESS CONFIG
5151
```
52-
##### Hook `post-merge`
52+
##### Hook `post-checkout`
5353
5454
```
5555
#!/bin/sh
5656
## START BRANCHLESS CONFIG
5757
58-
git branchless hook-post-merge "$@"
58+
git branchless hook-post-checkout "$@"
5959
6060
## END BRANCHLESS CONFIG
6161
```
62-
##### Hook `post-rewrite`
62+
##### Hook `post-commit`
6363
6464
```
6565
#!/bin/sh
6666
## START BRANCHLESS CONFIG
6767
68-
git branchless hook-post-rewrite "$@"
68+
git branchless hook-post-commit "$@"
6969
7070
## END BRANCHLESS CONFIG
7171
```
72-
##### Hook `post-checkout`
72+
##### Hook `post-merge`
7373
7474
```
7575
#!/bin/sh
7676
## START BRANCHLESS CONFIG
7777
78-
git branchless hook-post-checkout "$@"
78+
git branchless hook-post-merge "$@"
79+
80+
## END BRANCHLESS CONFIG
81+
```
82+
##### Hook `post-rewrite`
83+
84+
```
85+
#!/bin/sh
86+
## START BRANCHLESS CONFIG
87+
88+
git branchless hook-post-rewrite "$@"
7989
8090
## END BRANCHLESS CONFIG
8191
```

git-branchless/tests/test_hooks.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,46 @@ fn test_merge_commit_recorded() -> eyre::Result<()> {
345345

346346
Ok(())
347347
}
348+
349+
#[test]
350+
fn test_git_am_recorded() -> eyre::Result<()> {
351+
let git = make_git()?;
352+
git.init_repo()?;
353+
354+
git.detach_head()?;
355+
git.commit_file("test1", 1)?;
356+
git.run(&["format-patch", "HEAD^"])?;
357+
git.run(&["reset", "--hard", "HEAD^"])?;
358+
359+
{
360+
let (stdout, _stderr) = git.run(&["am", "0001-create-test1.txt.patch"])?;
361+
insta::assert_snapshot!(stdout, @r###"
362+
Applying: create test1.txt
363+
"###);
364+
}
365+
366+
{
367+
let (stdout, _stderr) = git.run(&["smartlog"])?;
368+
insta::assert_snapshot!(stdout, @r###"
369+
O f777ecc (master) create initial.txt
370+
|\
371+
| @ 047b7ad create test1.txt
372+
|
373+
o 62fc20d create test1.txt
374+
"###);
375+
}
376+
377+
git.run(&["reset", "--hard", "HEAD^"])?;
378+
{
379+
let (stdout, _stderr) = git.run(&["smartlog"])?;
380+
insta::assert_snapshot!(stdout, @r###"
381+
@ f777ecc (master) create initial.txt
382+
|\
383+
| o 047b7ad create test1.txt
384+
|
385+
o 62fc20d create test1.txt
386+
"###);
387+
}
388+
389+
Ok(())
390+
}

git-branchless/tests/test_init.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn test_old_git_version_warning() -> eyre::Result<()> {
185185
Created config file at <repo-path>/.git/branchless/config
186186
Auto-detected your main branch as: master
187187
If this is incorrect, run: git branchless init --main-branch <branch>
188-
Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
188+
Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
189189
Warning: the branchless workflow's `git undo` command requires Git
190190
v2.29 or later, but your Git version is: <git version output>
191191
@@ -225,7 +225,7 @@ fn test_init_basic() -> eyre::Result<()> {
225225
Created config file at <repo-path>/.git/branchless/config
226226
Auto-detected your main branch as: master
227227
If this is incorrect, run: git branchless init --main-branch <branch>
228-
Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
228+
Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
229229
Successfully installed git-branchless.
230230
To uninstall, run: git branchless init --uninstall
231231
"###);
@@ -264,7 +264,7 @@ fn test_init_prompt_for_main_branch() -> eyre::Result<()> {
264264
Your main branch name could not be auto-detected!
265265
Examples of a main branch: master, main, trunk, etc.
266266
See https://github.com/arxanas/git-branchless/wiki/Concepts#main-branch
267-
Enter the name of your main branch: Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
267+
Enter the name of your main branch: Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
268268
Successfully installed git-branchless.
269269
To uninstall, run: git branchless init --uninstall
270270
"###);
@@ -352,7 +352,7 @@ fn test_init_uninstall() -> eyre::Result<()> {
352352
insta::assert_snapshot!(stderr, @"");
353353
insta::assert_snapshot!(stdout, @r###"
354354
Removing config file: <repo-path>/.git/branchless/config
355-
Uninstalling hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
355+
Uninstalling hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
356356
"###);
357357
}
358358

@@ -538,7 +538,7 @@ fn test_init_core_hooks_path_warning() -> eyre::Result<()> {
538538
Created config file at <repo-path>/.git/branchless/config
539539
Auto-detected your main branch as: master
540540
If this is incorrect, run: git branchless init --main-branch <branch>
541-
Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
541+
Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
542542
Warning: the configuration value core.hooksPath was set to: my-hooks
543543
The Git hooks above may have been installed to an unexpected location.
544544
Successfully installed git-branchless.

0 commit comments

Comments
 (0)