fix tar priority
[small-misc-bash] / gitget
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 gitget() {
17 local help="Usage: gitget [--help] [REPO_URL] [REPO_DIR]
18 Idempotent git clone/pull
19
20 If repo exists, pull.
21 Without arguments, just git pull.
22 REPO_DIR is, or will become the root of the repo."
23
24 if [[ $1 == --help ]]; then
25 echo "$help"
26 return
27 fi
28 if (( $# >= 3 )); then
29 echo error: expected 2 or less arguments, got $#
30 echo "$help"
31 return 1
32 fi
33 if [[ $# == 0 ]]; then
34 git pull
35 return $?
36 fi
37
38 local workingdir
39 local orig_dir="$PWD"
40 local ret
41 if [[ $1 == *[^/].git ]]; then
42 if [[ $2 ]]; then
43 workingdir="$2"
44 else
45 workingdir="${1##*/}"
46 workingdir="${workingdir%.git}"
47 fi
48 elif (( $# == 2 )); then
49 echo error: 2 arguments given but the first does not end in .git
50 echo "$help"
51 return 1
52 else
53 workingdir="$1"
54 if [[ ! -d $workingdir/.git ]]; then
55 echo "error: expected $workingdir/.git to exist"
56 return 1
57 fi
58 fi
59 [[ -d $workingdir ]] || mkdir -p "$workingdir"
60 cd "$workingdir"
61 if [[ -d .git ]]; then
62 git pull
63 ret=$?
64 else
65 git clone "$1" .
66 ret=$?
67 fi
68 cd "$orig_dir"
69 return $ret
70 }
71 gitget "$@"