Ejemplo n.º 1
0
func (db *DiffBuiltin) Execute(p *Params, args []string) {
	if len(args) < 2 {
		fmt.Printf("expected two arguments; got %d\n", len(args))
		return
	}
	var e error
	var oa, ob objects.Object
	if oa, e = api.ObjectFromRevision(p.Repo, args[0]); e != nil {
		fmt.Printf(e.Error())
		return
	}
	if ob, e = api.ObjectFromRevision(p.Repo, args[1]); e != nil {
		fmt.Printf(e.Error())
		return
	}
	var ot objects.ObjectType
	if oah, obh := oa.Header().Type(), ob.Header().Type(); oah != obh {
		fmt.Printf("objects are not the same type: \n%s [%s]\n%s [%s]\n", args[0], oah.String(), args[1], obh.String())
		return
	} else {
		ot = oah
	}
	switch ot {
	case objects.ObjectBlob:
		oab, _ := oa.(*objects.Blob)
		obb, _ := ob.(*objects.Blob)
		d := diff.NewBlobComparator().Diff(oab, obb, gdiff.LineSplit)
		gdiff.Unified().Print(d, os.Stdout)
	case objects.ObjectTree:
		oat, _ := oa.(*objects.Tree)
		obt, _ := ob.(*objects.Tree)
		td := diff.NewTreeDiffer(p.Repo, diff.NewBlobComparator())
		if result, err := td.Diff(oat, obt); err != nil {
			fmt.Printf(err.Error())
		} else {
			fmt.Printf(result.String())
		}
		//TODO
	default:
		fmt.Printf("objects are of type %s; only blobs (files) and trees are currently supported", ot)
	}
}
Ejemplo n.º 2
0
func (b *CatFileBuiltin) Execute(p *Params, args []string) {
	CatFile.Parse(args)
	args = CatFile.Args()
	expected := 2
	if b.flagPrettyPrint || b.flagShowSize || b.flagShowType {
		expected = 1
	}
	if len(args) != expected {
		b.HelpInfo.WriteUsage(p.Werr)
		return
	}
	id := args[expected-1]
	o, err := api.ObjectFromRevision(p.Repo, id)
	if err != nil {
		fmt.Fprintln(p.Werr, err)
		return
	}

	switch {
	case b.flagPrettyPrint:
		f := format.NewPrettyFormat(p.Wout)
		f.ObjectPretty(o)
	case b.flagShowType:
		fmt.Fprintln(p.Wout, o.Header().Type())
	case b.flagShowSize:
		fmt.Fprintln(p.Wout, o.Header().Size())
	default:
		t := args[0]
		if o.Header().Type().String() != t {
			b.HelpInfo.WriteUsage(p.Werr)
			return
		}
		f := format.NewFormat(p.Wout)
		f.Object(o)
	}
}