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

Commit d95149d

Browse files
committed
Move IGitClient.Compare method to IGitService
1 parent c222de2 commit d95149d

File tree

8 files changed

+50
-54
lines changed

8 files changed

+50
-54
lines changed

src/GitHub.App/SampleData/GitServiceDesigner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ class GitServiceDesigner : IGitService
1717
public UriString GetUri(IRepository repository, string remote = "origin") => null;
1818
public Task<Patch> Compare(IRepository repository, string sha1, string sha2, string path) => null;
1919
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents) => null;
20+
public Task<TreeChanges> Compare(IRepository repository, string sha1, string sha2, bool detectRenames = false) => null;
2021
}
2122
}

src/GitHub.App/Services/GitClient.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class GitClient : IGitClient
2222
readonly PullOptions pullOptions;
2323
readonly PushOptions pushOptions;
2424
readonly FetchOptions fetchOptions;
25-
readonly CompareOptions compareOptions;
2625

2726
[ImportingConstructor]
2827
public GitClient(IGitHubCredentialProvider credentialProvider, IGitService gitService)
@@ -39,11 +38,6 @@ public GitClient(IGitHubCredentialProvider credentialProvider, IGitService gitSe
3938
FetchOptions = fetchOptions,
4039
MergeOptions = new MergeOptions(),
4140
};
42-
43-
compareOptions = new CompareOptions
44-
{
45-
IndentHeuristic = true
46-
};
4741
}
4842

4943
public Task Pull(IRepository repository)
@@ -199,38 +193,6 @@ public Task CreateBranch(IRepository repository, string branchName)
199193
});
200194
}
201195

202-
public Task<TreeChanges> Compare(
203-
IRepository repository,
204-
string sha1,
205-
string sha2,
206-
bool detectRenames)
207-
{
208-
Guard.ArgumentNotNull(repository, nameof(repository));
209-
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
210-
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
211-
212-
return Task.Run(() =>
213-
{
214-
var options = new CompareOptions
215-
{
216-
Similarity = detectRenames ? SimilarityOptions.Renames : SimilarityOptions.None,
217-
IndentHeuristic = compareOptions.IndentHeuristic
218-
};
219-
220-
var commit1 = repository.Lookup<Commit>(sha1);
221-
var commit2 = repository.Lookup<Commit>(sha2);
222-
223-
if (commit1 != null && commit2 != null)
224-
{
225-
return repository.Diff.Compare<TreeChanges>(commit1.Tree, commit2.Tree, options);
226-
}
227-
else
228-
{
229-
return null;
230-
}
231-
});
232-
}
233-
234196
public Task<T> GetConfig<T>(IRepository repository, string key)
235197
{
236198
Guard.ArgumentNotNull(repository, nameof(repository));
@@ -381,7 +343,7 @@ public Task<bool> IsModified(IRepository repository, string path, byte[] content
381343
using (var s = contents != null ? new MemoryStream(contents) : new MemoryStream())
382344
{
383345
var blob2 = repository.ObjectDatabase.CreateBlob(s, path);
384-
var diff = repository.Diff.Compare(blob1, blob2, compareOptions);
346+
var diff = repository.Diff.Compare(blob1, blob2);
385347
return diff.LinesAdded != 0 || diff.LinesDeleted != 0;
386348
}
387349
}

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public IObservable<TreeChanges> GetTreeChanges(LocalRepositoryModel repository,
631631
{
632632
var remote = await gitClient.GetHttpRemote(repo, "origin");
633633
await gitClient.Fetch(repo, remote.Name);
634-
var changes = await gitClient.Compare(repo, pullRequest.BaseRefSha, pullRequest.HeadRefSha, detectRenames: true);
634+
var changes = await gitService.Compare(repo, pullRequest.BaseRefSha, pullRequest.HeadRefSha, detectRenames: true);
635635
return Observable.Return(changes);
636636
}
637637
});

src/GitHub.Exports.Reactive/Services/IGitClient.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@ public interface IGitClient
7272
/// <returns></returns>
7373
Task CreateBranch(IRepository repository, string branchName);
7474

75-
/// <summary>
76-
/// Compares two commits.
77-
/// </summary>
78-
/// <param name="repository">The repository</param>
79-
/// <param name="sha1">The SHA of the first commit.</param>
80-
/// <param name="sha2">The SHA of the second commit.</param>
81-
/// <param name="detectRenames">Whether to detect renames</param>
82-
/// <returns>
83-
/// A <see cref="TreeChanges"/> object or null if one of the commits could not be found in the repository,
84-
/// (e.g. it is from a fork).
85-
/// </returns>
86-
Task<TreeChanges> Compare(IRepository repository, string sha1, string sha2, bool detectRenames = false);
87-
8875
/// <summary>
8976
/// Gets the value of a configuration key.
9077
/// </summary>

src/GitHub.Exports/Services/GitService.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,37 @@ public Task<ContentChanges> CompareWith(IRepository repository, string sha1, str
289289
return null;
290290
});
291291
}
292+
293+
public Task<TreeChanges> Compare(
294+
IRepository repository,
295+
string sha1,
296+
string sha2,
297+
bool detectRenames)
298+
{
299+
Guard.ArgumentNotNull(repository, nameof(repository));
300+
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
301+
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
302+
303+
return Task.Run(() =>
304+
{
305+
var options = new CompareOptions
306+
{
307+
Similarity = detectRenames ? SimilarityOptions.Renames : SimilarityOptions.None,
308+
IndentHeuristic = defaultCompareOptions.IndentHeuristic
309+
};
310+
311+
var commit1 = repository.Lookup<Commit>(sha1);
312+
var commit2 = repository.Lookup<Commit>(sha2);
313+
314+
if (commit1 != null && commit2 != null)
315+
{
316+
return repository.Diff.Compare<TreeChanges>(commit1.Tree, commit2.Tree, options);
317+
}
318+
else
319+
{
320+
return null;
321+
}
322+
});
323+
}
292324
}
293325
}

src/GitHub.Exports/Services/IGitService.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,18 @@ public interface IGitService
9696
/// A <see cref="Patch"/> object or null if the commit could not be found in the repository.
9797
/// </returns>
9898
Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents);
99+
100+
/// <summary>
101+
/// Compares two commits.
102+
/// </summary>
103+
/// <param name="repository">The repository</param>
104+
/// <param name="sha1">The SHA of the first commit.</param>
105+
/// <param name="sha2">The SHA of the second commit.</param>
106+
/// <param name="detectRenames">Whether to detect renames</param>
107+
/// <returns>
108+
/// A <see cref="TreeChanges"/> object or null if one of the commits could not be found in the repository,
109+
/// (e.g. it is from a fork).
110+
/// </returns>
111+
Task<TreeChanges> Compare(IRepository repository, string sha1, string sha2, bool detectRenames = false);
99112
}
100113
}

test/GitHub.App.UnitTests/Services/GitClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public async Task ContentChangesAsync(int linesAdded, int linesDeleted, bool exp
8282
var changes = Substitute.For<ContentChanges>();
8383
changes.LinesAdded.Returns(linesAdded);
8484
changes.LinesDeleted.Returns(linesDeleted);
85-
repo.Diff.Compare(null as Blob, null as Blob, null as CompareOptions).ReturnsForAnyArgs(changes);
85+
repo.Diff.Compare(null as Blob, null as Blob).ReturnsForAnyArgs(changes);
8686
var gitClient = CreateGitClient();
8787

8888
var modified = await gitClient.IsModified(repo, path, null);

test/GitHub.TeamFoundation.UnitTests/VSGitExtTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,6 @@ public LocalRepositoryModel CreateLocalRepositoryModel(string localPath)
321321
public UriString GetUri(string path, string remote = "origin") => throw new NotImplementedException();
322322
public Task<Patch> Compare(IRepository repository, string sha1, string sha2, string path) => throw new NotImplementedException();
323323
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents) => throw new NotImplementedException();
324+
public Task<TreeChanges> Compare(IRepository repository, string sha1, string sha2, bool detectRenames = false) => throw new NotImplementedException();
324325
}
325326
}

0 commit comments

Comments
 (0)