aboutsummaryrefslogtreecommitdiff
path: root/git-shell-commands/new-project
blob: 4e2cb15952e95595fca6704fd8fd8a99d388892f (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
#!/bin/bash
set -euo pipefail
# this script allows the creation of projects over ssh on git server
# public repos will get a public.conf containing the remote
# private repos will just exist on the server
#
# USAGE:
# ssh git@<host> new-project <name> [--public] [--description <desc>]
#
# EXAMPLES:
# ssh git@treybastian.com new-project myrepo
# ssh git@treybastian.com new-project myrepo --public
# ssh git@treybastian.com new-project myrepo --public --description "my cool repo"

REMOTE_BASE_URL="git@treybastian.com:repos"

usage() {
  echo "Usage: new-project <name> [--public] [--description <desc>]"
  echo ""
  echo "Examples:"
  echo "  new-project myrepo"
  echo "  new-project myrepo --public"
  echo "  new-project myrepo --public --description \"my cool repo\""
  exit 1
}

if [ "$#" -lt 1 ]; then
  echo "Error: expected at least 1 argument, got $#"
  usage
fi

PROJECT_NAME="$1"
PUBLIC=false
DESCRIPTION=""
shift

while [ "$#" -gt 0 ]; do
  case "$1" in
    --public)
      PUBLIC=true
      shift
      ;;
    --description)
      [ "$#" -ge 2 ] || { echo "Error: --description requires a value"; usage; }
      DESCRIPTION="$2"
      shift 2
      ;;
    *)
      echo "Error: unknown flag '$1'"
      usage
      ;;
  esac
done

if [[ "$PROJECT_NAME" != *.git ]]; then
  PROJECT_NAME="${PROJECT_NAME}.git"
fi

if [ -d "$PROJECT_NAME" ]; then
  echo "Error: '$PROJECT_NAME' already exists"
  exit 1
fi

git --bare init "${PROJECT_NAME}"

if [ -n "$DESCRIPTION" ]; then
  echo "$DESCRIPTION" > "${PROJECT_NAME}/description"
fi

if [ "$PUBLIC" = true ]; then
  echo "${REMOTE_BASE_URL}/${PROJECT_NAME}" > "${PROJECT_NAME}/public.conf"
  REMOTE_ARGS=("repos/${PROJECT_NAME}")
  if [ -n "$DESCRIPTION" ]; then
    REMOTE_ARGS+=(--description "$(printf '%q' "$DESCRIPTION")")
  fi
  ssh git@treybastian.com new-repo "${REMOTE_ARGS[@]}"
  echo "public repo: ${REMOTE_BASE_URL}/${PROJECT_NAME}"
fi

echo "git url: ${USER}@${HOSTNAME}:${PROJECT_NAME}"

# vim: filetype=bash