Debian Codegraph @

examples/fake-git/fakegit.go

raw symbols SCIP
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 parser := &docopt.Parser{OptionsFirst: true}
34 args, _ := parser.ParseArgs(usage, nil, "git version 1.7.4.4")
35
36 cmd := args["<command>"].(string)
37 cmdArgs := args["<args>"].([]string)
38
39 fmt.Println("global arguments:", args)
40 fmt.Println("command arguments:", cmd, cmdArgs)
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 := append([]string{cmd}, args...)
66 switch cmd {
67 case "add":
68 // subcommand is a function call
69 return cmdAdd(argv)
70 case "branch":
71 // subcommand is a script
72 return goRun("branch/git_branch.go", argv)
73 case "checkout", "clone", "commit", "push", "remote":
74 // subcommand is a script
75 scriptName := fmt.Sprintf("%s/git_%s.go", cmd, cmd)
76 return goRun(scriptName, argv)
77 case "help", "":
78 return goRun("git.go", append(argv[1:], "--help"))
79 }
80
81 return fmt.Errorf("%s is not a git command. See 'git help'", cmd)
82 }
83
84 func cmdAdd(argv []string) (err error) {
85 usage := `usage: git add [options] [--] [<filepattern>...]
86
87 options:
88 -h, --help
89 -n, --dry-run dry run
90 -v, --verbose be verbose
91 -i, --interactive interactive picking
92 -p, --patch select hunks interactively
93 -e, --edit edit current diff and apply
94 -f, --force allow adding otherwise ignored files
95 -u, --update update tracked files
96 -N, --intent-to-add record only the fact that the path will be added later
97 -A, --all add all, noticing removal of tracked files
98 --refresh don't add, only refresh the index
99 --ignore-errors just skip files which cannot be added because of errors
100 --ignore-missing check if - even missing - files are ignored in dry run
101 `
102
103 args, _ := docopt.ParseDoc(usage)
104 fmt.Println(args)
105 return
106 }