Skip to content

Commit 9a512dc

Browse files
Extend git commands (#50)
* feat: create bin dir This creates a bin/ directory in the user's home directory and puts it in the path * feat: symlink git commands this ensures the home dir has a bin dir and is in the path, then symlinks the git commands * feat: git tickets command This command is used to get the new tickets between two branches * feat: git make-release command This command automates the process of preparing a new software release. It creates a release branch from the current branch, increments the version number, updates the `CHANGELOG.md` * docs: document the setup scripts and how to use the git scripts * style: fix markdown formatting * refactor: fix linting errors * style: readme line length * style: remove double word * refactor: delcare and assign separately * style: remove pesky trailing spaces * ci: disable shell check SC1091 * feat: remove adding bin dir to home * feat: remove linking git commands * refactor: move git scripts to hop project bin * docs: remove the install section * docs: add CLI tooling setup section Stub a section for Hop * fix: correctly get the versioning segments and increment * feat: improve branch processing if a target branch doesn't exist * style: fix formatting * style: SC2181 (style): Check exit code directly * docs: remove shell command example, decided it's just easier to add bin to path than to symlink it * Update README.md Co-authored-by: Erik Perri <[email protected]> * docs: add simple hop description
1 parent 1c16ca2 commit 9a512dc

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,68 @@ _For running an S3 service locally._
8383
---
8484

8585
## Scripts
86+
8687
Inside the `scripts` folder you will find one-off scripts to help with tasks.
8788

8889
* `db_migrate.sh` - Helps migrate databases between versions of mysql.
8990

91+
## CLI Tooling Setup
92+
93+
Add the `bin` directory to your path to use **Hop** and **Git Scripts**.
94+
95+
## Hop
96+
97+
A script that makes it easy to hop into your project containers and run commands.
98+
99+
## Git Scripts
100+
101+
Custom scripts that extend Git functionality, to streamline the process of tracking tickets and managing releases
102+
103+
### `git-tickets`
104+
105+
This command is used to get the tickets since staging was last updated.
106+
By default, it does not update the branches.
107+
108+
```shell
109+
git tickets [options] [arguments]
110+
```
111+
112+
| Options | Description | Default |
113+
|----------|-----------------------------------------------------------------|---------|
114+
| --update | Update the specified branches from the remote before comparison | N/A |
115+
116+
| Arguments | Description | Default | Any of |
117+
|-----------|--------------------------------------|---------|------------|
118+
| branch 1 | the target branch that is up to date | master | any branch |
119+
| branch 2 | the branch that is behind | staging | any branch |
120+
121+
### Tickets Example
122+
123+
```shell
124+
git tickets --update master staging
125+
```
126+
127+
### `git-make-release`
128+
129+
This command automates the process of preparing a new software release.
130+
It creates a release branch from the current branch, increments the version number, updates the `CHANGELOG.md`
131+
132+
```shell
133+
git make-release [options]
134+
```
135+
136+
| Options | Description | Default |
137+
|---------|-----------------------------------------------------------|---------|
138+
| --dry | Perform a dry run without any changes to branches or tags | N/A |
139+
140+
### Make Release Example
141+
142+
```shell
143+
git make-release --dry
144+
```
145+
90146
## Docs
147+
91148
* [Hop](docs/hop/README.md)
92149
* [Setting up Nginx-Proxy](docs/nginx-proxy/README.md)
93150
* [Setting up PHP Testing in PhpStorm](docs/phpstorm-docker/README.md)

bin/git-make-release

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bash
2+
3+
# Prompt for the version number
4+
prompt_for_version() {
5+
read -rp "Enter the version number (default: $1): " version
6+
echo "${version:-$1}" # Return the entered version number or the default if none was entered
7+
}
8+
9+
# Increment version number
10+
increment_version() {
11+
# Split the version string into parts using read -a
12+
IFS='.'
13+
read -ra version_parts <<< "$1"
14+
15+
# Ensure that we have exactly three parts for major, minor, and patch versions
16+
if [ ${#version_parts[@]} -ne 3 ]; then
17+
echo "Error: Invalid version format '$1'. Expected format 'Major.Minor.Patch'." >&2
18+
return 1 # Exit the function with an error status
19+
fi
20+
21+
# Increment the minor version and reset the patch version to 0
22+
local next_version="${version_parts[0]}.$((version_parts[1] + 1)).0"
23+
24+
# Call prompt_for_version, using the next version as the default
25+
prompt_for_version "$next_version"
26+
}
27+
28+
# Get the latest version tag
29+
get_latest_version() {
30+
git describe --tags "$(git rev-list --tags --max-count=1)" 2>/dev/null || echo "v0.0.0"
31+
}
32+
33+
# Prepend the tickets to the CHANGELOG.md
34+
prepend_to_changelog() {
35+
local version=$1
36+
local tickets=$2
37+
local date
38+
39+
date=$(date +"%b %d, %Y")
40+
local changelog="CHANGELOG.md"
41+
42+
# Create a backup of the CHANGELOG.md
43+
cp "$changelog" "$changelog.bak"
44+
45+
# Prepend the new content to the CHANGELOG.md
46+
{
47+
echo "# v$version ($date)"
48+
for ticket in $tickets; do
49+
echo "* $ticket - "
50+
done
51+
echo ""
52+
cat "$changelog.bak"
53+
} > "$changelog"
54+
55+
rm "$changelog.bak"
56+
}
57+
58+
# Check if the --dry flag is set
59+
DRY_RUN=false
60+
if [ "$1" == "--dry" ]; then
61+
DRY_RUN=true
62+
fi
63+
64+
# Get the latest tag and suggest the next version number
65+
LATEST_TAG=$(get_latest_version)
66+
NEXT_VERSION=$(increment_version "${LATEST_TAG#v}") # Assuming the tag is in the 'v1.2.3' format
67+
68+
if $DRY_RUN; then
69+
# Perform a dry run
70+
echo "This is a dry run. The release branch will not be created."
71+
echo "The next version number would be: ${NEXT_VERSION}"
72+
git tickets | grep -Eo '(\w+)-([0-9]+)'
73+
else
74+
# Perform the actual release process
75+
# Capture the output of git-tickets
76+
TICKETS_OUTPUT=$(git tickets --update)
77+
78+
# Use grep to filter the output
79+
TICKET_IDS=$(echo "$TICKETS_OUTPUT" | grep -Eo '(\w+)-([0-9]+)')
80+
81+
# Prepend the tickets to the CHANGELOG.md
82+
prepend_to_changelog "$NEXT_VERSION" "$TICKET_IDS"
83+
84+
# Checkout a new branch with the pattern release-###
85+
git checkout -b "release-${NEXT_VERSION}"
86+
87+
# Add the CHANGELOG.md to the staging area
88+
git add CHANGELOG.md
89+
90+
echo "Tickets to be included in this release:"
91+
echo "$TICKET_IDS"
92+
fi

bin/git-tickets

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
# Prompt for a branch name with a default
3+
prompt_for_branch() {
4+
read -rp "Enter the name of the $1 branch (default: $2): " branch
5+
echo "${branch:-$2}" # Return the entered branch name or the default if none was entered
6+
}
7+
8+
# Check for --update flag
9+
UPDATE_BRANCHES=false
10+
for arg in "$@"; do
11+
if [ "$arg" == "--update" ]; then
12+
UPDATE_BRANCHES=true
13+
break
14+
fi
15+
done
16+
17+
# Prompt for the first branch name if not provided
18+
if [ -z "$1" ] || [ "$1" == "--update" ]; then
19+
BRANCH1=$(prompt_for_branch "first" "master")
20+
else
21+
BRANCH1="$1"
22+
fi
23+
24+
# Prompt for the second branch name if not provided
25+
if [ -z "$2" ] || [ "$2" == "--update" ]; then
26+
BRANCH2=$(prompt_for_branch "second" "staging")
27+
else
28+
BRANCH2="$2"
29+
fi
30+
# Function to update or create temporary branches
31+
process_branch() {
32+
local branch_name=$1
33+
local temp_branch="temp_${branch_name}_$$"
34+
if $UPDATE_BRANCHES; then
35+
git checkout "$branch_name" &> /dev/null
36+
if ! git checkout "$branch_name" &> /dev/null; then
37+
echo "Failed to checkout branch $branch_name" >&2
38+
exit 1
39+
fi
40+
git pull origin "$branch_name" &> /dev/null
41+
echo "$branch_name"
42+
else
43+
git fetch origin "$branch_name":"$temp_branch" &> /dev/null
44+
if ! git rev-parse --verify "$temp_branch" &> /dev/null; then
45+
echo "Failed to fetch and create temp branch for $branch_name" >&2
46+
exit 1
47+
fi
48+
echo "$temp_branch"
49+
fi
50+
}
51+
52+
53+
# Process the first branch
54+
BRANCH1=$(process_branch "$BRANCH1")
55+
56+
# Process the second branch
57+
BRANCH2=$(process_branch "$BRANCH2")
58+
59+
# If not updating branches, use the temporary branches for log
60+
if ! $UPDATE_BRANCHES; then
61+
# Run the git log command and process the output on temporary branches
62+
git log "$BRANCH2".."$BRANCH1" --oneline --no-merges | grep -Eio "(\w+)-([0-9]+)" | tr '[:lower:]' '[:upper:]' | sort -u
63+
64+
# Delete the temporary branches
65+
git branch -D "$BRANCH1" &> /dev/null
66+
git branch -D "$BRANCH2" &> /dev/null
67+
git status;
68+
else
69+
# Run the git log command and process the output on updated branches
70+
git log "$BRANCH2".."$BRANCH1" --oneline --no-merges | grep -Eio "(\w+)-([0-9]+)" | tr '[:lower:]' '[:upper:]' | sort -u
71+
72+
# Checkout to the first branch (assumed to be 'master')
73+
git checkout "$BRANCH1" &> /dev/null
74+
git status
75+
fi

0 commit comments

Comments
 (0)