Skip to content

Commit

Permalink
Merge pull request #47 from zaquestion/snippets
Browse files Browse the repository at this point in the history
WIP: Support creating project and personal snippets
  • Loading branch information
zaquestion authored Dec 5, 2017
2 parents bb50819 + c7b260f commit 3a73de7
Show file tree
Hide file tree
Showing 45 changed files with 2,192 additions and 377 deletions.
11 changes: 8 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@

[[constraint]]
name = "github.com/xanzy/go-gitlab"
version = "0.6.0"
revision = "5759674b9a31780c812771c21f3b08c0b3978ef7"
2 changes: 1 addition & 1 deletion cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var cloneCmd = &cobra.Command{
// treating forks as origin. Add upstream as remoted pointing
// to forked from repo
if project.ForkedFromProject != nil &&
strings.Contains(project.PathWithNamespace, gitlab.User) {
strings.Contains(project.PathWithNamespace, gitlab.User()) {
if len(args) > 1 {
os.Chdir(args[1])
} else {
Expand Down
4 changes: 2 additions & 2 deletions cmd/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var forkCmd = &cobra.Command{
}

func forkFromOrigin(cmd *cobra.Command, args []string) {
if _, err := gitconfig.Local("remote." + lab.User + ".url"); err == nil {
if _, err := gitconfig.Local("remote." + lab.User() + ".url"); err == nil {
log.Println("remote:", lab.User, "already exists")
return
}
Expand All @@ -53,7 +53,7 @@ func forkFromOrigin(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

err = git.RemoteAdd(lab.User, remote)
err = git.RemoteAdd(lab.User(), remote)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/issueCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var issueCreateCmd = &cobra.Command{
Short: "Open an issue on GitLab",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rn, err := git.PathWithNameSpace(targetRemote)
rn, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/issueList.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var issueListCmd = &cobra.Command{
Short: "List issues",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
rn, err := git.PathWithNameSpace(targetRemote)
rn, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/mrCheckout.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var checkoutCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rn, err := git.PathWithNameSpace(targetRemote)
rn, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand All @@ -40,7 +40,7 @@ var checkoutCmd = &cobra.Command{
// https://docs.gitlab.com/ee/user/project/merge_requests/#checkout-merge-requests-locally
branch := mrs[0].SourceBranch
mr := fmt.Sprintf("refs/merge-requests/%s/head", mrIDStr)
gitf := git.New("fetch", targetRemote, fmt.Sprintf("%s:%s", mr, branch))
gitf := git.New("fetch", forkedFromRemote, fmt.Sprintf("%s:%s", mr, branch))
err = gitf.Run()
if err != nil {
log.Fatal(err)
Expand Down
23 changes: 7 additions & 16 deletions cmd/mrCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ const (
targetBranch = "master"
)

var (
// Will be updated to upstream in init() if upstream remote exists
targetRemote = "origin"
)

// mrCmd represents the mr command
var mrCreateCmd = &cobra.Command{
Use: "create",
Expand All @@ -39,10 +34,6 @@ var mrCreateCmd = &cobra.Command{

func init() {
mrCmd.AddCommand(mrCreateCmd)
_, err := gitconfig.Local("remote.upstream.url")
if err == nil {
targetRemote = "upstream"
}
}

func runMRCreate(cmd *cobra.Command, args []string) {
Expand All @@ -61,7 +52,7 @@ func runMRCreate(cmd *cobra.Command, args []string) {
log.Fatal("aborting MR, branch not present on remote: ", sourceRemote)
}

targetProjectName, err := git.PathWithNameSpace(targetRemote)
targetProjectName, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand All @@ -70,7 +61,7 @@ func runMRCreate(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

msg, err := mrMsg(targetBranch, branch, sourceRemote, targetRemote)
msg, err := mrMsg(targetBranch, branch, sourceRemote, forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -106,16 +97,16 @@ func determineSourceRemote(branch string) string {
}

// If not, check if the fork is named after the user
_, err = gitconfig.Local("remote." + lab.User + ".url")
_, err = gitconfig.Local("remote." + lab.User() + ".url")
if err == nil {
return lab.User
return lab.User()
}

// If not, default to origin
return "origin"
}

func mrMsg(base, head, sourceRemote, targetRemote string) (string, error) {
func mrMsg(base, head, sourceRemote, forkedFromRemote string) (string, error) {
lastCommitMsg, err := git.LastCommitMessage()
if err != nil {
return "", err
Expand All @@ -134,7 +125,7 @@ func mrMsg(base, head, sourceRemote, targetRemote string) (string, error) {

mrTmpl := lab.LoadGitLabTmpl(lab.TmplMR)

remoteBase := fmt.Sprintf("%s/%s", targetRemote, base)
remoteBase := fmt.Sprintf("%s/%s", forkedFromRemote, base)
commitLogs, err := git.Log(remoteBase, head)
if err != nil {
return "", err
Expand All @@ -160,7 +151,7 @@ func mrMsg(base, head, sourceRemote, targetRemote string) (string, error) {
InitMsg: lastCommitMsg,
Tmpl: mrTmpl,
CommentChar: commentChar,
Base: targetRemote + ":" + base,
Base: forkedFromRemote + ":" + base,
Head: sourceRemote + ":" + head,
CommitLogs: commitLogs,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/mrList.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var listCmd = &cobra.Command{
Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rn, err := git.PathWithNameSpace(targetRemote)
rn, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand Down
66 changes: 58 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
"log"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"text/template"
"unicode"

"github.com/pkg/errors"
"github.com/spf13/cobra"
gitconfig "github.com/tcnksm/go-gitconfig"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)

// RootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -49,13 +53,7 @@ var templateFuncs = template.FuncMap{
}

const labUsageTmpl = `{{range .Commands}}{{if (and (or .IsAvailableCommand (ne .Name "help")) (and (ne .Name "clone") (ne .Name "version")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{end}}`
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}`

func labUsage(c *cobra.Command) string {
t := template.New("top")
Expand All @@ -70,11 +68,63 @@ func labUsage(c *cobra.Command) string {
return buf.String()
}

// parseArgsRemote returns the remote and a number if parsed. Many commands
// accept a remote to operate on and number such as a page id
func parseArgsRemote(args []string) (string, int64, error) {
if len(args) == 2 {
n, err := strconv.ParseInt(args[1], 0, 64)
if err != nil {
return "", 0, err
}
ok, err := git.IsRemote(args[0])
if err != nil {
return "", 0, err
} else if !ok {
return "", 0, errors.Errorf("%s is not a valid remote", args[0])
}
return args[0], n, nil
}
if len(args) == 1 {
ok, err := git.IsRemote(args[0])
if err != nil {
return "", 0, err
}
if ok {
return args[0], 0, nil
}
n, err := strconv.ParseInt(args[0], 0, 64)
if err == nil {
return "", n, nil
}
return "", 0, errors.Errorf("%s is not a valid remote or number", args[0])
}
return "", 0, nil
}

var (
// Will be updated to upstream in init() if "upstream" remote exists
forkedFromRemote = "origin"
// Will be updated to lab.User() in init() if forkedFrom is "origin"
forkRemote = "origin"
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
_, err := gitconfig.Local("remote.upstream.url")
if err == nil {
forkedFromRemote = "upstream"
}

if forkedFromRemote == "origin" {
// Check if the user fork exists
_, err = gitconfig.Local("remote." + lab.User() + ".url")
if err == nil {
forkRemote = lab.User()
}
}
if cmd, _, err := RootCmd.Find(os.Args[1:]); err != nil || cmd.Use == "clone" {
// Determine if any undefined flags were passed to "clone
// Determine if any undefined flags were passed to "clone"
if cmd.Use == "clone" && len(os.Args) > 2 {
// ParseFlags will err in these cases
err = cmd.ParseFlags(os.Args[1:])
Expand Down
44 changes: 44 additions & 0 deletions cmd/snippet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"github.com/spf13/cobra"
)

// snippetCmd represents the snippet command
var snippetCmd = &cobra.Command{
Use: "snippet",
Aliases: []string{"snip"},
Short: snippetCreateCmd.Short,
Long: snippetCreateCmd.Long,
Run: func(cmd *cobra.Command, args []string) {

if list, _ := cmd.Flags().GetBool("list"); list {
snippetListCmd.Run(cmd, args)
return
}

if deleteID, _ := cmd.Flags().GetInt("delete"); deleteID != 0 {
// append the args here so that remote can stil be
// properly passed in
snippetDeleteCmd.Run(cmd, append(args, string(deleteID)))
return
}
if len(args) == 0 && file == "" {
cmd.Help()
return
}
snippetCreateCmd.Run(cmd, args)
},
}

var (
global bool
)

func init() {
snippetCmd.PersistentFlags().BoolVarP(&global, "global", "g", false, "create as a personal snippet")
snippetCmd.Flags().BoolP("list", "l", false, "list snippets")
snippetCmd.Flags().IntP("delete", "d", 0, "delete snippet with id")
// Create flags added in snippetCreate.go
RootCmd.AddCommand(snippetCmd)
}
Loading

0 comments on commit 3a73de7

Please sign in to comment.