Skip to content

Commit 2edd57f

Browse files
committed
make script fail safe, interactive, and get started
1 parent 22f2166 commit 2edd57f

File tree

2 files changed

+110
-28
lines changed

2 files changed

+110
-28
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ To execute just run the following commaind inside a demo repository:
2323
# Get started
2424

2525
```bash
26-
cwd=$(pwd) && \
27-
dir=$(mktemp -d -p $cwd test-git-repo-XXXXXXXXX) && \
28-
mkdir -p $dir && cd $dir && git init && \
29-
curl -sL 'https://raw.githubusercontent.com/bobbyiliev/github-activity-bash-script/main/activity.sh' | \
30-
bash -x && \
31-
git branch -m master main ; \
32-
echo -e "\n\nTODO push your changes:\n\t\
33-
git remote add origin https://github.com/username/reponame\n\t\
34-
git push origin --force --set-upstream origin main\n\n" \
35-
\
26+
ACTIVITY_BR=main && MAX_PAST_DAYS=365 && COMMIT_NB= && COMMIT_MAX=7 && \
27+
curl -sL 'https://raw.githubusercontent.com/bobbyiliev/github-activity-bash-script/main/activity.sh' \
28+
| bash ;
3629
```
3730

31+
# Environment variables
32+
33+
| env | description | type | default value |
34+
|:-------------------:|:---------------------:|:-------------:|:------------------------------:|
35+
| `ACTIVITY_BR` | working git branch | `string` | `main` |
36+
| `MAX_PAST_DAYS` | number of past days | `integer` | `365` |
37+
| `COMMIT_NB` | exactly git commit number each past day. | `integer` | |
38+
| `COMMIT_MAX` | randomly git commit number each past day between [1..max] | `integer` | `7` |
39+
40+
* `COMMIT_MAX` used only if `COMMIT_NB` is empty
41+
* if both `COMMIT_MAX` and `COMMIT_NB`, randomly commit number each past day between [1..7]
42+
3843
# Introduction to Bash Scripting
3944

4045
In case that you are interested in learning more about Bash Scripting, make sure to checkout this open-source eBook:

activity.sh

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,128 @@
11
#!/bin/bash
22

3-
####
3+
set -e
4+
5+
#
46
## Use for demo purposes only!
57
## To execute just run the following commaind inside a demo repository:
68
#
79
# wget https://raw.githubusercontent.com/bobbyiliev/github-activity-bash-script/main/activity.sh
810
# bash activity.sh
9-
# # Finllly push your changes to GitHub:
11+
#
12+
## Finllly push your changes to GitHub:
13+
#
1014
# git push origin -f your_branch_name"
11-
####
15+
#
16+
17+
if [[ ! -d ".git" ]] ; then
18+
cwd=$(pwd)
19+
dir=$(mktemp -d -p $cwd test-git-repo-XXXXXXXXX)
20+
mkdir -p $dir
21+
cd $dir
22+
git init
23+
>&2 echo NO. NOT git repo...
24+
exit 1
25+
fi
26+
27+
# thomas-nyman CC BY-SA 3.0 https://unix.stackexchange.com/a/155077
28+
if [[ -z "$(git status --porcelain)" ]] ; then
29+
echo OK. Working directory clean...
30+
else
31+
>&2 echo NO. Working directory NOT clean. Uncommitted changes...
32+
exit 2
33+
fi
34+
35+
if [[ -z "ACTIVITY_BR" ]] ; then
36+
ACTIVITY_BR="main"
37+
fi
38+
git checkout --orphan $ACTIVITY_BR >/dev/null 2>&1 || git checkout $ACTIVITY_BR > /dev/null 2>&1
1239

13-
##
1440
# Create temp commits direcotry
15-
##
1641
if [[ ! -d .commits ]] ; then
17-
mkdir -p .commits
42+
mkdir -p .commits
1843
fi
19-
##
44+
2045
# Add changes file log
21-
##
2246
if [[ ! -f .commits/changes ]] ; then
23-
touch .commits/changes
24-
git add .
47+
touch .commits/changes
48+
fi
49+
50+
if [[ -z "$MAX_PAST_DAYS" ]] ; then
51+
MAX_PAST_DAYS=365
2552
fi
2653

27-
##
2854
# Create commits for the past 365 days
29-
##
30-
for day in {1..365} ; do
55+
for (( day=$MAX_PAST_DAYS; day>=1; day-- )) ; do
3156
# Get the past date of the commit
3257
day2=$(date --date="-${day} day" "+%a, %d %b %Y %X %z")
3358

3459
echo "Creating commits for ${day}"
3560

3661
# Generate random number of commits for that date
37-
commits=$(( ( RANDOM % 6 ) + 2 ))
62+
if [[ -z "$COMMIT_NB" ]] ; then
63+
if [[ -z "$COMMIT_MAX" ]] ; then
64+
commits=$(( ( RANDOM % 6 ) + 2 ))
65+
else
66+
commits=$(( ( RANDOM % $COMMIT_MAX ) + 1 ))
67+
fi
68+
else
69+
commits=$COMMIT_NB
70+
fi
3871

3972
# Create the comits
4073
echo "Creating ${commits} commits"
4174
for ((i=1;i<=${commits};i++)); do
4275
content=$(date -d "${day2}" +"%s")
4376
echo ${content}-${i} >> .commits/changes
44-
git commit -am "Commit number ${content}-${i}"
77+
git add .commits/changes
78+
git commit -m "Commit number ${content}-${i}"
4579
git commit --amend --no-edit --date "${day2}"
4680
done
4781
done
4882

49-
echo "Generating commits completed..."
50-
echo "To push your commits run:"
51-
echo "git push origin -f your_branch_name"
83+
function yes_or_no {
84+
# author : tiago-lopo john-kugelman CC BY-SA 3.0 https://stackoverflow.com/a/29436423
85+
# usage : yes_or_no "$message" && do_something
86+
# modified
87+
while true; do
88+
read -p "$* [y/n]: " yn
89+
case $yn in
90+
[YyOo]*) return 0 ;;
91+
[Nn]*) echo "Aborted" ; return 1 ;;
92+
esac
93+
done
94+
}
95+
96+
if command -v gh ; then
97+
echo
98+
echo OK. github cli found.
99+
yes_or_no "Did you want to create repo on github ? " && \
100+
gh repo create $(basename $(pwd)) \
101+
-y \
102+
--private \
103+
--description 'generated by https://github.com/ccdd12/github-activity-bash-script' \
104+
--homepage 'https://github.com/ccdd12/github-activity-bash-script' \
105+
>/dev/null 2>&1 || \
106+
echo NO. repo already exist.
107+
fi
108+
109+
echo
110+
yes_or_no "Did you want to push to remote 'origin' ? " && \
111+
git push --force --set-upstream origin $ACTIVITY_BR || \
112+
echo OK. push to your own remote remote/branch.
113+
114+
cat << EOF
115+
116+
117+
118+
Generating commits completed...
119+
120+
To push your changes later :
121+
122+
git remote add origin https://github.com/username/$(basename $(pwd))
123+
124+
gh repo create
125+
git push --force --set-upstream origin $ACTIVITY_BR
126+
127+
128+
EOF

0 commit comments

Comments
 (0)