Skip to content
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

Add using alternative branch support for custom indexes #833

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 7 additions & 3 deletions cmd/krew/cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ each configured index in table format.`,
}

var indexAddCmd = &cobra.Command{
Use: "add",
Short: "Add a new index",
Long: "Configure a new index to install plugins from.",
Use: "add",
Short: "Add a new index",
Long: `Configure a new index to install plugins from.

This command also supports alternative branch other than the default one
by appending the branch name to the end of git url with # symbol
(e.g. /kubernetes-sigs/krew-index.git#another_branch).`,
Example: "kubectl krew index add default " + constants.DefaultIndexURI,
Args: cobra.ExactArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
Expand Down
7 changes: 6 additions & 1 deletion internal/gitutil/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func EnsureCloned(uri, destinationPath string) error {
if ok, err := IsGitCloned(destinationPath); err != nil {
return err
} else if !ok {
_, err = Exec("", "clone", "-v", uri, destinationPath)
parsedURI := strings.Split(uri, "#")
if len(parsedURI) > 1 {
_, err = Exec("", "clone", "-v", "--single-branch", "--branch", parsedURI[1], parsedURI[0], destinationPath)
} else {
_, err = Exec("", "clone", "-v", uri, destinationPath)
}
return err
}
return nil
Expand Down
15 changes: 15 additions & 0 deletions internal/index/indexoperations/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ func TestAddIndexFailure(t *testing.T) {
}
}

func TestAddIndexFailureWithCustomBranch(t *testing.T) {
tmpDir := testutil.NewTempDir(t)

indexName := "foo"
localRepo := tmpDir.Path("local/" + indexName)
tmpDir.InitEmptyGitRepo(localRepo, "")

paths := environment.NewPaths(tmpDir.Root())

localRepoWithBranch := localRepo + "#test_branch"
if err := AddIndex(paths, indexName, localRepoWithBranch); err == nil {
t.Errorf("expected error test_branch not found")
}
}

func TestDeleteIndex(t *testing.T) {
// root directory does not exist
if err := DeleteIndex(environment.NewPaths(filepath.FromSlash("/tmp/does-not-exist/foo")), "bar"); err == nil {
Expand Down