aboutsummaryrefslogtreecommitdiff
path: root/nix/per-host/git/git-shell-commands/manage
blob: 01fd52ef86e33d84314d91ec8291f0a1e7a32dd7 (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
#!/usr/bin/env bash
# git-shell-commands(5) subcommand: interactive repo manager.
# Run: ssh -t git@git.kumardamani.net manage
# Also symlinked as no-interactive-login, so a bare interactive ssh
# (ssh -t git@git.kumardamani.net) lands here directly.

cmds=/srv/git/git-shell-commands

valid_name() {
  case "$1" in
    ""|*[!a-zA-Z0-9._-]*|*..*) return 1 ;;
    *) return 0 ;;
  esac
}

list_repos() {
  printf '%-24s %-8s %s\n' REPO STATE DESCRIPTION
  for repo in /srv/git/*.git; do
    [ -d "$repo" ] || continue
    name="${repo##*/}"
    name="${name%.git}"
    if [ -f "$repo/git-daemon-export-ok" ]; then
      state=public
    else
      state=private
    fi
    desc="$(cat "$repo/description" 2>/dev/null)"
    case "$desc" in Unnamed*) desc= ;; esac
    printf '%-24s %-8s %s\n' "$name" "$state" "$desc"
  done
}

ask_name() {
  printf 'repo name: '
  read -r name
  if ! valid_name "$name"; then
    echo "invalid name '$name' (allowed: a-z 0-9 . _ -)" >&2
    return 1
  fi
}

while true; do
  echo
  echo "== git repo manager =="
  echo "  l) list repos"
  echo "  c) create repo"
  echo "  p) publish repo"
  echo "  h) hide repo"
  echo "  d) set description"
  echo "  x) delete repo"
  echo "  q) quit"
  printf '> '
  read -r choice || break
  case "$choice" in
    l) list_repos ;;
      c) "$cmds/create" ;; # create prompts for name + settings itself
    p) ask_name && "$cmds/publish" "$name" ;;
    h) ask_name && "$cmds/hide" "$name" ;;
    d)
      ask_name && {
        printf 'description: '
        read -r text
        "$cmds/desc" "$name" "$text"
      }
      ;;
    x)
      ask_name && [ -d "/srv/git/$name.git" ] && {
        printf 'type the repo name again to confirm DELETE: '
        read -r confirm
        if [ "$confirm" = "$name" ]; then
          rm -rf "/srv/git/$name.git"
          echo "deleted '$name'"
        else
          echo "aborted"
        fi
      }
      ;;
    q|quit|exit) break ;;
    *) echo "unknown choice '$choice'" ;;
  esac
done