GitSharp/Examples

From eqqon

(Difference between revisions)
Jump to: navigation, search
m (Commit)
m (Playing around with git's objects)
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|Console.WriteLine(master.CurrentCommit.ShortHash);}}
+
{{code|master.CurrentCommit.ShortHash}}
=== Ref ===
=== Ref ===
Line 23: Line 23:
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:
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);}}
+
{{code|(new Ref(repo, "HEAD^").Target as Commit).Message}}
=== Commit ===
=== Commit ===
Actually the example above (to get the message of the previous commit) can be shortened like so:
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);}}
+
{{code|new Commit(repo, "HEAD^").Message}}
Print a list of changes between two commits c1 and c2:
Print a list of changes between two commits c1 and c2:
-
{{code|<nowiki>foreach(var change in c1.CompareAgainst(c2)) { Console.WriteLine(change.ChangeType+": "+change.Path); }</nowiki>}}
+
{{code|foreach(var change in c1.CompareAgainst(c2))<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(change.ChangeType+": "+change.Path);}}
Print all previous commits of HEAD of the repository repo
Print all previous commits of HEAD of the repository repo
-
{{code|<nowiki>foreach(var commit in repo.Head.CurrentCommit.Ancestors) { Console.WriteLine(commit.ShortHash+": "+commit.Message+", "+commit.Author.Name+", "+commit.AuthoredDate); }</nowiki>}}
+
{{code|foreach(var commit in repo.Head.CurrentCommit.Ancestors)<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(commit.ShortHash+": "+commit.Message+", "+commit.Author.Name+", "+commit.AuthoredDate);}}
== Using git commands ==
== Using git commands ==

Revision as of 21:36, 13 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

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);

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();