Skip to content

Commit 6900034

Browse files
committed
chore: 🔧 refactor gitutils
1 parent 707b2e6 commit 6900034

File tree

5 files changed

+96
-65
lines changed

5 files changed

+96
-65
lines changed

src/gitutils/_degit.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,36 @@ if [ "$#" -lt 1 ]; then
66
exit 1
77
fi
88

9-
# Get repository URL from argument
10-
repo="${1}"
9+
set -e
1110

12-
# Get directory from argument or use current directory
13-
directory="${2:-.}"
11+
# Source colors script
12+
. zz_colors
13+
14+
# Function to print help and manage arguments
15+
eval $(
16+
zz_args "Clone and degit a repository" $0 "$@" <<-help
17+
- repo repo repository to clone
18+
- directory directory directory to degit into
19+
help
20+
)
1421

1522
# Get the repository host
1623
host=$(echo "${repo}" | sed -E 's/https?:\/\/([^/]+)\/.*/\1/')
1724

1825
# Keep only the repository name
1926
repo=$(echo "${repo}" | sed -E 's/.*github.com\/([^/]+)\/([^/]+).*/\1\/\2/')
2027

21-
# Create directory if it doesn't exist
22-
mkdir -p "${directory}"
28+
# Check if the directory is provided
29+
if [ -z "${directory}" ]; then
30+
directory=.
31+
fi
2332

2433
# Trace
25-
echo "Repository: ${repo}"
26-
echo "Directory: ${directory}"
34+
zz_log i "Repository: ${repo}"
35+
zz_log i "Directory: ${directory}"
36+
37+
# Create directory if it doesn't exist
38+
mkdir -p "${directory}"
2739

2840
# Download and extract repository per host
2941
case $host in
@@ -40,7 +52,7 @@ case $host in
4052
tar --extract --ungzip --strip-components=1 --directory "${directory}"
4153
;;
4254
*)
43-
echo "Unsupported host: ${host}"
55+
zz_log e "Unsupported host: {U ${host}}"
4456
exit 1
4557
;;
4658
esac

src/gitutils/_fixup.sh

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
#!/bin/sh
22

3+
set -e
4+
5+
# Source colors script
6+
. zz_colors
7+
8+
# Function to print help and manage arguments
9+
eval $(
10+
zz_args "Fix git history" $0 "$@" <<-help
11+
f - force allow overwritting pushed history
12+
- sha sha sha commit to fixup
13+
help
14+
)
15+
316
## Do not fixup if staged files contains composer.lock or package-lock.json
417
if [ -n "$(git diff --cached --name-only | grep -E 'composer.lock|package-lock.json')" ]; then
518
echo 'Packages lock file are staged, fixup is not allowed.'
@@ -12,43 +25,32 @@ cd "$(git rev-parse --show-toplevel)" >/dev/null
1225
#### Integrate modifications from remote
1326
git fetch --progress --prune --recurse-submodules=no origin >/dev/null
1427

15-
#### Display help
16-
if [ "$1" = '--help' ]; then
17-
echo 'Usage: git fixup [--force|<commit>]'
18-
exit 0
19-
fi
20-
2128
#### Check if fixup commit exists
22-
echo 'Check if fixup commit exists...'
23-
if ! git isFixup; then
24-
25-
## Do not fixup if no files are staged
26-
if [ -z "$(git diff --cached --name-only)" ]; then
27-
echo 'No files are staged, fixup is not allowed.'
28-
exit 1
29-
fi
30-
31-
#### Get commit to fixup
32-
33-
sha=$(git getcommit "$@")
29+
if git isFixup; then
30+
zz_log e 'Fixup commit found, please continue rebasing...'
31+
exit 1
32+
fi
3433

35-
#### Display commit to fixup
36-
echo 'Fixup commit given:' $sha
34+
## Do not fixup if no files are staged
35+
if [ -z "$(git diff --cached --name-only)" ]; then
36+
zz_log e 'No files are staged, fixup is not allowed.'
37+
exit 1
38+
fi
3739

38-
## Create fixup commit and exit if commit is not done
39-
if ! git commit --fixup $sha --no-verify; then
40-
echo 'Fixup commit failed...'
41-
exit 1
42-
fi
40+
#### Get commit to fixup
41+
sha=$(git getcommit $sha)
4342

44-
#### Start rebase
45-
git rebase -i --autosquash $sha~ --autostash --no-verify --exec '[ -f .git/hooks/pre-commit ] && (.git/hooks/pre-commit --name-only HEAD HEAD~1 && git commit --amend --no-edit --no-verify) || true'
46-
else
47-
echo -e "\e[32mExisting !fixup commit found. Continue rebasing...\e[0m"
43+
#### Display commit to fixup
44+
zz_log i "Fixup commit given: $sha"
4845

49-
#### Stage all changes
50-
git add --update && git rebase --continue
46+
## Create fixup commit and exit if commit is not done
47+
if ! git commit --fixup $sha --no-verify; then
48+
zz_log e 'Fixup commit failed...'
49+
exit 1
5150
fi
5251

52+
#### Start rebase
53+
git rebase -i --autosquash $sha~ --autostash --no-verify --exec '[ -f .git/hooks/pre-commit ] && (.git/hooks/pre-commit --name-only HEAD HEAD~1 && git commit --amend --no-edit --no-verify) || true'
54+
5355
#### Back to previous directory
5456
cd - >/dev/null

src/gitutils/_getcommit.sh

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#!/bin/sh
22

3-
#### Go to repository root
4-
cd "$(git rev-parse --show-toplevel)"
3+
set -e
54

6-
#### Get calling script name without extension and starting _
7-
script=$(basename $0 .sh | sed 's/^_//')
5+
# Source colors script
6+
. zz_colors
87

9-
#### Display help
10-
if [ "$1" = '--help' ]; then
11-
echo 'Usage: git '$script' [--force|<commit>]'
12-
exit 0
13-
fi
8+
# Function to print help and manage arguments
9+
eval $(
10+
zz_args "List git history and asks for commit" $0 "$@" <<-help
11+
f - force allow overwritting pushed history
12+
- sha sha sha commit to fixup
13+
help
14+
)
15+
16+
#### Go to repository root
17+
cd "$(git rev-parse --show-toplevel)"
1418

15-
#### List commits to fixup and ask user to choose one
16-
if [ "$1" = "--force" ]; then
17-
echo 'Get commit to fixup by overwritting pushed history...' >&2
19+
if [ -n "$force" ]; then
20+
zz_log w "Force mode enabled, overwriting pushed history"
1821
git forceable >&2
1922
read -p 'What commit to fix? ' sha
20-
elif [ -z "$1" ]; then
21-
echo 'Get commit to fixup without overwritting pushed history...' >&2
23+
elif [ -z "$sha" ]; then
2224
git fixable >&2
2325
read -p 'What commit to fix? ' sha
2426
else

src/gitutils/_release-prod.sh

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,70 @@
11
#!/bin/sh
22

3+
set -e
4+
5+
# Source colors script
6+
. zz_colors
7+
8+
# Function to print help and manage arguments
9+
eval $(
10+
zz_args "Release production branch" $0 "$@" <<-help
11+
help
12+
)
13+
314
#### Goto repository root
415
cd "$(git rev-parse --show-toplevel)" >/dev/null
516

617
#### CHECK IF ON A HOTFIX BRANCH, EXTRACT BRANCH NAME
718
if [ -n "$(git branch --list hotfix/*)" ]; then
819
flow=hotfix
920
name=$(git branch --list hotfix/* | sed 's/.*hotfix\///')
10-
npx --yes chalk-cli -t "{green ✔} Hotfix branch found: {yellow $name}"
21+
zz_log i "Hotfix branch found: {Yellow $name}"
1122
fi
1223

1324
#### CHECK IF ON A RELEASE BRANCH, EXTRACT BRANCH NAME
1425
if [ -n "$(git branch --list release/*)" ]; then
1526
flow=release
1627
name=$(git branch --list release/* | sed 's/.*release\///')
17-
npx --yes chalk-cli -t "{green ✔} Release branch found: {blue $name}"
28+
zz_log i "Release branch found: {Blue $name}"
1829
fi
1930

2031
#### CHECK IF A FLOW BRANCH IS FOUND
2132
if [ -z "$flow" ] && [ -f .git/RELEASE ]; then
2233
flow=release
2334
name=$(cat .git/RELEASE)
24-
25-
npx --yes chalk-cli -t "{green ✔} Release branch found: {blue $name}"
35+
zz_log i "Release branch found: {Blue $name}"
2636
git checkout $flow/$name
2737
fi
2838

2939
#### EXIT IF NO FLOW BRANCH IS FOUND
3040
if [ -z "$flow" ] || [ -z "$name" ]; then
31-
npx --yes chalk-cli -t "{red ✘} No flow branch found"
41+
zz_log e "No flow branch found"
3242
exit 1
3343
fi
3444

3545
#### GET BUMP VERSION
3646
GBV=$(gitversion -config .gitversion -showvariable MajorMinorPatch)
37-
npx --yes chalk-cli -t "{green ✔} Bump version: {green $GBV}"
47+
if [ -z "$GBV" ]; then
48+
zz_log e "Cannot get version from .gitversion"
49+
exit 1
50+
fi
51+
52+
zz_log i "Bump version: {Blue $GBV}"
3853

3954
#### PREVENT GIT EDITOR PROMPT
4055
GIT_EDITOR=:
4156

4257
#### UPDATE VERSION & CHANGELOG & FINISH RELEASE
43-
if npx --yes json --validate -q -f package.json && npx --yes commit-and-tag-version --skip.tag --no-verify --release-as $GBV; then
58+
if npx --yes commit-and-tag-version --skip.tag --no-verify --release-as $GBV; then
4459
if git flow $flow finish $name --tagname $GBV --message $GBV --push; then
45-
npx --yes chalk-cli -t "{green ✔} Release finished: {blue $name -> $GBV}"
60+
zz_log s "Release finished: {B $GBV}"
4661
rm -f .git/RELEASE
4762
else
4863
git undo
49-
npx --yes chalk-cli -t "{red ✘} Cannot finish release. CHANGELOG & VERSION are not updated."
64+
zz_log e "Cannot finish release. CHANGELOG & VERSION are not updated."
5065
fi
5166
else
52-
npx --yes chalk-cli -t "{red ✘} Cannot update version & finish release"
67+
zz_log e "Cannot update version & finish release"
5368
fi
5469

5570
#### BACK

src/gitutils/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "gitutils",
33
"name": "Git Aliases",
44
"description": "A feature to add useful Git aliases to your shell.",
5-
"version": "5.4.3",
5+
"version": "5.5.1",
66
"dependsOn": {
77
"ghcr.io/devcontainers/features/node:1": "lts",
88
"ghcr.io/tomgrv/devcontainer-features/common-utils:3": {

0 commit comments

Comments
 (0)