-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add support for git prepare-commit-msg hook #142
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c608f80
feat: add support for git prepare-commit-msg hook
rm-hull 80c6b18
refactor: remove unnecessary exePath from InstallHook
rm-hull 0d4221e
Merge branch 'main' of github.com:rm-hull/git-commit-summary into fea…
rm-hull fa82523
feat: prevent recursive hooks during commit
rm-hull c8ea4cb
Update internal/setup/hooks.go
rm-hull 5e4b290
Update internal/setup/hooks.go
rm-hull 7b5745d
Update internal/setup/hooks.go
rm-hull 3f4b343
fix: improve git directory path cleanup and error handling
rm-hull ebdc9bb
Merge branch 'main' into feat/hooks
rm-hull 6ee2908
Update main.go
rm-hull 919a489
Update internal/setup/hooks.go
rm-hull c62b935
Update internal/setup/hooks.go
rm-hull 5a8d46c
Update internal/setup/hooks.go
rm-hull b5a0037
Update internal/setup/hooks.go
rm-hull ccfa19e
fix: remove duplicated CommitMsgFile logic
rm-hull 99e2796
refactor: consolidate error handling into RunOptions
rm-hull 9d16fe3
refactor: update git hook to use shell path lookup
rm-hull 9ec3909
Merge branch 'main' into feat/hooks
rm-hull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package setup | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/rm-hull/git-commit-summary/internal/ui" | ||
| ) | ||
|
|
||
| func InstallHook() error { | ||
| gitDir, err := exec.Command("git", "rev-parse", "--git-dir").Output() | ||
| if err != nil { | ||
| return fmt.Errorf("not a git repository: %w", err) | ||
| } | ||
|
|
||
| gitDirStr := strings.TrimSpace(string(gitDir)) | ||
| hookPath := filepath.Join(gitDirStr, "hooks", "prepare-commit-msg") | ||
|
|
||
| if _, err := os.Stat(hookPath); err == nil { | ||
| existingContent, err := os.ReadFile(hookPath) | ||
| if err == nil && !strings.Contains(string(existingContent), "git-commit-summary") { | ||
| return fmt.Errorf("a prepare-commit-msg hook already exists at %s; please back up and remove it before installing", hookPath) | ||
| } | ||
| } | ||
|
|
||
| err = os.MkdirAll(filepath.Dir(hookPath), 0755) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create hooks directory: %w", err) | ||
| } | ||
|
|
||
| content := "#!/bin/sh\nCMD=$(which git-commit-summary)\nif [ -n \"$CMD\" ]; then\n \"$CMD\" --yolo \"$1\"\nfi\n" | ||
| err = os.WriteFile(hookPath, []byte(content), 0755) | ||
|
rm-hull marked this conversation as resolved.
|
||
| if err != nil { | ||
| return fmt.Errorf("failed to write hook file: %w", err) | ||
| } | ||
|
|
||
| fmt.Println(ui.Green.Bold(true).Render("Git hook installed successfully!")) | ||
| return nil | ||
| } | ||
|
|
||
| func UninstallHook() error { | ||
| gitDir, err := exec.Command("git", "rev-parse", "--git-dir").Output() | ||
| if err != nil { | ||
| return fmt.Errorf("not a git repository: %w", err) | ||
| } | ||
|
|
||
| gitDirStr := strings.TrimSpace(string(gitDir)) | ||
| hookPath := filepath.Join(gitDirStr, "hooks", "prepare-commit-msg") | ||
| if _, err := os.Stat(hookPath); os.IsNotExist(err) { | ||
| fmt.Println(ui.BoldYellow.Render("Hook not found, nothing to uninstall.")) | ||
| return nil | ||
| } | ||
|
|
||
| existingContent, err := os.ReadFile(hookPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read hook file: %w", err) | ||
| } | ||
| if !strings.Contains(string(existingContent), "git-commit-summary") { | ||
| fmt.Println(ui.BoldYellow.Render("Hook was not installed by git-commit-summary, skipping uninstall.")) | ||
| return nil | ||
| } | ||
|
|
||
| err = os.Remove(hookPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to remove hook file: %w", err) | ||
| } | ||
|
|
||
| fmt.Println(ui.Green.Bold(true).Render("Git hook uninstalled successfully!")) | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.