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 d53bb47
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 36 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/push-master.yml
Original file line number Diff line number Diff line change
@@ -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
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
102 changes: 76 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,66 @@ 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")
}
start++

end = strings.LastIndexFunc(str[start:], isDigit)
if end < 0 {
return 0, 0, 0, fmt.Errorf("no number found")
}
return replaceSequence(s, []string{"const", "NUMBER", "="}, replaceLine)
end += start + 1

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 +123,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,18 +143,23 @@ 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 autIncrementNumber {
newContent, err = IncrementNumber(newContent)
if err != nil {
return nil, err
}
} else if version > 0 {
newContent, err = replaceOrIncrementNumber(newContent, version)
if err != nil {
return nil, err
}
}
return []byte(newContent), nil
})
Expand Down
20 changes: 16 additions & 4 deletions script/git-hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func preCommitCheck(noCommit bool, noUpdateVersion bool) error {
var affectedFiles []string
const updateRevision = true
if updateRevision {
commitHash, err := revision.GetCommitHash("", "HEAD")
commitHash, err := revision.GetCommitHash(rootDir, "HEAD")
if err != nil {
return err
}
Expand All @@ -95,13 +95,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

0 comments on commit d53bb47

Please sign in to comment.