From db7275a852b0907612e868fbdab232c3c8befa9d Mon Sep 17 00:00:00 2001 From: xhd2015 Date: Sun, 19 May 2024 10:30:48 +0800 Subject: [PATCH] add push-master workflow --- .github/workflows/push-master.yml | 39 +++++++++ cmd/xgo/runtime_gen/core/version.go | 4 +- cmd/xgo/version.go | 4 +- runtime/core/version.go | 4 +- script/build-release/revision/revision.go | 101 ++++++++++++++++------ script/git-hooks/main.go | 48 ++++++++-- 6 files changed, 161 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/push-master.yml diff --git a/.github/workflows/push-master.yml b/.github/workflows/push-master.yml new file mode 100644 index 00000000..7832a35a --- /dev/null +++ b/.github/workflows/push-master.yml @@ -0,0 +1,39 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "test-ci-push" ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + cache: false + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + cache: false + + - name: Run Git Hooks + run: | + echo 'before' + cat cmd/xgo/version.go + go run ./sript/git-hooks pre-commit --no-commit + git status + echo 'after' + cat cmd/xgo/version.go + + - name: Check + run: git status \ No newline at end of file diff --git a/cmd/xgo/runtime_gen/core/version.go b/cmd/xgo/runtime_gen/core/version.go index 159f8e11..aca7e833 100755 --- a/cmd/xgo/runtime_gen/core/version.go +++ b/cmd/xgo/runtime_gen/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.35" -const REVISION = "61cf57819a9977020a457c10b44c8aa881984fd8+1" -const NUMBER = 219 +const REVISION = "c490c2dbaaecd04df3888542f830cb715dd9b285+1" +const NUMBER = 220 // these fields will be filled by compiler const XGO_VERSION = "" diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 66ed53b3..b8fe2858 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -3,8 +3,8 @@ package main import "fmt" const VERSION = "1.0.35" -const REVISION = "61cf57819a9977020a457c10b44c8aa881984fd8+1" -const NUMBER = 219 +const REVISION = "c490c2dbaaecd04df3888542f830cb715dd9b285+1" +const NUMBER = 220 func getRevision() string { revSuffix := "" diff --git a/runtime/core/version.go b/runtime/core/version.go index 159f8e11..aca7e833 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -7,8 +7,8 @@ import ( ) const VERSION = "1.0.35" -const REVISION = "61cf57819a9977020a457c10b44c8aa881984fd8+1" -const NUMBER = 219 +const REVISION = "c490c2dbaaecd04df3888542f830cb715dd9b285+1" +const NUMBER = 220 // these fields will be filled by compiler const XGO_VERSION = "" diff --git a/script/build-release/revision/revision.go b/script/build-release/revision/revision.go index 4a46101d..b05ab6c4 100644 --- a/script/build-release/revision/revision.go +++ b/script/build-release/revision/revision.go @@ -13,14 +13,22 @@ import ( func GetCommitHash(dir string, ref string) (string, error) { var args []string - if dir != "" { - args = append(args, "-C", dir) - } args = append(args, "log", "--format=%H", "-1") if ref != "" { args = append(args, ref) } - return cmd.Output("git", args...) + return cmd.Dir(dir).Output("git", args...) +} + +// git cat-file -p REF:FILE +func GetFileContent(dir string, ref string, file string) (string, error) { + if ref == "" { + return "", fmt.Errorf("requires ref") + } + if file == "" { + return "", fmt.Errorf("requires file") + } + return cmd.Dir(dir).Output("git", "cat-file", "-p", fmt.Sprintf("%s:%s", ref, file)) } func ReplaceRevision(s string, revision string) (string, error) { @@ -43,37 +51,65 @@ func ReplaceRevision(s string, revision string) (string, error) { return replaceSequence(s, []string{"const", "REVISION", "="}, replaceLine) } +var constNumberSeq = []string{"const", "NUMBER", "="} + func IncrementNumber(s string) (string, error) { + return replaceOrIncrementNumber(s, -1) +} +func replaceOrIncrementNumber(s string, version int) (string, error) { replaceLine := func(line string, index int) (string, error) { - isDigit := func(r rune) bool { - return '0' <= r && r <= '9' + start, end, num, err := parseNum(line[index:]) + if err != nil { + return "", fmt.Errorf("line %q: %w", line, err) } - start := strings.IndexFunc(line[index+1:], isDigit) - if start < 0 { - return "", fmt.Errorf("no number found: %s", line) + start += index + end += index + if version < 0 { + version = num + 1 } - start += index + 1 - end := strings.LastIndexFunc(line[start:], isDigit) - if end < 0 { - return "", fmt.Errorf("no number found: %s", line) - } - end += start + 1 + return line[:start] + strconv.Itoa(version) + line[end:], nil + } + return replaceSequence(s, constNumberSeq, replaceLine) +} - numStr := line[start:end] +func isDigit(r rune) bool { + return '0' <= r && r <= '9' +} - num, err := strconv.ParseInt(numStr, 10, 64) - if err != nil { - return "", fmt.Errorf("invalid number %s: %w", line, err) - } - newNum := num + 1 - return line[:start] + strconv.FormatInt(newNum, 10) + line[end:], nil +func parseNum(str string) (start int, end int, num int, err error) { + start = strings.IndexFunc(str, isDigit) + if start < 0 { + return 0, 0, 0, fmt.Errorf("no number found") + } + + end = strings.LastIndexFunc(str[start+1:], isDigit) + if end < 0 { + return 0, 0, 0, fmt.Errorf("no number found") } - return replaceSequence(s, []string{"const", "NUMBER", "="}, replaceLine) + end += start + 2 + + numStr := str[start:end] + + num, err = strconv.Atoi(numStr) + if err != nil { + return 0, 0, 0, fmt.Errorf("invalid number: %w", err) + } + return start, end, int(num), nil } -func replaceSequence(s string, seq []string, replaceLine func(line string, index int) (string, error)) (string, error) { +func GetVersionNumber(s string) (int, error) { + // const NUMBER = lines := strings.Split(s, "\n") + lineIdx, byteIdx := IndexLinesSequence(lines, constNumberSeq) + if lineIdx < 0 { + return 0, fmt.Errorf("sequence %v not found", constNumberSeq) + } + _, _, num, err := parseNum(lines[lineIdx][byteIdx:]) + return num, err +} + +func IndexLinesSequence(lines []string, seq []string) (lineIndex int, byteIndex int) { n := len(lines) lineIdx := -1 byteIdx := -1 @@ -86,6 +122,14 @@ func replaceSequence(s string, seq []string, replaceLine func(line string, index break } } + if lineIdx < 0 { + return -1, -1 + } + return lineIdx, byteIdx +} +func replaceSequence(s string, seq []string, replaceLine func(line string, index int) (string, error)) (string, error) { + lines := strings.Split(s, "\n") + lineIdx, byteIdx := IndexLinesSequence(lines, seq) if lineIdx < 0 { return "", fmt.Errorf("sequence %v not found", seq) } @@ -98,14 +142,19 @@ func replaceSequence(s string, seq []string, replaceLine func(line string, index return strings.Join(lines, "\n"), nil } -func PatchVersionFile(file string, rev string, incrementNumber bool) error { +func PatchVersionFile(file string, rev string, autIncrementNumber bool, version int) error { err := fileutil.Patch(file, func(data []byte) ([]byte, error) { content := string(data) newContent, err := ReplaceRevision(content, rev) if err != nil { return nil, err } - if incrementNumber { + if version > 0 { + newContent, err = replaceOrIncrementNumber(newContent, version) + if err != nil { + return nil, err + } + } else if autIncrementNumber { newContent, err = IncrementNumber(newContent) if err != nil { return nil, err diff --git a/script/git-hooks/main.go b/script/git-hooks/main.go index 4edfd45f..5a37e6cc 100644 --- a/script/git-hooks/main.go +++ b/script/git-hooks/main.go @@ -28,11 +28,16 @@ func main() { var noCommit bool var noUpdateVersion bool + var amend bool for _, arg := range args { if arg == "--no-commit" { noCommit = true continue } + if arg == "--amend" { + amend = true + continue + } if arg == "--no-update-version" { noUpdateVersion = true continue @@ -52,7 +57,7 @@ func main() { if cmd == "install" { err = install() } else if cmd == "pre-commit" { - err = preCommitCheck(noCommit, noUpdateVersion) + err = preCommitCheck(noCommit, amend, noUpdateVersion) } else if cmd == "post-commit" { err = postCommitCheck(noCommit) } else { @@ -66,12 +71,24 @@ func main() { } const preCommitCmdHead = "# xgo check" -const preCommitCmd = "go run ./script/git-hooks pre-commit" + +// NOTE: no empty lines in between +const preCommitCmd = `# see: https://stackoverflow.com/questions/19387073/how-to-detect-commit-amend-by-pre-commit-hook +is_amend=$(ps -ocommand= -p $PPID | grep -e '--amend') +# echo "is amend: $is_amend" +# args is always empty +# echo "args: ${args[@]}" +flags=() +if [[ -n $is_amend ]];then + flags=("${flags[@]}" --amend) +fi +go run ./script/git-hooks pre-commit "${flags[@]}" +` const postCommitCmdHead = "# xgo check" const postCommitCmd = "go run ./script/git-hooks post-commit" -func preCommitCheck(noCommit bool, noUpdateVersion bool) error { +func preCommitCheck(noCommit bool, amend bool, noUpdateVersion bool) error { gitDir, err := git.ShowTopLevel("") if err != nil { return err @@ -84,7 +101,11 @@ func preCommitCheck(noCommit bool, noUpdateVersion bool) error { var affectedFiles []string const updateRevision = true if updateRevision { - commitHash, err := revision.GetCommitHash("", "HEAD") + refLast := "HEAD" + if amend { + refLast = "HEAD~1" + } + commitHash, err := revision.GetCommitHash(rootDir, refLast) if err != nil { return err } @@ -95,13 +116,25 @@ func preCommitCheck(noCommit bool, noUpdateVersion bool) error { // suffix "+1" to indicate this rev := commitHash + "+1" - versionFiles := revision.GetVersionFiles(rootDir) - for _, file := range versionFiles { - err = revision.PatchVersionFile(file, rev, !noUpdateVersion) + relVersionFiles := revision.GetVersionFiles("") + versionFiles := make([]string, 0, len(relVersionFiles)) + for _, relFile := range relVersionFiles { + file := filepath.Join(rootDir, relFile) + versionFiles = append(versionFiles, file) + content, err := revision.GetFileContent(rootDir, commitHash, relFile) + if err != nil { + return err + } + version, err := revision.GetVersionNumber(content) + if err != nil { + return err + } + err = revision.PatchVersionFile(file, rev, !noUpdateVersion, version+1) if err != nil { return err } } + affectedFiles = append(affectedFiles, versionFiles...) } @@ -133,6 +166,7 @@ func postCommitCheck(noCommit bool) error { } func install() error { + // NOTE: is git dir, not toplevel dir when in worktree mode gitDir, err := git.GetGitDir("") if err != nil { return err