-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(git): Improve change detection (#5)
- Try find Git repository - Guard against not found repository - Get last commit when working directory has no changes
- Loading branch information
Showing
8 changed files
with
106 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/FlowPair/ChangeTracking/FileChange.cs → src/FlowPair/Git/GetChanges/FileChange.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace Ciandt.FlowTools.FlowPair.ChangeTracking; | ||
namespace Ciandt.FlowTools.FlowPair.Git.GetChanges; | ||
|
||
public sealed record FileChange(string Path, string Diff); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Collections.Immutable; | ||
using System.IO.Abstractions; | ||
using AutomaticInterface; | ||
using Ciandt.FlowTools.FlowPair.Common; | ||
using LibGit2Sharp; | ||
using Spectre.Console; | ||
|
||
namespace Ciandt.FlowTools.FlowPair.Git.GetChanges; | ||
|
||
public partial interface IGitGetChangesHandler; | ||
|
||
[GenerateAutomaticInterface] | ||
public class GitGetChangesHandler( | ||
IFileSystem fileSystem, | ||
IAnsiConsole console) | ||
: IGitGetChangesHandler | ||
{ | ||
public Option<ImmutableList<FileChange>> Extract(string? path) | ||
{ | ||
path = TryFindRepository(fileSystem, path).UnwrapOrNull(); | ||
if (path is null) | ||
{ | ||
console.MarkupLine("[red]Error:[/] Could not locate Git repository."); | ||
return None; | ||
} | ||
|
||
using var repo = new Repository(path); | ||
var builder = ImmutableList.CreateBuilder<FileChange>(); | ||
|
||
FillChanges(repo, builder, DiffTargets.Index); | ||
|
||
if (builder.Count == 0) | ||
{ | ||
FillChanges(repo, builder, DiffTargets.WorkingDirectory); | ||
} | ||
|
||
if (builder.Count == 0) | ||
{ | ||
FillChangesFromLastCommit(repo, builder); | ||
} | ||
|
||
console.MarkupLine($"Found {builder.Count} changed files"); | ||
return Some(builder.ToImmutable()); | ||
} | ||
|
||
private static void FillChanges(Repository repo, ImmutableList<FileChange>.Builder builder, DiffTargets diffTargets) | ||
{ | ||
foreach (var changes in repo.Diff | ||
.Compare<Patch>(repo.Head.Tip?.Tree, diffTargets) | ||
.Where(p => !p.IsBinaryComparison && p.Status != ChangeKind.Deleted && p.Mode != Mode.Directory)) | ||
{ | ||
builder.Add(new FileChange(changes.Path, changes.Patch)); | ||
} | ||
} | ||
|
||
private static void FillChangesFromLastCommit(Repository repo, ImmutableList<FileChange>.Builder builder) | ||
{ | ||
var lastCommit = repo.Head.Tip; | ||
var parentCommit = lastCommit.Parents.FirstOrDefault(); | ||
|
||
foreach (var changes in repo.Diff | ||
.Compare<Patch>(parentCommit?.Tree, lastCommit.Tree) | ||
.Where(p => !p.IsBinaryComparison && p.Status != ChangeKind.Deleted && p.Mode != Mode.Directory)) | ||
{ | ||
builder.Add(new FileChange(changes.Path, changes.Patch)); | ||
} | ||
} | ||
|
||
private static Option<string> TryFindRepository(IFileSystem fileSystem, string? path) | ||
{ | ||
var currentDirectory = fileSystem.DirectoryInfo.New(path ?? fileSystem.Directory.GetCurrentDirectory()); | ||
while (currentDirectory != null) | ||
{ | ||
if (currentDirectory | ||
.EnumerateDirectories(".git", SearchOption.TopDirectoryOnly) | ||
.Any()) | ||
{ | ||
return currentDirectory.FullName; | ||
} | ||
|
||
currentDirectory = currentDirectory.Parent; | ||
} | ||
|
||
return None; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Ciandt.FlowTools.FlowPair.Git.GetChanges; | ||
using Jab; | ||
|
||
namespace Ciandt.FlowTools.FlowPair.Git.Infrastructure; | ||
|
||
[ServiceProviderModule] | ||
[Singleton(typeof(IGitGetChangesHandler), typeof(GitGetChangesHandler))] | ||
public interface IGitModule; |