GitSharp/Examples

From eqqon

(Difference between revisions)
Jump to: navigation, search
(Using git commands)
(Branch)
Line 16: Line 16:
Get the abbreviated hash of the last commit on master
Get the abbreviated hash of the last commit on master
-
{{code|master.CurrentCommit.ShortHash}}
+
{{code|Console.WriteLine(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.
 +
{{code|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:
 +
{{code|Console.WriteLine((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:
 +
{{code|Console.WriteLine((new Commit(repo, "HEAD^").Message);}}
== Using git commands ==
== Using git commands ==

Revision as of 19:09, 4 October 2009

Contents

Playing around with git's objects

Repository

Opening an existing git repository

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

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

Console.WriteLine(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:

Console.WriteLine((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:

Console.WriteLine((new Commit(repo, "HEAD^").Message);

Using git commands

Init

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

Git.Commands.Init();

Initializing a new repository in the specified location

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

Initializing a new repository with options

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