Skip to content

Commit

Permalink
update check-merge workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed May 19, 2024
1 parent d3b65d5 commit 327c9ab
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 76 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/check-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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

# This workflow run pre-commit and amend version and commit info
# Before merging into master

name: Check Merge

on:
pull_request:
branches: [ "master" ]

jobs:

check-merge:
runs-on: ubuntu-latest
steps:

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

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

- 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
- name: Check If Git Hooks Generate No New Files
run: |
if ! git diff --quiet;then
echo "diff found after run 'go run ./script/git-hooks pre-commit --no-commit', ensure run git hooks before you commit" >&2
exit 1
fi
- name: Check If Can Merge Into Master Without Intermediate Commit
run: go run ./script/github-actions check-merge
4 changes: 2 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master", "pre-master" ]
branches: [ "master" ]

jobs:

build:
check-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/go-windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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 Windows

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

tests:
strategy:
matrix:
os: [ windows-latest ]
go: [ '1.20' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '${{ matrix.go }}'

- name: Test
run: go run ./script/run-test --reset-instrument --debug -v
11 changes: 7 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ on:

jobs:

build:
runs-on: ubuntu-latest
tests-with-coverage:
strategy:
matrix:
os: [ ubuntu-latest]
go: [ '1.20' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: false
go-version: '${{ matrix.go }}'

- name: Test
run: go run ./script/run-test --reset-instrument --debug -v -cover -coverpkg github.com/xhd2015/xgo/runtime/... -coverprofile cover.out
Expand Down
63 changes: 0 additions & 63 deletions .github/workflows/pre-master.yml

This file was deleted.

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 = "c490c2dbaaecd04df3888542f830cb715dd9b285+1"
const NUMBER = 220
const REVISION = "d3b65d5d532f17ee01275785d7872692f3a7b76a+1"
const NUMBER = 221

// 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 = "c490c2dbaaecd04df3888542f830cb715dd9b285+1"
const NUMBER = 220
const REVISION = "d3b65d5d532f17ee01275785d7872692f3a7b76a+1"
const NUMBER = 221

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 = "c490c2dbaaecd04df3888542f830cb715dd9b285+1"
const NUMBER = 220
const REVISION = "d3b65d5d532f17ee01275785d7872692f3a7b76a+1"
const NUMBER = 221

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
75 changes: 75 additions & 0 deletions script/github-actions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"fmt"
"os"

"github.com/xhd2015/xgo/script/build-release/revision"
"github.com/xhd2015/xgo/support/git"
)

func main() {
err := handle(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}

func handle(args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires cmd")
}
cmd := args[0]
args = args[1:]

if cmd == "check-merge" {
return checkMerge(args)
} else {
return fmt.Errorf("unrecognized command: %s", cmd)
}
}

// check if merge to master
// will not result in merge commit
func checkMerge(args []string) error {
gitDir, err := git.ShowTopLevel("")
if err != nil {
return err
}

err = git.FetchRef(gitDir, "origin", "master")
if err != nil {
return err
}

masterCommitHash, err := revision.GetCommitHash(gitDir, "origin/master")
if err != nil {
return err
}

var found bool
n := 20
for i := 0; i < n; i++ {
ref := "HEAD"
if i > 0 {
ref = fmt.Sprintf("HEAD~%d", i)
}
commitHash, err := revision.GetCommitHash(gitDir, ref)
if err != nil {
return err
}
if commitHash == masterCommitHash {
found = true
break
}
}
if !found {
branch, err := git.GetCurrentBranch(gitDir)
if err != nil {
return err
}
return fmt.Errorf("%s cannot be merged into master without creating intermediate commit", branch)
}
return nil
}
20 changes: 19 additions & 1 deletion support/git/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package git

import "github.com/xhd2015/xgo/support/cmd"
import (
"fmt"

"github.com/xhd2015/xgo/support/cmd"
)

// worktree root
func ShowTopLevel(dir string) (string, error) {
Expand All @@ -12,3 +16,17 @@ func ShowTopLevel(dir string) (string, error) {
func GetGitDir(dir string) (string, error) {
return cmd.Dir(dir).Output("git", "rev-parse", "--git-dir")
}

func GetCurrentBranch(dir string) (string, error) {
return cmd.Dir(dir).Output("git", "branch", "--show-current")
}

func FetchRef(dir string, origin string, ref string) error {
if origin == "" {
return fmt.Errorf("requires origin")
}
if ref == "" {
return fmt.Errorf("requires ref")
}
return cmd.Dir(dir).Run("git", "fetch", origin, ref)
}

0 comments on commit 327c9ab

Please sign in to comment.