Skip to content

Commit 64d41fe

Browse files
Git branch validation (#30)
* Added branch validator * close #28
1 parent a7c53e2 commit 64d41fe

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

src/Incrementalist.Cmd/Commands/EmitAffectedFoldersTask.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public async Task<IEnumerable<string>> Run()
3636
// load the git repository
3737
var repoResult = GitRunner.FindRepository(Settings.WorkingDirectory);
3838

39+
if (!repoResult.foundRepo)
40+
{
41+
Logger.LogError("Unable to find Git repository located in {0}. Shutting down.", Settings.WorkingDirectory);
42+
return new List<string>();
43+
}
44+
45+
// validate the target branch
46+
if (!DiffHelper.HasBranch(repoResult.repo, Settings.TargetBranch))
47+
{
48+
Logger.LogError("Current git repository doesn't have any branch named [{0}]. Shutting down.", Settings.TargetBranch);
49+
return new List<string>();
50+
}
51+
3952
// start the cancellation timer.
4053
_cts.CancelAfter(Settings.TimeoutDuration);
4154
var listAllFilesCmd = new ListAffectedFilesCmd(Logger, _cts.Token, Settings.TargetBranch);

src/Incrementalist.Cmd/Commands/EmitDependencyGraphTask.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Collections.Generic;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Incrementalist.Git;
11+
using Incrementalist.ProjectSystem;
1012
using Incrementalist.ProjectSystem.Cmds;
1113
using Microsoft.CodeAnalysis.MSBuild;
1214
using Microsoft.Extensions.Logging;
@@ -39,6 +41,7 @@ public async Task<IEnumerable<string>> Run()
3941
{
4042
// start the cancellation timer.
4143
_cts.CancelAfter(Settings.TimeoutDuration);
44+
4245
var loadSln = new LoadSolutionCmd(Logger, _workspace, _cts.Token);
4346
var slnFile = await loadSln.Process(Task.FromResult(Settings.SolutionFile));
4447

src/Incrementalist.Cmd/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
// -----------------------------------------------------------------------
66

77
using System;
8+
using System.Collections.Generic;
89
using System.IO;
910
using System.Runtime.InteropServices;
1011
using System.Threading.Tasks;
1112
using CommandLine;
1213
using Incrementalist.Cmd.Commands;
14+
using Incrementalist.Git;
1315
using Incrementalist.ProjectSystem;
1416
using LibGit2Sharp;
1517
using Microsoft.Build.Locator;
@@ -78,9 +80,25 @@ private static async Task<int> RunIncrementalist(SlnOptions options)
7880
return -2;
7981
}
8082

83+
8184
var repoFolder = Repository.Discover(pwd);
8285
var workingFolder = Directory.GetParent(repoFolder).Parent;
8386

87+
var repoResult = GitRunner.FindRepository(workingFolder.FullName);
88+
89+
if (!repoResult.foundRepo)
90+
{
91+
Console.WriteLine("Unable to find Git repository located in {0}. Shutting down.", workingFolder.FullName);
92+
return -3;
93+
}
94+
95+
// validate the target branch
96+
if (!DiffHelper.HasBranch(repoResult.repo, options.GitBranch))
97+
{
98+
Console.WriteLine("Current git repository doesn't have any branch named [{0}]. Shutting down.", options.GitBranch);
99+
return -4;
100+
}
101+
84102

85103
if (!string.IsNullOrEmpty(repoFolder))
86104
{
@@ -137,7 +155,7 @@ private static async Task ProcessSln(SlnOptions options, string sln, DirectoryIn
137155

138156
private static void HandleAffectedFiles(SlnOptions options, string affectedFilesStr)
139157
{
140-
// Check to see if we're planning on writing out to the file system or not.
158+
// Check to see if we're planning on writing out to the file system or not.
141159
if (!string.IsNullOrEmpty(options.OutputFile))
142160
File.WriteAllText(options.OutputFile, affectedFilesStr);
143161
else
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="GitBranchDetectionSpecs.cs" company="Petabridge, LLC">
3+
// Copyright (C) 2015 - 2019 Petabridge, LLC <https://petabridge.com>
4+
// </copyright>
5+
// -----------------------------------------------------------------------
6+
7+
using System;
8+
using FluentAssertions;
9+
using Incrementalist.Git;
10+
using Incrementalist.Tests.Helpers;
11+
using Xunit;
12+
13+
namespace Incrementalist.Tests.Git
14+
{
15+
public class GitBranchDetectionSpecs : IDisposable
16+
{
17+
public GitBranchDetectionSpecs()
18+
{
19+
Repository = new DisposableRepository();
20+
}
21+
22+
public void Dispose()
23+
{
24+
Repository?.Dispose();
25+
}
26+
27+
public DisposableRepository Repository { get; }
28+
29+
[Fact(DisplayName = "Should detect existing Git branch")]
30+
public void ShouldDetectExistingBranch()
31+
{
32+
Repository.CreateBranch("foo");
33+
DiffHelper.HasBranch(Repository.Repository, "foo").Should().BeTrue();
34+
}
35+
}
36+
}

src/Incrementalist/Git/DiffHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ public static IEnumerable<string> ChangedFiles(Repository repo, string targetBra
2121
return repo.Diff.Compare<TreeChanges>(repo.Branches[targetBranch].Tip.Tree, DiffTargets.Index)
2222
.Select(x => Path.GetFullPath(Path.Combine(repo.Info.WorkingDirectory, x.Path)));
2323
}
24+
25+
public static bool HasBranch(Repository repo, string targetBranch)
26+
{
27+
return repo.Branches.Any(x => x.FriendlyName.Equals(targetBranch));
28+
}
2429
}
2530
}

src/Incrementalist/ProjectSystem/Cmds/FilterAffectedProjectFilesCmd.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ protected override async Task<Dictionary<string, SlnFile>> ProcessImpl(
4141
var repoResult = GitRunner.FindRepository(_workingDirectory);
4242
if (!repoResult.foundRepo)
4343
{
44-
var errMsg = $"Unable to find suitable git repository starting in directory {_workingDirectory}";
45-
Logger.LogError(errMsg);
46-
throw new InvalidOperationException(errMsg);
44+
Logger.LogError("Unable to find Git repository located in {0}. Shutting down.", _workingDirectory);
45+
return new Dictionary<string, SlnFile>();
46+
}
47+
48+
// validate the target branch
49+
if (!DiffHelper.HasBranch(repoResult.repo, _targetGitBranch))
50+
{
51+
Logger.LogError("Current git repository doesn't have any branch named [{0}]. Shutting down.", _targetGitBranch);
52+
return new Dictionary<string, SlnFile>();
4753
}
4854

4955
var repo = repoResult.repo;

0 commit comments

Comments
 (0)