@@ -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+
17142if [[ ! -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
25149fi
26150
27151# thomas-nyman CC BY-SA 3.0 https://unix.stackexchange.com/a/155077
28152if [[ -z " $( git status --porcelain) " ]] ; then
29153 echo OK. Working directory clean...
30154else
31155 >&2 echo NO. Working directory NOT clean. Uncommitted changes...
32- exit 2
156+ exit 1
33157fi
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
41163if [[ ! -d .commits ]] ; then
@@ -47,11 +169,7 @@ if [[ ! -f .commits/changes ]] ; then
47169 touch .commits/changes
48170fi
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
55173for (( 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
81199done
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
107229fi
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
114238cat << 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