-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCommitActionChmodHandler.cs
40 lines (33 loc) · 1.39 KB
/
CommitActionChmodHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#if NET7_0_OR_GREATER
using System;
#endif
using System.IO;
using LibGit2Sharp;
using NGitLab.Models;
namespace NGitLab.Mock;
internal sealed class CommitActionChmodHandler : ICommitActionHandler
{
public void Handle(CreateCommitAction action, string repoPath, LibGit2Sharp.Repository repository)
{
if (!Directory.Exists(repoPath))
throw new DirectoryNotFoundException();
var filePath = Path.Combine(repoPath, action.FilePath);
if (!System.IO.File.Exists(filePath))
throw new FileNotFoundException("File does not exist.");
#if NET7_0_OR_GREATER
if (!OperatingSystem.IsWindows())
{
var fileMode = UnixFileMode.UserRead | UnixFileMode.UserWrite |
UnixFileMode.GroupRead | UnixFileMode.GroupWrite |
UnixFileMode.OtherRead | UnixFileMode.OtherWrite |
(action.IsExecutable
? UnixFileMode.UserExecute | UnixFileMode.GroupExecute | UnixFileMode.OtherExecute
: UnixFileMode.None);
System.IO.File.SetUnixFileMode(filePath, fileMode);
}
#endif
var blob = repository.ObjectDatabase.CreateBlob(filePath);
repository.Index.Add(blob, action.FilePath, action.IsExecutable ? Mode.ExecutableFile : Mode.NonExecutableFile);
repository.Index.Write();
}
}