-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepareCommitMsgHook.cs
47 lines (38 loc) · 1.61 KB
/
PrepareCommitMsgHook.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
41
42
43
44
45
46
47
using GitHooksCsharp.Utils;
namespace GitHooksCsharp.Hooks.PrepareCommitMsg;
/// <summary>
/// Represents the hook for preparing the commit message.
/// </summary>
public static class PrepareCommitMsgHook
{
/// <summary>
/// Tasks to execute when preparing the commit message.
/// </summary>
public static List<BaseHookTask> Tasks { get; } = [];
/// <summary>
/// Executes the prepare commit message hook.
/// </summary>
public static void Execute()
{
// TODO: Indexes of the arguments can be different.
string[] args = Environment.GetCommandLineArgs();
string commitMessageFilePath = CmdUtils.GetCommandLineArg(args, 2);
string commitType = CmdUtils.GetCommandLineArg(args, 3);
// Exit if the user already gave a message.
if (commitType.Equals("message"))
Environment.Exit(0);
// Synchronize the configuration.
Configuration.Synchronize();
// Initialize the hook logger for the PrepareCommitMsg hook.
HookLogger hookLogger = new(HookTypes.PrepareCommitMsg);
hookLogger.StartHook();
// Add the MessageTemplateTask to the list of tasks and execute all tasks.
Tasks.Add(new MessageTemplateTask(hookLogger, commitMessageFilePath));
Tasks.ForEach(t => t.Execute());
// Check if all tasks passed successfully.
bool hookPassed = Tasks.TrueForAll(t => t.Success);
// Finish the hook and exit with the appropriate status code.
hookLogger.FinishHook(hookPassed);
Environment.Exit(hookPassed ? 0 : 1);
}
}