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

feat: adds refspec support for func repo add #1558

Merged
merged 6 commits into from
Feb 16, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup: removes --branch tag from repo commands
Removes the currently in-flight work on the `--branch` tag, simply
supporting a `refspec` on the repository URI. For example,
https://github.com/knative-sandbox/func-tastic#metacontroller will pull
the `func-tastic` repo, and set the currently active HEAD to the
`metacontroller` branch. Adds support for displaying this with the
command `func repo list -v`.

Signed-off-by: Lance Ball <lball@redhat.com>
lance committed Feb 15, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
lance Lance Ball
commit c815f9d0688c5c77a43137873dc2cc31aca69e9c
11 changes: 4 additions & 7 deletions cmd/repository.go
Original file line number Diff line number Diff line change
@@ -180,7 +180,7 @@ func NewRepositoryAddCmd(newClient ClientFactory) *cobra.Command {
Short: "Add a repository",
Use: "add <name> <url>",
SuggestFor: []string{"ad", "install"},
PreRunE: bindEnv("confirm", "branch"),
PreRunE: bindEnv("confirm"),
}

cfg, err := config.NewDefault()
@@ -189,7 +189,6 @@ func NewRepositoryAddCmd(newClient ClientFactory) *cobra.Command {
}

cmd.Flags().BoolP("confirm", "c", cfg.Confirm, "Prompt to confirm all options interactively (Env: $FUNC_CONFIRM)")
cmd.Flags().StringP("branch", "", "", "The repository branch to be added.")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
return runRepositoryAdd(cmd, args, newClient)
@@ -349,17 +348,15 @@ func runRepositoryAdd(_ *cobra.Command, args []string, newClient ClientFactory)
// Extract Params
// Populate a struct with the arguments (if provided)
params := struct {
Name string
URL string
Branch string
Name string
URL string
}{}
if len(args) > 0 {
params.Name = args[0]
}
if len(args) > 1 {
params.URL = args[1]
}
params.Branch = viper.GetString("branch")

// Prompt/Confirm
// If confirming/prompting, interactively populate the params from the user
@@ -404,7 +401,7 @@ func runRepositoryAdd(_ *cobra.Command, args []string, newClient ClientFactory)

// Add repository
var n string
if n, err = client.Repositories().Add(params.Name, params.URL, params.Branch); err != nil {
if n, err = client.Repositories().Add(params.Name, params.URL); err != nil {
return
}
if cfg.Verbose {
4 changes: 2 additions & 2 deletions cmd/repository_test.go
Original file line number Diff line number Diff line change
@@ -33,8 +33,9 @@ func TestRepository_List(t *testing.T) {
// arguments, respects the repositories' path flag, and the expected name is echoed
// upon subsequent 'list'.
func TestRepository_Add(t *testing.T) {
url := ServeRepo("repository.git", t)
url := ServeRepo("repository.git#main", t)
_ = fromTempDirectory(t)
t.Log(url)

var (
add = NewRepositoryAddCmd(NewClient)
@@ -47,7 +48,6 @@ func TestRepository_Add(t *testing.T) {

// add [flags] <old> <new>
add.SetArgs([]string{
"--branch=tag",
"newrepo",
url,
})
4 changes: 0 additions & 4 deletions docs/reference/func_create.md
Original file line number Diff line number Diff line change
@@ -31,9 +31,6 @@ DESCRIPTION
-------- --------
go cloudevents
go http
go tanstic/improve
go tanstic/redis
go tanstic/uppercase
node cloudevents
node http
python cloudevents
@@ -44,7 +41,6 @@ DESCRIPTION
rust http
springboot cloudevents
springboot http
springboot tanstic/uppercase
typescript cloudevents
typescript http

5 changes: 2 additions & 3 deletions docs/reference/func_repository_add.md
Original file line number Diff line number Diff line change
@@ -9,9 +9,8 @@ func repository add <name> <url>
### Options

```
--branch string The repository branch to be added.
-c, --confirm Prompt to confirm all options interactively (Env: $FUNC_CONFIRM)
-h, --help help for add
-c, --confirm Prompt to confirm all options interactively (Env: $FUNC_CONFIRM)
-h, --help help for add
```

### Options inherited from parent commands
2 changes: 1 addition & 1 deletion pkg/functions/repositories.go
Original file line number Diff line number Diff line change
@@ -161,7 +161,7 @@ func (r *Repositories) Get(name string) (repo Repository, err error) {
// Add a repository of the given name from the URI. Name, if not provided,
// defaults to the repo name (sans optional .git suffix). Returns the final
// name as added.
func (r *Repositories) Add(name, uri, branch string) (string, error) {
func (r *Repositories) Add(name, uri string) (string, error) {
if r.path == "" {
return "", fmt.Errorf("repository %v(%v) not added. "+
"No repositories path provided", name, uri)
18 changes: 9 additions & 9 deletions pkg/functions/repositories_test.go
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ func TestRepositories_All(t *testing.T) {
}

// Add one
_, err = client.Repositories().Add("", uri, "")
_, err = client.Repositories().Add("", uri)
if err != nil {
t.Fatal(err)
}
@@ -116,7 +116,7 @@ func TestRepositories_Add(t *testing.T) {

// Add the repository, explicitly specifying a name. See other tests for
// defaulting from repository names and manifest-defined name.
if _, err := client.Repositories().Add("example", uri, ""); err != nil {
if _, err := client.Repositories().Add("example", uri); err != nil {
t.Fatal(err)
}

@@ -150,7 +150,7 @@ func TestRepositories_AddDeafultName(t *testing.T) {

client := fn.New(fn.WithRepositoriesPath(root))

name, err := client.Repositories().Add("", uri, "")
name, err := client.Repositories().Add("", uri)
if err != nil {
t.Fatal(err)
}
@@ -185,7 +185,7 @@ func TestRepositories_AddWithManifest(t *testing.T) {

client := fn.New(fn.WithRepositoriesPath(root))

name, err := client.Repositories().Add("", uri, "")
name, err := client.Repositories().Add("", uri)
if err != nil {
t.Fatal(err)
}
@@ -220,10 +220,10 @@ func TestRepositories_AddExistingErrors(t *testing.T) {

// Add twice.
name := "example"
if _, err := client.Repositories().Add(name, uri, ""); err != nil {
if _, err := client.Repositories().Add(name, uri); err != nil {
t.Fatal(err)
}
if _, err := client.Repositories().Add(name, uri, ""); err == nil {
if _, err := client.Repositories().Add(name, uri); err == nil {
t.Fatalf("did not receive expected error adding an existing repository")
}

@@ -253,7 +253,7 @@ func TestRepositories_Rename(t *testing.T) {
client := fn.New(fn.WithRepositoriesPath(root))

// Add and Rename
if _, err := client.Repositories().Add("foo", uri, ""); err != nil {
if _, err := client.Repositories().Add("foo", uri); err != nil {
t.Fatal(err)
}
if err := client.Repositories().Rename("foo", "bar"); err != nil {
@@ -288,7 +288,7 @@ func TestRepositories_Remove(t *testing.T) {

// Add and Remove
name := "example"
if _, err := client.Repositories().Add(name, uri, ""); err != nil {
if _, err := client.Repositories().Add(name, uri); err != nil {
t.Fatal(err)
}
if err := client.Repositories().Remove(name); err != nil {
@@ -320,7 +320,7 @@ func TestRepositories_URL(t *testing.T) {
client := fn.New(fn.WithRepositoriesPath(root))

// Add the test repo
_, err := client.Repositories().Add("newrepo", uri, "")
_, err := client.Repositories().Add("newrepo", uri)
if err != nil {
t.Fatal(err)
}
19 changes: 13 additions & 6 deletions pkg/functions/repository.go
Original file line number Diff line number Diff line change
@@ -126,15 +126,14 @@ type templateConfig = funcDefaults
// Name (optional), if provided takes precedence over name derived from repo at
// the given URI.
//
// URI (optional), the path either locally or remote from which to load
//
// the repository files. If not provided, the internal default is assumed.
// uri (optional), the path either locally or remote from which to load
// the repository files. If not provided, the internal default is assumed.
func NewRepository(name, uri string) (r Repository, err error) {
r = Repository{
uri: uri,
}

fs, err := filesystemFromURI(uri, branch) // Get a Filesystem from the URI
fs, err := filesystemFromURI(uri) // Get a Filesystem from the URI
if err != nil {
return Repository{}, fmt.Errorf("failed to get repository from URI (%q): %w", uri, err)
}
@@ -231,7 +230,7 @@ func FilesystemFromRepo(uri string) (filesystem.Filesystem, error) {
return nil, nil
}
if isBranchNotFoundError(err) {
return nil, fmt.Errorf("failed to clone repository: branch %s not found", branch)
return nil, fmt.Errorf("failed to clone repository: branch not found for uri %s", uri)
}
return nil, fmt.Errorf("failed to clone repository: %w", err)
}
@@ -449,6 +448,13 @@ func checkDir(fs filesystem.Filesystem, path string) error {
}

func getGitCloneOptions(uri string) *git.CloneOptions {
branch := ""
splitUri := strings.Split(uri, "#")
if len(splitUri) > 1 {
uri = splitUri[0]
branch = splitUri[1]
}

opt := &git.CloneOptions{URL: uri, Depth: 1, Tags: git.NoTags,
RecurseSubmodules: git.NoRecurseSubmodules}
if branch != "" {
@@ -562,10 +568,11 @@ func (r *Repository) URL() string {
return "" // Has no .git/config or other error.
}

ref, _ := repo.Head()
if _, ok := c.Remotes["origin"]; ok {
urls := c.Remotes["origin"].URLs
if len(urls) > 0 {
return urls[0]
return urls[0] + "#" + ref.Name().Short()
}
}
return ""