`Args.InsertParam` resets index to param size if it’s out of bound

`Args.InsertParam` panics about index out of bound if the index is larger than param size. These changes reset the index to param size for such case. This is equivalent to appending new items to the end.

This fixes #812.
This commit is contained in:
Jingwen Owen Ou 2015-02-17 10:04:45 -08:00
Родитель ad2a46893c
Коммит 2884da15b2
4 изменённых файлов: 43 добавлений и 5 удалений

Просмотреть файл

@ -95,11 +95,15 @@ func (a *Args) HasSubcommand() bool {
} }
func (a *Args) InsertParam(i int, items ...string) { func (a *Args) InsertParam(i int, items ...string) {
if i < 0 || (i != 0 && i > a.ParamsSize()-1) { if i < 0 {
panic(fmt.Sprintf("Index %d is out of bound", i)) panic(fmt.Sprintf("Index %d is out of bound", i))
} }
newParams := []string{} if i > a.ParamsSize() {
i = a.ParamsSize()
}
newParams := make([]string, 0)
newParams = append(newParams, a.Params[:i]...) newParams = append(newParams, a.Params[:i]...)
newParams = append(newParams, items...) newParams = append(newParams, items...)
newParams = append(newParams, a.Params[i:]...) newParams = append(newParams, a.Params[i:]...)

Просмотреть файл

@ -62,6 +62,19 @@ func TestArgs_Insert(t *testing.T) {
assert.Equal(t, 5, args.ParamsSize()) assert.Equal(t, 5, args.ParamsSize())
assert.Equal(t, "foo", args.Params[3]) assert.Equal(t, "foo", args.Params[3])
args = NewArgs([]string{"checkout", "-b"})
args.InsertParam(1, "foo")
assert.Equal(t, 2, args.ParamsSize())
assert.Equal(t, "-b", args.Params[0])
assert.Equal(t, "foo", args.Params[1])
args = NewArgs([]string{"checkout"})
args.InsertParam(1, "foo")
assert.Equal(t, 1, args.ParamsSize())
assert.Equal(t, "foo", args.Params[0])
} }
func TestArgs_Remove(t *testing.T) { func TestArgs_Remove(t *testing.T) {

Просмотреть файл

@ -97,9 +97,14 @@ func transformCheckoutArgs(args *Args) error {
args.Before("git", "remote", "add", "-f", "-t", branch, user, u) args.Before("git", "remote", "add", "-f", "-t", branch, user, u)
} }
idx := args.IndexOfParam(checkoutURL) remoteName := fmt.Sprintf("%s/%s", user, branch)
args.RemoveParam(idx) replaceCheckoutParam(args, checkoutURL, newBranchName, remoteName)
args.InsertParam(idx, "--track", "-B", newBranchName, fmt.Sprintf("%s/%s", user, branch))
return nil return nil
} }
func replaceCheckoutParam(args *Args, checkoutURL, branchName, remoteName string) {
idx := args.IndexOfParam(checkoutURL)
args.RemoveParam(idx)
args.InsertParam(idx, "--track", "-B", branchName, remoteName)
}

16
commands/checkout_test.go Normal file
Просмотреть файл

@ -0,0 +1,16 @@
package commands
import (
"testing"
"github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert"
)
func TestReplaceCheckoutParam(t *testing.T) {
checkoutURL := "https://github.com/github/hub/pull/12"
args := NewArgs([]string{"checkout", "-b", checkoutURL})
replaceCheckoutParam(args, checkoutURL, "jingweno", "origin/master")
cmd := args.ToCmd()
assert.Equal(t, "git checkout -b --track -B jingweno origin/master", cmd.String())
}