add note about license
[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 [[ $2 ]]; then
42 workingdir="$2"
43 else
44 workingdir="${1##*/}"
45 workingdir="${workingdir%.git}"
46 fi
47 [[ -d $workingdir ]] || mkdir -p "$workingdir"
48 cd "$workingdir"
49 if [[ -d .git ]]; then
50 git pull
51 ret=$?
52 else
53 git clone "$1" .
54 ret=$?
55 fi
56 cd "$orig_dir"
57 return $ret
58 }
59 gitget "$@"