//dorefresh actually run the refresh command, it is unsafe to call it without caution. It should only update errcode, and result func (j *job) dorefresh(w io.Writer) error { // NB: this is a mammoth function. I know it, but I wasn't able to // split it down before having done everything. // // Step by step I will extract subffunctions to appropriate set of objects // wd, err := os.Getwd() if err != nil { return err } _, err = os.Stat(j.name) if os.IsNotExist(err) { // target does not exist, make it. fmt.Fprintf(w, "%s dir does not exists. Will create one.\n", j.name) result, err := git.Clone(wd, j.name, j.remote, j.branch) fmt.Fprintln(w, result) if err != nil { return err } } wk := sbr.NewWorkspace(filepath.Join(wd, j.name)) ch := sbr.NewCheckouter(wk, w) ch.SetFastForwardOnly(true) ch.SetPrune(true) digest, err := ch.Checkout() if err != nil { return err } copy(j.refresh.version[:], digest) return nil }
func (c *CheckoutCmd) Run(args []string) { workspace, err := sbr.FindWorkspace(os.Getwd()) if err != nil { exit(CodeNoWorkingDir, "%v", err) } if *c.dry { // compute patches dirs, err := workspace.Scan() if err != nil { exit(CodeNoWorkingDir, "%v", err) } sbrs, err := workspace.Read() if err != nil { exit(CodeNoWorkingDir, "%v", err) } ins, del, upd := sbr.Diff(dirs, sbrs) if len(del)+len(ins)+len(upd) > 0 { w := tabwriter.NewWriter(os.Stdout, 3, 8, 3, ' ', tabwriter.AlignRight) fmt.Fprintf(w, "\033[00;31mOPS \033[00m\tpath\tremote\tbranch\t\n") for _, s := range del { fmt.Fprintf(w, "\033[00;32mPRUNE \033[00m\t%s\t%s\t%s\t\n", s.Rel(), s.Remote(), s.Branch()) } for _, s := range ins { fmt.Fprintf(w, "\033[00;31mCLONE \033[00m\t%s\t%s\t%s\t\n", s.Rel(), s.Remote(), s.Branch()) } for _, s := range upd { fmt.Fprintf(w, "\033[00;34mCHANGED\033[00m\t%s\t%s\t%s\t\n", s.String()) } w.Flush() } return } if *c.prune { fmt.Printf("PRUNE mode\n") } ch := sbr.NewCheckouter(workspace, os.Stdout) ch.SetPrune(*c.prune) ch.SetFastForwardOnly(*c.ffonly) ch.SetRebase(*c.rebase) _, err = ch.Checkout() if err != nil { fmt.Printf("checkout error: %s", err.Error()) os.Exit(-1) } }
func (c *CloneCmd) Run(args []string) { // use wd by default wd, err := os.Getwd() if err != nil { fmt.Printf("Error, cannot determine the current directory. %s\n", err.Error()) os.Exit(CodeNoWorkingDir) } var rel, remote string switch len(args) { case 0: fmt.Printf("Usage sbr clone [-b branch] <remote> [target]\n") os.Exit(-1) case 1: remote = args[0] rel = strings.TrimSuffix(filepath.Base(remote), ".git") case 2: remote = args[0] rel = args[1] } res, err := git.Clone(wd, rel, remote, *c.branch) fmt.Println(res) if err != nil { fmt.Printf("Error, cannot clone %s: %s\n", remote, err.Error()) os.Exit(-1) } //creates a workspace to be able to read from/to sets workspace := sbr.NewWorkspace(filepath.Join(wd, rel)) ch := sbr.NewCheckouter(workspace, os.Stdout) _, err = ch.Checkout() if err != nil { fmt.Printf("checkout error: %s", err.Error()) os.Exit(-1) } }