Skip to content

Commit

Permalink
ggman clone: Allow using native 'git'
Browse files Browse the repository at this point in the history
  • Loading branch information
tkw1536 committed Apr 11, 2019
1 parent 06538d9 commit 6626ec2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ This page includes releases for Linux, Mac OS X and Windows.
Note that these binaries are compressed with [`upx`](https://upx.github.io) in order to decrease executable size.

After obtaining the binary (through either of the two means), simply place it in your `$PATH`.
`ggman` does not depend on any external software (no need for `git` even).
`ggman` does not depend on any external software (although having `git` in `$PATH` allows for passing through arguments to `clone`).

## LICENSE

Expand Down
2 changes: 1 addition & 1 deletion src/commands/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func CloneCommand(runtime *program.SubRuntime) (retval int, err string) {
targetPath := path.Join(append([]string{root}, remote.Components()...)...)

// and finish
return gitwrap.CloneRepository(cloneURI, targetPath)
return gitwrap.CloneRepository(cloneURI, targetPath, argv[1:]...)
}
2 changes: 1 addition & 1 deletion src/constants/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const StringSimulateFlagUsage string = "If set, only print what would be done. "
const StringCompsURIUsage string = "Repository URI to print components of. "

// StringCloneURIUsage represents the Clone URI usage string
const StringCloneURIUsage string = "URI of repository to clone. "
const StringCloneURIUsage string = "URI of repository to clone and arguments to pass to 'git clone'"

// StringLinkPathUsage represents the Link PATH usage string
const StringLinkPathUsage string = "Path of repository to symlink. "
44 changes: 41 additions & 3 deletions src/gitwrap/clone.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package gitwrap

import (
"errors"
"fmt"
"os"
"os/exec"

"github.com/tkw1536/ggman/src/constants"
git "gopkg.in/src-d/go-git.v4"
)

// CloneRepository clones a repository into a given location
func CloneRepository(from string, to string) (retval int, err string) {
func CloneRepository(from string, to string, args ...string) (retval int, err string) {
// tell the user what we are doing
fmt.Printf("Cloning %q into %q ...\n", from, to)

Expand All @@ -27,8 +29,7 @@ func CloneRepository(from string, to string) (retval int, err string) {
return
}

// do the clone
if _, e := git.PlainClone(to, false, &git.CloneOptions{URL: from, Progress: os.Stdout}); e != nil {
if e := cloneRepositorySmart(from, to, args...); e != nil {
err = e.Error()
retval = constants.ErrorCodeCustom
return
Expand All @@ -37,3 +38,40 @@ func CloneRepository(from string, to string) (retval int, err string) {
// and be done
return
}

// clones a repository either internally or externally
func cloneRepositorySmart(from string, to string, args ...string) error {
if hasExternalGit() {
return cloneRepositoryExternal(from, to, args...)
} else if len(args) != 0 {
return errors.New("External 'git' not found, can not pass any additional arguments to 'git clone'. ")
} else {
return cloneRepositoryInternal(from, to)
}
}

// checks if we have an external git in $PATH
func hasExternalGit() bool {
_, err := exec.LookPath("git")
return err == nil
}

// clones a repository using the built-in git
func cloneRepositoryInternal(from string, to string) error {
_, e := git.PlainClone(to, false, &git.CloneOptions{URL: from, Progress: os.Stdout})
return e
}

// clones a repository using an external 'git' command
func cloneRepositoryExternal(from string, to string, args ...string) error {
gargs := append([]string{"clone", from, to}, args...)
cmd := exec.Command("git", gargs...)

cmd.Dir = to

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
}
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Main(argv []string) (retval int, err string) {

ggman.Register("fix", commands.FixCommand, &program.SubOptions{ForArgument: program.OptionalFor, Flag: "--simulate", UsageDescription: constants.StringSimulateFlagUsage, NeedsRoot: true, NeedsCANFILE: true})

ggman.Register("clone", commands.CloneCommand, &program.SubOptions{MinArgs: 1, MaxArgs: 1, Metavar: "URI", UsageDescription: constants.StringCloneURIUsage, NeedsRoot: true, NeedsCANFILE: true})
ggman.Register("clone", commands.CloneCommand, &program.SubOptions{MinArgs: 1, MaxArgs: -1, Metavar: "ARG", UsageDescription: constants.StringCloneURIUsage, NeedsRoot: true, NeedsCANFILE: true})
ggman.Register("link", commands.LinkCommand, &program.SubOptions{MinArgs: 1, MaxArgs: 1, Metavar: "PATH", UsageDescription: constants.StringLinkPathUsage, NeedsRoot: true})

ggman.Register("license", commands.LicenseCommand, &program.SubOptions{})
Expand Down
14 changes: 10 additions & 4 deletions src/program/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@ func (opt *SubOptions) Usage(name string) (usage string) {
mv = "ARGUMENT"
}

// write out the argument an appropriate number of times
flagString += strings.Repeat(" "+mv, opt.MinArgs)
flagString += strings.Repeat(" ["+mv, opt.MaxArgs-opt.MinArgs)
flagString += strings.Repeat("]", opt.MaxArgs-opt.MinArgs)
if opt.MaxArgs == -1 {
// write out the argument an appropriate number of times
flagString += strings.Repeat(" "+mv, opt.MinArgs)
flagString += " [" + mv + " ... ]"
} else {
// write out the argument an appropriate number of times
flagString += strings.Repeat(" "+mv, opt.MinArgs)
flagString += strings.Repeat(" ["+mv, opt.MaxArgs-opt.MinArgs)
flagString += strings.Repeat("]", opt.MaxArgs-opt.MinArgs)
}
}

usage += flagString
Expand Down

0 comments on commit 6626ec2

Please sign in to comment.