Skip to content

Commit 717d0bc

Browse files
committed
add git example
eg: go run git.go git_add.go remote rename old new
1 parent c3becde commit 717d0bc

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed

examples/git/git.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/docopt/docopt.go"
6+
"os"
7+
"os/exec"
8+
)
9+
10+
func main() {
11+
usage := `usage: git [--version] [--exec-path=<path>] [--html-path]
12+
[-p|--paginate|--no-pager] [--no-replace-objects]
13+
[--bare] [--git-dir=<path>] [--work-tree=<path>]
14+
[-c <name>=<value>] [--help]
15+
<command> [<args>...]
16+
17+
options:
18+
-c <name=value>
19+
-h, --help
20+
-p, --paginate
21+
22+
The most commonly used git commands are:
23+
add Add file contents to the index
24+
branch List, create, or delete branches
25+
checkout Checkout a branch or paths to the working tree
26+
clone Clone a repository into a new directory
27+
commit Record changes to the repository
28+
push Update remote refs along with associated objects
29+
remote Manage set of tracked repositories
30+
31+
See 'git help <command>' for more information on a specific command.
32+
`
33+
args, _ := docopt.Parse(usage, nil, true, "git version 1.7.4.4", true)
34+
35+
fmt.Println("global arguments:")
36+
fmt.Println(args)
37+
38+
fmt.Println("command arguments:")
39+
cmd := args["<command>"].(string)
40+
cmdArgs := args["<args>"].([]string)
41+
42+
err := runCommand(cmd, cmdArgs)
43+
if err != nil {
44+
fmt.Println(err)
45+
os.Exit(1)
46+
}
47+
}
48+
49+
func goRun(scriptName string, args []string) (err error) {
50+
cmdArgs := make([]string, 2)
51+
cmdArgs[0] = "run"
52+
cmdArgs[1] = scriptName
53+
cmdArgs = append(cmdArgs, args...)
54+
osCmd := exec.Command("go", cmdArgs...)
55+
var out []byte
56+
out, err = osCmd.Output()
57+
fmt.Println(string(out))
58+
if err != nil {
59+
return
60+
}
61+
return
62+
}
63+
64+
func runCommand(cmd string, args []string) (err error) {
65+
argv := make([]string, 1)
66+
argv[0] = cmd
67+
argv = append(argv, args...)
68+
switch cmd {
69+
case "add":
70+
// subcommand is a function call
71+
return cmdAdd(argv)
72+
case "branch":
73+
// subcommand is a script
74+
return goRun("git_branch.go", argv)
75+
case "checkout", "clone", "commit", "push", "remote":
76+
// subcommand is a script
77+
scriptName := fmt.Sprintf("git_%s.go", cmd)
78+
return goRun(scriptName, argv)
79+
case "help", "":
80+
return goRun("git.go", []string{"git_add.go", "--help"})
81+
}
82+
83+
return fmt.Errorf("%s is not a git command. See 'git help'", cmd)
84+
}

examples/git/git_add.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/docopt/docopt.go"
6+
)
7+
8+
func cmdAdd(argv []string) (err error) {
9+
usage := `usage: git add [options] [--] [<filepattern>...]
10+
11+
options:
12+
-h, --help
13+
-n, --dry-run dry run
14+
-v, --verbose be verbose
15+
-i, --interactive interactive picking
16+
-p, --patch select hunks interactively
17+
-e, --edit edit current diff and apply
18+
-f, --force allow adding otherwise ignored files
19+
-u, --update update tracked files
20+
-N, --intent-to-add record only the fact that the path will be added later
21+
-A, --all add all, noticing removal of tracked files
22+
--refresh don't add, only refresh the index
23+
--ignore-errors just skip files which cannot be added because of errors
24+
--ignore-missing check if - even missing - files are ignored in dry run
25+
`
26+
27+
args, _ := docopt.Parse(usage, nil, true, "", false)
28+
fmt.Println(args)
29+
return
30+
}

examples/git/git_branch.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/docopt/docopt.go"
6+
)
7+
8+
func main() {
9+
usage := `usage: git branch [options] [-r | -a] [--merged=<commit> | --no-merged=<commit>]
10+
git branch [options] [-l] [-f] <branchname> [<start-point>]
11+
git branch [options] [-r] (-d | -D) <branchname>
12+
git branch [options] (-m | -M) [<oldbranch>] <newbranch>
13+
14+
Generic options:
15+
-h, --help
16+
-v, --verbose show hash and subject, give twice for upstream branch
17+
-t, --track set up tracking mode (see git-pull(1))
18+
--set-upstream change upstream info
19+
--color=<when> use colored output
20+
-r act on remote-tracking branches
21+
--contains=<commit> print only branches that contain the commit
22+
--abbrev=<n> use <n> digits to display SHA-1s
23+
24+
Specific git-branch actions:
25+
-a list both remote-tracking and local branches
26+
-d delete fully merged branch
27+
-D delete branch (even if not merged)
28+
-m move/rename a branch and its reflog
29+
-M move/rename a branch, even if target exists
30+
-l create the branch's reflog
31+
-f, --force force creation (when already exists)
32+
--no-merged=<commit> print only not merged branches
33+
--merged=<commit> print only merged branches
34+
`
35+
36+
args, _ := docopt.Parse(usage, nil, true, "", false)
37+
fmt.Println(args)
38+
}

examples/git/git_checkout.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/docopt/docopt.go"
6+
)
7+
8+
func main() {
9+
usage := `usage: git checkout [options] <branch>
10+
git checkout [options] <branch> -- <file>...
11+
12+
options:
13+
-q, --quiet suppress progress reporting
14+
-b <branch> create and checkout a new branch
15+
-B <branch> create/reset and checkout a branch
16+
-l create reflog for new branch
17+
-t, --track set upstream info for new branch
18+
--orphan <new branch>
19+
new unparented branch
20+
-2, --ours checkout our version for unmerged files
21+
-3, --theirs checkout their version for unmerged files
22+
-f, --force force checkout (throw away local modifications)
23+
-m, --merge perform a 3-way merge with the new branch
24+
--conflict <style> conflict style (merge or diff3)
25+
-p, --patch select hunks interactively
26+
`
27+
28+
args, _ := docopt.Parse(usage, nil, true, "", false)
29+
fmt.Println(args)
30+
}

examples/git/git_clone.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/docopt/docopt.go"
6+
)
7+
8+
func main() {
9+
usage := `usage: git clone [options] [--] <repo> [<dir>]
10+
11+
options:
12+
-v, --verbose be more verbose
13+
-q, --quiet be more quiet
14+
--progress force progress reporting
15+
-n, --no-checkout don't create a checkout
16+
--bare create a bare repository
17+
--mirror create a mirror repository (implies bare)
18+
-l, --local to clone from a local repository
19+
--no-hardlinks don't use local hardlinks, always copy
20+
-s, --shared setup as shared repository
21+
--recursive initialize submodules in the clone
22+
--recurse-submodules initialize submodules in the clone
23+
--template <template-directory>
24+
directory from which templates will be used
25+
--reference <repo> reference repository
26+
-o, --origin <branch>
27+
use <branch> instead of 'origin' to track upstream
28+
-b, --branch <branch>
29+
checkout <branch> instead of the remote's HEAD
30+
-u, --upload-pack <path>
31+
path to git-upload-pack on the remote
32+
--depth <depth> create a shallow clone of that depth
33+
`
34+
35+
args, _ := docopt.Parse(usage, nil, true, "", false)
36+
fmt.Println(args)
37+
}

examples/git/git_push.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/docopt/docopt.go"
6+
)
7+
8+
func main() {
9+
usage := `usage: git push [options] [<repository> [<refspec>...]]
10+
11+
options:
12+
-h, --help
13+
-v, --verbose be more verbose
14+
-q, --quiet be more quiet
15+
--repo <repository> repository
16+
--all push all refs
17+
--mirror mirror all refs
18+
--delete delete refs
19+
--tags push tags (can't be used with --all or --mirror)
20+
-n, --dry-run dry run
21+
--porcelain machine-readable output
22+
-f, --force force updates
23+
--thin use thin pack
24+
--receive-pack <receive-pack>
25+
receive pack program
26+
--exec <receive-pack>
27+
receive pack program
28+
-u, --set-upstream set upstream for git pull/status
29+
--progress force progress reporting
30+
`
31+
32+
args, _ := docopt.Parse(usage, nil, true, "", false)
33+
fmt.Println(args)
34+
}

examples/git/git_remote.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/docopt/docopt.go"
6+
)
7+
8+
func main() {
9+
usage := `usage: git remote [-v | --verbose]
10+
git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>
11+
git remote rename <old> <new>
12+
git remote rm <name>
13+
git remote set-head <name> (-a | -d | <branch>)
14+
git remote [-v | --verbose] show [-n] <name>
15+
git remote prune [-n | --dry-run] <name>
16+
git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
17+
git remote set-branches <name> [--add] <branch>...
18+
git remote set-url <name> <newurl> [<oldurl>]
19+
git remote set-url --add <name> <newurl>
20+
git remote set-url --delete <name> <url>
21+
22+
options:
23+
-v, --verbose be verbose; must be placed before a subcommand
24+
`
25+
26+
args, _ := docopt.Parse(usage, nil, true, "", false)
27+
fmt.Println(args)
28+
}

0 commit comments

Comments
 (0)