-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-get
executable file
·98 lines (82 loc) · 2.37 KB
/
git-get
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
#!/bin/bash
#
# Clone a github or other remote git repository to a predefined local location
# Similar to "go get"
#
# source config file
if [[ -f ~/.gitgetrc ]]; then
# possible values:
# projects=<directory where to clone to, current if unspecified>
# org=<default github user or organization, $USER if unspecified>
. ~/.gitgetrc
fi
# compute $PROJECTS; default to $HOME/projects
projects=${projects:-"$HOME/projects"}
PROJECTS=${PROJECTS:-"$projects"}
# compute $ORG; default to current user's username
org=${org:-"$USER"}
ORG=${ORG:-"$org"}
usage()
{
cat 1>&2 <<-EOF
git-get: git clone a repository into \$PROJECTS (default: $PROJECTS).
Usage:
git get [-h|--help] <repository_url>
Examples:
git get [email protected]:mjuric/lsd
git get https://github.com/mjuric/lsd
git get mjuric/lsd
git get lsd
Author: Mario Juric <[email protected]>
License: MIT (https://opensource.org/licenses/MIT)
EOF
}
# parse cmdline options
if [[ $1 == "--help" || $1 == "-h" || -z $1 || $1 == -* ]]; then
usage
exit -1;
fi
# Supported URL types:
# [email protected]:base/dir/reponame
# https://host.name/base/dir/reponame
# org/reponame (defaults to looking for it at [email protected])
# reponame (if $PWD is in $PROJECTS/github.com/SOME_ORG/)
URL="$1"
if [[ $URL == "https://"* ]]; then
HOST=$(cut -d / -f 3 <<< "$URL")
DIR=$(cut -d / -f 4- <<< "$URL")
elif [[ $URL == *"@"* ]]; then
IFS=':' read -ra A <<< "$URL"
FRONT="${A[0]}"
DIR="${A[1]}"
[[ -z $DIR ]] && { echo "error: malformed URL -- $URL is missing a colon."; exit -1; }
HOST=$(sed -E 's#^[^@]+@(.*)$#\1#' <<< "$FRONT")
else
IFS='/' read -ra A <<< "$URL"
if [[ ${#A[@]} == 1 ]]; then
# see if $PWD is in a subdirectory of $PROJECTS/org/
if [[ $PWD = $PROJECTS/github.com/* ]]; then
SUFFIX=${PWD#"$PROJECTS/"}
IFS='/' read -ra A <<< "$SUFFIX"
DEST="${A[1]}"
else
DEST="$ORG"
fi
DIR="$DEST/$URL"
else
[[ ${#A[@]} == 2 ]] || { echo "error: malformed URL -- expected organization/repo_name." 1>&2; exit -1; }
DIR="$URL"
fi
HOST="github.com"
URL="[email protected]:$DIR"
fi
# Construct destination directory, removing any .git extensions
DIR="$PROJECTS/$HOST/$DIR"
DIR="${DIR%.git}"
if [[ ! -e "$DIR" ]]; then
# create the destination directory and clone
mkdir -p "$(dirname DIR)"
git clone "$URL" "$DIR" && echo "cloned to $DIR"
else
echo "already exists in $DIR"
fi