Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(init): install post-applypatch hook #724

Merged
merged 2 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions git-branchless-opts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ pub enum Command {
#[clap(hide = true)]
HookPreAutoGc,

/// Internal use.
#[clap(hide = true)]
HookPostApplypatch,

/// Internal use.
#[clap(hide = true)]
HookPostCheckout {
Expand Down
8 changes: 8 additions & 0 deletions git-branchless/src/commands/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ pub fn hook_post_merge(effects: &Effects, _is_squash_merge: isize) -> eyre::Resu
hook_post_commit_common(effects, "post-merge")
}

/// Handle Git's `post-applypatch` hook.
///
/// See the man-page for `githooks(5)`.
#[instrument]
pub fn hook_post_applypatch(effects: &Effects) -> eyre::Result<()> {
hook_post_commit_common(effects, "post-applypatch")
}

mod reference_transaction {
use std::collections::HashMap;
use std::fs::File;
Expand Down
18 changes: 12 additions & 6 deletions git-branchless/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ use lib::core::repo_ext::RepoExt;
use lib::git::{BranchType, Config, ConfigRead, ConfigWrite, GitRunInfo, GitVersion, Repo};

pub const ALL_HOOKS: &[(&str, &str)] = &[
(
"post-applypatch",
r#"
git branchless hook-post-applypatch "$@"
"#,
),
(
"post-checkout",
r#"
git branchless hook-post-checkout "$@"
"#,
),
(
"post-commit",
r#"
Expand All @@ -35,12 +47,6 @@ git branchless hook-post-merge "$@"
"post-rewrite",
r#"
git branchless hook-post-rewrite "$@"
"#,
),
(
"post-checkout",
r#"
git branchless hook-post-checkout "$@"
"#,
),
(
Expand Down
5 changes: 5 additions & 0 deletions git-branchless/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ fn do_main_and_drop_locals() -> eyre::Result<i32> {
ExitCode(0)
}

Command::HookPostApplypatch => {
hooks::hook_post_applypatch(&effects)?;
ExitCode(0)
}

Command::HookPostCheckout {
previous_commit,
current_commit,
Expand Down
28 changes: 19 additions & 9 deletions git-branchless/tests/test_bug_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,55 @@ fn test_bug_report() -> eyre::Result<()> {

insta::assert_snapshot!(stdout, @r###"
<details>
<summary>Show 6 hooks</summary>
<summary>Show 7 hooks</summary>

##### Hook `post-commit`
##### Hook `post-applypatch`

```
#!/bin/sh
## START BRANCHLESS CONFIG

git branchless hook-post-commit "$@"
git branchless hook-post-applypatch "$@"

## END BRANCHLESS CONFIG
```
##### Hook `post-merge`
##### Hook `post-checkout`

```
#!/bin/sh
## START BRANCHLESS CONFIG

git branchless hook-post-merge "$@"
git branchless hook-post-checkout "$@"

## END BRANCHLESS CONFIG
```
##### Hook `post-rewrite`
##### Hook `post-commit`

```
#!/bin/sh
## START BRANCHLESS CONFIG

git branchless hook-post-rewrite "$@"
git branchless hook-post-commit "$@"

## END BRANCHLESS CONFIG
```
##### Hook `post-checkout`
##### Hook `post-merge`

```
#!/bin/sh
## START BRANCHLESS CONFIG

git branchless hook-post-checkout "$@"
git branchless hook-post-merge "$@"

## END BRANCHLESS CONFIG
```
##### Hook `post-rewrite`

```
#!/bin/sh
## START BRANCHLESS CONFIG

git branchless hook-post-rewrite "$@"

## END BRANCHLESS CONFIG
```
Expand Down
43 changes: 43 additions & 0 deletions git-branchless/tests/test_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,46 @@ fn test_merge_commit_recorded() -> eyre::Result<()> {

Ok(())
}

#[test]
fn test_git_am_recorded() -> eyre::Result<()> {
let git = make_git()?;
git.init_repo()?;

git.detach_head()?;
git.commit_file("test1", 1)?;
git.run(&["format-patch", "HEAD^"])?;
git.run(&["reset", "--hard", "HEAD^"])?;

{
let (stdout, _stderr) = git.run(&["am", "0001-create-test1.txt.patch"])?;
insta::assert_snapshot!(stdout, @r###"
Applying: create test1.txt
"###);
}

{
let (stdout, _stderr) = git.run(&["smartlog"])?;
insta::assert_snapshot!(stdout, @r###"
O f777ecc (master) create initial.txt
|\
| @ 047b7ad create test1.txt
|
o 62fc20d create test1.txt
"###);
}

git.run(&["reset", "--hard", "HEAD^"])?;
{
let (stdout, _stderr) = git.run(&["smartlog"])?;
insta::assert_snapshot!(stdout, @r###"
@ f777ecc (master) create initial.txt
|\
| o 047b7ad create test1.txt
|
o 62fc20d create test1.txt
"###);
}

Ok(())
}
10 changes: 5 additions & 5 deletions git-branchless/tests/test_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn test_old_git_version_warning() -> eyre::Result<()> {
Created config file at <repo-path>/.git/branchless/config
Auto-detected your main branch as: master
If this is incorrect, run: git branchless init --main-branch <branch>
Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
Warning: the branchless workflow's `git undo` command requires Git
v2.29 or later, but your Git version is: <git version output>

Expand Down Expand Up @@ -225,7 +225,7 @@ fn test_init_basic() -> eyre::Result<()> {
Created config file at <repo-path>/.git/branchless/config
Auto-detected your main branch as: master
If this is incorrect, run: git branchless init --main-branch <branch>
Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
Successfully installed git-branchless.
To uninstall, run: git branchless init --uninstall
"###);
Expand Down Expand Up @@ -264,7 +264,7 @@ fn test_init_prompt_for_main_branch() -> eyre::Result<()> {
Your main branch name could not be auto-detected!
Examples of a main branch: master, main, trunk, etc.
See https://github.com/arxanas/git-branchless/wiki/Concepts#main-branch
Enter the name of your main branch: Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
Enter the name of your main branch: Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
Successfully installed git-branchless.
To uninstall, run: git branchless init --uninstall
"###);
Expand Down Expand Up @@ -352,7 +352,7 @@ fn test_init_uninstall() -> eyre::Result<()> {
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stdout, @r###"
Removing config file: <repo-path>/.git/branchless/config
Uninstalling hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
Uninstalling hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
"###);
}

Expand Down Expand Up @@ -538,7 +538,7 @@ fn test_init_core_hooks_path_warning() -> eyre::Result<()> {
Created config file at <repo-path>/.git/branchless/config
Auto-detected your main branch as: master
If this is incorrect, run: git branchless init --main-branch <branch>
Installing hooks: post-commit, post-merge, post-rewrite, post-checkout, pre-auto-gc, reference-transaction
Installing hooks: post-applypatch, post-checkout, post-commit, post-merge, post-rewrite, pre-auto-gc, reference-transaction
Warning: the configuration value core.hooksPath was set to: my-hooks
The Git hooks above may have been installed to an unexpected location.
Successfully installed git-branchless.
Expand Down
5 changes: 4 additions & 1 deletion git-branchless/tests/test_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ fn remove_nondeterministic_lines(output: String) -> String {
!line.contains("Fetching")
// This line is produced in a different order in some Git versions.
&& !line.contains("Your branch is up to date")
// This line is only sometimes produced in CI for some reason? I
// don't understand how it would only sometimes print this
// message, but it does.
&& !line.contains("Switched to branch")
})
.map(|line| format!("{line}\n"))
.collect()
Expand Down Expand Up @@ -369,7 +373,6 @@ fn test_sync_no_delete_main_branch() -> eyre::Result<()> {
branchless: processing 2 updates: branch master, branch should-be-deleted
branchless: creating working copy snapshot
branchless: running command: <git-executable> checkout master
Switched to branch 'master'
branchless: processing checkout
:
@ 96d1c37 (> master) create test2.txt
Expand Down