Пример #1
0
func CmdLsTree(c *cli.Context) {
	repo, err := git4go.OpenRepositoryExtended(".")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if len(c.Args()) == 0 {
		cli.ShowSubcommandHelp(c)
	} else {
		var tree *git4go.Tree
		var commit *git4go.Commit

		oid, err := git4go.NewOid(c.Args().First())

		if err == nil {
			obj, err := repo.Lookup(oid)
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
			if obj.Type() == git4go.ObjectTree {
				tree = obj.(*git4go.Tree)
			} else if obj.Type() == git4go.ObjectCommit {
				commit = obj.(*git4go.Commit)
			} else {
				os.Stderr.WriteString("fatal: not a tree object")
				os.Exit(1)
			}
		} else {
			ref, err := repo.DwimReference(c.Args().First())
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
			resolved, err := ref.Resolve()
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
			commit, err = repo.LookupCommit(resolved.Target())
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
		}
		if commit != nil {
			tree, err = commit.Tree()
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
		}
		for _, entry := range tree.Entries {
			fileMode := fmt.Sprintf("%06o", int(entry.Filemode))
			fmt.Printf("%s %s %s\t%s\n", fileMode, entry.Type.String(), entry.Id.String(), entry.Name)
		}
	}
}
Пример #2
0
func CmdCatFile(c *cli.Context) {
	repo, err := git4go.OpenRepositoryExtended(".")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if len(c.Args()) < 2 {
		cli.ShowSubcommandHelp(c)
	} else {
		objType := git4go.TypeString2Type(c.Args().First())
		if objType == git4go.ObjectBad {
			fmt.Fprintln(os.Stderr, `fatal: invalid object type "bad"`)
			os.Exit(1)
		}
		odb, err := repo.Odb()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		oid, err := git4go.NewOid(c.Args()[1])
		if err != nil {
			ref, err := repo.DwimReference(c.Args()[1])
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
			resolved, err := ref.Resolve()
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
			oid = resolved.Target()
		}
		obj, err := odb.Read(oid)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		if obj.Type == git4go.ObjectCommit && objType == git4go.ObjectTree {
			commit, _ := repo.LookupCommit(oid)
			obj, err = odb.Read(commit.TreeId())
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
		}
		if obj.Type != objType {
			fmt.Fprintf(os.Stderr, "fatal: got cat-file: %s: bad file\n", c.Args()[1])
			os.Exit(1)
		}
		os.Stdout.Write(obj.Data)
	}
}
Пример #3
0
func CmdTag(c *cli.Context) {
	repo, err := git4go.OpenRepositoryExtended(".")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	switch len(c.Args()) {
	case 0:
		tags, err := repo.ListTag()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		for _, tag := range tags {
			fmt.Println(tag)
		}
	}
}