Automating Version Control in Laravel: Using Git Pre-Commit Hooks for Seamless Versioning
In software development, maintaining version control is essential. One effective way to manage versioning in your Laravel application is by using Git hooks, specifically the pre-commit hook. This guide will walk you through automating version updates with a pre-commit hook that updates a version file based on the current date each time you commit.
What Are Git Hooks?
Git hooks are scripts that Git executes before or after events such as commits, merges, and push operations. The pre-commit hook runs right before a commit is finalized. This is the perfect place to automate tasks like updating version numbers.
Setting Up the Pre-Commit Hook
Step 1: Create the Pre-Commit Hook
- Navigate to Your Project Directory:
Open your terminal and navigate to the root of your Laravel project. - Access the Hooks Directory:
Go to the.git/hooks/
directory:
cd .git/hooks/
- Create or Edit the Pre-Commit File:
Create a file namedpre-commit
if it doesn’t already exist:
touch pre-commit
Step 2: Write the Script
Open the pre-commit
file in your favorite text editor and add the following script:
#!/bin/bash # Define the path to your version.php file VERSION_FILE="config/version.php" # Get the current date in YYYYMMDD format DATE=$(date +'%Y%m%d') # Update the last_commit_date in the version.php file sed -i '' "s/'last_commit_date' => [0-9]*/'last_commit_date' => $DATE/" "$VERSION_FILE" # Stage the changes to version.php git add "$VERSION_FILE" # Print success message echo "Successfully updated last_commit_date to: $DATE in $VERSION_FILE and staged the changes."
Step 3: Make the Hook Executable
Run the following command to make your pre-commit
hook executable:
chmod +x pre-commit
How the Script Works
- Fetch Current Date: The script retrieves the current date in
YYYYMMDD
format. - Update the Version File: It uses
sed
to find the line that contains thelast_commit_date
and updates it with the current date. - Stage Changes: The modified
version.php
file is automatically staged for commit. - Success Message: A message is displayed in the terminal to confirm the update.
Accessing Version Information in Laravel
Update the Version File
Ensure that your config/version.php
file has the structure below:
return [ 'major' => 2, 'minor' => 0, 'last_commit_date' => 0, ];
Accessing Version Data in Your Code
You can access the version information anywhere in your Laravel application using the config()
helper function. For example, in your SMIRDVersion.php
file:
class SMVersion { protected static int $major = 2; protected static int $minor = 0; public static function getFullVersion(): string { $lastCommitDate = config('version.last_commit_date'); return self::$major . '.' . self::$minor . '.' . $lastCommitDate; } }
Conclusion
Using a Git pre-commit hook to automate versioning in your Laravel application can streamline your development process and ensure that version information is always up to date. This approach not only enhances efficiency but also reduces human error when managing version numbers.
By following this guide, you can create a robust versioning system that automatically updates your application’s version information with each commit, ensuring clarity and accuracy in your project’s progress. Happy coding!