Skip to content

Commit

Permalink
add push-master workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed May 19, 2024
1 parent c490c2d commit 18a7605
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 39 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/push-master.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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:

- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: false

- name: Run Git Hooks
run: |
set -x
echo 'before'
cat cmd/xgo/version.go
go run ./script/git-hooks pre-commit --amend --no-commit
git status
echo 'after'
cat cmd/xgo/version.go
git add -A
git config user.email "$(git log --format=%ae -n1 HEAD)"
git config user.name "$(git log --format=%an -n1 HEAD)"
git commit --amend --no-edit --no-verify
git status
- name: Check
run: git status
4 changes: 2 additions & 2 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
101 changes: 75 additions & 26 deletions script/build-release/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
48 changes: 41 additions & 7 deletions script/git-hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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...)
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 18a7605

Please sign in to comment.