Пример #1
0
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)
	}

}
Пример #2
0
func (c *VersionCmd) Run(args []string) {

	workspace, err := sbr.FindWorkspace(os.Getwd())
	if err != nil {
		exit(-1, "%v", err)
	}

	v, err := workspace.Version()
	if err != nil {
		fmt.Printf("Cannot compute version: %v\n", err)
	}
	fmt.Printf("%x\n", v)
}
Пример #3
0
func (c *FetchCmd) Run(args []string) {

	//get the revision to compare to (defaulted to origin/master)

	//creates a workspace to be able to read from/to sets
	workspace, err := sbr.FindWorkspace(os.Getwd())
	if err != nil {
		exit(-1, "%v", err)
	}
	fmt.Printf("Fetching all...")

	executions := ExecConcurrently(workspace, "git", "fetch")
	ExecutionPrinter(executions)
}
Пример #4
0
func (c *FormatCmd) Run(args []string) {
	// use wd by default
	workspace, err := sbr.FindWorkspace(os.Getwd())
	if err != nil {
		exit(CodeNoWorkingDir, "%v", err)
	}

	current, err := workspace.Read()

	f, err := os.Create(workspace.Sbrfile())
	if err != nil {
		fmt.Printf("Error Cannot write dependency file: %s", err.Error())
		os.Exit(-1)
	}
	defer f.Close()
	sbr.WriteTo(f, current)
}
Пример #5
0
func (d *DiffCmd) Run(args []string) {

	// use wd by default
	//creates a workspace to be able to read from/to sets
	workspace, err := sbr.FindWorkspace(os.Getwd())
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(CodeNoWorkingDir)
	}

	if *d.meld {
		//generate a temp file
		current, err := workspace.Scan()
		if err != nil {
			exit(-1, "Cannot scan working dir: %v", err)
		}

		f, err := ioutil.TempFile("", "sbr")
		if err != nil {
			exit(-1, "Cannot generate temp file: %v", err)
		}
		sbr.WriteTo(f, current)
		f.Close() //no defer to open it up just after.
		err = meld.Diff(workspace.Wd(), ".sbr set  |  disk set", workspace.Sbrfile(), f.Name())
		if err != nil {
			fmt.Printf("Meld returned with error: %s", err.Error())
			os.Exit(-1)
		}

		return
	}

	//compute patches (to be made to the working dir)
	current, err := workspace.Read()
	if err != nil {
		exit(-1, "Cannot read .sbr: %v", err)
	}

	dest, err := workspace.Scan()
	if err != nil {
		exit(-1, "Cannot scan working dir: %v", err)
	}

	ins, del, upd := sbr.Diff(current, dest)
	//print them
	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 upd {
			fmt.Fprintf(w, "\033[00;34mEDIT\033[00m\t%s\t%s\t%s\t\n", d.diff(s.Old.Rel(), s.New.Rel()), d.diff(s.Old.Remote(), s.New.Remote()), d.diff(s.Old.Branch(), s.New.Branch()))
		}
		for _, s := range ins {
			fmt.Fprintf(w, "\033[00;31mADD \033[00m\t%s\t%s\t%s\t\n", s.Rel(), s.Remote(), s.Branch())
		}
		for _, s := range del {
			fmt.Fprintf(w, "\033[00;32mDEL \033[00m\t%s\t%s\t%s\t\n", s.Rel(), s.Remote(), s.Branch())
		}
		w.Flush()
	}

	if !*d.apply { // end of the road, below we actually apply changes to .sbr
		return
	}

	//del, ins, upd := workspace.WorkingDirPatches()
	//WorkingDirPatches > (ins, del) are for the wd, here we are interested in the reverse
	// so we permute the assignmeent
	// therefore del are subrepo to be deleted from disk
	// the output will be fully tabbed

	//read ".sbr" content
	//current := workspace.FileSubrepositories()

	current, _ = sbr.RemoveAll(current, del...)
	current = append(current, ins...)
	_, err = sbr.UpdateAll(current, upd...)
	if err != nil {
		exit(-1, "Cannot Update: %v", err)
	}

	//always rewrite the file
	f, err := os.Create(workspace.Sbrfile())
	if err != nil {
		exit(-1, "Error Cannot write dependency file: %v", err)
	}
	defer f.Close()
	sbr.WriteTo(f, current)
	fmt.Printf("Done (\033[00;32m%v\033[00m INS) (\033[00;32m%v\033[00m DEL) (\033[00;32m%v\033[00m UPD)\n", len(ins), len(del), len(upd))
}
Пример #6
0
func (c *StatusCmd) Run(args []string) {

	//get the revision to compare to (defaulted to origin/master)

	//creates a workspace to be able to read from/to sets
	workspace, err := sbr.FindWorkspace(os.Getwd())
	if err != nil {
		exit(-1, "%v", err)
	}

	//get all path, and sort them in alpha order
	all := workspace.ScanRel()

	//little trick to keep project sorted.
	// this is only possible when I execute sync commands.
	sort.Strings(all)

	//basically just running  git rev-list on each subrepo
	// left, right, err := git.RevListCountHead(x, branch)
	// and all the rest is "printing" stuff

	//pretty tab printer
	w := tabwriter.NewWriter(os.Stdout, 4, 8, 2, ' ', 0)

	//we are going to print a gran total
	var tW, tLeft, tRight int

	for _, x := range all {

		// the real deal

		left, right, giterr := git.RevListCountHead(x)
		tLeft += left
		tRight += right
		wd, werr := git.StatusWCL(x)
		tW += wd

		//computes the relative path (prettier to print)
		rel, err := filepath.Rel(workspace.Wd(), x)
		if err != nil {
			rel = x // rel is only use for presentation
		}

		//compute the left,right  string
		l := strconv.FormatInt(int64(left), 10)  //Defualt
		r := strconv.FormatInt(int64(right), 10) //default
		iw := strconv.FormatInt(int64(wd), 10)   //default
		//pretty print 0 as -
		if left == 0 {
			l = "-"
		}
		if right == 0 {
			r = "-"
		}
		if wd == 0 {
			iw = "-"
		}

		errmess := ""
		if werr != nil { //pretty print err as ?
			iw = "?"
			errmess += strings.Replace(werr.Error(), "\n", "; ", -1)
		}
		if giterr != nil { //pretty print err as ?
			l = "?"
			r = "?"
			errmess += strings.Replace(giterr.Error(), "\n", "; ", -1)
		}

		//equals if there is not changes
		equals := left == 0 && right == 0 && wd == 0
		if !*c.short || !equals { // print only if required
			//and print
			fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%v\n", iw, l, r, rel, errmess)
		}
	}

	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "%v\t%v\t%v\t%s\t \n", tW, tLeft, tRight, "Total")
	w.Flush()
	fmt.Println()
}