-
Notifications
You must be signed in to change notification settings - Fork 520
Add commit support for changelog subcommand #1014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/blang/semver" | ||
"github.com/pkg/errors" | ||
|
@@ -386,6 +387,14 @@ func (r *Repo) CheckoutBranch(name string) error { | |
}) | ||
} | ||
|
||
// Checkout can be used to checkout any revision inside the repository | ||
func (r *Repo) Checkout(rev string, args ...string) error { | ||
cmdArgs := append([]string{"checkout", rev}, args...) | ||
return command. | ||
NewWithWorkDir(r.Dir(), gitExecutable, cmdArgs...). | ||
RunSilentSuccess() | ||
} | ||
|
||
func IsReleaseBranch(branch string) bool { | ||
re := regexp.MustCompile(branchRE) | ||
if !re.MatchString(branch) { | ||
|
@@ -598,3 +607,35 @@ func (r *Repo) tagsForBranch(branch string) ([]string, error) { | |
|
||
return strings.Fields(status.Output()), nil | ||
} | ||
|
||
// Add adds a file to the staging area of the repo | ||
func (r *Repo) Add(filename string) error { | ||
worktree, err := r.inner.Worktree() | ||
if err != nil { | ||
return err | ||
} | ||
_, err = worktree.Add(filename) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Commit commits the current repository state | ||
func (r *Repo) Commit(msg string) error { | ||
worktree, err := r.inner.Worktree() | ||
if err != nil { | ||
return err | ||
} | ||
_, err = worktree.Commit(msg, &git.CommitOptions{ | ||
Author: &object.Signature{ | ||
Name: "Anago GCB", | ||
Email: "[email protected]", | ||
When: time.Now(), | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ type testRepo struct { | |
secondTagName string | ||
thirdTagCommit string | ||
thirdTagName string | ||
testFileName string | ||
} | ||
|
||
// newTestRepo creates a test repo with the following structure: | ||
|
@@ -198,6 +199,7 @@ func newTestRepo(t *testing.T) *testRepo { | |
secondTagCommit: secondTagRef.Hash().String(), | ||
thirdTagName: thirdTagName, | ||
thirdTagCommit: thirdTagRef.Hash().String(), | ||
testFileName: testFileName, | ||
} | ||
} | ||
|
||
|
@@ -496,3 +498,82 @@ func TestTagsForBranchFailureWrongBranch(t *testing.T) { | |
require.NotNil(t, err) | ||
require.Nil(t, result) | ||
} | ||
|
||
func TestCheckoutSuccess(t *testing.T) { | ||
testRepo := newTestRepo(t) | ||
defer testRepo.cleanup(t) | ||
|
||
require.Nil(t, ioutil.WriteFile( | ||
filepath.Join(testRepo.sut.Dir(), 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) | ||
|
||
err = testRepo.sut.Checkout(Master, testRepo.testFileName) | ||
require.Nil(t, err) | ||
|
||
res, err = command.NewWithWorkDir( | ||
testRepo.sut.Dir(), "git", "diff", "--name-only").Run() | ||
require.Nil(t, err) | ||
require.True(t, res.Success()) | ||
require.Empty(t, res.Output()) | ||
} | ||
|
||
func TestCheckoutFailureWrongRevision(t *testing.T) { | ||
testRepo := newTestRepo(t) | ||
defer testRepo.cleanup(t) | ||
|
||
err := testRepo.sut.Checkout("wrong") | ||
require.NotNil(t, err) | ||
require.Contains(t, err.Error(), "checkout wrong did not succeed") | ||
} | ||
|
||
func TestAddSuccess(t *testing.T) { | ||
testRepo := newTestRepo(t) | ||
defer testRepo.cleanup(t) | ||
|
||
f, err := ioutil.TempFile(testRepo.sut.Dir(), "test") | ||
require.Nil(t, err) | ||
require.Nil(t, f.Close()) | ||
|
||
filename := filepath.Base(f.Name()) | ||
err = testRepo.sut.Add(filename) | ||
require.Nil(t, err) | ||
|
||
res, err := command.NewWithWorkDir( | ||
testRepo.sut.Dir(), "git", "diff", "--cached", "--name-only").Run() | ||
require.Nil(t, err) | ||
require.True(t, res.Success()) | ||
require.Contains(t, res.Output(), filename) | ||
} | ||
|
||
func TestAddFailureWrongPath(t *testing.T) { | ||
testRepo := newTestRepo(t) | ||
defer testRepo.cleanup(t) | ||
|
||
err := testRepo.sut.Add("wrong") | ||
require.NotNil(t, err) | ||
require.Contains(t, err.Error(), "entry not found") | ||
} | ||
|
||
func TestCommitSuccess(t *testing.T) { | ||
testRepo := newTestRepo(t) | ||
defer testRepo.cleanup(t) | ||
|
||
commitMessage := "My commit message for this test" | ||
err := testRepo.sut.Commit(commitMessage) | ||
require.Nil(t, err) | ||
|
||
res, err := command.NewWithWorkDir( | ||
testRepo.sut.Dir(), "git", "log", "-1", | ||
).Run() | ||
require.Nil(t, err) | ||
require.True(t, res.Success()) | ||
require.Contains(t, res.Output(), "Author: Anago GCB <[email protected]>") | ||
require.Contains(t, res.Output(), commitMessage) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would be useful to add tests for
Add
andCommit
? Per usual, I would be fine with them coming in a follow-up PR.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes sure, I added three more unit tests on top. I was not completely sure how to test a failing commit (should usually succeed), but I think this is not a critical path.