Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
feat: clone project over ssh (#164)
Browse files Browse the repository at this point in the history
Clone a repository using the ssh url
. Add upstream as remote pointing to forked repo's ssh url

enhances #70
  • Loading branch information
profclems authored Aug 24, 2020
2 parents 96a7b7b + d515d54 commit e113401
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
16 changes: 9 additions & 7 deletions cmd/glab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) {
return
}

re := regexp.MustCompile(`(?s){(.*)}`)
m := re.FindAllStringSubmatch(err.Error(), -1)
if len(m) != 0 {
if len(m[0]) >= 1 {
_, _ = fmt.Fprintln(out, m[0][1])
if !debug {
re := regexp.MustCompile(`(?s){(.*)}`)
m := re.FindAllStringSubmatch(err.Error(), -1)
if len(m) != 0 {
if len(m[0]) >= 1 {
_, _ = fmt.Fprintln(out, m[0][1])
return
}
}
} else {
_, _ = fmt.Fprintln(out, err)
}
_, _ = fmt.Fprintln(out, err)

var flagError *utils.FlagError
if errors.As(err, &flagError) || strings.HasPrefix(err.Error(), "unknown command ") {
Expand Down
6 changes: 3 additions & 3 deletions commands/repo_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var repoArchiveCmd = &cobra.Command{
Short: `Get an archive of the repository.`,
Example: heredoc.Doc(`
$ glab repo archive profclems/glab
$ glab repo archive // Downloads zip file of current repository
$ glab repo clone profclems/glab mydirectory // Clones repo into mydirectory
$ glab repo clone profclems/glab --format=zip // Finds repo for current user and download in zip format
$ glab repo archive # Downloads zip file of current repository
$ glab repo archive profclems/glab mydirectory # Downloads repo zip file into mydirectory
$ glab repo archive profclems/glab --format=zip # Finds repo for current user and download in zip format
`),
Long: heredoc.Doc(`
Clone supports these shorthands
Expand Down
69 changes: 57 additions & 12 deletions commands/repo_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,90 @@ package commands

import (
"fmt"
"strconv"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"glab/internal/config"
"github.com/xanzy/go-gitlab"
"glab/internal/git"
"strings"
)

var repoCloneCmd = &cobra.Command{
Use: "clone <command> [flags]",
Short: `Clone or download a repository/project`,
Short: `Clone a Gitlab repository/project`,
Example: heredoc.Doc(`
$ glab repo clone profclems/glab
$ glab repo clone https://gitlab.com/profclems/glab
$ glab repo clone profclems/glab mydirectory // Clones repo into mydirectory
$ glab repo clone glab --format=zip // Finds repo for current user and download in zip format
$ glab repo clone profclems/glab mydirectory # Clones repo into mydirectory
$ glab repo clone glab # clones repo glab for current user
$ glab repo clone 4356677 # finds the project by the ID provided and clones it
`),
Long: heredoc.Doc(`
Clone supports these shorthands
- repo
- namespace/repo
- namespace/group/repo
- url/namespace/group/repo
- project ID
`),
Args: cobra.MaximumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmdErr(cmd, args)
return
return nil
}

var (
project *gitlab.Project = nil
err error
)

repo := args[0]
fmt.Println(repo)
u, _ := currentUser()
if !git.IsValidURL(repo) {
repo = config.GetEnv("GITLAB_URI") + "/" + repo
}
if !strings.HasSuffix(repo, ".git") {
// Assuming that repo is a project ID if it is an integer
if _, err := strconv.ParseInt(repo, 10, 64); err != nil {
// Assuming that "/" in the project name means its owned by an organisation
if !strings.Contains(repo, "/") {
repo = fmt.Sprintf("%s/%s", u, repo)
}
}
project, err = getProject(repo)
if err != nil {
return err
}
repo = project.SSHURLToRepo
} else if !strings.HasSuffix(repo, ".git") {
repo += ".git"
}
git.RunClone(repo, args[1:])
_, err = git.RunClone(repo, args[1:])
if err != nil {
return err
}
// Cloned project was a fork belonging to the user; user is
// treating fork's ssh url as origin. Add upstream as remote pointing
// to forked repo's ssh url
if project != nil {
if project.ForkedFromProject != nil &&
strings.Contains(project.PathWithNamespace, u) {
var dir string
if len(args) > 1 {
dir = args[1]
} else {
dir = "./" + project.Path
}
fProject, err := getProject(project.ForkedFromProject.PathWithNamespace)
if err != nil {
return err
}
err = git.AddUpstreamRemote(fProject.SSHURLToRepo, dir)
if err != nil {
return err
}
}
}
return nil
},
}

Expand Down
2 changes: 1 addition & 1 deletion commands/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"glab/internal/git"
)

func currentUser(token string) (string, error) {
func currentUser() (string, error) {
gLab, _ := git.InitGitlabClient()
u, _, err := gLab.Users.CurrentUser()
if err != nil {
Expand Down

0 comments on commit e113401

Please sign in to comment.