<commit_before>++++
+author = ["<NAME>"]
+date = "2014-12-05T08:00:00+00:00"
+title = "Git2go inception"
+series = ["Advent 2014"]
++++

How many levels of inception we need to master git?

This tutorial that explains how to use [git2go](https://github.com/libgit2/git2go) to publish and article for the Go Advent 2014.

Git2go implements go bindings for [libgit2](https://github.com/libgit2/libgit2), a pure C implementation of the Git core methods. This is important because, unlike other libraries, libgit2 doesn't talk with any git binary.

You won't find the installation complicated if you've worked already with other C libraries. I recommend you to read the installation section in the [readme](https://github.com/libgit2/git2go/blob/master/README.md#installing). There is a detailed list of pre-requisites that you'll need to install.

The first step to publish our new article is to fetch the project from GitHub. We're going to clone the repository for that.

```go
import "github.com/libgit2/git2go"

repo, err := git.Clone("git://github.com/gopheracademy/gopheracademy-web.git", "web", &git.CloneOptions{})
if err != nil {
	panic(err)
}
```

With `web`, we're telling git2go to clone the project in the directory `web` from our current one.

Now that we have the project download in our machine, let's create a branch to put our article. We need to important pieces of information before that. First we need the commit that we'll use as a target for the branch. We also need to create a signature for the author, myself in this case.

```go
import (
	"time"
	"github.com/libgit2/git2go"
)

signature := &git.Signature{
	Name: "<NAME>",
	Email: "<EMAIL>",
	When: time.Now(),
}

head, err := repo.Head()
if err != nil {
	panic(err)
}

headCommit, err := repo.LookupCommit(head.Target())
if err != nil {
	panic(err)
}

branch, err = repo.CreateBranch("git2go-tutorial", head.Target(), false, signature, "Branch for git2go's tutorial")
if err != nil {
	panic(err)
}
```

Once we have the branch created, we need to add our markdown document to the index, sometime referred as the staging area. This is the same operation that you would do using `git add file`.

```go
import "github.com/libgit2/git2go"

idx, err := repo.Index()
if err != nil {
	panic(err)
}

err = idx.AddByPath("content/advent-2014/git2go-tutorial.md")
if err != nil {
	panic(err)
}

treeId, err := idx.WriteTree()
if err != nil {
	panic(err)
}
```

That will put our article in the staging area. The next step is to commit this changes!

We're going to use some information from the branch we just created and the tree where the index is pointing to to create this commit. We'll also reuse my previous signature as a committer and author.

```go
import "github.com/libgit2/git2go"

tree, err := repo.LookupTree(treeId)
if err != nil {
	panic(err)
}

commitTarget, err := repo.LookupCommit(branch.Target())
if err != nil {<commit_msg>Write index on disk to reflect changes after commit<commit_after>++++
+author = ["<NAME>"]
+date = "2014-12-05T08:00:00+00:00"
+title = "Git2go inception"
+series = ["Advent 2014"]
++++

How many levels of inception we need to master git?

This tutorial that explains how to use [git2go](https://github.com/libgit2/git2go) to publish and article for the Go Advent 2014.

Git2go implements go bindings for [libgit2](https://github.com/libgit2/libgit2), a pure C implementation of the Git core methods. This is important because, unlike other libraries, libgit2 doesn't talk with any git binary.

You won't find the installation complicated if you've worked already with other C libraries. I recommend you to read the installation section in the [readme](https://github.com/libgit2/git2go/blob/master/README.md#installing). There is a detailed list of pre-requisites that you'll need to install.

The first step to publish our new article is to fetch the project from GitHub. We're going to clone the repository for that.

```go
import "github.com/libgit2/git2go"

repo, err := git.Clone("git://github.com/gopheracademy/gopheracademy-web.git", "web", &git.CloneOptions{})
if err != nil {
	panic(err)
}
```

With `web`, we're telling git2go to clone the project in the directory `web` from our current one.

Now that we have the project download in our machine, let's create a branch to put our article. We need to important pieces of information before that. First we need the commit that we'll use as a target for the branch. We also need to create a signature for the author, myself in this case.

```go
import (
	"time"
	"github.com/libgit2/git2go"
)

signature := &git.Signature{
	Name: "<NAME>",
	Email: "<EMAIL>",
	When: time.Now(),
}

head, err := repo.Head()
if err != nil {
	panic(err)
}

headCommit, err := repo.LookupCommit(head.Target())
if err != nil {
	panic(err)
}

branch, err = repo.CreateBranch("git2go-tutorial", head.Target(), false, signature, "Branch for git2go's tutorial")
if err != nil {
	panic(err)
}
```

Once we have the branch created, we need to add our markdown document to the index, sometime referred as the staging area. This is the same operation that you would do using `git add file`.

```go
import "github.com/libgit2/git2go"

idx, err := repo.Index()
if err != nil {
	panic(err)
}

err = idx.AddByPath("content/advent-2014/git2go-tutorial.md")
if err != nil {
	panic(err)
}

treeId, err := idx.WriteTree()
if err != nil {
	panic(err)
}

err = idx.Write()
if err != nil {
	panic(err)
}
```

That will put our article in the staging area. The next step is to commit this changes!

We're going to use some information from the branch we just created and the tree where the index is pointing to to create this commit. We'll also reuse my previous signature as a committer and author.

```go
import "github.com/libgit2/git2go"

tree, err := repo.LookupTree(treeId)
if err != nil {
	panic(err)
}

commitTarget, err := repo.LookupCommit(branch.Target())
if err != nil {
{'fraction_non_alphanumeric': 0.11436305193706649, 'fraction_numerical': 0.01793266790729149, 'mean_word_length': 4.355072463768116, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5271210', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1836, 'n_words': 770}