GitSharp/Examples
From eqqon
m (→Blob) |
(→Repository) |
||
Line 1: | Line 1: | ||
== Playing around with git's objects == | == Playing around with git's objects == | ||
=== Repository === | === Repository === | ||
- | Opening an existing git repository | + | <span class="S0"> </span><span class="S2">//Opening an existing git repository</span><br /> |
- | + | <span class="S0"> </span>repo<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Repository<span class="S10">(</span><span class="S6">"path/to/repo"</span><span class="S10">);</span><br /> | |
- | + | <br /> | |
- | Now suppose you have created some new files and want to commit them | + | <span class="S0"> </span><span class="S2">// Now suppose you have created some new files and want to commit them</span><br /> |
- | + | <span class="S0"> </span>repo<span class="S10">.</span>Index<span class="S10">.</span>Add<span class="S10">(</span><span class="S6">"README"</span><span class="S10">,</span><span class="S0"> </span><span class="S6">"License.txt"</span><span class="S10">);</span><br /> | |
- | + | <span class="S0"> </span>Commit<span class="S0"> </span>commit<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>repo<span class="S10">.</span>Commit<span class="S10">(</span><span class="S6">"My first commit with gitsharp"</span><span class="S10">,</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Author<span class="S10">(</span><span class="S6">"henon"</span><span class="S10">,</span><span class="S0"> </span><span class="S6">"meinrad.recheis@gmail.com"</span><span class="S10">));</span><br /> | |
- | + | <br /> | |
- | + | <span class="S0"> </span><span class="S2">// Easy, isn't it? Now let's have a look at the changes of this commit:</span><br /> | |
- | Easy, isn't it? Now let's have a look at the changes of this commit: | + | <span class="S0"> </span><span class="S5">foreach</span><span class="S0"> </span><span class="S10">(</span>Change<span class="S0"> </span>change<span class="S0"> </span><span class="S5">in</span><span class="S0"> </span>commit<span class="S10">.</span>Changes<span class="S10">)</span><br /> |
- | + | <span class="S0"> </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span>change<span class="S10">.</span>Name<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">" "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>change<span class="S10">.</span>ChangeType<span class="S10">);</span><br /> | |
- | + | <br /> | |
- | + | <span class="S0"> </span><span class="S2">//Get the staged changes from the index</span><br /> | |
- | Get the staged changes from the index | + | <span class="S0"> </span>repo<span class="S10">.</span>Status<span class="S10">.</span>Added<span class="S10">.</span>Contains<span class="S10">(</span><span class="S6">"README"</span><span class="S10">);</span><br /> |
- | + | <br /> | |
- | + | <span class="S0"> </span><span class="S2">//Access and manipulate the configuration</span><br /> | |
- | + | <span class="S0"> </span>repo<span class="S10">.</span>Config<span class="S10">[</span><span class="S6">"core.autocrlf"</span><span class="S10">]</span><span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S6">"false"</span><span class="S10">;</span><br /> | |
- | Access and manipulate the configuration | + | |
- | + | ||
- | + | ||
=== Commit === | === Commit === |
Revision as of 10:51, 5 June 2010
Contents |
Playing around with git's objects
Repository
//Opening an existing git repository
repo = new Repository("path/to/repo");
// Now suppose you have created some new files and want to commit them
repo.Index.Add("README", "License.txt");
Commit 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 (Change 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";
Commit
Get the message of the previous commit
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, "49322bb17d3acc9146f98c97d078513228bbf3c0").Data
Blob also let's you access the raw data as byte array
new Blob(repo, "49322bb17d3acc9146f98c97d078513228bbf3c0").RawData
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
Create a new branch
var b = Branch.Create(repo, "foo");
7 ways to switch to our new branch
b.Checkout();
new Branch(repo, "foo").Checkout();
repo["foo"].Checkout();
Branch.SwitchTo(b);
Branch.SwitchTo("foo");
repo.CheckoutBranch("foo");
repo.CheckoutBranch(b);
Check if foo is current branch
Assert.IsTrue( b.IsCurrent );
Reset the branch to a previous commit (hard or soft)
master.Reset("HEAD^", ResetBehavior.Hard)
master.Reset("HEAD^", ResetBehavior.Soft)
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});