summaryrefslogtreecommitdiff
path: root/mkproj.sh
blob: 75f41f9c59c8457ee77d1d50a2d793a57a0b3914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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
######################################################################

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

# 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

# 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}"
    exit 1
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