Skip to content

Commit

Permalink
Fix CRLF detection fails for files checked in with CRLF line endings (c…
Browse files Browse the repository at this point in the history
…lose #24)
  • Loading branch information
laurentkempe committed Aug 9, 2014
1 parent 6a60fcc commit 5556418
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions GitDiffMargin/Git/GitCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using EnvDTE;
using LibGit2Sharp;
using Microsoft.VisualStudio.Text;
Expand Down Expand Up @@ -36,22 +35,20 @@ public IEnumerable<HunkRangeInfo> GetGitDiffFor(ITextDocument textDocument, ITex
var content = GetCompleteContent(textDocument, snapshot);
if (content == null) yield break;

content = AdaptCrlf(repo, content, textDocument);

using (var currentContent = new MemoryStream(content))
{
var newBlob = repo.ObjectDatabase.CreateBlob(currentContent);

var directoryInfo = new DirectoryInfo(discoveredPath).Parent;
if (directoryInfo == null) yield break;

var relativeFilepath = filename.Replace(directoryInfo.FullName + "\\", string.Empty);

var newBlob = repo.ObjectDatabase.CreateBlob(currentContent, relativeFilepath);

var from = TreeDefinition.From(repo.Head.Tip.Tree);

if (!repo.ObjectDatabase.Contains(@from[relativeFilepath].TargetId)) yield break;
if (!repo.ObjectDatabase.Contains(from[relativeFilepath].TargetId)) yield break;

var blob = repo.Lookup<Blob>(@from[relativeFilepath].TargetId);
var blob = repo.Lookup<Blob>(from[relativeFilepath].TargetId);

var treeChanges = repo.Diff.Compare(blob, newBlob, new CompareOptions { ContextLines = ContextLines, InterhunkLines = 0 });

Expand All @@ -66,21 +63,6 @@ public IEnumerable<HunkRangeInfo> GetGitDiffFor(ITextDocument textDocument, ITex
}
}

private byte[] AdaptCrlf(IRepository repo, byte[] content, ITextDocument textDocument)
{
var docu = _dte.Documents.AllDocuments().FirstOrDefault(doc => doc.FullName == textDocument.FilePath);

if (docu != null && docu.Language == "HTML") return content;

var autocrlf = repo.Config.Get<string>("core.autocrlf");

if (autocrlf == null || autocrlf.Value != "true") return content;

var contentText = Encoding.UTF8.GetString(content);
content = Encoding.UTF8.GetBytes(contentText.Replace("\r\n", "\n"));
return content;
}

private static byte[] GetCompleteContent(ITextDocument textDocument, ITextSnapshot snapshot)
{
var currentText = snapshot.GetText();
Expand Down

0 comments on commit 5556418

Please sign in to comment.