Skip to content

Commit

Permalink
Rough blame command implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
slluis committed Aug 27, 2010
1 parent 2d0288d commit 35507d1
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions GitSharp/Commit.cs
Expand Up @@ -44,6 +44,7 @@
using GitSharp.Core.RevWalk;
using GitSharp.Core.Util;
using GitSharp.Core.Util.JavaHelper;
using GitSharp.Core.Diff;
using ObjectId = GitSharp.Core.ObjectId;
using CoreRef = GitSharp.Core.Ref;
using CoreTree = GitSharp.Core.Tree;
Expand Down Expand Up @@ -343,6 +344,53 @@ public void Checkout(params string[] paths)

#endregion

#region --> Blame

public Commit[] Blame (string path)
{
Leaf leaf = Tree [path] as Leaf;
if (leaf == null)
throw new ArgumentException("The given path does not exist in this commit: " + path);
byte[] data = leaf.RawData;
int lineCount = RawParseUtils.lineMap (data, 0, data.Length).size ();
Commit[] lines = new Commit [lineCount];
var curText = new RawText (data);
Commit prevAncestor = this;

Leaf prevLeaf = null;
Commit prevCommit = null;
int emptyLines = lineCount;

foreach (Commit ancestor in Ancestors) {
Leaf cleaf = ancestor.Tree [path] as Leaf;
if (prevCommit != null && (cleaf == null || cleaf.Hash != prevLeaf.Hash)) {
byte[] prevData = prevLeaf.RawData;
if (prevData == null)
break;
var prevText = new RawText (prevData);
var differ = new MyersDiff (prevText, curText);
foreach (Edit e in differ.getEdits ()) {
for (int n = e.BeginB; n < e.EndB; n++) {
if (lines [n] == null) {
lines [n] = prevCommit;
emptyLines--;
}
}
}
if (cleaf == null || emptyLines <= 0)
break;
}
prevCommit = ancestor;
prevLeaf = cleaf;
}
for (int n=0; n<lines.Length; n++)
if (lines [n] == null)
lines [n] = prevAncestor;
return lines;
}

#endregion

#region --> Diffing commits


Expand Down

0 comments on commit 35507d1

Please sign in to comment.