From eca5c99563e35226963a78fd9add694b0c030c19 Mon Sep 17 00:00:00 2001 From: garhve Date: Wed, 31 Jul 2024 01:21:56 +0800 Subject: improve mkproj.sh to be able to use it on local machine. --- mkproj.sh | 149 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 79 insertions(+), 70 deletions(-) diff --git a/mkproj.sh b/mkproj.sh index 6ebcd86..0cd1048 100755 --- a/mkproj.sh +++ b/mkproj.sh @@ -1,83 +1,92 @@ #!/usr/bin/env sh - ###################################################################### # @author : pico (pico@$HOSTNAME) # @file : mkproj # @created : Tuesday Dec 06, 2022 13:33:51 CST # # @description : shell script to simplify cgit +# only works for bash shell ###################################################################### -NC='\x1b[0m' -BLUE='\x1b[0;34m' -YELLOW='\033[0;33m' -RED='\x1b[0;31m' - -path="$HOME/.local/git" -config="$HOME/.config/git/git_template/hooks" -work="$HOME/git" - -echo "Start making bare repo" -echo -e "The repo will be located in $BLUE$path$NC" -read -r -p "Enter the repo name: " repo - -repo="${repo:-null}" -[ "${repo}" == 'null' ] && echo "no specific repo name was given" && exit 1 +host="ssh ip" +port=" -p 'port'" +user="user name" -# dealing with bare repo -if [ ! -d $path ]; then - echo -e "$RED$path$NC doesn't exist." - read -n1 -r -p "Do you want to create it(y or n) " option - - # set default value to 'n' - if [ $option != 'y' ] && [ $option != 'n' ]; then - option='n' +run() { +#!/usr/bin/env sh + path="$HOME/.local/git" + config="$HOME/.config/git/git_template/hooks" + work="$HOME/git" + + NC='\x1b[0m' + BLUE='\x1b[0;34m' + YELLOW='\033[0;33m' + RED='\x1b[0;31m' + + echo "Start making bare repo" + echo -e "The repo will be located in $BLUE$path$NC" + read -r -p "Enter the repo name: " repo + + repo="${repo:-null}" + [ "${repo}" == 'null' ] && echo "no specific repo name was given" && exit 1 + + # dealing with bare repo + if [ ! -d $path ]; then + echo -e "$RED$path$NC doesn't exist." + read -n1 -r -p "Do you want to create it(y or n) " option + + # set default value to 'n' + if [ $option != 'y' ] && [ $option != 'n' ]; then + option='n' + fi + + # if y is given, create git directory + if [ $option == 'y' ]; then + mkdir -p "$path" + [ "$?" -ne 0 ] && echo "failed create path" || echo "$path created, now create repo" + fi + + # quit script + [ $option == 'n' ] && echo -e "${YELLOW}quit script$NC" + else + echo -e "${BLUE}$path${NC} exists, check if $repo exists" fi - - # if y is given, create git directory - if [ $option == 'y' ]; then - mkdir -p "$path" - [ "$?" -ne 0 ] && echo "failed create path" || echo "$path created, now create repo" + + # check if repo already exists + [ -d "$path/$repo" ] && \ + if [ -z "$(git -C $path/$repo rev-parse)" ]; then + echo -e "${YELLOW}$repo$NC exists, please check your git repo" + elif [ -s "$path/$repo" ]; then + echo -e "${YELLOW}$repo$NC is not empty, please check directory" + fi && \ + exit 1 + + echo -e "${YELLOW}$repo$NC does not exist, create repo" + git --bare init "$path/$repo" > /dev/null 2>&1 + #read owner + read -r -p "Enter the owner of the repo: " owner + #read description + read -r -p "Enter the description for this repo: " desc + # set info + [ ! -z "$owner" ] && echo "[gitweb]" >> "$path/$repo/config" && echo " owner = $owner" >> "$path/$repo/config" + [ ! -z "$desc" ] && echo "$desc" > "$path/$repo/description" + + echo -e "${YELLOW}finished bare repo creation!$NC" + + # set working tree + echo "" + echo -e "Centralized hooks are already set in ${BLUE}$config$NC" + echo -e -n "Do you want to set working tree? (git repo will be created in ${BLUE}$work$NC y/n): " + read -r wt + + if [ $wt == 'n' ] || [ $wt != 'y' ]; then + echo -e "${YELLOW}creation finished${NC}" + else + mkdir -p "$work/$repo" + [ "$?" -ne 0 ] && echo -e "failed creating ${BLUE}$work/$repo${NC}, please check permission" || echo -e "${YELLOW}creation finished${NC}" fi + + echo -e "you may now add remote with ${RED}git remote --add origin ssh://user@ip:port$path/$repo$NC command." +} - # quit script - [ $option == 'n' ] && echo -e "${YELLOW}quit script$NC" -else - echo -e "${BLUE}$path${NC} exists, check if $repo exists" -fi - -# check if repo already exists -[ -d "$path/$repo" ] && \ - if [ -z "$(git -C $path/$repo rev-parse)" ]; then - echo -e "${YELLOW}$repo$NC exists, please check your git repo" - elif [ -s "$path/$repo" ]; then - echo -e "${YELLOW}$repo$NC is not empty, please check directory" - fi && \ - exit 1 - -echo -e "${YELLOW}$repo$NC does not exist, create repo" -git --bare init "$path/$repo" > /dev/null 2>&1 -#read owner -read -r -p "Enter the owner of the repo: " owner -#read description -read -r -p "Enter the description for this repo: " desc -# set info -[ ! -z "$owner" ] && echo "[gitweb]" >> "$path/$repo/config" && echo " owner = $owner" >> "$path/$repo/config" -[ ! -z "$desc" ] && echo "$desc" > "$path/$repo/description" - -echo -e "${YELLOW}finished bare repo creation!$NC" - -# set working tree -echo "" -echo -e "Centralized hooks are already set in ${BLUE}$config$NC" -echo -e -n "Do you want to set working tree? (git repo will be created in ${BLUE}$work$NC y/n): " -read -r wt - -if [ $wt == 'n' ] || [ $wt != 'y' ]; then - echo -e "${YELLOW}creation finished${NC}" -else - mkdir -p "$work/$repo" - [ "$?" -ne 0 ] && echo -e "failed creating ${BLUE}$work/$repo${NC}, please check permission" || echo -e "${YELLOW}creation finished${NC}" -fi - -echo -e "you may now add remote with ${RED}git remote --add origin ssh://user@ip:port$path/$repo$NC command." +ssh -t ${user}@${host}${port} "$(typeset -f run); run" -- cgit v1.2.3-70-g09d2