Skip to content

Commit

Permalink
[HUP-785] Feature: Created git hooks pre-commit and prepare-commit-ms…
Browse files Browse the repository at this point in the history
…g and added git commit message templates.

-Added gitconfig for make a settings other changes like githooks, commit-message-template.
-Added pre-commit hooks for checking branch names like "main, master and production". If branch names are these names not allowed commit.
-Added prepare-commit-msg hooks for checking commit message template. If doesn't follow this template that gives an error message.
-Also created some classes for unity project start event and added some git configuration for applying to local git configuration for just developer.
  • Loading branch information
Andronovo-bit authored and alihan98ersoy committed Feb 6, 2024
1 parent a53c309 commit c3d2a0a
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[core]
hooksPath = .github/.githooks
[commit]
template = .github/.gitmessages
[pull]
rebase = true
[alias]
co = checkout # exampöe: git co master
ci = commit # example: git ci -m "commit message"
st = status # example: git st
br = branch # example: git br
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short # example: git hist
16 changes: 16 additions & 0 deletions .github/.githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

branch="$(git rev-parse --abbrev-ref HEAD)"
protected_branches=("main" "master" "production")

for i in "${protected_branches[@]}"
do
if [ "$branch" = "$i" ]; then
echo "You cannot commit to the $i branch. Please create a new branch and make a Pull Request."
exit 1
fi
done

exit 0

# Path: .githooks/post-commit
37 changes: 37 additions & 0 deletions .github/.githooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Define the valid keywords and prefixes
type_keyword=("Feature" "Fix" "Hotfix" "Docs" "Style" "Refactor" "Perf" "Test" "Chore" "Revert")

# Get the first line of the commit message
START_LINE=`head -n1 $1`
SIZE=${#START_LINE}

# Check if the commit message starts with a valid prefix(Jira Key)
if [[ $START_LINE == "[HUP-"*"]"* ]]; then
prefix_found=true
else
prefix_found=false
fi

# Check if the commit message starts with a valid keyword
for keyword in "${type_keyword[@]}"; do
if [[ $START_LINE == *"$keyword"* ]]; then
keyword_found=true
break
fi
done

# Validate the commit message
if [[ $prefix_found != true ]]; then
echo "Jira Key must be written in the commit message."
exit 1
elif [[ $keyword_found != true ]]; then
echo "Commit message must include with one of these keywords: ${type_keyword[@]}"
exit 1
elif (( $SIZE < 50 )); then
echo "Enter a descriptive commit message. (Min 50 characters)"
exit 1
fi

exit 0
14 changes: 14 additions & 0 deletions .github/.gitmessages
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[<Jira Issue Key>] <Type>: <Short Description>

<Additional Details if Necessary>

[Optional] Related Issues:
- <Jira Issue Key>: <Description>
- <Jira Issue Key>: <Description>

[Optional] Screenshots or Visuals:
- [Description or Link ]
- [Description or Link ]

[Optional] Notes:
- Any additional information, considerations, or important notes
78 changes: 78 additions & 0 deletions Assets/Editor/HMSConfigureGitStandards.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using UnityEngine;

/// <summary>
/// This class is used to configure Git when the Unity project starts.
/// It uses the System.Diagnostics.Process class to run a Git command in a new process.
/// </summary>
public class HMSConfigureGitStandards
{
// Define Git commands as constants
#region Git Commands
private const string GitCommand = "git";
private const string GitConfigCommand = "config --local include.path ../.gitconfig";
private const string GitUnsetConfigCommand = "config --local --unset include.path";
private const string GitGetConfigCommand = "config --local --get include.path";
private const string GitVersionCommand = "--version";
#endregion

public static void Start()
{
if (IsGitInstalled())
{
if (!IsGitConfigured())
{
ConfigureGit();
}
}
else
{
Debug.LogWarning("Git is not installed");

}
}
private static void ConfigureGit()
{
Debug.Log("Configuring Git");
try
{
RunProcess(GitCommand, GitConfigCommand);
}
catch (System.Exception e)
{
Debug.Log("Error: " + e.Message);
RunProcess(GitCommand, GitUnsetConfigCommand);
}
finally
{
Debug.Log("Git Configured");
}
}
private static int RunProcess(string fileName, string arguments)
{
var proc = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
CreateNoWindow = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true
}
};

proc.Start();
proc.WaitForExit();

return proc.ExitCode;
}
private static bool IsGitInstalled()
{
return RunProcess(GitCommand, GitVersionCommand) == 0;
}
private static bool IsGitConfigured()
{
return RunProcess(GitCommand, GitGetConfigCommand) == 0;
}
}
11 changes: 11 additions & 0 deletions Assets/Editor/HMSConfigureGitStandards.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Assets/Editor/HMSUnityProjectStart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEditor;
using UnityEngine;


[InitializeOnLoad]
public class HMSUnityProjectStart
{
static HMSUnityProjectStart()
{
EditorApplication.update += RunOnce;
}

static void RunOnce()
{
HMSConfigureGitStandards.Start();

EditorApplication.update -= RunOnce;
}

}
11 changes: 11 additions & 0 deletions Assets/Editor/HMSUnityProjectStart.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c3d2a0a

Please sign in to comment.