Avoid leaving git remotes after `hub cherry-pick`

With the new approach, create a temporary remote, fetch it, and delete
it immediately. Its git objects will still remain to be available for
cherry-picking.
This commit is contained in:
Mislav Marohnić 2016-09-12 03:32:09 +02:00
Родитель 86e49b7304
Коммит 30c3cb4fbe
3 изменённых файлов: 13 добавлений и 61 удалений

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

@ -17,13 +17,6 @@ cherry-pick <USER>@<SHA>
`, `,
Long: `Cherry-pick a commit from a fork on GitHub. Long: `Cherry-pick a commit from a fork on GitHub.
## Examples:
$ hub cherry-pick https://github.com/jingweno/gh/commit/a319d88#comments
> git remote add -f --no-tags jingweno git://github.com/jingweno/gh.git
> git cherry-pick a319d88
$ hub cherry-pick jingweno@a319d88
## See also: ## See also:
hub-am(1), hub(1), git-cherry-pick(1) hub-am(1), hub(1), git-cherry-pick(1)
@ -53,7 +46,10 @@ func transformCherryPickArgs(args *Args) {
if remote := gitRemoteForProject(project); remote != nil { if remote := gitRemoteForProject(project); remote != nil {
args.Before("git", "fetch", remote.Name) args.Before("git", "fetch", remote.Name)
} else { } else {
args.Before("git", "remote", "add", "-f", "--no-tags", project.Owner, project.GitURL("", "", isPrivate)) tmpName := "_hub-cherry-pick"
args.Before("git", "remote", "add", tmpName, project.GitURL("", "", isPrivate))
args.Before("git", "fetch", "-q", "--no-tags", tmpName)
args.Before("git", "remote", "rm", tmpName)
} }
} }
} }

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

@ -1,50 +0,0 @@
package commands
import (
"os"
"testing"
"github.com/bmizerany/assert"
"github.com/github/hub/fixtures"
)
func TestParseCherryPickProjectAndSha(t *testing.T) {
testConfigs := fixtures.SetupTestConfigs()
defer testConfigs.TearDown()
ref := "https://github.com/jingweno/gh/commit/a319d88#comments"
project, sha := parseCherryPickProjectAndSha(ref)
assert.Equal(t, "jingweno", project.Owner)
assert.Equal(t, "gh", project.Name)
assert.Equal(t, "github.com", project.Host)
assert.Equal(t, "https", project.Protocol)
assert.Equal(t, "a319d88", sha)
ref = "https://github.com/jingweno/gh/commit/a319d88#comments"
project, sha = parseCherryPickProjectAndSha(ref)
assert.Equal(t, "jingweno", project.Owner)
assert.Equal(t, "gh", project.Name)
assert.Equal(t, "a319d88", sha)
}
func TestTransformCherryPickArgs(t *testing.T) {
testConfigs := fixtures.SetupTestConfigs()
defer testConfigs.TearDown()
args := NewArgs([]string{})
transformCherryPickArgs(args)
cmds := args.Commands()
assert.Equal(t, 1, len(cmds))
os.Setenv("HUB_PROTOCOL", "git")
defer os.Setenv("HUB_PROTOCOL", "")
args = NewArgs([]string{"cherry-pick", "https://github.com/jingweno/gh/commit/a319d88#comments"})
transformCherryPickArgs(args)
cmds = args.Commands()
assert.Equal(t, 2, len(cmds))
assert.Equal(t, "git remote add -f --no-tags jingweno git://github.com/jingweno/gh.git", cmds[0].String())
assert.Equal(t, "git cherry-pick a319d88", cmds[1].String())
}

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

@ -27,7 +27,9 @@ Feature: hub cherry-pick
} }
""" """
When I run `hub cherry-pick https://github.com/blueyed/ronn/pull/560/commits/a319d88` When I run `hub cherry-pick https://github.com/blueyed/ronn/pull/560/commits/a319d88`
Then "git remote add -f --no-tags mislav git@github.com:mislav/ronin.git" should be run Then "git remote add _hub-cherry-pick git@github.com:mislav/ronin.git" should be run
And "git fetch -q --no-tags _hub-cherry-pick" should be run
And "git remote rm _hub-cherry-pick" should be run
And "git cherry-pick a319d88" should be run And "git cherry-pick a319d88" should be run
Scenario: From fork that has existing remote Scenario: From fork that has existing remote
@ -58,10 +60,14 @@ Feature: hub cherry-pick
Scenario: Using GitHub owner@SHA notation with remote add Scenario: Using GitHub owner@SHA notation with remote add
When I run `hub cherry-pick mislav@a319d88` When I run `hub cherry-pick mislav@a319d88`
Then "git remote add -f --no-tags mislav git://github.com/mislav/ronn.git" should be run Then "git remote add _hub-cherry-pick git://github.com/mislav/ronn.git" should be run
And "git fetch -q --no-tags _hub-cherry-pick" should be run
And "git remote rm _hub-cherry-pick" should be run
And "git cherry-pick a319d88" should be run And "git cherry-pick a319d88" should be run
Scenario: From fork that doesn't have a remote Scenario: From fork that doesn't have a remote
When I run `hub cherry-pick https://github.com/jingweno/ronn/commit/a319d88` When I run `hub cherry-pick https://github.com/jingweno/ronn/commit/a319d88`
Then "git remote add -f --no-tags jingweno git://github.com/jingweno/ronn.git" should be run Then "git remote add _hub-cherry-pick git://github.com/jingweno/ronn.git" should be run
And "git fetch -q --no-tags _hub-cherry-pick" should be run
And "git remote rm _hub-cherry-pick" should be run
And "git cherry-pick a319d88" should be run And "git cherry-pick a319d88" should be run