From cb1bc1938d78a1073027f5335b385b727c4c5c17 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Thu, 13 Apr 2023 17:46:44 +0100 Subject: [PATCH] Fix --base-branch handling --- cmd/pint/ci.go | 4 +- .../tests/0134_ci_base_branch_flag_path.txt | 39 +++++++++++++++++ .../tests/0135_ci_base_branch_config_path.txt | 42 +++++++++++++++++++ docs/changelog.md | 8 +++- 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 cmd/pint/tests/0134_ci_base_branch_flag_path.txt create mode 100644 cmd/pint/tests/0135_ci_base_branch_config_path.txt diff --git a/cmd/pint/ci.go b/cmd/pint/ci.go index d14c5958..2c62707f 100644 --- a/cmd/pint/ci.go +++ b/cmd/pint/ci.go @@ -69,7 +69,7 @@ func actionCI(c *cli.Context) error { } meta.cfg.CI = detectCI(meta.cfg.CI) - baseBranch := strings.Split(meta.cfg.CI.BaseBranch, "/")[len(strings.Split(meta.cfg.CI.BaseBranch, "/"))-1] + baseBranch := meta.cfg.CI.BaseBranch if c.String(baseBranchFlag) != "" { baseBranch = c.String(baseBranchFlag) } @@ -78,7 +78,7 @@ func actionCI(c *cli.Context) error { return fmt.Errorf("failed to get the name of current branch") } log.Debug().Str("current", currentBranch).Str("base", baseBranch).Msg("Got branch information") - if currentBranch == baseBranch { + if currentBranch == strings.Split(baseBranch, "/")[len(strings.Split(baseBranch, "/"))-1] { log.Info().Str("branch", currentBranch).Msg("Running from base branch, skipping checks") return nil } diff --git a/cmd/pint/tests/0134_ci_base_branch_flag_path.txt b/cmd/pint/tests/0134_ci_base_branch_flag_path.txt new file mode 100644 index 00000000..6a14e927 --- /dev/null +++ b/cmd/pint/tests/0134_ci_base_branch_flag_path.txt @@ -0,0 +1,39 @@ +mkdir testrepo +cd testrepo +exec git init --initial-branch=main . + +cp ../src/v1.yml rules.yml +cp ../src/.pint.hcl . +env GIT_AUTHOR_NAME=pint +env GIT_AUTHOR_EMAIL=pint@example.com +env GIT_COMMITTER_NAME=pint +env GIT_COMMITTER_EMAIL=pint@example.com +exec git add . +exec git commit -am 'import rules and config' + +exec git checkout -b v2 +cp ../src/v2.yml rules.yml +exec git commit -am 'v2' + +pint.error -l debug --no-color ci --base-branch=origin/main +! stdout . +cmp stderr ../stderr.txt + +-- stderr.txt -- +level=info msg="Loading configuration file" path=.pint.hcl +level=debug msg="Running git command" args=["rev-parse","--abbrev-ref","HEAD"] +level=debug msg="Got branch information" base=origin/main current=v2 +level=debug msg="Running git command" args=["log","--format=%H","--no-abbrev-commit","--reverse","origin/main..HEAD"] +level=fatal msg="Fatal error" error="failed to get the list of commits to scan: fatal: ambiguous argument 'origin/main..HEAD': unknown revision or path not in the working tree.\nUse '--' to separate paths from revisions, like this:\n'git [...] -- [...]'\n" +-- src/v1.yml -- +- record: rule1 + expr: sum(foo) by(job) + +-- src/v2.yml -- +- record: rule1 + expr: sum(foo) bi(job) + +-- src/.pint.hcl -- +parser { + relaxed = [".*"] +} diff --git a/cmd/pint/tests/0135_ci_base_branch_config_path.txt b/cmd/pint/tests/0135_ci_base_branch_config_path.txt new file mode 100644 index 00000000..c96166b1 --- /dev/null +++ b/cmd/pint/tests/0135_ci_base_branch_config_path.txt @@ -0,0 +1,42 @@ +mkdir testrepo +cd testrepo +exec git init --initial-branch=main . + +cp ../src/v1.yml rules.yml +cp ../src/.pint.hcl . +env GIT_AUTHOR_NAME=pint +env GIT_AUTHOR_EMAIL=pint@example.com +env GIT_COMMITTER_NAME=pint +env GIT_COMMITTER_EMAIL=pint@example.com +exec git add . +exec git commit -am 'import rules and config' + +exec git checkout -b v2 +cp ../src/v2.yml rules.yml +exec git commit -am 'v2' + +pint.error -l debug --no-color ci +! stdout . +cmp stderr ../stderr.txt + +-- stderr.txt -- +level=info msg="Loading configuration file" path=.pint.hcl +level=debug msg="Running git command" args=["rev-parse","--abbrev-ref","HEAD"] +level=debug msg="Got branch information" base=origin/main current=v2 +level=debug msg="Running git command" args=["log","--format=%H","--no-abbrev-commit","--reverse","origin/main..HEAD"] +level=fatal msg="Fatal error" error="failed to get the list of commits to scan: fatal: ambiguous argument 'origin/main..HEAD': unknown revision or path not in the working tree.\nUse '--' to separate paths from revisions, like this:\n'git [...] -- [...]'\n" +-- src/v1.yml -- +- record: rule1 + expr: sum(foo) by(job) + +-- src/v2.yml -- +- record: rule1 + expr: sum(foo) bi(job) + +-- src/.pint.hcl -- +parser { + relaxed = [".*"] +} +ci { + baseBranch = "origin/main" +} diff --git a/docs/changelog.md b/docs/changelog.md index 780652e1..77a6bfed 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,11 @@ # Changelog +## v0.43.1 + +### Fixed + +- Fixed `--base-branch` flag handling when branch name contains `/`. + ## v0.43.0 ### Added @@ -29,7 +35,7 @@ ### Fixed -- Fixed `--initial-branch` flag handling. +- Fixed `--base-branch` flag handling. Value of this flag wasn't being used correctly - #559. ## v0.42.0