Skip to content

Commit 52d9337

Browse files
committed
upgraded version...
1 parent 27a05b0 commit 52d9337

File tree

2 files changed

+257
-41
lines changed

2 files changed

+257
-41
lines changed

README.md

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

2525
```bash
26-
ACTIVITY_BR=main && MAX_PAST_DAYS=365 && COMMIT_NB= && COMMIT_MAX=7 && \
27-
curl -sL 'https://raw.githubusercontent.com/ccdd12/github-activity-bash-script/main/activity.sh' \
28-
| bash ;
26+
export ACTIVITY_BR=main ; export MAX_PAST_DAYS=365 ; export COMMIT_NB= ; export COMMIT_MAX=7 ; \
27+
curl -sL https://raw.githubusercontent.com/ccdd12/github-activity-bash-script/main/activity.sh \
28+
| bash ;
2929
```
3030

31-
# Environment variables
31+
# Install
3232

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` |
33+
```bash
34+
bash -c 'UPDATE_BIN="${HOME}/.local/bin" ; \
35+
curl -sL https://raw.githubusercontent.com/ccdd12/github-activity-bash-script/main/activity.sh \
36+
--output ${UPDATE_BIN}/${gh-gen-activity.sh} && \
37+
${UPDATE_BIN}/${gh-gen-activity.sh} --update && \
38+
${UPDATE_BIN}/${gh-gen-activity.sh} --interactive --branch=main --past=365 --commit-max=10 '
39+
```
3940

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]
41+
# Documentation
42+
43+
<table>
44+
<thead>
45+
<tr>
46+
<th style="font-weight: bold;text-align:center">env</th>
47+
<th style="font-weight: bold;text-align:center">description</th>
48+
<th style="font-weight: bold;text-align:center">type</th>
49+
<th style="font-weight: bold;text-align:center">default value</th>
50+
<th style="font-weight: bold;text-align:center" colspan="2" >command line arguments</th>
51+
</tr>
52+
</thead>
53+
<tbody>
54+
<tr>
55+
<td style="text-align:center"><code>ACTIVITY_BR</code></td>
56+
<td style="text-align:center">working git branch</td>
57+
<td style="text-align:center"><code>string</code></td>
58+
<td style="text-align:center"><code>main</code></td>
59+
<td style="text-align:center"><code>--branch | --br | -b <i>{value}</i></code></td>
60+
<td style="text-align:center"><code>--branch=<i>{value}</i></code></td>
61+
</tr>
62+
<tr>
63+
<td style="text-align:center"><code>MAX_PAST_DAYS</code></td>
64+
<td style="text-align:center">number of past days</td>
65+
<td style="text-align:center"><code>integer</code></td>
66+
<td style="text-align:center"><code>365</code></td>
67+
<td style="text-align:center"><code>--past | -p <i>{value}</i></code></td>
68+
<td style="text-align:center"><code>--past=<i>{value}</i></code></td>
69+
</tr>
70+
<tr>
71+
<td style="text-align:center"><code>COMMIT_NB</code></td>
72+
<td style="text-align:center">exactly git commit number each past day.</td>
73+
<td style="text-align:center"><code>integer</code></td>
74+
<td style="text-align:center">∅</td>
75+
<td style="text-align:center"><code>--commit-nb | --nb | -n <i>{value}</i></code></td>
76+
<td style="text-align:center"><code>--commit-nb=<i>{value}</i> | --nb=<i>{value}</i></code></td>
77+
</tr>
78+
<tr>
79+
<td style="text-align:center"><code>COMMIT_MAX</code></td>
80+
<td style="text-align:center">randomly git commit number each past day between [1…max]. <b>used only if <code>COMMIT_NB</code> is empty.<br/>if both <code>COMMIT_MAX</code> and <code>COMMIT_NB</code> empty, randomly commit number each past day between <code>[1..7]</code></b></td>
81+
<td style="text-align:center"><code>integer</code></td>
82+
<td style="text-align:center"><code>7</code></td>
83+
<td style="text-align:center"><code>--commit-max | --max | -m <i>{value}</i></code></td>
84+
<td style="text-align:center"><code>--commit-max=<i>{value}</i> | --nb=<i>{value}</i></code></td>
85+
</tr>
86+
<tr>
87+
<td style="text-align:center"><code>GITHUB_AUTHOR</code></td>
88+
<td style="text-align:center">owner of github repo to update from</td>
89+
<td style="text-align:center"><code>string</code></td>
90+
<td style="text-align:center"><code>ccdd12</code></td>
91+
<td style="text-align:center"><code>--gh-author <i>{value}</i></code></td>
92+
<td style="text-align:center"><code>--gh-author=<i>{value}</i></code></td>
93+
</tr>
94+
<tr>
95+
<td style="text-align:center"><code>REPONAME</code></td>
96+
<td style="text-align:center">github repo to update from</td>
97+
<td style="text-align:center"><code>string</code></td>
98+
<td style="text-align:center"><code>github-activity-bash-script</code></td>
99+
<td style="text-align:center"><code>--reponame <i>{value}</i></code></td>
100+
<td style="text-align:center"><code>--reponame=<i>{value}</i></code></td>
101+
</tr>
102+
<tr>
103+
<td style="text-align:center"><code>ASSET_FILENAME</code></td>
104+
<td style="text-align:center">release asset filename to download</td>
105+
<td style="text-align:center"><code>string</code></td>
106+
<td style="text-align:center"><code>gh-gen-activity.sh</code></td>
107+
<td style="text-align:center"><code>--asset-fname <i>{value}</i></code></td>
108+
<td style="text-align:center"><code>--asset-fname=<i>{value}</i></code></td>
109+
</tr>
110+
<tr>
111+
<td style="text-align:center"><code>UPDATE_BIN</code></td>
112+
<td style="text-align:center">update and install to given path</td>
113+
<td style="text-align:center"><code>string</code></td>
114+
<td style="text-align:center"><code>${HOME}/.local/bin</code></td>
115+
<td style="text-align:center"><code>--update <i>{value}</i></code></td>
116+
<td style="text-align:center"><code>--update=<i>{value}</i></code></td>
117+
</tr>
118+
<tr>
119+
<td style="text-align:center"><code>SELECTED_UPDATE_FUN</code></td>
120+
<td style="text-align:center"><code>bash</code> <code>function</code> to run when update requested. <b>this foes not launch update action</b></td>
121+
<td style="text-align:center"><code>string</code></td>
122+
<td style="text-align:center"><code>update_ghrelease</code> | <code>update_ghrepo</code> | <code>update_ghpage</code></td>
123+
<td style="text-align:center"><code>--update-fun <i>{value}</i></code></td>
124+
<td style="text-align:center"><code>--update-fun=<i>{value}</i></code></td>
125+
</tr>
126+
<tr>
127+
<td style="text-align:center">∅</td>
128+
<td style="text-align:center">interactive mode, ask user to create repo on github and push to it automatically. <b color="style:red;">does not work in pipe (ex. <code>curl -sL https://...../activity.sh | bash</code>)</b></td>
129+
<td style="text-align:center" colspan="2">∅</td>
130+
<td style="text-align:center" colspan="2"><code>--interactive | -i</code></td>
131+
</tr>
132+
</tbody>
133+
</table>
42134

43135
# Introduction to Bash Scripting
44136

activity.sh

100644100755
Lines changed: 153 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,150 @@ set -e
1414
# git push origin -f your_branch_name"
1515
#
1616

17+
GITHUB_AUTHOR='ccdd12'
18+
REPONAME='github-activity-bash-script'
19+
ASSET_FILENAME='gh-gen-activity.sh'
20+
SELECTED_UPDATE_FUN="update_ghrelease" # update_ghrelease update_ghrepo update_ghpage
21+
22+
dw_gh_release(){
23+
echo -n 'https://github.com'$(\
24+
curl -isL https://github.com/${GITHUB_AUTHOR}/${REPONAME}/releases/latest | \
25+
grep -Fhi "${ASSET_FILENAME}" | \
26+
sed -nEe 's~^\s+<a\shref="(.+)"\srel=.+$~\1~ip'\
27+
)
28+
}
29+
update_ghrelease(){
30+
[[ -z "$UPDATE_BIN" ]] && UPDATE_BIN="${HOME}/.local/bin"
31+
echo OK. updating...
32+
mkdir -p $UPDATE_BIN
33+
curl -L $(dw_gh_release) --output $UPDATE_BIN/$ASSET_FILENAME
34+
chmod u+x $UPDATE_BIN/$ASSET_FILENAME
35+
echo update saved to $UPDATE_BIN/$ASSET_FILENAME ...
36+
ls -lah --color=auto $UPDATE_BIN/$ASSET_FILENAME
37+
}
38+
update_ghrepo(){
39+
[[ -z "$UPDATE_BIN" ]] && UPDATE_BIN="${HOME}/.local/bin"
40+
echo OK. updating...
41+
mkdir -p $UPDATE_BIN
42+
curl -L "https://raw.githubusercontent.com/\
43+
${GITHUB_AUTHOR}/${REPONAME}/main/activity.sh" --output $UPDATE_BIN/$ASSET_FILENAME
44+
chmod u+x $UPDATE_BIN/$ASSET_FILENAME
45+
echo update saved to $UPDATE_BIN/$ASSET_FILENAME ...
46+
ls -lah --color=auto $UPDATE_BIN/$ASSET_FILENAME
47+
}
48+
update_ghpage(){
49+
[[ -z "$UPDATE_BIN" ]] && UPDATE_BIN="${HOME}/.local/bin"
50+
echo OK. updating...
51+
mkdir -p $UPDATE_BIN
52+
curl -L https://${GITHUB_AUTHOR}.github.io/${REPONAME}/activity.sh
53+
--output $UPDATE_BIN/$ASSET_FILENAME
54+
chmod u+x $UPDATE_BIN/$ASSET_FILENAME
55+
echo update saved to $UPDATE_BIN/$ASSET_FILENAME ...
56+
ls -lah --color=auto $UPDATE_BIN/$ASSET_FILENAME
57+
}
58+
59+
if [[ -z "ACTIVITY_BR" ]] ; then
60+
ACTIVITY_BR="main"
61+
fi
62+
if [[ -z "$MAX_PAST_DAYS" ]] ; then
63+
MAX_PAST_DAYS=365
64+
fi
65+
66+
while [[ "$#" -gt 0 ]] ; do
67+
case "$1" in
68+
--branch|--br|-b)
69+
ACTIVITY_BR="$2"
70+
shift
71+
;;
72+
--branch=*)
73+
ACTIVITY_BR=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
74+
;;
75+
--past|-p)
76+
MAX_PAST_DAYS="$2"
77+
shift
78+
;;
79+
--past=*)
80+
MAX_PAST_DAYS=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
81+
;;
82+
--commit-nb|--nb|-n)
83+
COMMIT_NB="$2"
84+
shift
85+
;;
86+
--commit-nb=*|--nb=*)
87+
COMMIT_NB=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
88+
;;
89+
--commit-max|--max|-m)
90+
COMMIT_MAX="$2"
91+
shift
92+
;;
93+
--commit-max=*|--max=*)
94+
COMMIT_MAX=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
95+
;;
96+
--interactive|-i)
97+
INTERACTIVE="true"
98+
;;
99+
--gh-author)
100+
GITHUB_AUTHOR="$2"
101+
shift
102+
;;
103+
--gh-author=*)
104+
GITHUB_AUTHOR=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
105+
;;
106+
--reponame)
107+
REPONAME="$2"
108+
shift
109+
;;
110+
--reponame=*)
111+
REPONAME=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
112+
;;
113+
----asset-fname)
114+
ASSET_FILENAME="$2"
115+
shift
116+
;;
117+
--asset-fname=*)
118+
ASSET_FILENAME=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
119+
;;
120+
--update|-u)
121+
UPDATE_BIN="${HOME}/.local/bin"
122+
;;
123+
--update=*)
124+
UPDATE_BIN=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
125+
;;
126+
--update-fun)
127+
SELECTED_UPDATE_FUN="$2"
128+
shift
129+
;;
130+
--update-fun=*)
131+
SELECTED_UPDATE_FUN=$(echo -n "$1" | tr '=' '\n' | tail -n 1)
132+
;;
133+
esac
134+
shift
135+
done
136+
137+
if [[ ! -z "$UPDATE_BIN" ]] ; then
138+
$SELECTED_UPDATE_FUN
139+
exit
140+
fi
141+
17142
if [[ ! -d ".git" ]] ; then
143+
>&2 echo NO. NOT git repo. making one...
18144
cwd=$(pwd)
19145
dir=$(mktemp -d -p $cwd test-git-repo-XXXXXXXXX)
20146
mkdir -p $dir
21147
cd $dir
22148
git init
23-
>&2 echo NO. NOT git repo...
24-
exit 1
25149
fi
26150

27151
# thomas-nyman CC BY-SA 3.0 https://unix.stackexchange.com/a/155077
28152
if [[ -z "$(git status --porcelain)" ]] ; then
29153
echo OK. Working directory clean...
30154
else
31155
>&2 echo NO. Working directory NOT clean. Uncommitted changes...
32-
exit 2
156+
exit 1
33157
fi
34158

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
159+
git checkout --orphan $ACTIVITY_BR >/dev/null 2>&1 || \
160+
git checkout $ACTIVITY_BR > /dev/null 2>&1 || echo ok >/dev/null
39161

40162
# Create temp commits direcotry
41163
if [[ ! -d .commits ]] ; then
@@ -47,11 +169,7 @@ if [[ ! -f .commits/changes ]] ; then
47169
touch .commits/changes
48170
fi
49171

50-
if [[ -z "$MAX_PAST_DAYS" ]] ; then
51-
MAX_PAST_DAYS=365
52-
fi
53-
54-
# Create commits for the past 365 days
172+
# Create commits for the past $MAX_PAST_DAYS days
55173
for (( day=$MAX_PAST_DAYS; day>=1; day-- )) ; do
56174
# Get the past date of the commit
57175
day2=$(date --date="-${day} day" "+%a, %d %b %Y %X %z")
@@ -80,7 +198,7 @@ for (( day=$MAX_PAST_DAYS; day>=1; day-- )) ; do
80198
done
81199
done
82200

83-
function yes_or_no {
201+
yes_or_no(){
84202
# author : tiago-lopo john-kugelman CC BY-SA 3.0 https://stackoverflow.com/a/29436423
85203
# usage : yes_or_no "$message" && do_something
86204
# modified
@@ -93,23 +211,29 @@ function yes_or_no {
93211
done
94212
}
95213

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.
214+
if [[ ! -z "$INTERACTIVE" ]] ; then
215+
if command -v gh ; then
216+
echo
217+
echo OK. github cli found.
218+
yes_or_no "Did you want to create repo on github ? " && \
219+
gh repo create $(basename $(pwd)) \
220+
-y \
221+
--private \
222+
--description 'generated by https://github.com/ccdd12/github-activity-bash-script' \
223+
--homepage 'https://github.com/ccdd12/github-activity-bash-script' \
224+
>/dev/null 2>&1 || \
225+
echo NO. repo already exist.
226+
else
227+
>&2 echo error "'gh'" command not found
228+
fi
107229
fi
108230

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.
231+
if [[ ! -z "$INTERACTIVE" ]] ; then
232+
echo
233+
yes_or_no "Did you want to push to remote 'origin' ? " && \
234+
git push --force --set-upstream origin $ACTIVITY_BR || \
235+
echo OK. push to your own remote remote/branch.
236+
fi
113237

114238
cat << EOF
115239
@@ -119,7 +243,7 @@ cat << EOF
119243
120244
To push your changes later :
121245
122-
git remote add origin https://github.com/username/$(basename $(pwd))
246+
git remote add origin https://github.com/username/$(basename $(pwd))
123247
124248
gh repo create
125249
git push --force --set-upstream origin $ACTIVITY_BR

0 commit comments

Comments
 (0)