autils

Unnamed repository; edit this file 'description' to name the repository.
git clone https://codeberg.org/emmett1/autils
Log | Files | Refs | README | LICENSE

reposync (1982B)


      1 #!/bin/sh -e
      2 #
      3 # script to sync repos
      4 # required /etc/reposync.conf
      5 #
      6 # /etc/reposync.conf format:
      7 #  <git url>|<branch>|<local path to sync>
      8 #
      9 
     10 log() {
     11     msg="$1"
     12     if [ "$LOG" ]; then
     13 		echo "$(date +'%Y-%m-%d %H:%M:%S') $msg" | tee -a "$LOGFILE"
     14 	else
     15 		echo "$msg"
     16 	fi
     17 }
     18 
     19 run_cmd() {
     20     if [ "$DRY_RUN" ]; then
     21         log " [dry-run] $*"
     22     else
     23         log " $*"
     24         eval "$@"
     25     fi
     26 }
     27 
     28 syncrepo() {
     29     url=$1
     30     branch=$2
     31     path=$3
     32     
     33     # if url is empty, do nothing
     34     if [ ! "$url" ]; then
     35 		exit 0
     36 	fi
     37     
     38     # if $path empty, your format is wrong
     39     if [ ! "$path" ]; then
     40 		echo "/etc/reposync.conf format: <git url>|<branch>|<local path to sync>"
     41 		exit
     42 	fi
     43 
     44     if [ "$FORCE" ]; then
     45         if [ -d "$path" ]; then
     46             log "=> Force removing repo directory $path..."
     47             run_cmd "rm -rf \"$path\""
     48         fi
     49     fi
     50 
     51     if [ ! -d "$path/.git" ]; then
     52         log "=> Sync repo $path..."
     53         run_cmd "mkdir -p \"$path\""
     54         run_cmd "git clone --branch \"$branch\" --single-branch \"$url\" \"$path\""
     55     else
     56         log "=> Updating repo $path..."
     57         run_cmd "cd \"$path\" && git fetch origin \"$branch\""
     58         run_cmd "cd \"$path\" && git reset --hard \"origin/$branch\""
     59         run_cmd "cd \"$path\" && git clean -fdx"
     60     fi
     61 }
     62 
     63 usage() {
     64     echo "Usage: $0 [-n|-l|-f]"
     65     echo " -n  dry-run"
     66     echo " -l  logging (/var/log/reposync.log)"
     67     echo " -f  force sync"
     68     exit 1
     69 }
     70 
     71 while [ "$1" ]; do
     72     case "$1" in
     73         -n) DRY_RUN=1;;
     74         -l) LOG=1;;
     75         -f) FORCE=1;;
     76         -h) usage;;
     77     esac
     78     shift
     79 done
     80 
     81 command -v git >/dev/null 2>&1 || {
     82     echo "error: git is not installed." >&2
     83     exit 1
     84 }
     85 
     86 LOGFILE="/var/log/reposync.log"
     87 CONFFILE="/etc/reposync.conf"
     88 
     89 if [ ! -f "$CONFFILE" ]; then
     90 	echo "error: config file not found: $CONFFILE" >&2
     91 	exit 1
     92 fi
     93 
     94 grep -Ev '^#|^$' "$CONFFILE" | while IFS='|' read -r url branch path _; do
     95     syncrepo "$url" "$branch" "$path"
     96 done
     97 
     98 exit 0