Skip to content

Commit

Permalink
feat: add support for setting execute_filemode on commit create
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwoods-7 committed Sep 3, 2024
1 parent 59fb347 commit 3b65179
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
40 changes: 40 additions & 0 deletions NGitLab.Mock/CommitActionChmodHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
1 change: 1 addition & 0 deletions NGitLab.Mock/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ private static ICommitActionHandler GetActionHandler(string action)
CommitAction.Delete => new CommitActionDeleteHandler(),
CommitAction.Update => new CommitActionUpdateHandler(),
CommitAction.Move => new CommitActionMoveHandler(),
CommitAction.chmod => new CommitActionChmodHandler(),
_ => throw new NotSupportedException(),
};
}
Expand Down
37 changes: 37 additions & 0 deletions NGitLab.Tests/CommitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,41 @@ public async Task Test_commit_can_be_created_from_sha()
});
Assert.That(newCommit, Is.Not.Null);
}

[Test]
public async Task Test_commit_can_set_executable_flag()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject(initializeWithCommits: true);
var commitClient = context.Client.GetCommits(project.Id);

var commit = commitClient.GetCommit("main");
Assert.That(commit, Is.Not.Null);

var newCommit = commitClient.Create(new CommitCreate
{
Branch = "test-set-executable-flag",
CommitMessage = "New commit",
StartBranch = project.DefaultBranch,
Actions = new List<CreateCommitAction>
{
new CreateCommitAction
{
Action = "create",
FilePath = "script.sh",
Content = "echo 'Hello, world!'",
},
new CreateCommitAction
{
Action = "chmod",
FilePath = "script.sh",
IsExecutable = true,
},
},
});
Assert.That(newCommit, Is.Not.Null);

var diff = context.Client.GetRepository(project.Id).GetCommitDiff(newCommit.Id).Single();
Assert.That(diff.BMode, Is.EqualTo("100755"));
}
}
3 changes: 3 additions & 0 deletions NGitLab/Models/CommitCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ public class CreateCommitAction

[JsonPropertyName("encoding")]
public string Encoding { get; set; }

[JsonPropertyName("execute_filemode")]
public bool IsExecutable { get; set; }
}
2 changes: 2 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,8 @@ NGitLab.Models.CreateCommitAction.Encoding.get -> string
NGitLab.Models.CreateCommitAction.Encoding.set -> void
NGitLab.Models.CreateCommitAction.FilePath.get -> string
NGitLab.Models.CreateCommitAction.FilePath.set -> void
NGitLab.Models.CreateCommitAction.IsExecutable.get -> bool
NGitLab.Models.CreateCommitAction.IsExecutable.set -> void
NGitLab.Models.CreateCommitAction.PreviousPath.get -> string
NGitLab.Models.CreateCommitAction.PreviousPath.set -> void
NGitLab.Models.Deployment
Expand Down

0 comments on commit 3b65179

Please sign in to comment.