summaryrefslogtreecommitdiff
path: root/repo.sh
blob: f10ef1d1eada2c6e35fbfb0133d2fb3919e333c7 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
reposDirectory=/home/git/repositories
reposList=/home/git/repos.list

echo "1) Create a new repo"
echo "2) Edit a repo"
echo "3) Delete a repo"
echo "4) Exit"
while [[ -z $option ]] || [[ $option -gt 4 ]] || [[ $option -lt 1 ]];
do
    read -p "Choose an option: " option
done

if [[ $option == 1 ]];
then
    read -p "What's the repo name? " repoName
    read -p "What's the repo description? [default: ''] " repoDesc
    # chronic hides output
    if chronic git config user.name && chronic git config user.email;
    then
        username=$(git config user.name)
        email="<$(git config user.email)>"
        echo "Your git configuration has a username and email set:"
        echo "Username: $username"
        echo "Email: $email"
        read -p "Set owner to your git config? [default: 'y'] (y/n) " usegc
        if [[ $usegc == [yY] ]] || [[ -z $usegc ]];
        then
            repoOwner="$username $email"
            echo "Author set to \"$repoOwner\""
        fi
    fi
    if [[ $usegc == [nN] ]] || [[ -z $usegc ]];
    then
        read -p "What's the owner name? [default: ''] " repoOwner
    fi
    echo "Performing magic wizardry..."

    echo "Adding repo to list..."
    echo $repoName >> $reposList

    echo "Creating directory for $repoName.."
    mkdir $reposDirectory/$repoName

    echo "Initializing bare repository and setting permissions..."
    git init --bare $reposDirectory/$repoName
    chown git:git $reposDirectory/$repoName --recursive

    echo "Setting repository owner..."
    echo [gitweb] >> $reposDirectory/$repoName/config
    echo "owner = $repoOwner" >> $reposDirectory/$repoName/config

    echo "Setting repository description..."
    echo -n "" > $reposDirectory/$repoName/description
    if [[ -n "$repoDesc" ]];
    then
        echo "$repoDesc" >> $reposDirectory/$repoName/description
    fi
    echo "$repoName magically appeared!"

elif [[ $option == 2 ]];
then
    i=1
    while read line; do
        echo "$i) $line"
        i=$((i+1))
    done <$reposList
    while [[ -z $repo ]] || [[ $repo -gt $((i-1)) ]] || [[ $repo -lt 1 ]];
    do
        read -p "Choose a repo to edit: " repo
    done
    repo=$(sed "$repo!d" $reposList)
    echo "1) Edit repo name"
    echo "2) Edit repo description"
    echo "3) Edit repo author"
    while [[ -z $editOption ]] || [[ $editOption -gt 3 ]]  || [[ $editOption -lt 1 ]];
    do
        read -p "Choose an option: " editOption
    done
    if [[ $editOption == 1 ]];
    then
        read -p "What is the repo's new name? " newName
        sed -i "s/$repo/$newName/" $reposList
        echo $reposDirectory/$repo $repoDirectory/$newName
        mv $reposDirectory/$repo $reposDirectory/$newName
        echo "$repo has transformed into $newName!"
    elif [[ $editOption == 2 ]];
    then
        read -p "What is the repo's new description? " newDesc
        mv $reposDirectory/$repo/description $reposDirectory/$repo/description.bk
        echo $newDesc >> $reposDirectory/$repo/description
        rm $reposDirectory/$repo/description.bk
        echo "$repo's description has altered!"
    elif [[ $editOption == 3 ]];
    then
        read -p "What is the repo's new author? " newAuthor
        sed -i "s/\(owner = \)\(.*\)/\1$newAuthor/g" $reposDirectory/$repo/config
        echo "$repo's author has been refashioned!"
    fi
elif [[ $option == 3 ]];
then
    i=1
    while read line; do
        echo "$i) $line"
        i=$((i+1))
    done <$reposList
    while [[ -z $repoDelete ]] || [[ $repoDelete -gt $((i-1)) ]] || [[ $repoDelete -lt 1 ]];
    do
        read -p "Choose a repo to delete: " repoDelete
    done
    echo "Performing magic wizardry..."
    repoName=$(sed "$repoDelete!d" $reposList)
    echo "Removing $repoName from $reposList"
    sed -i "/$repoName/d" $reposList
    echo "Deleting $repoName directory..."
    rm -rf $reposDirectory/$repoName
    echo "$repoName has vanished!"
else
    exit
fi