-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-guclone.main
88 lines (74 loc) · 2.54 KB
/
git-guclone.main
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
# -*- mode: shell-script -*-
# vim:ft=sh
ARGUMENTS=( "$@" )
INPUT2="${ARGUMENTS[${#ARGUMENTS[@]}-1]}"
if [[ ${#ARGUMENTS[@]} -ge 2 ]]; then
INPUT1="${ARGUMENTS[${#ARGUMENTS[@]}-2]}"
if [[ "$INPUT1" != //* && "$INPUT1" != gitu://* ]]; then
INPUT1="$INPUT2"
DIRECTORY=""
unset 'ARGUMENTS[${#ARGUMENTS[@]}-1]'
else
DIRECTORY="$INPUT2"
unset 'ARGUMENTS[${#ARGUMENTS[@]}-1]'
unset 'ARGUMENTS[${#ARGUMENTS[@]}-1]'
fi
else
INPUT1="$INPUT2"
unset 'ARGUMENTS[${#ARGUMENTS[@]}-1]'
fi
INPUT1="${INPUT1#gitu://}"
INPUT1="${INPUT1#//}"
REGEX='^[0-9a-zA-ZµºÀ-ÖØ-áãäæ-öøú-ÿĀ-ƔƖ-ơƤ-ǃǍ-DZdz-ǵǷ-Ɉɋ-ɏḀ-ẝẟ-ỹỻͻ-ͽΆΈΑ-ΡΣ-ΩΫ-ψϋ-ώϐ-ϒϔ-Ϡϥϧ-ϩϫ-ϲϵϷ-Ϲϻ-ϿЀ-џѡ-ѣѥѫѱ-ѳѵѷѻѽѿ-ҁҋ-ҟҡ-ҥҩ-ҳҵ-һӀ-ӌӎӐ-ӿא-דטך-מס-פ]+$'
# Input given?
if [[ -z "$INPUT1" ]]; then
echo "Please provide gitu://... URL (see git guclone -h for help)."
exit 1
fi
# Proper input?
if [[ ! "$INPUT1" =~ $REGEX ]]; then
echo "Please provide gitu://... (or //... or just ... part) URL as last argument,"
echo "it should consist only from unicode letters. If last argument is the target"
echo "directory, then prefix gitu:// or // is required. See git guclone -h for help."
exit 1
fi
# Select backend
declare TOOL
choose_tool
TOOL="$REPLY"
DATA=`"$TOOL" -qnd "$INPUT1" 2>/dev/null`
# Read repository fetch URL
declare URL="${DATA%% / *}"
# Read current revision
declare REF="${DATA##* / rev:}"
if [[ "$REF" != "$DATA" ]]; then
REF="${REF%% / *}"
else
REF=""
fi
# Read encoded path
declare MYPATH="${DATA##* / file:}"
[[ "$MYPATH" = "$DATA" ]] && MYPATH=""
echo -en "${YELLOW}Cloning URL ${MAGENTA}${URL}"
[[ -n "$REF" ]] && echo -en "${YELLOW} for revision ${GREEN}${REF}"
echo -e ${RESET}
[[ -n "$MYPATH" ]] && echo -e "${YELLOW}Pointed file(s): ${GREEN}${MYPATH}${RESET}"
echo ""
if ! git clone "${ARGUMENTS[@]}" "${URL}" ${DIRECTORY:+"$DIRECTORY"}; then
echo -e "${YELLOW}Clone failed, error message is above, not proceeding further."
exit 1
fi
# Establish target directory for message and checkout
if [[ -z "$DIRECTORY" ]]; then
DIRECTORY="${URL##*/}"
DIRECTORY="${DIRECTORY%.git}"
fi
# Perform checkout if needed
if [[ -n "$REF" ]]; then
echo ""
echo -en "${YELLOW}Checking out revision/reference ${GREEN}${REF}${YELLOW}...${RESET}"
cd "$DIRECTORY"
git checkout -q --progress "$REF" && echo " OK" || echo " Failed"
fi
get_ref
echo -e "HEAD is at: ${RED}${REPLY}${RESET}, created directory ${GREEN}${DIRECTORY}${RESET}"