Skip to content

Commit

Permalink
fix(staged): add no-partial option to support normal staging process #39
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Mar 9, 2022
1 parent 69bb303 commit b065ec1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/Husky/Cli/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public bool Quiet
[CommandOption("args", 'a', Description = "Pass custom arguments to tasks")]
public IReadOnlyList<string>? Arguments { get; set; }

[CommandOption("no-partial", 'p', Description = "Disable partial commit and temporary file generation")]
public bool NoPartial { get; set; }

public RunCommand(IServiceProvider provider)
{
_provider = provider;
Expand Down
18 changes: 10 additions & 8 deletions src/Husky/TaskRunner/ExecutableTaskFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using Husky.Services.Contracts;
using Husky.Stdout;
Expand All @@ -9,7 +8,7 @@ namespace Husky.TaskRunner;

public interface IExecutableTaskFactory
{
Task<IExecutableTask?> CreateAsync(HuskyTask huskyTask, string[]? optionArguments);
Task<IExecutableTask?> CreateAsync(HuskyTask huskyTask, IRunOption options);
}

public class ExecutableTaskFactory : IExecutableTaskFactory
Expand All @@ -26,7 +25,7 @@ public ExecutableTaskFactory(IServiceProvider provider, IGit git, IArgumentParse
_argumentParser = argumentParser;
}

public async Task<IExecutableTask?> CreateAsync(HuskyTask huskyTask, string[]? optionArguments)
public async Task<IExecutableTask?> CreateAsync(HuskyTask huskyTask, IRunOption options)
{
if (huskyTask.Command == null)
{
Expand All @@ -37,7 +36,7 @@ public ExecutableTaskFactory(IServiceProvider provider, IGit git, IArgumentParse
huskyTask.Name ??= huskyTask.Command;

var cwd = await _git.GetTaskCwdAsync(huskyTask);
var argsInfo = await _argumentParser.ParseAsync(huskyTask, optionArguments);
var argsInfo = await _argumentParser.ParseAsync(huskyTask, options.Arguments?.ToArray());

if (huskyTask.Args != null && huskyTask.Args.Length > argsInfo.Length)
{
Expand All @@ -51,7 +50,7 @@ public ExecutableTaskFactory(IServiceProvider provider, IGit git, IArgumentParse
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
&& totalCommandLength > MAX_ARG_LENGTH
)
return CreateChunkTask(huskyTask, totalCommandLength, argsInfo, cwd);
return CreateChunkTask(huskyTask, totalCommandLength, argsInfo, cwd, options.NoPartial);

return CreateExecutableTask(
new TaskInfo(
Expand All @@ -60,7 +59,8 @@ public ExecutableTaskFactory(IServiceProvider provider, IGit git, IArgumentParse
argsInfo.Select(q => q.Argument).ToArray(),
cwd,
huskyTask.Output ?? OutputTypes.Always,
argsInfo
argsInfo,
options.NoPartial
)
);
}
Expand All @@ -69,7 +69,8 @@ private IExecutableTask CreateChunkTask(
HuskyTask huskyTask,
int totalCommandLength,
ArgumentInfo[] args,
string cwd
string cwd,
bool noPartial
)
{
var chunks = GetChunks(totalCommandLength, args);
Expand All @@ -85,7 +86,8 @@ string cwd
chunkedArgs,
cwd,
huskyTask.Output ?? OutputTypes.Always,
argInfo
argInfo,
noPartial
);
// staged-task
subTasks[i] = CreateExecutableTask(taskInfo);
Expand Down
1 change: 1 addition & 0 deletions src/Husky/TaskRunner/IRunOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public interface IRunOption
{
public string? Name { get; set; }
public string? Group { get; set; }
public bool NoPartial { get; set; }
public IReadOnlyList<string>? Arguments { get; set; }
}
21 changes: 21 additions & 0 deletions src/Husky/TaskRunner/StagedTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public StagedTask(ICliWrap cliWrap, IGit git, IFileSystem fileSystem, TaskInfo t

public override async Task<double> Execute()
{

if (TaskInfo.NoPartial) return await NormalExecution();

// Check if any partial staged files are present
var diffNames = await _git.GetDiffNameOnlyAsync();
var fileArgumentInfo = TaskInfo.ArgumentInfo.OfType<FileArgumentInfo>().ToList();
Expand All @@ -41,6 +44,24 @@ public override async Task<double> Execute()
var executionTime = await base.Execute();
await ReStageFiles(partialStagedFiles);
return executionTime;

}

private async Task<double> NormalExecution()
{
var executionTime = await base.Execute();
// in staged mode, we should update the git index
try
{
await _git.ExecAsync("update-index -g");
}
catch (Exception)
{
// Silently ignore the error if happens, we don't want to break the execution
"⚠️ Can not update git index".Husky(ConsoleColor.Yellow);
}

return executionTime;
}

private async Task<double> PartialExecution(List<FileArgumentInfo> partialStagedFiles)
Expand Down
5 changes: 4 additions & 1 deletion src/Husky/TaskRunner/TaskInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public TaskInfo(
string[] arguments,
string workingDirectory,
OutputTypes outputType,
ArgumentInfo[] argumentInfo
ArgumentInfo[] argumentInfo,
bool noPartial
)
{
Name = name;
Expand All @@ -17,6 +18,7 @@ ArgumentInfo[] argumentInfo
WorkingDirectory = workingDirectory;
OutputType = outputType;
ArgumentInfo = argumentInfo;
NoPartial = noPartial;
}

public string Name { get; set; }
Expand All @@ -25,4 +27,5 @@ ArgumentInfo[] argumentInfo
public string WorkingDirectory { get; set; }
public OutputTypes OutputType { get; set; }
public ArgumentInfo[] ArgumentInfo { get; set; }
public bool NoPartial { get; set; }
}
2 changes: 1 addition & 1 deletion src/Husky/TaskRunner/TaskRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async ValueTask Run()
task.Name = task.Command;

$"⚡ Preparing task '{task.Name}'".Husky();
var executableTask = await _factory.CreateAsync(task, _options.Arguments?.ToArray());
var executableTask = await _factory.CreateAsync(task, _options);

if (executableTask is null)
continue;
Expand Down

0 comments on commit b065ec1

Please sign in to comment.