Skip to content

Commit 12105e5

Browse files
Merge pull request #2 from Aaronontheweb/libgit2-playground
[WIP] Libgit2 integration
2 parents 9727427 + 09d306c commit 12105e5

File tree

10 files changed

+281
-10
lines changed

10 files changed

+281
-10
lines changed

Incrementalist.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{79D71264
1717
build.sh = build.sh
1818
EndProjectSection
1919
EndProject
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Incrementalist.Cmd", "src\Incrementalist.Cmd\Incrementalist.Cmd.csproj", "{48A85CA0-FA3A-4F7A-A651-B091C5F3A9CB}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -35,6 +37,10 @@ Global
3537
{CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
3638
{CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
3739
{CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{48A85CA0-FA3A-4F7A-A651-B091C5F3A9CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{48A85CA0-FA3A-4F7A-A651-B091C5F3A9CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{48A85CA0-FA3A-4F7A-A651-B091C5F3A9CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{48A85CA0-FA3A-4F7A-A651-B091C5F3A9CB}.Release|Any CPU.Build.0 = Release|Any CPU
3844
EndGlobalSection
3945
GlobalSection(SolutionProperties) = preSolution
4046
HideSolutionNode = FALSE

NuGet.config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<solution>
4+
<add key="disableSourceControlIntegration" value="true" />
5+
</solution>
6+
<packageSources>
7+
<clear />
8+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
9+
<add key="dotnet-nightly" value="https://dotnet.myget.org/F/msbuild/api/v3/index.json" />
10+
</packageSources>
11+
</configuration>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="..\common.props" />
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<IsPackable>false</IsPackable>
7+
<LangVersion>7.1</LangVersion>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.2" />
11+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.0.0-beta4-final" />
12+
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.0.0-beta4-final" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Incrementalist\Incrementalist.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

src/Incrementalist.Cmd/Program.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
8+
using Incrementalist.FileSystem;
9+
using Incrementalist.Git;
10+
using LibGit2Sharp;
11+
using Microsoft.Build.Locator;
12+
using Microsoft.CodeAnalysis.MSBuild;
13+
14+
namespace Incrementalist.Cmd
15+
{
16+
enum FileType
17+
{
18+
Code,
19+
Project,
20+
Solution,
21+
Other
22+
}
23+
24+
class Program
25+
{
26+
private static string _originalTitle = null;
27+
private static bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
28+
29+
private static void SetTitle()
30+
{
31+
if (IsWindows) // changing console title is not supported on OS X or Linux
32+
{
33+
_originalTitle = Console.Title;
34+
Console.Title = StartupData.ConsoleWindowTitle;
35+
}
36+
}
37+
38+
private static void ResetTitle()
39+
{
40+
if (IsWindows)
41+
Console.Title = _originalTitle; // reset the console window title back
42+
}
43+
44+
static async Task<int> Main(string[] args)
45+
{
46+
SetTitle();
47+
48+
if (args.Length >= 1)
49+
{
50+
if (args[0].ToLowerInvariant().Equals("help"))
51+
{
52+
StartupData.ShowHelp();
53+
ResetTitle();
54+
return 0;
55+
}
56+
}
57+
58+
var insideRepo = Repository.IsValid(Directory.GetCurrentDirectory());
59+
Console.WriteLine("Are we inside repository? {0}", insideRepo);
60+
//if (insideRepo)
61+
//{
62+
var repoFolder = Repository.Discover(Directory.GetCurrentDirectory());
63+
var repository = new Repository(repoFolder);
64+
Console.WriteLine("Repo base is located in {0}", repoFolder);
65+
var workingFolder = Directory.GetParent(repoFolder).Parent;
66+
67+
var affectedFiles = DiffHelper.ChangedFiles(repository, "master").Select(x => Path.GetFullPath(x, workingFolder.FullName)).ToList();
68+
foreach (var file in affectedFiles)
69+
Console.WriteLine("Modified file: {0}", file);
70+
//}
71+
72+
// Locate and register the default instance of MSBuild installed on this machine.
73+
MSBuildLocator.RegisterDefaults();
74+
75+
var msBuild = MSBuildWorkspace.Create();
76+
var progress = new Progress<ProjectLoadProgress>();
77+
progress.ProgressChanged += ProgressOnProgressChanged;
78+
79+
if (!string.IsNullOrEmpty(repoFolder))
80+
{
81+
82+
foreach (var sln in SolutionFinder.GetSolutions(workingFolder.FullName))
83+
{
84+
Console.WriteLine("Found solution file {0}", sln);
85+
86+
var fullSln = await msBuild.OpenSolutionAsync(sln, progress);
87+
var allDocuments = fullSln.Projects.SelectMany(x => x.Documents)
88+
.GroupBy(x => x.FilePath, document => FileType.Code)
89+
.ToDictionary(x => Path.GetFullPath(x.Key, workingFolder.FullName), x => x.First())
90+
.Concat(fullSln.Projects.ToDictionary(x => Path.GetFullPath(x.FilePath, workingFolder.FullName), x => FileType.Project))
91+
.Concat(new Dictionary<string, FileType>{ { Path.GetFullPath(fullSln.FilePath, workingFolder.FullName), FileType.Solution } })
92+
.ToDictionary(x => x.Key, x => x.Value);
93+
94+
Console.WriteLine("Checking to see if affected files are in solution...");
95+
foreach (var affectedFile in affectedFiles)
96+
{
97+
var fileInSln = allDocuments.ContainsKey(affectedFile);
98+
Console.WriteLine("{0} in solution? {1}", affectedFile, fileInSln);
99+
}
100+
}
101+
}
102+
103+
ResetTitle();
104+
return 0;
105+
}
106+
107+
private static void ProgressOnProgressChanged(object sender, ProjectLoadProgress e)
108+
{
109+
Console.WriteLine("{0} {1} {2}", e.ElapsedTime, e.Operation, e.FilePath);
110+
}
111+
}
112+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Text;
4+
5+
namespace Incrementalist.Cmd
6+
{
7+
/// <summary>
8+
/// Used for printing startup messages and help.
9+
/// </summary>
10+
internal static class StartupData
11+
{
12+
public static readonly string VersionNumber =
13+
FileVersionInfo.GetVersionInfo(typeof(StartupData).Assembly.Location).FileVersion;
14+
15+
public static readonly string ConsoleWindowTitle = $"Incrementalist {VersionNumber}";
16+
17+
public static void ShowSplash()
18+
{
19+
Console.OutputEncoding = Encoding.Unicode;
20+
Console.ForegroundColor = ConsoleColor.Cyan;
21+
Console.ForegroundColor = ConsoleColor.Green;
22+
Console.WriteLine("Incrementalist ({0})", VersionNumber);
23+
Console.ResetColor();
24+
Console.WriteLine("Copyright 2015 - {0}, Petabridge®.", DateTime.UtcNow.Year);
25+
Console.WriteLine();
26+
}
27+
28+
public static void ShowHelp()
29+
{
30+
Console.WriteLine(
31+
@"Incrementalist " + VersionNumber + @"
32+
33+
Usage: incrementalist targetBranch [solutionFile]
34+
35+
Options:
36+
incrementalist help Show help
37+
38+
Instructions:
39+
40+
");
41+
}
42+
}
43+
}

src/Incrementalist/Class1.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace Incrementalist.FileSystem
7+
{
8+
/// <summary>
9+
/// Used to look for .sln files in a given directory.
10+
/// </summary>
11+
public static class SolutionFinder
12+
{
13+
/// <summary>
14+
/// The default filter used to look for solutions in a given folder.
15+
/// </summary>
16+
public const string DefaultSolutionFilter = "*.sln";
17+
18+
/// <summary>
19+
/// Enumerate all of the MSBuild solution files in a given folder.
20+
/// </summary>
21+
/// <param name="folderPath">The top level path to search.</param>
22+
/// <param name="searchFilter">Optional. A wildcard filter in the form of "*.sln".</param>
23+
/// <param name="searchOption">Optional. Specifies whether to recurse sub-directories or not.</param>
24+
/// <returns>If any solutions are found, will return an enumerable list of them in order in which they are discovered.</returns>
25+
public static IEnumerable<string> GetSolutions(string folderPath, string searchFilter = null, SearchOption? searchOption = null)
26+
{
27+
if (string.IsNullOrEmpty(searchFilter))
28+
{
29+
return Directory.EnumerateFileSystemEntries(folderPath, DefaultSolutionFilter, searchOption ?? SearchOption.AllDirectories);
30+
}
31+
32+
return Directory.EnumerateFileSystemEntries(folderPath, searchFilter, searchOption ?? SearchOption.AllDirectories);
33+
34+
}
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using LibGit2Sharp;
7+
8+
namespace Incrementalist.Git
9+
{
10+
/// <summary>
11+
/// Generate diffs for the current repository between branches
12+
/// </summary>
13+
public static class DiffHelper
14+
{
15+
public static IEnumerable<string> ChangedFiles(Repository repo, string targetBranch)
16+
{
17+
return repo.Diff.Compare<TreeChanges>(repo.Branches[targetBranch].Tip.Tree, DiffTargets.Index).Select(x => x.Path);
18+
}
19+
}
20+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\common.props" />
33

44

55
<PropertyGroup>
66
<TargetFramework>netstandard2.0</TargetFramework>
7-
<Description>Your description here.</Description>
7+
<Description>Library for running incremental, Git-based builds and tests in .NET and .NET Core.</Description>
88
</PropertyGroup>
99

10+
11+
<ItemGroup>
12+
<PackageReference Include="Libgit2Sharp" Version="0.26.0" />
13+
</ItemGroup>
14+
1015
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace Incrementalist
4+
{
5+
/// <summary>
6+
/// The settings used for this execution of incremental build analysis.
7+
/// </summary>
8+
public class IncrementalistBuildSettings
9+
{
10+
public IncrementalistBuildSettings(string targetBranch, string solutionFile)
11+
{
12+
TargetBranch = targetBranch;
13+
SolutionFile = solutionFile;
14+
}
15+
16+
/// <summary>
17+
/// The target branch to compare the current Git HEAD against,
18+
/// i.e. the `dev` or `master` branch of the repository.
19+
/// </summary>
20+
public string TargetBranch { get; }
21+
22+
/// <summary>
23+
/// The current solution file for us to analyze inside this repository.
24+
/// </summary>
25+
public string SolutionFile { get; }
26+
}
27+
}

0 commit comments

Comments
 (0)