GitSharp/Examples

From eqqon

(Difference between revisions)
Jump to: navigation, search
m (Repository)
m (Repository)
Line 15: Line 15:
Get the staged changes from the index
Get the staged changes from the index
-
{{code|repository.Status.Added.Contains("README")}}
+
{{code|repo.Status.Added.Contains("README")}}
Access and manipulate the configuration
Access and manipulate the configuration
-
{{code|repository.Config["core.autocrlf"]<nowiki>=</nowiki>"false"}}
+
{{code|repo.Config["core.autocrlf"]<nowiki>=</nowiki>"false"}}
=== Branch ===
=== Branch ===

Revision as of 21:06, 28 October 2009

Contents

Playing around with git's objects

Repository

Opening an existing git repository

var repo=new Repository("path/to/repo")

Now suppose you have created some new files and want to stage them for committing:

repo.Index.Add("README", "License.txt");
var commit=repo.Commit("My first commit with gitsharp", new Author("henon", "meinrad.recheis@gmail.com"));

Easy, isn't it? Now let's have a look at the changes of this commit:

foreach(var change in commit.Changes) Console.WriteLine(change.Name + " " + change.ChangeType);

Get the staged changes from the index

repo.Status.Added.Contains("README")

Access and manipulate the configuration

repo.Config["core.autocrlf"]="false"

Branch

Get the current branch

var branch=repo.CurrentBranch
Console.WriteLine("Current branch is "+branch.Name);

Another way to get the current branch

repo.Head

Get master branch

var master = new Branch(repo, "master");

Get the abbreviated hash of the last commit on master

master.CurrentCommit.ShortHash

Ref

You can use Ref to resolve typical git references without needing to know their type. The following expression should be true if "version1.0.0" is a Tag and not a Branch.

new Ref(repo, "version1.0.0").Target.IsTag

Branch is a Ref, but not all Ref's necessarily need to be branches. For instance, we want to get the message of the previous commit:

(new Ref(repo, "HEAD^").Target as Commit).Message

Commit

Actually the example above (to get the message of the previous commit) can be shortened like so:

new Commit(repo, "HEAD^").Message

Print a list of changes between two commits c1 and c2:

foreach(var change in c1.CompareAgainst(c2))
    Console.WriteLine(change.ChangeType+": "+change.Path);

Print all previous commits of HEAD of the repository repo

foreach(var commit in repo.Head.CurrentCommit.Ancestors)
    Console.WriteLine(commit.ShortHash+": "+commit.Message+", "+commit.Author.Name+", "+commit.AuthoredDate);

Tree & Leaf

Get the root tree of the most recent commit

var tree = repo.Head.CurrentCommit.Tree

It has no Parent so IsRoot should be true

Debug.Assert(tree.Parent==null);
Debug.Assert(tree.IsRoot);

Now you can browse throught that tree by iterating over its child trees

foreach(var subtree in tree.Trees) { ... }

Or printing the names of the files it contains

foreach(var leaf in tree.Leaves) { Console.WriteLine( leaf.Path); }

Blob

A Leaf is a Blob and inherits from it a method to retrieve the data as a UTF8 encoded string:

new Blob(repo, "e287f54").Data

Blob also let's you access the raw data as byte array

new Blob(repo, "e287f54").RawData

Using git commands

Init

Initializing a new repository in the current directory (if GID_DIR environment variable is not set)

GitSharp.Commands.Init();

Initializing a new repository in the specified location

GitSharp.Commands.Init("path/to/repo");

Initializing a new repository with options

var cmd = new GitSharp.InitCommand("path/to/repo") { Quiet=false, Bare=true };
cmd.Execute();

Clone

Clone a repository from a public repository via http

GitSharp.Commands.Clone("git://github.com/henon/GitSharp.git", "path/to/local/copy");

Or using options

GitSharp.Commands.Clone(new CloneCommand() { Source="git://github.com/henon/GitSharp.git", GitDirectory="path/to/local/copy", Quiet=false, Bare=true});