//Scan the working dir and return subrepositories found func (x *Workspace) Scan() (sbrs []Sub, err error) { sbrs = make([]Sub, 0, 100) for _, prj := range x.ScanRel() { // this is a git repo, read all three fields branch, err := git.Branch(prj) if err != nil { return sbrs, fmt.Errorf("%s doesn't have branches: %s", prj, err.Error()) } origin, err := git.RemoteOrigin(prj) if err != nil { return sbrs, fmt.Errorf("%s doesn't declare a remote 'origin': %s", prj, err.Error()) } rel, err := filepath.Rel(x.wd, prj) if err != nil { return sbrs, fmt.Errorf("%s not in the current directory %s\n", err.Error()) } if rel != "." { sbrs = append(sbrs, New(rel, origin, branch)) } } Sort(sbrs) return }
//Update remote updates the remote origin func (ch *Checkouter) UpdateRemote(delta Delta) (updated bool, err error) { path := ch.locate(delta.Old.rel) oldremote, err := git.RemoteOrigin(path) if err != nil { return } remote := delta.New.Remote() if remote == oldremote { return false, nil // nothing to do } err = git.RemoteSetOrigin(path, remote) if err != nil { return } return true, nil }
func (c *SubscribeCmd) Run(args []string) { wd := FindRootCmd() var err error if *c.server == "" { // no one specified use the one in config get *c.server, err = git.ConfigGet(wd, "ci.server") if err != nil { fmt.Printf("Error, cannot read ci remote address in git config. %s\n Use \ngit config --add ci.server <ci address>\n", err.Error()) os.Exit(CodeMissingServerConfig) } } else { // there was one, store it err := git.ConfigAdd(wd, "ci.server", *c.server) if err != nil { fmt.Printf("Warning, cannot add ci remote address in git config. %v\n", err) } fmt.Printf("git config' key \"ci.server\" = %s\n", *c.server) } if *c.jobname == "" { // no one specified use the one in config get *c.jobname, err = git.ConfigGet(wd, "ci.job.name") if err != nil { fmt.Printf("Error, cannot read ci remote job name in git config. %s\n Use \ngit config --add ci.job.name <jobname>\n", err.Error()) os.Exit(CodeMissingJobConfig) } } else { // there was one, store it git.ConfigAdd(wd, "ci.job.name", *c.jobname) if err != nil { fmt.Printf("Warning, cannot add ci remote job name in git config. %v\n", err) } fmt.Printf("git config' key \"ci.job.name\" = %s\n", *c.jobname) } cl := ci.NewClient(*c.server) if *c.remove { // with only remove I don't need to go further cl.RemoveJob(*c.jobname) if err != nil { fmt.Printf("Error, cannot delete remote job %s. %v\n", *c.jobname, err) os.Exit(CodeCannotDelete) } return } branch, err := git.Branch(wd) if err != nil { fmt.Printf("Error, cannot get local branch. %v\n", err) os.Exit(CodeMissingBranch) } remote, err := git.RemoteOrigin(wd) if err != nil { fmt.Printf("Error, cannot get remote/origin url. %v\n", err) os.Exit(CodeMissingRemoteOrigin) } // I now have server job name, and branch, they have been stored for later use if *c.force { // needs a delete first err := cl.RemoveJob(*c.jobname) // don't care about the error if err != nil { fmt.Printf("Warning: deletion error: %v", err) } } err = cl.AddJob(*c.jobname, remote, branch) if err != nil { fmt.Println(err) os.Exit(CodeCannotAddJob) } // notify success fmt.Printf("added %s %s %s\n", *c.jobname, remote, branch) }