Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add skipAutoStage flag #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 30 additions & 28 deletions docs/guide/task-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

Each task in `task-runner.json` is a JSON object with the following properties:

| name | optional | type | default | description |
|----------|----------|--------------------------|------------------------|------------------------------------------------------------------------|
| command | false | string | - | path to the executable file or script or executable name |
| args | true | [string array] | - | command arguments |
| include | true | [array of glob] | `**/*` | glob pattern to select files |
| name | true | string | command | name of the task (recommended) |
| group | true | string | - | group of the task (usually it should be the hook name) |
| branch | true | string (regex) | - | run task on specific branches only |
| pathMode | true | [absolute, relative] | relative | file path style (relative or absolute) |
| cwd | true | string | project root directory | current working directory for the command, can be relative or absolute |
| output | true | [always, verbose, never] | always | output log level |
| exclude | true | [array of glob] | - | glob pattern to exclude files |
| windows | true | object | - | overrides all the above settings for windows |
| name | optional | type | default | description |
| --------------------------- | -------- | ------------------------ | ---------------------- | ---------------------------------------------------------------------- |
| command | false | string | - | path to the executable file or script or executable name |
| args | true | [string array] | - | command arguments |
| include | true | [array of glob] | `**/*` | glob pattern to select files |
| name | true | string | command | name of the task (recommended) |
| group | true | string | - | group of the task (usually it should be the hook name) |
| branch | true | string (regex) | - | run task on specific branches only |
| pathMode | true | [absolute, relative] | relative | file path style (relative or absolute) |
| cwd | true | string | project root directory | current working directory for the command, can be relative or absolute |
| output | true | [always, verbose, never] | always | output log level |
| exclude | true | [array of glob] | - | glob pattern to exclude files |
| windows | true | object | - | overrides all the above settings for windows |
| skipAutoStage | true | bool | false | Re-staging staged files |
| ignoreValidateCommandResult | true | bool | false | Ignores validate command result |

## Glob patterns

Expand All @@ -25,24 +27,24 @@ Husky.Net supports the standard dotnet `FileSystemGlobbing` patterns for include
- path/to/file.txt
- Wildcards * in file and directory names that represent zero to many characters not including separator characters.

| Value | Description |
|-------|-------------|
| *.txt | All files with .txt file extension. |
| *.* | All files with an extension. |
| * | All files in top-level directory. |
| .* | File names beginning with '.'. |
| *word*| All files with 'word' in the filename. |
| readme.* | All files named 'readme' with any file extension. |
| styles/*.css | All files with extension '.css' in the directory 'styles/'. |
| scripts/*/* | All files in 'scripts/' or one level of subdirectory under 'scripts/'. |
| images*/* | All files in a folder with name that is or begins with 'images'. |
| Value | Description |
| ------------ | ---------------------------------------------------------------------- |
| *.txt | All files with .txt file extension. |
| *.* | All files with an extension. |
| * | All files in top-level directory. |
| .* | File names beginning with '.'. |
| *word* | All files with 'word' in the filename. |
| readme.* | All files named 'readme' with any file extension. |
| styles/*.css | All files with extension '.css' in the directory 'styles/'. |
| scripts/*/* | All files in 'scripts/' or one level of subdirectory under 'scripts/'. |
| images*/* | All files in a folder with name that is or begins with 'images'. |

- Arbitrary directory depth (/**/).

| Value | Description |
|-------|-------------|
|**/* | All files in any subdirectory.|
|dir/**/* | All files in any subdirectory under 'dir/'.|
| Value | Description |
| -------- | ------------------------------------------- |
| **/* | All files in any subdirectory. |
| dir/**/* | All files in any subdirectory under 'dir/'. |

- Relative paths.

Expand Down
122 changes: 61 additions & 61 deletions src/Husky/Husky.csproj
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<PackageId>Husky</PackageId>
<Version>0.6.0-Preview2</Version>
<Title>Husky</Title>
<Authors>AliReza Sabouri</Authors>
<Description>Git hooks made easy, woof!</Description>
<Copyright>2021</Copyright>
<RepositoryUrl>https://github.com/alirezanet/husky.net</RepositoryUrl>
<Company>TuxTeam</Company>
<PackAsTool>true</PackAsTool>
<ToolCommandName>husky</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageDescription>Git hooks made easy with Husky.Net internal task runner! 🐶 It brings the dev-dependency concept to the .NET world!</PackageDescription>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IncludeSymbols>true</IncludeSymbols>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="templates\husky.sh" />
<EmbeddedResource Include="templates\task-runner.json" />
<None Remove="husky.sh" />
<None Remove="task-runner.json" />
<None Remove="templates\hook" />
<EmbeddedResource Include="templates\hook" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CliFx" Version="2.2.5" />
<PackageReference Include="CliWrap" Version="3.4.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="6.0.0" />
<PackageReference Include="System.IO.Abstractions" Version="17.0.13" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Utils\Dotnet\ParallelRangeManager.cs" />
<Compile Remove="Utils\Dotnet\ParallelLoopState.cs" />
<Compile Remove="Utils\Dotnet\ParallelForEach.cs" />
<Compile Remove="Utils\Dotnet\ParallelETWProvider.cs" />
<Compile Remove="Utils\Dotnet\Parallel.cs" />
</ItemGroup>
<Target Name="Husky" BeforeTargets="Restore;CollectPackageReferences" Condition="'$(HUSKY)' != 0 and '$(IsCrossTargetingBuild)' == 'true'">
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High" />
<Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High" WorkingDirectory="..\.." />
</Target>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<PackageId>Husky</PackageId>
<Version>0.6.13-hahn</Version>
<Title>Husky</Title>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Hahn</Authors>
<Description>Hahn Git hooks made easy</Description>
<PackAsTool>true</PackAsTool>
<ToolCommandName>husky</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageDescription>Git hooks made easy with Husky.Net internal task runner! 🐶 It brings the dev-dependency concept to the .NET world!</PackageDescription>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IncludeSymbols>true</IncludeSymbols>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="templates\husky.sh" />
<EmbeddedResource Include="templates\task-runner.json" />
<None Remove="husky.sh" />
<None Remove="task-runner.json" />
<None Remove="templates\hook" />
<EmbeddedResource Include="templates\hook" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CliFx" Version="2.2.5" />
<PackageReference Include="CliWrap" Version="3.4.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="6.0.0" />
<PackageReference Include="System.IO.Abstractions" Version="17.0.13" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Utils\Dotnet\ParallelRangeManager.cs" />
<Compile Remove="Utils\Dotnet\ParallelLoopState.cs" />
<Compile Remove="Utils\Dotnet\ParallelForEach.cs" />
<Compile Remove="Utils\Dotnet\ParallelETWProvider.cs" />
<Compile Remove="Utils\Dotnet\Parallel.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="nupkg\" />
</ItemGroup>
<Target Name="Husky" BeforeTargets="Restore;CollectPackageReferences" Condition="'$(HUSKY)' != 0 and '$(IsCrossTargetingBuild)' == 'true'">
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High" />
<Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High" WorkingDirectory="..\.." />
</Target>
</Project>
43 changes: 40 additions & 3 deletions src/Husky/TaskRunner/ArgumentParser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.RegularExpressions;
using CliWrap.Buffered;
using Husky.Services.Contracts;
using Husky.Stdout;
Expand All @@ -22,7 +23,7 @@ public ArgumentParser(IGit git)
_git = git;
_customVariableTasks = new Lazy<Task<IList<HuskyTask>>>(GetCustomVariableTasks);
}

const string pattern = @"^(.*?)(\$\{[^}]+\})(.*?)$";
public async Task<ArgumentInfo[]> ParseAsync(HuskyTask task, string[]? optionArguments = null)
{
var args = new List<ArgumentInfo>();
Expand Down Expand Up @@ -69,6 +70,14 @@ public async Task<ArgumentInfo[]> ParseAsync(HuskyTask task, string[]? optionArg
await AddCustomVariable(x, matcher, args, pathMode);
break;
}
case { } x when Regex.Match(x, pattern).Success:
{
var reg = Regex.Match(x, pattern);

await AddCustomVariable(reg.Groups[2].Value, matcher, args, pathMode, reg.Groups[1].Value, reg.Groups[3].Value, true);

break;
}
default:
args.Add(new ArgumentInfo(ArgumentTypes.Static, arg));
break;
Expand Down Expand Up @@ -97,7 +106,10 @@ private async Task AddCustomVariable(
string x,
Matcher matcher,
List<ArgumentInfo> args,
PathModes pathMode
PathModes pathMode,
string arg1 = "",
string arg2 = "",
bool isCustom = false
)
{
var customVariables = await _customVariableTasks.Value;
Expand All @@ -114,9 +126,34 @@ PathModes pathMode
var gitPath = await _git.GetGitPathAsync();

// get relative paths for matcher
var files = (await GetCustomVariableOutput(huskyVariableTask))

var items = (await GetCustomVariableOutput(huskyVariableTask)).ToArray();

if (isCustom)
{
items = items.Where(item => !string.IsNullOrWhiteSpace(item)).ToArray();
}


if (items.Count() == 1)
{
var item = items.First();
if (arg1 != null && !string.IsNullOrEmpty(arg1))
{
item = arg1 + item;
}

if (arg2 != null && !string.IsNullOrEmpty(arg2))
{
item += arg2;
}
items = new[] { item };
}

var files = items
.Where(q => !string.IsNullOrWhiteSpace(q))
.Select(q => Path.IsPathFullyQualified(q) ? Path.GetRelativePath(gitPath, q) : q);

var matches = matcher.Match(gitPath, files);
AddMatchedFiles(args, pathMode, ArgumentTypes.CustomVariable, matches, gitPath);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Husky/TaskRunner/ExecutableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override async Task<double> Execute()
TaskInfo.WorkingDirectory,
TaskInfo.OutputType
);
if (result.ExitCode != 0)
if (result.ExitCode != 0 && !TaskInfo.IgnoreValidateCommandResult)
throw new CommandException(
$"\n ❌ Task '{TaskInfo.Name}' failed in {result.RunTime.TotalMilliseconds:n0}ms\n"
);
Expand Down
8 changes: 6 additions & 2 deletions src/Husky/TaskRunner/ExecutableTaskFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public ExecutableTaskFactory(IServiceProvider provider, IGit git, IArgumentParse
cwd,
huskyTask.Output ?? OutputTypes.Always,
argsInfo,
options.NoPartial
options.NoPartial,
huskyTask.SkipAutoStage,
huskyTask.IgnoreValidateCommandResult
)
);
}
Expand All @@ -87,7 +89,9 @@ bool noPartial
cwd,
huskyTask.Output ?? OutputTypes.Always,
argInfo,
noPartial
noPartial,
huskyTask.SkipAutoStage,
huskyTask.IgnoreValidateCommandResult
);
// staged-task
subTasks[i] = CreateExecutableTask(taskInfo);
Expand Down
2 changes: 2 additions & 0 deletions src/Husky/TaskRunner/HuskyTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public class HuskyTask
public HuskyTask? Windows { get; set; }
public string[]? Include { get; set; }
public string[]? Exclude { get; set; }
public bool SkipAutoStage { get; set; }
public bool IgnoreValidateCommandResult { get; set; }
}
9 changes: 6 additions & 3 deletions src/Husky/TaskRunner/StagedTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public override async Task<double> Execute()
return await PartialExecution(partialStagedFiles);

var executionTime = await base.Execute();
await ReStageFiles(partialStagedFiles);

if (!TaskInfo.SkipAutoStage)
await ReStageFiles(partialStagedFiles);

return executionTime;

}
Expand Down Expand Up @@ -130,8 +133,8 @@ await _git.ExecAsync(
}
}

// re-staged staged files
await ReStageFiles(partialStagedFiles);
if (!TaskInfo.SkipAutoStage)
await ReStageFiles(partialStagedFiles);// re-staged staged files

return executionTime;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Husky/TaskRunner/TaskInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public TaskInfo(
string workingDirectory,
OutputTypes outputType,
ArgumentInfo[] argumentInfo,
bool noPartial
bool noPartial,
bool skipAutoStage,
bool ignoreValidateCommandResult

)
{
Name = name;
Expand All @@ -19,6 +22,8 @@ bool noPartial
OutputType = outputType;
ArgumentInfo = argumentInfo;
NoPartial = noPartial;
SkipAutoStage = skipAutoStage;
IgnoreValidateCommandResult = ignoreValidateCommandResult;
}

public string Name { get; set; }
Expand All @@ -28,4 +33,6 @@ bool noPartial
public OutputTypes OutputType { get; set; }
public ArgumentInfo[] ArgumentInfo { get; set; }
public bool NoPartial { get; set; }
public bool SkipAutoStage { get; set; }
public bool IgnoreValidateCommandResult { get; set; }
}
2 changes: 1 addition & 1 deletion tests/HuskyTest/HuskyTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down