]> gitweb.fluxo.info Git - keyringer.git/commitdiff
Checking if a folder is inside a git repository
authorSilvio Rhatto <rhatto@riseup.net>
Tue, 5 Jan 2010 15:30:01 +0000 (13:30 -0200)
committerSilvio Rhatto <rhatto@riseup.net>
Tue, 5 Jan 2010 15:30:01 +0000 (13:30 -0200)
keyringer
lib/keyringer/functions

index ed95a09f9274519514af13276e73ce5982f9cbba..b92c066d74cea8d20bf81c8fbf94cb3ccf5f6d79 100755 (executable)
--- a/keyringer
+++ b/keyringer
@@ -21,6 +21,7 @@
 function keyringer_init {
   BASEDIR="$3"
   URL="$4"
+  RECIPIENTS="$BASEDIR/config/recipients"
 
   # Parse
   if [ -z "$BASEDIR" ]; then
@@ -39,10 +40,19 @@ function keyringer_init {
       exit 1
     fi
   else
-    mkdir -p $BASEDIR/{config,keys}
-    echo "# Use entries in the form of 'john@doe.com XXXXXXXX" > $BASEDIR/config/recipients
-    echo "" >> $BASEDIR/config/recipients
-    chmod 600 $BASEDIR/config/recipients
+    if [ -e "$BASEDIR" ]; then
+      if [ ! -d "$BASEDIR/keys" ] || [ ! -e "$RECIPIENTS" ]; then
+        echo "Invalid keyring $BASEDIR: incomplete installation"
+        exit 1
+      fi
+    else
+      mkdir -p $BASEDIR/{config,keys}
+      echo "# Use entries in the form of 'john@doe.com XXXXXXXX" > $RECIPIENTS
+      echo "" >> $RECIPIENTS
+    fi
+
+    # Secure
+    chmod 600 $RECIPIENTS
   fi
 
   # Reparse basedir to force absolute folder
@@ -53,7 +63,7 @@ function keyringer_init {
   echo "$KEYRING=\"$BASEDIR\"" >> $CONFIG
 
   # Init
-  if [ ! -d "BASEDIR/.git" ]; then
+  if ! keyringer_is_git $BASEDIR; then
     keyringer_exec git $BASEDIR init
     keyringer_exec git $BASEDIR add .
     keyringer_exec git $BASEDIR commit -m Importing
index f0c4e0fdb9742810e66ff8c13303fcf1ecbec874..9c03000993398245b8f4ada786c3e97d5ecad7ba 100644 (file)
@@ -3,6 +3,7 @@
 # Common functions.
 #
 
+# Load a parameter from config
 function keyringer_config {
   if [ -z "$CONFIG" ]; then
     echo "Your have to set CONFIG variable in the code"
@@ -15,10 +16,12 @@ function keyringer_config {
   fi
 }
 
+# Return the list of recipients
 function keyringer_recipients {
   grep -v '^#' $1 | grep -v '^$' | awk '{ print "-r " $2 }' | xargs
 }
 
+# Check if keyringer has a given action
 function keyringer_has_action {
  if [ -z "$ACTIONS" ]; then
    echo "Your have to set ACTIONS variable in the code"
@@ -32,6 +35,7 @@ function keyringer_has_action {
  fi
 }
 
+# Execute an action
 function keyringer_exec {
   # Setup
   action="$1"
@@ -44,6 +48,27 @@ function keyringer_exec {
   fi
 }
 
+# Return a filename with correct extension
 function keyringer_filename {
   echo `dirname $1`/`basename $1 .gpg`.gpg
 }
+
+# Check if a folder is inside a git repository
+function keyringer_is_git {
+  if [ -z "$1" ]; then
+    false
+  elif [ ! -d "$1" ]; then
+    false
+  elif [ -d "$1/.git" ]; then
+    true
+  else
+    cwd="`pwd`"
+    cd $1 && git="`git status &> /dev/null`" && cd $cwd
+
+    if [ "$git" != "128" ]; then
+      true
+    else
+      false
+    fi
+  fi
+}