aboutsummaryrefslogblamecommitdiffstats
path: root/examples/basic/main.go
blob: 4c65a7060f0f4b6138f42e4e35129fcc9ebcac83 (plain) (tree)
1
2
3
4
5
6
7
8



             

                                
 
                                  


             
                                      
 



                                                                              
 


                                                                 
 
























                                                                             

                          
 
package main

import (
	"fmt"

	"github.com/fatih/color"

	"gopkg.in/src-d/go-git.v4"
)

func main() {
	r := git.NewMemoryRepository()

	// Clone the given repository, creating the remote, the local branches
	// and fetching the objects, exactly as:
	// > git clone https://github.com/git-fixtures/basic.git
	color.Blue("git clone https://github.com/git-fixtures/basic.git")

	r.Clone(&git.RepositoryCloneOptions{
		URL: "https://github.com/git-fixtures/basic.git",
	})

	// Getting the latest commit on the current branch
	// > git log -1
	color.Blue("git log -1")

	// ... retrieving the branch being pointed by HEAD
	ref, _ := r.Head()
	// ... retrieving the commit object
	commit, _ := r.Commit(ref.Hash())
	fmt.Println(commit)

	// List the tree from HEAD
	// > git ls-tree -r HEAD
	color.Blue("git ls-tree -r HEAD")

	// ... retrieve the tree from the commit
	tree := commit.Tree()
	// ... create a tree walker, allows to you intereste all nested trees
	walker := git.NewTreeWalker(r, tree)
	walker.ForEach(func(fullpath string, e git.TreeEntry) error {
		// we ignore the tree
		if e.Mode.Perm() == 0 {
			return nil
		}

		fmt.Printf("100644 blob %s    %s\n", e.Hash, fullpath)
		return nil
	})
}