8000 Update to use modern version of LibGit2Sharp and use indent heuristic when diffing by jcansdale · Pull Request #2142 · github/VisualStudio · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Update to use modern version of LibGit2Sharp and use indent heuristic when diffing #2142

Merged
merged 26 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e8d9527
Update to be compatible with LigGit2Sharp 0.26
jcansdale Dec 18, 2018
4658122
Fix tests that use LibGit2Sharp
jcansdale Dec 18, 2018
b0c037b
Merge branch 'master' into fixes/1469-indent-heuristic
jcansdale Dec 18, 2018
8b6a488
Remove obsolete IQueryableCommitLog.QueryBy imple
jcansdale Dec 18, 2018
32c4aac
Update to use LibGit2Sharp 0.26.0-preview-0080
jcansdale Dec 18, 2018
bbc0504
Commit[] no longer accepts \ in path
jcansdale Dec 19, 2018
128cfbe
Remove unused defaultOriginName field
jcansdale Jan 7, 2019
057844a
Enable IndentHeuristic when comparing
jcansdale Jan 8, 2019
62ccc9b
Move Compare and CompareWith to IGitService
jcansdale Jan 8, 2019
e6ebab6
Add failing Indent_Heuristic_Is_Enabled test
jcansdale Jan 8, 2019
e0eb92c
Add tests for GitService.CompareWith
jcansdale Jan 8, 2019
c222de2
Enable IndentHeuristic and make tests pass
jcansdale Jan 8, 2019
d95149d
Move IGitClient.Compare method to IGitService
jcansdale Jan 8, 2019
df677af
Add test for Compare that returns TreeChanges
jcansdale Jan 8, 2019
ca6de27
Merge branch 'master' into fixes/1469-indent-heuristic
jcansdale Jan 9, 2019
b348527
Add failing test for GitService.Compare
jcansdale Jan 9, 2019
cc43965
Change to use / separator for Git path
jcansdale Jan 9, 2019
7362f56
Merge branch 'master' into fixes/1469-indent-heuristic
jcansdale Jan 9, 2019
f685a22
Throw ArgumentException if path contains \
jcansdale Jan 9, 2019
346bfe1
Factor out Guard.ArgumentIsGitPath
jcansdale Jan 9, 2019
7d87a40
Add Guard.ArgumentIsGitPath to appropriate methods
jcansdale Jan 9, 2019
7006897
Fix issue where a Windows path is being used
jcansdale Jan 9, 2019
8c5d1f7
Add guards to HasChangesInWorkingDirectory
jcansdale Jan 9, 2019
6fa16e8
Merge branch 'master' into fixes/1469-indent-heuristic
jcansdale Mar 19, 2019
f9c9cce
Updatre to use LibGit2Sharp 0.26.0 final
jcansdale Mar 19, 2019
f14b646
Merge branch 'master' into fixes/1469-indent-heuristic
jcansdale Mar 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move Compare and CompareWith to IGitService
IGitService knows about IRepository but not remote servers. IGitClient
knows about remote git repositories. IGitService  is a more appropriate
home for Compare and CompareWith.
  • Loading branch information
jcansdale committed Jan 8, 2019
commit 62ccc9bd341f9531b2929eb9c161a6d244e7042a
2 changes: 2 additions & 0 deletions src/GitHub.App/SampleData/GitServiceDesigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ class GitServiceDesigner : IGitService
public IRepository GetRepository(string path) => null;
public UriString GetUri(string path, string remote = "origin") => null;
public UriString GetUri(IRepository repository, string remote = "origin") => null;
public Task<Patch> Compare(IRepository repository, string sha1, string sha2, string path) => null;
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents) => null;
}
}
60 changes: 0 additions & 60 deletions src/GitHub.App/Services/GitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,66 +231,6 @@ public Task<TreeChanges> Compare(
});
}

public Task<Patch> Compare(
IRepository repository,
string sha1,
string sha2,
string path)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
Guard.ArgumentNotEmptyString(path, nameof(path));

return Task.Run(() =>
{
var commit1 = repository.Lookup<Commit>(sha1);
var commit2 = repository.Lookup<Commit>(sha2);

if (commit1 != null && commit2 != null)
{
return repository.Diff.Compare<Patch>(
commit1.Tree,
commit2.Tree,
new[] { path },
compareOptions);
}
else
{
return null;
}
});
}

public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
Guard.ArgumentNotEmptyString(sha2, nameof(sha1));
Guard.ArgumentNotEmptyString(path, nameof(path));

return Task.Run(() =>
{
var commit1 = repository.Lookup<Commit>(sha1);
var commit2 = repository.Lookup<Commit>(sha2);

var treeChanges = repository.Diff.Compare<TreeChanges>(commit1.Tree, commit2.Tree, compareOptions);
var normalizedPath = path.Replace("/", "\\");
var renamed = treeChanges.FirstOrDefault(x => x.Path == normalizedPath);
var oldPath = renamed?.OldPath ?? path;

if (commit1 != null)
{
var contentStream = contents != null ? new MemoryStream(contents) : new MemoryStream();
var blob1 = commit1[oldPath]?.Target as Blob ?? repository.ObjectDatabase.CreateBlob(new MemoryStream());
var blob2 = repository.ObjectDatabase.CreateBlob(contentStream, path);
return repository.Diff.Compare(blob1, blob2, compareOptions);
}

return null;
});
}

public Task<T> GetConfig<T>(IRepository repository, string key)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Expand Down
25 changes: 0 additions & 25 deletions src/GitHub.Exports.Reactive/Services/IGitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,6 @@ public interface IGitClient
/// </returns>
Task<TreeChanges> Compare(IRepository repository, string sha1, string sha2, bool detectRenames = false);

/// <summary>
/// Compares a file in two commits.
/// </summary>
/// <param name="repository">The repository</param>
/// <param name="sha1">The SHA of the first commit.</param>
/// <param name="sha2">The SHA of the second commit.</param>
/// <param name="path">The relative path to the file.</param>
/// <returns>
/// A <see cref="Patch"/> object or null if one of the commits could not be found in the repository.
/// </returns>
Task<Patch> Compare(IRepository repository, string sha1, string sha2, string path);

/// <summary>
/// Compares a file in a commit to a string.
/// </summary>
/// <param name="repository">The repository</param>
/// <param name="sha1">The SHA of the first commit.</param>
/// <param name="sha2">The SHA of the second commit.</param>
/// <param name="path">The relative path to the file.</param>
/// <param name="contents">The contents to compare with the file.</param>
/// <returns>
/// A <see cref="Patch"/> object or null if the commit could not be found in the repository.
/// </returns>
Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents);

/// <summary>
/// Gets the value of a configuration key.
/// </summary>
Expand Down
63 changes: 62 additions & 1 deletion src/GitHub.Exports/Services/GitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using GitHub.UI;
using GitHub.Models;
Expand All @@ -17,11 +16,13 @@ namespace GitHub.Services
public class GitService : IGitService
{
readonly IRepositoryFacade repositoryFacade;
readonly CompareOptions defaultCompareOptions;

[ImportingConstructor]
public GitService(IRepositoryFacade repositoryFacade)
{
this.repositoryFacade = repositoryFacade;
defaultCompareOptions = new CompareOptions();
}

/// <summary>
Expand Down Expand Up @@ -228,5 +229,65 @@ public Task<string> GetLatestPushedSha(string path, string remote = "origin")
}
});
}

public Task<Patch> Compare(
IRepository repository,
string sha1,
string sha2,
string path)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
Guard.ArgumentNotEmptyString(path, nameof(path));

return Task.Run(() =>
{
var commit1 = repository.Lookup<Commit>(sha1);
var commit2 = repository.Lookup<Commit>(sha2);

if (commit1 != null && commit2 != null)
{
return repository.Diff.Compare<Patch>(
commit1.Tree,
commit2.Tree,
new[] { path },
defaultCompareOptions);
}
else
{
return null;
}
});
}

public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
Guard.ArgumentNotEmptyString(sha2, nameof(sha1));
Guard.ArgumentNotEmptyString(path, nameof(path));

return Task.Run(() =>
{
var commit1 = repository.Lookup<Commit>(sha1);
var commit2 = repository.Lookup<Commit>(sha2);

var treeChanges = repository.Diff.Compare<TreeChanges>(commit1.Tree, commit2.Tree, defaultCompareOptions);
var normalizedPath = path.Replace("/", "\\");
var renamed = treeChanges.FirstOrDefault(x => x.Path == normalizedPath);
var oldPath = renamed?.OldPath ?? path;

if (commit1 != null)
{
var contentStream = contents != null ? new MemoryStream(contents) : new MemoryStream();
var blob1 = commit1[oldPath]?.Target as Blob ?? repository.ObjectDatabase.CreateBlob(new MemoryStream());
var blob2 = repository.ObjectDatabase.CreateBlob(contentStream, path);
return repository.Diff.Compare(blob1, blob2, defaultCompareOptions);
}

return null;
});
}
}
}
25 changes: 25 additions & 0 deletions src/GitHub.Exports/Services/IGitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,30 @@ public interface IGitService
/// <param name="remote">The remote name to look for</param>
/// <returns></returns>
Task<string> GetLatestPushedSha(string path, string remote = "origin");

/// <summary>
/// Compares a file in two commits.
/// </summary>
/// <param name="repository">The repository</param>
/// <param name="sha1">The SHA of the first commit.</param>
/// <param name="sha2">The SHA of the second commit.</param>
/// <param name="path">The relative path to the file.</param>
/// <returns>
/// A <see cref="Patch"/> object or null if one of the commits could not be found in the repository.
/// </returns>
Task<Patch> Compare(IRepository repository, string sha1, string sha2, string path);

/// <summary>
/// Compares a file in a commit to a string.
/// </summary>
/// <param name="repository">The repository</param>
/// <param name="sha1">The SHA of the first commit.</param>
/// <param name="sha2">The SHA of the second commit.</param>
/// <param name="path">The relative path to the file.</param>
/// <param name="contents">The contents to compare with the file.</param>
/// <returns>
/// A <see cref="Patch"/> object or null if the commit could not be found in the repository.
/// </returns>
Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents);
}
}
14 changes: 7 additions & 7 deletions src/GitHub.InlineReviews/Services/DiffService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ namespace GitHub.InlineReviews.Services
[PartCreationPolicy(CreationPolicy.NonShared)]
public class DiffService : IDiffService
{
readonly IGitClient gitClient;
readonly IGitService gitService;

[ImportingConstructor]
public DiffService(IGitClient gitClient)
public DiffService(IGitService gitService)
{
this.gitClient = gitClient;
this.gitService = gitService;
}

/// <inheritdoc/>
Expand All @@ -31,15 +31,15 @@ public async Task<IReadOnlyList<DiffChunk>> Diff(
string headSha,
string path)
{
var patch = await gitClient.Compare(repo, baseSha, headSha, path);
var patch = await gitService.Compare(repo, baseSha, headSha, path);

if (patch != null)
{
return DiffUtilities.ParseFragment(patch).ToList();
}
else
{
return new DiffChunk[0];
return Array.Empty<DiffChunk>();
}
}

Expand All @@ -51,15 +51,15 @@ public async Task<IReadOnlyList<DiffChunk>> Diff(
string path,
byte[] contents)
{
var changes = await gitClient.CompareWith(repo, baseSha, headSha, path, contents);
var changes = await gitService.CompareWith(repo, baseSha, headSha, path, contents);

if (changes?.Patch != null)
{
return DiffUtilities.ParseFragment(changes.Patch).ToList();
}
else
{
return new DiffChunk[0];
return Array.Empty<DiffChunk>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TheParseFragmentMethod
[Test]
public void ShouldParsePr960()
{
var target = new DiffService(Substitute.For<IGitClient>());
var target = new DiffService(Substitute.For<IGitService>());
var result = DiffUtilities.ParseFragment(Properties.Resources.pr_960_diff).ToList();

Assert.That(4, Is.EqualTo(result.Count));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ sealed class FakeDiffService : IDiffService, IDisposable
public FakeDiffService()
{
this.repository = CreateRepository();
this.inner = new DiffService(Substitute.For<IGitClient>());
this.inner = new DiffService(Substitute.For<IGitService>());
}

public FakeDiffService(string path, string contents)
{
this.repository = CreateRepository();
this.inner = new DiffService(Substitute.For<IGitClient>());
this.inner = new DiffService(Substitute.For<IGitService>());
AddFile(path, contents);
}

Expand Down
2 changes: 2 additions & 0 deletions test/GitHub.TeamFoundation.UnitTests/VSGitExtTests.cs
4311
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,7 @@ public LocalRepositoryModel CreateLocalRepositoryModel(string localPath)
public IRepository GetRepository(string path) => throw new NotImplementedException();
public UriString GetUri(IRepository repository, string remote = "origin") => throw new NotImplementedException();
public UriString GetUri(string path, string remote = "origin") => throw new NotImplementedException();
public Task<Patch> Compare(IRepository repository, string sha1, string sha2, string path) => throw new NotImplementedException();
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents) => throw new NotImplementedException();
}
}
0