~ubuntu-branches/ubuntu/utopic/gitolite3/utopic

« back to all changes in this revision

Viewing changes to src/commands/D

  • Committer: Package Import Robot
  • Author(s): David Bremner
  • Date: 2013-05-18 17:59:21 UTC
  • Revision ID: package-import@ubuntu.com-20130518175921-ac4xe6vd0jtxvjot
Tags: upstream-3.5.1+4
ImportĀ upstreamĀ versionĀ 3.5.1+4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
# ----------------------------------------------------------------------
 
4
# ADMINISTRATOR NOTES:
 
5
# ----------------------------------------------------------------------
 
6
 
 
7
# - set TRASH_CAN in the rc if you don't like the default.  It should be
 
8
#   relative to GL_REPO_BASE or an absolute value.  It should also be on the
 
9
#   same filesystem as GL_REPO_BASE, otherwise the 'mv' will take too long.
 
10
 
 
11
# - you could set TRASH_SUFFIX also but I recomend you leave it as it is
 
12
 
 
13
# - run a cron job to delete old repos based on age (the TRASH_SUFFIX has a
 
14
#   timestamp); your choice how/how often you do that
 
15
 
 
16
# - you can completely disable the 'rm' command by setting an rc variable
 
17
#   called D_DISABLE_RM to "1".
 
18
# ----------------------------------------------------------------------
 
19
 
 
20
# ----------------------------------------------------------------------
 
21
# Usage:    ssh git@host D <subcommand> <argument>
 
22
#
 
23
# The whimsically named "D" command deletes repos ("D" is a counterpart to the
 
24
# "C" permission which lets you create repos.  Which also means that, just
 
25
# like "C", it only works for wild repos).
 
26
#
 
27
# There are two kinds of deletions: 'rm' removes a repo completely, while
 
28
# 'trash' moves it to a trashcan which can be recovered later (upto a time
 
29
# limit that your admin will tell you).
 
30
#
 
31
# The 'rm', 'lock', and 'unlock' subcommands:
 
32
#     Initially, all repos are "locked" against 'rm'.  The correct sequence is
 
33
#         ssh git@host D unlock repo
 
34
#         ssh git@host D rm repo
 
35
#     Since the initial condition is always locked, the "lock" command is
 
36
#     rarely used but it is there if you want it.
 
37
#
 
38
# The 'trash', 'list-trash', and 'restore' subcommands:
 
39
#     You can 'trash' a repo, which moves it to a special place:
 
40
#         ssh git@host D trash repo
 
41
#     You can then 'list-trash'
 
42
#         ssh git@host D list-trash
 
43
#     which prints something like
 
44
#         repo/2012-04-11_05:58:51
 
45
#     allowing you to restore by saying
 
46
#         ssh git@host D restore repo/2012-04-11_05:58:51
 
47
 
 
48
die() { echo "$@" >&2; exit 1; }
 
49
usage() { perl -lne 'print substr($_, 2) if /^# Usage/../^$/' < $0; exit 1; }
 
50
[ -z "$1" ] && usage
 
51
[ "$1" = "-h" ] && usage
 
52
[ "$1" != "list-trash" ] && [ -z "$2" ] && usage
 
53
[ -z "$GL_USER" ] && die GL_USER not set
 
54
 
 
55
# ----------------------------------------------------------------------
 
56
cmd=$1
 
57
repo=$2
 
58
# ----------------------------------------------------------------------
 
59
RB=`gitolite query-rc GL_REPO_BASE`;            cd $RB
 
60
TRASH_CAN=`gitolite query-rc TRASH_CAN`;        tcan=Trash;                     TRASH_CAN=${TRASH_CAN:-$tcan}
 
61
TRASH_SUFFIX=`gitolite query-rc TRASH_SUFFIX`;  tsuf=`date +%Y-%m-%d_%H:%M:%S`; TRASH_SUFFIX=${TRASH_SUFFIX:-$tsuf}
 
62
# ----------------------------------------------------------------------
 
63
 
 
64
owner_or_die() {
 
65
    gitolite creator "$repo" $GL_USER || die You are not authorised
 
66
}
 
67
 
 
68
# ----------------------------------------------------------------------
 
69
 
 
70
if [ "$cmd" = "rm" ]
 
71
then
 
72
 
 
73
    gitolite query-rc -q D_DISABLE_RM && die "sorry, 'unlock' and 'rm' are disabled"
 
74
 
 
75
    owner_or_die
 
76
    [ -f $repo.git/gl-rm-ok ] || die "'$repo' is locked!"
 
77
    rm -rf $repo.git
 
78
    echo "'$repo' is now gone!"
 
79
 
 
80
elif [ "$cmd" = "lock" ]
 
81
then
 
82
 
 
83
    owner_or_die
 
84
    rm -f $repo.git/gl-rm-ok
 
85
    echo "'$repo' is now locked"
 
86
 
 
87
elif [ "$cmd" = "unlock" ]
 
88
then
 
89
 
 
90
    gitolite query-rc -q D_DISABLE_RM && die "sorry, 'unlock' and 'rm' are disabled"
 
91
 
 
92
    owner_or_die
 
93
    touch $repo.git/gl-rm-ok
 
94
    echo "'$repo' is now unlocked"
 
95
 
 
96
elif [ "$cmd" = "trash" ]
 
97
then
 
98
 
 
99
    owner_or_die
 
100
    mkdir -p $TRASH_CAN/$repo 2>/dev/null || die "failed creating directory in trashcan"
 
101
    [ -d $TRASH_CAN/$repo/$TRASH_SUFFIX ] && die "try again in a few seconds..."
 
102
    mv $repo.git $TRASH_CAN/$repo/$TRASH_SUFFIX
 
103
    echo "'$repo' moved to trashcan"
 
104
 
 
105
elif [ "$cmd" = "list-trash" ]
 
106
then
 
107
 
 
108
    cd $TRASH_CAN 2>/dev/null || exit 0
 
109
    find . -name gl-creator | sort | while read t
 
110
    do
 
111
        owner=
 
112
        owner=`cat "$t"`
 
113
        [ "$owner" = "$GL_USER" ] && dirname $t
 
114
    done | cut -c3-
 
115
 
 
116
elif [ "$cmd" = "restore" ]
 
117
then
 
118
 
 
119
    owner=
 
120
    owner=`cat $TRASH_CAN/$repo/gl-creator 2>/dev/null`
 
121
    [ "$owner" = "$GL_USER" ] || die "'$repo' is not yours!"
 
122
 
 
123
    cd $TRASH_CAN
 
124
    realrepo=`dirname $repo`
 
125
    [ -d $RB/$realrepo.git ] && die "'$realrepo' already exists"
 
126
    mv $repo $RB/$realrepo.git
 
127
    echo "'$repo' restored to '$realrepo'"
 
128
 
 
129
else
 
130
    die "unknown subcommand '$cmd'"
 
131
fi