You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/docopt/docopt-go"
  5. "os"
  6. "os/exec"
  7. )
  8. func main() {
  9. usage := `usage: git [--version] [--exec-path=<path>] [--html-path]
  10. [-p|--paginate|--no-pager] [--no-replace-objects]
  11. [--bare] [--git-dir=<path>] [--work-tree=<path>]
  12. [-c <name>=<value>] [--help]
  13. <command> [<args>...]
  14. options:
  15. -c <name=value>
  16. -h, --help
  17. -p, --paginate
  18. The most commonly used git commands are:
  19. add Add file contents to the index
  20. branch List, create, or delete branches
  21. checkout Checkout a branch or paths to the working tree
  22. clone Clone a repository into a new directory
  23. commit Record changes to the repository
  24. push Update remote refs along with associated objects
  25. remote Manage set of tracked repositories
  26. See 'git help <command>' for more information on a specific command.
  27. `
  28. args, _ := docopt.Parse(usage, nil, true, "git version 1.7.4.4", true)
  29. fmt.Println("global arguments:")
  30. fmt.Println(args)
  31. fmt.Println("command arguments:")
  32. cmd := args["<command>"].(string)
  33. cmdArgs := args["<args>"].([]string)
  34. err := runCommand(cmd, cmdArgs)
  35. if err != nil {
  36. fmt.Println(err)
  37. os.Exit(1)
  38. }
  39. }
  40. func goRun(scriptName string, args []string) (err error) {
  41. cmdArgs := make([]string, 2)
  42. cmdArgs[0] = "run"
  43. cmdArgs[1] = scriptName
  44. cmdArgs = append(cmdArgs, args...)
  45. osCmd := exec.Command("go", cmdArgs...)
  46. var out []byte
  47. out, err = osCmd.Output()
  48. fmt.Println(string(out))
  49. if err != nil {
  50. return
  51. }
  52. return
  53. }
  54. func runCommand(cmd string, args []string) (err error) {
  55. argv := make([]string, 1)
  56. argv[0] = cmd
  57. argv = append(argv, args...)
  58. switch cmd {
  59. case "add":
  60. // subcommand is a function call
  61. return cmdAdd(argv)
  62. case "branch":
  63. // subcommand is a script
  64. return goRun("branch/git_branch.go", argv)
  65. case "checkout", "clone", "commit", "push", "remote":
  66. // subcommand is a script
  67. scriptName := fmt.Sprintf("%s/git_%s.go", cmd, cmd)
  68. return goRun(scriptName, argv)
  69. case "help", "":
  70. return goRun("git.go", []string{"git_add.go", "--help"})
  71. }
  72. return fmt.Errorf("%s is not a git command. See 'git help'", cmd)
  73. }
  74. func cmdAdd(argv []string) (err error) {
  75. usage := `usage: git add [options] [--] [<filepattern>...]
  76. options:
  77. -h, --help
  78. -n, --dry-run dry run
  79. -v, --verbose be verbose
  80. -i, --interactive interactive picking
  81. -p, --patch select hunks interactively
  82. -e, --edit edit current diff and apply
  83. -f, --force allow adding otherwise ignored files
  84. -u, --update update tracked files
  85. -N, --intent-to-add record only the fact that the path will be added later
  86. -A, --all add all, noticing removal of tracked files
  87. --refresh don't add, only refresh the index
  88. --ignore-errors just skip files which cannot be added because of errors
  89. --ignore-missing check if - even missing - files are ignored in dry run
  90. `
  91. args, _ := docopt.Parse(usage, nil, true, "", false)
  92. fmt.Println(args)
  93. return
  94. }