From 34c91927111c63280b1e4868e5a15c370c3e185c Mon Sep 17 00:00:00 2001 From: m Date: Mon, 4 Nov 2024 09:39:44 +0800 Subject: [PATCH] refactor: repos init --- joj-repo | 65 +++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/joj-repo b/joj-repo index d259e94..14409e4 100644 --- a/joj-repo +++ b/joj-repo @@ -10,7 +10,6 @@ LXCHOST="tt@172.17.0.1" # # parse commit message # - parse_commit() { message=$(git log -1 --pretty=format:%s ${GITHUB_WORKFLOW_SHA}) @@ -26,7 +25,6 @@ parse_commit() { # # gen # - generate_json() { parse_commit @@ -42,42 +40,30 @@ generate_json() { # init() { - [ -d $COURSE-config ] && backup $COURSE-config + # backup current grading scoreboard + [ -d $COURSE-joj ] && backup $COURSE-joj - git clone $GITEA/$ORG/$COURSE-joj.git $COURSE-config + git clone $GITEA/$ORG/$COURSE-joj.git - cd $COURSE-config + cd $COURSE-config -# copy config files - rsync -r ./$HOME/ $HOME - -# check existence of grading branch - git branch -a | grep -q grading && br=1 - - cd .. - -# backup current grading scoreboard - [ -d $COURSE-joj ] && backup $COURSE-joj + # check existence of grading branch + git branch -a | grep -q grading && br=1 # clone grading branch or create one + push readme - if [ "x$br" = "x1" ]; then - git clone -b grading $GITEA/$COURSE-$SEMESTER/$COURSE-joj.git - cd $COURSE-joj - git switch grading - git pull - else - git clone $GITEA/$COURSE-$SEMESTER/$COURSE-joj.git +if [ "x$br" = "x1" ]; then + git switch grading + git pull +else + git switch --orphan grading - cd $COURSE-joj - git switch --orphan grading + echo "# $COURSE JOJ grading" > Readme.md + echo "This branch is automatically updated by JOJ, **never edit any file in this branch!**" >> Readme.md - echo "# $COURSE JOJ grading" > Readme.md - echo "This branch is automatically updated by JOJ, **never edit any file in this branch!**" >> Readme.md - - git add Readme.md - git commit -m"docs: readme" - git push - fi + git add Readme.md + git commit -m"docs: readme" + git push +fi } @@ -87,12 +73,12 @@ init() { # update() { - [ -d "$COURSE-joj" ] || ( echo "ERROR: init first!" 1>&2 && exit ) + [ -d "$COURSE-joj" ] || ( echo "ERROR: init first!" 1>&2 && exit ) -# genjson + # genjson - rsync -a ./$HOME/ $LXCHOST:$HOME + rsync -a ./$HOME/ $LXCHOST:$HOME } @@ -102,13 +88,16 @@ update() { # backup() { - BCK="$HOME/.local/share/joj/$1-$(date +%y%m%d-%H%M%S)" - echo "WARNING: $1 already exits, backing it up to $BCK" - mv "$1" "$BCK" + [ -d $HOME/.local/share/joj ] || mkdir -p $HOME/.local/share/joj + + BCK="$HOME/.local/share/joj/$1-$(date +%y%m%d-%H%M%S)" + echo "WARNING: $1 already exits, backing it up to $BCK" + mv "$1" "$BCK" } -[ -z "$1" ] && echo "Usage: $0 update|init" +[ -z "$1" ] && echo "Usage: $0 update|init" && exit 1 +[ -d $HOME/.cache ] || mkdir $HOME/.cache cd $HOME/.cache $1