|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -#### |
| 3 | +set -e |
| 4 | + |
| 5 | +# |
4 | 6 | ## Use for demo purposes only! |
5 | 7 | ## To execute just run the following commaind inside a demo repository: |
6 | 8 | # |
7 | 9 | # wget https://raw.githubusercontent.com/bobbyiliev/github-activity-bash-script/main/activity.sh |
8 | 10 | # bash activity.sh |
9 | | -# # Finllly push your changes to GitHub: |
| 11 | +# |
| 12 | +## Finllly push your changes to GitHub: |
| 13 | +# |
10 | 14 | # 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 |
12 | 39 |
|
13 | | -## |
14 | 40 | # Create temp commits direcotry |
15 | | -## |
16 | 41 | if [[ ! -d .commits ]] ; then |
17 | | - mkdir -p .commits |
| 42 | + mkdir -p .commits |
18 | 43 | fi |
19 | | -## |
| 44 | + |
20 | 45 | # Add changes file log |
21 | | -## |
22 | 46 | 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 |
25 | 52 | fi |
26 | 53 |
|
27 | | -## |
28 | 54 | # 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 |
31 | 56 | # Get the past date of the commit |
32 | 57 | day2=$(date --date="-${day} day" "+%a, %d %b %Y %X %z") |
33 | 58 |
|
34 | 59 | echo "Creating commits for ${day}" |
35 | 60 |
|
36 | 61 | # 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 |
38 | 71 |
|
39 | 72 | # Create the comits |
40 | 73 | echo "Creating ${commits} commits" |
41 | 74 | for ((i=1;i<=${commits};i++)); do |
42 | 75 | content=$(date -d "${day2}" +"%s") |
43 | 76 | 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}" |
45 | 79 | git commit --amend --no-edit --date "${day2}" |
46 | 80 | done |
47 | 81 | done |
48 | 82 |
|
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