Skip to content

Commit

Permalink
fix(StagedTask): The directory is not empty #23
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Jan 28, 2022
1 parent dac8b18 commit b832b8b
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions src/Husky/TaskRunner/StagedTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public override async Task<double> Execute(ICliWrap cli)
var diffNames = await _git.GetDiffNameOnlyAsync();
var fileArgumentInfo = TaskInfo.ArgumentInfo.OfType<FileArgumentInfo>().ToList();
var partialStagedFiles = fileArgumentInfo
.IntersectBy(diffNames, q => q.RelativePath)
.ToList();
.IntersectBy(diffNames, q => q.RelativePath)
.ToList();

var hasAnyPartialStagedFiles = partialStagedFiles.Any();

Expand All @@ -35,8 +35,8 @@ public override async Task<double> Execute(ICliWrap cli)
}

private async Task<double> PartialExecution(
ICliWrap cli,
List<FileArgumentInfo> partialStagedFiles
ICliWrap cli,
List<FileArgumentInfo> partialStagedFiles
)
{
// create tmp folder
Expand All @@ -63,8 +63,8 @@ List<FileArgumentInfo> partialStagedFiles
{
await using var output = File.Create(tmpFile);
await (
CliWrap.Cli.Wrap("git").WithArguments(new[] { "cat-file", "blob", hash }!)
| output
CliWrap.Cli.Wrap("git").WithArguments(new[] { "cat-file", "blob", hash }!)
| output
).ExecuteAsync();
}

Expand All @@ -91,17 +91,16 @@ List<FileArgumentInfo> partialStagedFiles
if (string.IsNullOrEmpty(newHash))
{
File.Delete(tf.tmp_path!);
Directory.Delete(cache);
throw new CommandException(
"Failed to hash temp file. Please check the partial staged files."
"Failed to hash temp file. Please check the partial staged files."
);
}

if (newHash != tf.dst_hash)
{
$"Updating index entry for {tf.src_path}".LogVerbose();
await _git.ExecAsync(
$"update-index --cacheinfo {tf.dst_mode},{newHash},{tf.src_path}"
$"update-index --cacheinfo {tf.dst_mode},{newHash},{tf.src_path}"
);
}
else
Expand All @@ -111,9 +110,12 @@ await _git.ExecAsync(

// remove the temporary file
File.Delete(tf.tmp_path!);
Directory.Delete(cache);
}

// clean up the cache folder
if (Directory.Exists(cache))
Directory.Delete(cache, true);

// re-staged staged files
await ReStageFiles(partialStagedFiles);

Expand All @@ -123,11 +125,11 @@ await _git.ExecAsync(
private async Task ReStageFiles(List<FileArgumentInfo> partialStagedFiles)
{
var stagedFiles = TaskInfo.ArgumentInfo
.OfType<FileArgumentInfo>()
.Where(q => q.ArgumentTypes == ArgumentTypes.StagedFile)
.Except(partialStagedFiles)
.Select(q => q.RelativePath)
.ToList();
.OfType<FileArgumentInfo>()
.Where(q => q.ArgumentTypes == ArgumentTypes.StagedFile)
.Except(partialStagedFiles)
.Select(q => q.RelativePath)
.ToList();
if (stagedFiles.Any())
{
"Re-staging staged files...".LogVerbose();
Expand All @@ -141,38 +143,38 @@ private async Task<List<DiffRecord>> GetStagedRecord()
var diffStaged = await _git.GetDiffStagedRecord();
var parsedDiff = diffStaged.Select(ParseDiff).AsQueryable();
var stagedRecord = parsedDiff
.Where(
x =>
x.dst_mode != "120000" // symlinks
&& TaskInfo.ArgumentInfo
.OfType<FileArgumentInfo>()
.Where(q => q.ArgumentTypes == ArgumentTypes.StagedFile)
.Select(q => q.RelativePath)
.Contains(x.src_path)
)
.ToList();
.Where(
x =>
x.dst_mode != "120000" // symlinks
&& TaskInfo.ArgumentInfo
.OfType<FileArgumentInfo>()
.Where(q => q.ArgumentTypes == ArgumentTypes.StagedFile)
.Select(q => q.RelativePath)
.Contains(x.src_path)
)
.ToList();
return stagedRecord;
}

private DiffRecord ParseDiff(string diff)
{
// Format: src_mode dst_mode src_hash dst_hash status/score? src_path dst_path?
var diff_pat = new Regex(
@"^:(\d+) (\d+) ([a-f0-9]+) ([a-f0-9]+) ([A-Z])(\d+)?\t([^\t]+)(?:\t([^\t]+))?$"
@"^:(\d+) (\d+) ([a-f0-9]+) ([a-f0-9]+) ([A-Z])(\d+)?\t([^\t]+)(?:\t([^\t]+))?$"
);
var match = diff_pat.Match(diff);
if (!match.Success)
throw new CommandException("Failed to parse diff-index line: + diff");

return new DiffRecord(
UnlessZeroed(match.Groups[1].Value),
UnlessZeroed(match.Groups[2].Value),
UnlessZeroed(match.Groups[3].Value),
UnlessZeroed(match.Groups[4].Value),
match.Groups[5].Value,
int.TryParse(match.Groups[6].Value, out var score) ? score : null,
match.Groups[7].Value,
match.Groups[8].Value
UnlessZeroed(match.Groups[1].Value),
UnlessZeroed(match.Groups[2].Value),
UnlessZeroed(match.Groups[3].Value),
UnlessZeroed(match.Groups[4].Value),
match.Groups[5].Value,
int.TryParse(match.Groups[6].Value, out var score) ? score : null,
match.Groups[7].Value,
match.Groups[8].Value
);
}

Expand All @@ -188,14 +190,14 @@ private DiffRecord ParseDiff(string diff)
}

private record DiffRecord(
string? src_mode,
string? dst_mode,
string? src_hash,
string? dst_hash,
string? status,
int? score,
string? src_path,
string? dst_path,
string? tmp_path = null
string? src_mode,
string? dst_mode,
string? src_hash,
string? dst_hash,
string? status,
int? score,
string? src_path,
string? dst_path,
string? tmp_path = null
);
}

0 comments on commit b832b8b

Please sign in to comment.