add gitget
authorIan Kelling <ian@iankelling.org>
Fri, 9 Sep 2016 20:51:38 +0000 (13:51 -0700)
committerIan Kelling <ian@iankelling.org>
Fri, 9 Sep 2016 21:06:17 +0000 (14:06 -0700)
gitget [new file with mode: 0755]

diff --git a/gitget b/gitget
new file mode 100755 (executable)
index 0000000..a61c114
--- /dev/null
+++ b/gitget
@@ -0,0 +1,71 @@
+#!/bin/bash
+# Copyright (C) 2016 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+#     http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+gitget() {
+    local help="Usage: gitget [--help] [REPO_URL] [REPO_DIR]
+Idempotent git clone/pull
+
+If repo exists, pull.
+Without arguments, just git pull.
+REPO_DIR is, or will become the root of the repo."
+
+    if [[ $1 == --help ]]; then
+        echo "$help"
+        return
+    fi
+    if (( $# >= 3 )); then
+        echo error: expected 2 or less arguments, got $#
+        echo "$help"
+        return 1
+    fi
+    if [[ $# == 0 ]]; then
+        git pull
+        return $?
+    fi
+
+    local workingdir
+    local orig_dir="$PWD"
+    local ret
+    if [[ $1 == *[^/].git ]]; then
+        if [[ $2 ]]; then
+            workingdir="$2"
+        else
+            workingdir="${1##*/}"
+            workingdir="${workingdir%.git}"
+        fi
+    elif (( $# == 2 )); then
+        echo error: 2 arguments given but the first does not end in .git
+        echo "$help"
+        return 1
+    else
+        workingdir="$1"
+        if [[ ! -d $workingdir/.git ]]; then
+            echo "error: expected $workingdir/.git to exist"
+            return 1
+        fi
+    fi
+    [[ -d $workingdir ]] || mkdir -p "$workingdir"
+    cd "$workingdir"
+    if [[ -d .git ]]; then
+        git pull
+        ret=$?
+    else
+        git clone "$1" .
+        ret=$?
+    fi
+    cd "$orig_dir"
+    return $ret
+}
+gitget "$@"