Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cmd/krel/cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
44 changes: 41 additions & 3 deletions pkg/git/git_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -505,15 +505,15 @@ 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,
))
res, err := command.NewWithWorkDir(
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)
Expand Down Expand Up @@ -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))
}