Skip to content

Commit

Permalink
Remove need for commit-msg hook
Browse files Browse the repository at this point in the history
instead use rebase with reword helper to add missing commit-ids

fixes #267, #179

commit-id:fcc60e1a
  • Loading branch information
ejoffe authored and Eitan Joffe committed Aug 17, 2022
1 parent 08062a5 commit e7d2462
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 151 deletions.
10 changes: 0 additions & 10 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ builds:
- darwin
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=gorelease
- main: ./cmd/commithook
id: commithook
binary: spr_commit_hook
goos:
- linux
- windows
- darwin
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=gorelease
- main: ./cmd/reword
id: reword
binary: spr_reword_helper
Expand Down Expand Up @@ -72,7 +63,6 @@ brews:
install: |
bin.install "git-spr"
bin.install "git-amend"
bin.install "spr_commit_hook"
bin.install "spr_reword_helper"
license: "MIT"
nfpms:
Expand Down
1 change: 1 addition & 0 deletions .spr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ requireChecks: true
requireApproval: true
githubRemote: origin
githubBranch: master
remoteBranches: []
mergeMethod: rebase
11 changes: 0 additions & 11 deletions cmd/commithook/main.go

This file was deleted.

41 changes: 38 additions & 3 deletions cmd/reword/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import (
"bufio"
"fmt"
"os"
"strings"

"github.com/google/uuid"
)

func main() {
Expand Down Expand Up @@ -31,10 +34,42 @@ func main() {
}
writefile.Close()
} else {
file, err := os.Open(filename)
check(err)
file.Close()
if shouldAppendCommitID(filename) {
appendCommitID(filename)
}
}
}

func shouldAppendCommitID(filename string) bool {
readfile, err := os.Open(filename)
check(err)
defer readfile.Close()

i := 0
nonEmptyCommitMessage := false
scanner := bufio.NewScanner(readfile)
for scanner.Scan() {
line := scanner.Text()
if line != "" && !strings.HasPrefix(line, "#") {
nonEmptyCommitMessage = true
}
if strings.HasPrefix(line, "commit-id:") {
return false
}
i++
}
check(scanner.Err())
return nonEmptyCommitMessage
}

func appendCommitID(filename string) {
appendfile, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0666)
check(err)
defer appendfile.Close()

commitID := uuid.New()
appendfile.WriteString("\n")
appendfile.WriteString(fmt.Sprintf("commit-id:%s\n", commitID.String()[:8]))
}

func check(err error) {
Expand Down
56 changes: 0 additions & 56 deletions hook/commithook.go

This file was deleted.

66 changes: 0 additions & 66 deletions hook/install.go

This file was deleted.

15 changes: 10 additions & 5 deletions spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/ejoffe/spr/git"
"github.com/ejoffe/spr/github"
"github.com/ejoffe/spr/github/githubclient"
"github.com/ejoffe/spr/hook"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -322,15 +322,20 @@ func (sd *stackediff) getLocalCommitStack() []git.Commit {
sd.mustgit(logCommand, &commitLog)
commits, valid := sd.parseLocalCommitStack(commitLog)
if !valid {
// if not valid - it means commit hook was not installed
// install commit-hook and try again
hook.InstallCommitHook(sd.config, sd.gitcmd)
// if not valid - run rebase to add commit ids
rewordPath, err := exec.LookPath("spr_reword_helper")
check(err)
targetBranch := githubclient.GetRemoteBranchName(sd.gitcmd, sd.config.Repo)
rebaseCommand := fmt.Sprintf("rebase %s/%s -i --autosquash --autostash",
sd.config.Repo.GitHubRemote, targetBranch)
sd.gitcmd.GitWithEditor(rebaseCommand, nil, rewordPath)

sd.mustgit(logCommand, &commitLog)
commits, valid = sd.parseLocalCommitStack(commitLog)
if !valid {
// if still not valid - panic
errMsg := "unable to fetch local commits\n"
errMsg += " most likely this is an issue with missing commit-id in the commit body\n"
errMsg += " which is caused by the commit-msg hook not being installed propertly\n"
panic(errMsg)
}
}
Expand Down

0 comments on commit e7d2462

Please sign in to comment.