Skip to content

Commit

Permalink
Git custom commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
ElfoLiNk committed Apr 20, 2018
1 parent 33d2d0e commit 1d05631
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
built-*
assets
.idea
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -39,6 +39,8 @@ The `git` driver works by modifying a file in a repository with every bump. The

* `depth`: *Optional.* If a positive integer is given, shallow clone the repository using the --depth option.

* `commit_message`: *Optional.* If specified overides the default commit message with the one provided. The user can use %version% and %file% to get them replaced automatically with the correct values.

### `s3` Driver

The `s3` driver works by modifying a file in an S3 compatible bucket.
Expand Down
15 changes: 8 additions & 7 deletions driver/driver.go
Expand Up @@ -91,13 +91,14 @@ func FromSource(source models.Source) (Driver, error) {
return &GitDriver{
InitialVersion: initialVersion,

URI: source.URI,
Branch: source.Branch,
PrivateKey: source.PrivateKey,
Username: source.Username,
Password: source.Password,
File: source.File,
GitUser: source.GitUser,
URI: source.URI,
Branch: source.Branch,
PrivateKey: source.PrivateKey,
Username: source.Username,
Password: source.Password,
File: source.File,
GitUser: source.GitUser,
CommitMessage: source.CommitMessage,
}, nil

case models.DriverSwift:
Expand Down
26 changes: 17 additions & 9 deletions driver/git.go
Expand Up @@ -29,14 +29,15 @@ func init() {
type GitDriver struct {
InitialVersion semver.Version

URI string
Branch string
PrivateKey string
Username string
Password string
File string
GitUser string
Depth string
URI string
Branch string
PrivateKey string
Username string
Password string
File string
GitUser string
Depth string
CommitMessage string
}

func (driver *GitDriver) Bump(bump version.Bump) (semver.Version, error) {
Expand Down Expand Up @@ -308,8 +309,15 @@ func (driver *GitDriver) writeVersion(newVersion semver.Version) (bool, error) {
if err := gitAdd.Run(); err != nil {
return false, err
}
var commitMessage string
if driver.CommitMessage == "" {
commitMessage = "bump to "+newVersion.String()
} else {
commitMessage = strings.Replace(driver.CommitMessage, "%version%", newVersion.String(), -1)
commitMessage = strings.Replace(commitMessage, "%file%", driver.File, -1)
}

gitCommit := exec.Command("git", "commit", "-m", "bump to "+newVersion.String())
gitCommit := exec.Command("git", "commit", "-m", commitMessage)
gitCommit.Dir = gitRepoDir

commitOutput, err := gitCommit.CombinedOutput()
Expand Down
15 changes: 8 additions & 7 deletions models/models.go
Expand Up @@ -61,13 +61,14 @@ type Source struct {
ServerSideEncryption string `json:"server_side_encryption"`
UseV2Signing bool `json:"use_v2_signing"`

URI string `json:"uri"`
Branch string `json:"branch"`
PrivateKey string `json:"private_key"`
Username string `json:"username"`
Password string `json:"password"`
File string `json:"file"`
GitUser string `json:"git_user"`
URI string `json:"uri"`
Branch string `json:"branch"`
PrivateKey string `json:"private_key"`
Username string `json:"username"`
Password string `json:"password"`
File string `json:"file"`
GitUser string `json:"git_user"`
CommitMessage string `json:"commit_message"`

OpenStack OpenStackOptions `json:"openstack"`

Expand Down
16 changes: 16 additions & 0 deletions test/helpers.sh
Expand Up @@ -172,3 +172,19 @@ put_uri_with_bump_and_initial() {
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}

put_uri_with_bump_and_message() {
jq -n "{
source: {
driver: \"git\",
uri: $(echo $1 | jq -R .),
branch: \"master\",
file: \"some-file\",
commit_message: \"$(echo $5)\"
},
params: {
bump: $(echo $3 | jq -R .),
pre: $(echo $4 | jq -R .)
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}
52 changes: 52 additions & 0 deletions test/put.sh
Expand Up @@ -127,9 +127,61 @@ it_can_put_and_bump_over_existing_version() {
test "$(cat $repo/some-file)" = 1.3.0-alpha.1
}

it_can_put_and_bump_with_message_over_existing_version() {
local repo=$(init_repo)

set_version $repo 1.2.3

local src=$(mktemp -d $TMPDIR/put-src.XXXXXX)

local message="This is a commit message"

# cannot push to repo while it's checked out to a branch
git -C $repo checkout refs/heads/master

put_uri_with_bump_and_message $repo $src minor alpha "$message" | jq -e "
.version == {number: \"1.3.0-alpha.1\"}
"

# switch back to master
git -C $repo checkout master

test -e $repo/some-file
test "$(cat $repo/some-file)" = 1.3.0-alpha.1
test "$(git -C $repo log -n1 --pretty=%B)" = "$message"
}

it_can_put_and_bump_with_message_and_replace_over_existing_version() {
local repo=$(init_repo)

set_version $repo 1.2.3

local src=$(mktemp -d $TMPDIR/put-src.XXXXXX)

local message="This is a commit message on %file% with %version%"

# cannot push to repo while it's checked out to a branch
git -C $repo checkout refs/heads/master

put_uri_with_bump_and_message $repo $src minor alpha "$message" | jq -e "
.version == {number: \"1.3.0-alpha.1\"}
"

# switch back to master
git -C $repo checkout master

local expected_message="This is a commit message on some-file with 1.3.0-alpha.1"

test -e $repo/some-file
test "$(cat $repo/some-file)" = 1.3.0-alpha.1
test "$(git -C $repo log -n1 --pretty=%B)" = "$expected_message"
}

run it_can_put_and_set_first_version
run it_can_put_and_set_same_version
run it_can_put_and_set_over_existing_version
run it_can_put_and_bump_first_version
run it_can_put_and_bump_first_version_with_initial
run it_can_put_and_bump_over_existing_version
run it_can_put_and_bump_with_message_over_existing_version
run it_can_put_and_bump_with_message_and_replace_over_existing_version

0 comments on commit 1d05631

Please sign in to comment.