GitSharp/Examples
From eqqon
m (→Playing around with git's objects) |
m |
||
Line 36: | Line 36: | ||
{{code|foreach(var commit in repo.Head.CurrentCommit.Ancestors)<br> | {{code|foreach(var commit in repo.Head.CurrentCommit.Ancestors)<br> | ||
Console.WriteLine(commit.ShortHash+": "+commit.Message+", "+commit.Author.Name+", "+commit.AuthoredDate);}} | Console.WriteLine(commit.ShortHash+": "+commit.Message+", "+commit.Author.Name+", "+commit.AuthoredDate);}} | ||
+ | |||
+ | === Tree & Leaf === | ||
== Using git commands == | == Using git commands == |
Revision as of 21:37, 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);
Tree & Leaf
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();