diff --git a/cmd/krel/cmd/changelog.go b/cmd/krel/cmd/changelog.go index 96e357e5035..ebc181a0581 100644 --- a/cmd/krel/cmd/changelog.go +++ b/cmd/krel/cmd/changelog.go @@ -62,7 +62,9 @@ the golang based 'release-notes' tool: announcement is done by another subcommand of 'krel', not "changelog'. 3. Commit the modified CHANGELOG-x.y.md into the master branch as well as the - corresponding release-branch of kubernetes/kubernetes. + corresponding release-branch of kubernetes/kubernetes. The release branch + will be pruned from all other CHANGELOG-*.md files which do not belong to + this release branch. `, SilenceUsage: true, SilenceErrors: true, @@ -403,6 +405,11 @@ func commitChanges(repo *git.Repo, branch string, tag semver.Version) error { return errors.Wrapf(err, "checking out release branch %s", branch) } + // Remove all other changelog files + if err := repo.Rm(true, "CHANGELOG-*.md"); err != nil { + return errors.Wrap(err, "unable to remove CHANGELOG-*.md files") + } + logrus.Info("Checking out changelog from master branch") if err := repo.Checkout(git.Master, filename); err != nil { return errors.Wrap(err, "checking out master branch changelog") diff --git a/pkg/git/git.go b/pkg/git/git.go index 5d4934383ca..eda7da3a78c 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -693,3 +693,16 @@ func (r *Repo) CurrentBranch() (branch string, err error) { return branch, nil } + +// Rm removes files from the repository +func (r *Repo) Rm(force bool, files ...string) error { + args := []string{"rm"} + if force { + args = append(args, "-f") + } + args = append(args, files...) + + return command. + NewWithWorkDir(r.Dir(), gitExecutable, args...). + RunSilentSuccess() +} diff --git a/pkg/git/git_integration_test.go b/pkg/git/git_integration_test.go index f520eccf168..338fe109419 100644 --- a/pkg/git/git_integration_test.go +++ b/pkg/git/git_integration_test.go @@ -200,7 +200,7 @@ func newTestRepo(t *testing.T) *testRepo { secondTagCommit: secondTagRef.Hash().String(), thirdTagName: thirdTagName, thirdTagCommit: thirdTagRef.Hash().String(), - testFileName: testFileName, + testFileName: filepath.Join(sut.Dir(), testFileName), } } @@ -505,7 +505,7 @@ func TestCheckoutSuccess(t *testing.T) { defer testRepo.cleanup(t) require.Nil(t, ioutil.WriteFile( - filepath.Join(testRepo.sut.Dir(), testRepo.testFileName), + testRepo.testFileName, []byte("hello world"), 0o644, )) @@ -513,7 +513,7 @@ func TestCheckoutSuccess(t *testing.T) { testRepo.sut.Dir(), "git", "diff", "--name-only").Run() require.Nil(t, err) require.True(t, res.Success()) - require.Contains(t, res.Output(), testRepo.testFileName) + require.Contains(t, res.Output(), filepath.Base(testRepo.testFileName)) err = testRepo.sut.Checkout(git.Master, testRepo.testFileName) require.Nil(t, err) @@ -597,3 +597,41 @@ func TestCurrentBranchMaster(t *testing.T) { require.Nil(t, err) require.Equal(t, git.Master, branch) } + +func TestRmSuccessForce(t *testing.T) { + testRepo := newTestRepo(t) + defer testRepo.cleanup(t) + require.Nil(t, ioutil.WriteFile(testRepo.testFileName, + []byte("test"), 0o755), + ) + + require.Nil(t, testRepo.sut.Rm(true, testRepo.testFileName)) + + _, err := os.Stat(testRepo.testFileName) + require.True(t, os.IsNotExist(err)) +} + +func TestRmFailureForce(t *testing.T) { + testRepo := newTestRepo(t) + defer testRepo.cleanup(t) + require.NotNil(t, testRepo.sut.Rm(true, "invalid")) +} + +func TestRmSuccess(t *testing.T) { + testRepo := newTestRepo(t) + defer testRepo.cleanup(t) + + require.Nil(t, testRepo.sut.Rm(true, testRepo.testFileName)) + + _, err := os.Stat(testRepo.testFileName) + require.True(t, os.IsNotExist(err)) +} + +func TestRmFailureModified(t *testing.T) { + testRepo := newTestRepo(t) + defer testRepo.cleanup(t) + require.Nil(t, ioutil.WriteFile(testRepo.testFileName, + []byte("test"), 0o755), + ) + require.NotNil(t, testRepo.sut.Rm(false, testRepo.testFileName)) +}