Skip to content

Commit 820562e

Browse files
committed
feat: Initial setup with basic functionality
0 parents  commit 820562e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3782
-0
lines changed

.editorconfig

Lines changed: 364 additions & 0 deletions
Large diffs are not rendered by default.

.gitignore

Lines changed: 946 additions & 0 deletions
Large diffs are not rendered by default.

GitHooksCSharp.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHooksCSharp", "src\GitHooksCSharp\GitHooksCSharp.csproj", "{E8894A50-ABDD-49AE-BD9F-505063327D63}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitHooksCSharp.Sample", "sample\GitHooksCSharp.Sample\GitHooksCSharp.Sample.csproj", "{A0994F60-DC74-40C1-BD75-E41DE1E2965F}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(NestedProjects) = preSolution
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{E8894A50-ABDD-49AE-BD9F-505063327D63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{E8894A50-ABDD-49AE-BD9F-505063327D63}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{E8894A50-ABDD-49AE-BD9F-505063327D63}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{E8894A50-ABDD-49AE-BD9F-505063327D63}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{A0994F60-DC74-40C1-BD75-E41DE1E2965F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{A0994F60-DC74-40C1-BD75-E41DE1E2965F}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{A0994F60-DC74-40C1-BD75-E41DE1E2965F}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{A0994F60-DC74-40C1-BD75-E41DE1E2965F}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
EndGlobal

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Pablo Rodriguez
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# git-hooks-csharp
2+
3+
git-hooks-csharp is a library enabling developers to execute C#
4+
code within Git hooks, offering automated checks to maintain
5+
code quality and consistency in Git workflows. Currently, it provides three
6+
hooks: `pre-commit`, `prepare-commit-msg`, and `commit-msg`.
7+
8+
## Table of Contents
9+
1. [Hook Overview](#hook-overview)
10+
2. [Key Features](#key-features)
11+
3. [Usage](#usage)
12+
4. [License](#license)
13+
5. [Acknowledgments](#acknowledgments)
14+
15+
## Hook Overview
16+
- [`PreCommit.cs`](src/GitHooksCSharp/Hooks/PreCommit/PreCommitHook.cs): Automates common dotnet commands for code formatting, cleaning, building, and testing.
17+
- [`PrepareCommitMsg.cs`](src/GitHooksCSharp/Hooks/PrepareCommitMsg/PrepareCommitMsgHook.cs): Generates a basic commit message template, making it easier to create consistent commit messages.
18+
- [`CommitMsg.cs`](src/GitHooksCSharp/Hooks/CommitMsg/CommitMsgHook.cs): Validates commit message format based on the
19+
[Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
20+
21+
> [!NOTE]
22+
> Part of this library is based on the Medium article [Using C# code in your git hooks](https://kaylumah.medium.com/using-c-code-in-your-git-hooks-66e507c01a0f).
23+
24+
## Key Features
25+
26+
### pre-commit Hook
27+
- Format code using `dotnet format`
28+
- Clean code using `dotnet clean`
29+
- Build projects using `dotnet build`
30+
- Run tests using `dotnet test`
31+
32+
### prepare-commit-msg Hook
33+
- Prepares a basic commit message template when using `git commit` without the `-m` flag.
34+
35+
### commit-msg Hook
36+
- Commit message format validation.
37+
- Configuration in a JSON format file to modify the
38+
constraints of the commit message format.
39+
40+
## Usage
41+
42+
1. You must install the `dotnet-script` tool:
43+
```bash
44+
dotnet tool install -g dotnet-script
45+
```
46+
47+
2. Compile this project and add the compiled DLL to the git hooks folder or wherever you need it.
48+
3. Create a git hook to execute the DLL. The git hook file must be a dotnet script file, for example:
49+
50+
### Pre Commit
51+
52+
```csharp
53+
#!/usr/bin/env dotnet-script
54+
#r "./GitHooksCSharp.dll"
55+
56+
using GitHooksCsharp.Hooks.PreCommit;
57+
58+
PreCommitHook.ProjectPaths.Add("./Sample/Sample.csproj");
59+
PreCommitHook.ProjectPaths.Add("./Sample2/Sample2.csproj");
60+
PreCommitHook.TestProjectPaths.Add("./Sample.Test/Sample.Test.csproj");
61+
PreCommitHook.TestProjectPaths.Add("./Sample2.Test/Sample2.Test.csproj");
62+
63+
PreCommitHook.Execute();
64+
```
65+
##### Prepare Commit Msg
66+
```csharp
67+
#!/usr/bin/env dotnet-script
68+
#r "./GitHooksCSharp.dll"
69+
70+
using GitHooksCsharp.Hooks.PrepareCommitMsg;
71+
72+
PrepareCommitMsgHook.Execute();
73+
```
74+
75+
##### Commit Msg
76+
```csharp
77+
#!/usr/bin/env dotnet-script
78+
#r "./GitHooksCSharp.dll"
79+
80+
using GitHooksCsharp.Hooks.CommitMsg;
81+
82+
CommitMsgHook.Execute();
83+
```
84+
85+
##### Commit Message Constraints
86+
[cshooks-config.json](sample/GitHooksCSharp.Sample/cshooks-config.json) file to configure the validation used in `commit-msg`:
87+
```json
88+
{
89+
90+
"subject": {
91+
"allowedTypes": [
92+
"feat",
93+
"fix",
94+
"chore",
95+
"test",
96+
"docs",
97+
"build",
98+
"ci",
99+
"style",
100+
"refactor",
101+
"perf",
102+
"revert"
103+
],
104+
"allowedScopes": [
105+
"[\\w\\s!@\\#\\$%\\^\u0026\\*\\(\\)_=\\\u002B~\u0060\u0027\\[\\{}\\*\u00BF\\?\\|:;,\\.\u003E\u003C\\\\\\-\\]\\/]{2,20}"
106+
],
107+
"isTypeRequired": true,
108+
"isScopeRequired": false,
109+
"descriptionRules": {
110+
"isRequired": true,
111+
"letterCase": "sentence",
112+
"minLength": 10,
113+
"maxLength": 70,
114+
"allowedSpecialChars": [],
115+
"mayEndWithPeriod": false,
116+
"mustEndWithPeriod": false
117+
}
118+
},
119+
120+
"body": {
121+
"isRequired": false,
122+
"letterCase": "any",
123+
"minLength": 0,
124+
"maxLength": 0,
125+
"allowedSpecialChars": [],
126+
"mayEndWithPeriod": true,
127+
"mustEndWithPeriod": false
128+
},
129+
130+
"footer": {
131+
"isRequired": false,
132+
"allowedKeywords": [
133+
"BREAKING CHANGE",
134+
"BREAKING-CHANGE",
135+
"[a-z]\u002B(?:-[a-z]\u002B)*"
136+
],
137+
"descriptionRules": {
138+
"isRequired": true,
139+
"letterCase": "sentence",
140+
"minLength": 10,
141+
"maxLength": 0,
142+
"allowedSpecialChars": [],
143+
"mayEndWithPeriod": false,
144+
"mustEndWithPeriod": false
145+
}
146+
}
147+
148+
}
149+
```
150+
151+
## License
152+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
153+
154+
## Acknowledgments
155+
- [Using C# code in your git hooks](https://kaylumah.medium.com/using-c-code-in-your-git-hooks-66e507c01a0f)
156+
- [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)
157+
- [dotnet-script](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script)

TASKS.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Tasks
2+
3+
### Important
4+
- [ ] Check TODOs in the code.
5+
- [ ] Add tests.
6+
- [ ] Add `RegexTextGenerator` to add regex restrictions dynamically using methods (e.g., `TextRegexGenerator.AddMinLength(...)`) or use a NuGet library.
7+
- [ ] Add log lines with multicolor.
8+
- [ ] Allow to specify the Command Line process to execute commands [CommandLine.cs](src/GitHooksCSharp/Cmd/CommandLine.cs)
9+
- [ ] Allow to set the target projects and test projects to be used in the configuration file [Configuration.cs](src/GitHooksCSharp/Configuration.cs)
10+
- [ ] Allow to provide custom config paths and change the name [Configuration.cs](src/GitHooksCSharp/Configuration.cs)
11+
- [ ] Allow to use parameters for the commands. [DotNetCommand.cs](src/GitHooksCSharp/DotnetCommands/Dotnet.cs)
12+
- [ ] Implement ILogger interface for loggers [HookLogger.cs](src/GitHooksCSharp/Hooks/HookLogger.cs) [Logger.cs](src/GitHooksCSharp/Logging/Logger.cs) and DI.
13+
- [ ] Indices of the arguments can be different, fix it. [PrepareCommitMsgHook.cs](src/GitHooksCSharp/Hooks/PrepareCommitMsg/PrepareCommitMsgHook.cs)
14+
- [ ] Fix HACK or at least refactor this duplicated method. [CheckSubjectTask.cs](src/GitHooksCSharp/Hooks/CommitMsg/CheckSubjectTask.cs),
15+
[CheckBodyTask.cs](src/GitHooksCSharp/Hooks/CommitMsg/CheckBodyTask.cs) and [CheckFooterTask.cs](src/GitHooksCSharp/Hooks/CommitMsg/CheckFooterTask.cs)
16+
17+
### Hooks
18+
- [ ] Add hook execution config to specify which hook scripts must be executed.
19+
- [ ] Add an automatic message formatter.
20+
- [ ] Add more descriptive error messages to indicate what is wrong with the commit message.
21+
- [ ] Add a pre-push hook.
22+
23+
### Configuration File
24+
- [ ] Add a property to set a limit on the number of footers.
25+
- [ ] Add a whitespace check (body or descriptions with only whitespaces).
26+
- [ ] Add disallowed keywords for Subject and Footers.
27+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>latestmajor</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
</PropertyGroup>
10+
11+
</Project>
41.5 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env dotnet-script
2+
#r "./GitHooksCSharp.dll"
3+
4+
using GitHooksCsharp.Hooks.CommitMsg;
5+
6+
CommitMsgHook.Execute();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"subject": {
3+
"allowedTypes": [
4+
"feat",
5+
"fix",
6+
"chore",
7+
"test",
8+
"docs",
9+
"build",
10+
"ci",
11+
"style",
12+
"refactor",
13+
"perf",
14+
"revert"
15+
],
16+
"allowedScopes": [
17+
"[\\w\\s!@\\#\\$%\\^\u0026\\*\\(\\)_=\\\u002B~\u0060\u0027\\[\\{}\\*\u00BF\\?\\|:;,\\.\u003E\u003C\\\\\\-\\]\\/]{2,20}"
18+
],
19+
"isTypeRequired": true,
20+
"isScopeRequired": false,
21+
"descriptionRules": {
22+
"isRequired": true,
23+
"letterCase": "sentence",
24+
"minLength": 10,
25+
"maxLength": 70,
26+
"allowedSpecialChars": [],
27+
"mayEndWithPeriod": false,
28+
"mustEndWithPeriod": false
29+
}
30+
},
31+
"body": {
32+
"isRequired": false,
33+
"letterCase": "any",
34+
"minLength": 0,
35+
"maxLength": 0,
36+
"allowedSpecialChars": [],
37+
"mayEndWithPeriod": true,
38+
"mustEndWithPeriod": false
39+
},
40+
"footer": {
41+
"isRequired": false,
42+
"allowedKeywords": [
43+
"BREAKING CHANGE",
44+
"BREAKING-CHANGE",
45+
"[a-z]\u002B(?:-[a-z]\u002B)*"
46+
],
47+
"descriptionRules": {
48+
"isRequired": true,
49+
"letterCase": "sentence",
50+
"minLength": 10,
51+
"maxLength": 0,
52+
"allowedSpecialChars": [],
53+
"mayEndWithPeriod": false,
54+
"mustEndWithPeriod": false
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)