115 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| #!/bin/bash
 | |
| 
 | |
| GITEA="ssh://git@focs.ji.sjtu.edu.cn:2222"
 | |
| ORG=${GITHUB_ACTION_REPOSITORY%/*}
 | |
| COURSE=${ORG/-*}
 | |
| 
 | |
| LXCHOST="tt@172.17.0.1"
 | |
| 
 | |
| 
 | |
| #
 | |
| # parse commit message
 | |
| #
 | |
| 
 | |
| parse_commit() {
 | |
| 
 | |
| 	message=$(git log -1 --pretty=format:%s ${GITHUB_WORKFLOW_SHA})
 | |
| 
 | |
| 	# add empty scope if none provided, then split "type(scope): message"
 | |
| 	OLDIFS=$IFS; IFS=$'\n'
 | |
| 	commit=( $(sed 's/\(^[^(]*\):/\1( ):/g; s/\([^(]*\)(\?\([^)]*\))\?: \(.*\)/\1\n\2\n\3/g' <<< ${message}) )
 | |
| 	IFS=$OLDIFS
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| #
 | |
| # gen 
 | |
| #
 | |
| 
 | |
| generate_json() {
 | |
| 
 | |
| 	parse_commit
 | |
| 	for i in ${commit[1]}; do 
 | |
| 		echo joj-conf-generator $HOME/.config/joj/$i
 | |
| 	done
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| #
 | |
| # initial repo/config setup
 | |
| #
 | |
| init() {
 | |
| 
 | |
|         [ -d $COURSE-config ] && backup $COURSE-config
 | |
| 
 | |
|         git clone $GITEA/$ORG/$COURSE-joj.git $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
 | |
| 
 | |
| # 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
 | |
| 
 | |
|                 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
 | |
| 
 | |
|                 git add Readme.md
 | |
|                 git commit -m"docs: readme"
 | |
|                 git push
 | |
|         fi
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| #
 | |
| # update config repo + copy config files
 | |
| #
 | |
| update() {
 | |
| 
 | |
|         [ -d "$COURSE-joj" ] || ( echo "ERROR: init first!" 1>&2 && exit )
 | |
| 
 | |
| 
 | |
| #				genjson
 | |
| 
 | |
|         rsync -a ./$HOME/ $LXCHOST:$HOME
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| #
 | |
| # simple local backup
 | |
| #
 | |
| 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"
 | |
| 
 | |
| }
 | |
| 
 | |
| [ -z "$1" ] && echo "Usage: $0 update|init"
 | |
| 
 | |
| cd $HOME/.cache
 | |
| $1
 |