]> gitweb.fluxo.info Git - utils-git.git/commitdiff
Use repository path as identifier
authorSilvio Rhatto <rhatto@riseup.net>
Thu, 30 Nov 2017 21:08:59 +0000 (19:08 -0200)
committerSilvio Rhatto <rhatto@riseup.net>
Thu, 30 Nov 2017 21:08:59 +0000 (19:08 -0200)
git-config-save

index 9e742d17d1f6931b570aec9bf58cbc27f4ec6693..5c040e481b9dd4b776d8cf6cbf21c083ab5d2a80 100755 (executable)
 #
 # Storage format:
 #
-#   - Use ~/.config/gitconfigs/ as base
-#   - Repository identifier is determined by the first commit ID
+#   - Use ~/.config/gitconfigs/ as base.
+#   - Repository identifier is determined by it's path in the system.
+#     That's the best choice if you have the same repository checked out
+#     multiple times and with different configurations.
+#   - Repository identifier can also be determined by the first commit ID
 #     (not repo URL or any other volatile information). So this
 #     script may fail if you're doing improbable stuff like rebasing
 #     your repo and removing the initial commit.
 #
 # How it saves an item:
 #
-#   - Find all git configurations
-#   - Make a backup at $BASE/$ID/config.$DATE if config differs
-#   - Save the config at $BASE/ID
+#   - Find all git configurations.
+#   - Make a backup at $BASE/$ID/config.$DATE if config differs.
+#   - Save the config at $BASE/ID.
 #
 # How it restore an item:
 #
-#   - Copy each config from $BASE/ID, if available and if it differs
-#   - Restore always save a timestamped copy at .git/config.$DATE
+#   - Copy each config from $BASE/ID, if available and if it differs.
+#   - Restore always save a timestamped copy at .git/config.$DATE.
 
 # Parameters
 BASENAME="`basename $0`"
 BASE="$HOME/.config/gitconfigs"
 DATE="`date +%Y%m%d%I%M%S`"
+FILENAME="gitconfig.saved"
 
 # Ensure we have a base and that is minimally safe
 mkdir -p  $BASE
@@ -62,22 +66,23 @@ function git_config_save {
 
   # Repository ID
   # https://stackoverflow.com/questions/34874343/how-can-i-uniquely-identify-a-git-repository
-  ID="`git rev-list --parents HEAD | tail -1`"
+  #ID="`git rev-list --parents HEAD | tail -1`"
+  ID="$PWD"
 
   # Display ID
-  echo $ID
+  #echo $ID
 
   # Create config folder
   mkdir -p $BASE/$ID
 
   # Make a backup
-  if [ -f "$BASE/$ID/config" ] && ! diff .git/config $BASE/$ID/config &> /dev/null; then
+  if [ -f "$BASE/$ID/$FILENAME" ] && ! diff .git/config $BASE/$ID/$FILENAME &> /dev/null; then
     echo "Differences detected at `pwd`, making a backup..."
-    cp $BASE/$ID/config $BASE/$ID/config.$DATE
+    cp $BASE/$ID/$FILENAME $BASE/$ID/$FILENAME.$DATE
   fi
 
   # Save
-  cp .git/config $BASE/$ID/config
+  cp .git/config $BASE/$ID/$FILENAME
 }
 
 # Restore config tor a repository
@@ -90,22 +95,23 @@ function git_config_restore {
 
   # Repository ID
   # https://stackoverflow.com/questions/34874343/how-can-i-uniquely-identify-a-git-repository
-  ID="`git rev-list --parents HEAD | tail -1`"
+  #ID="`git rev-list --parents HEAD | tail -1`"
+  ID="$PWD"
 
   # Display ID
-  echo $ID
+  #echo $ID
 
   # Create config folder
   mkdir -p $BASE/$ID
 
   # Check if we have a saved config
-  if [ ! -f "$BASE/$ID/config" ]; then
+  if [ ! -f "$BASE/$ID/$FILENAME" ]; then
     echo "No config for `pwd`, skipping"
     return
   fi
 
   # Make a backup
-  if ! diff .git/config $BASE/$ID/config &> /dev/null; then
+  if ! diff .git/config $BASE/$ID/$FILENAME &> /dev/null; then
     cp .git/config .git/config.$DATE
   else
     echo "Identical configs for `pwd`, skipping"
@@ -113,7 +119,7 @@ function git_config_restore {
   fi
 
   # Restore
-  cp $BASE/$ID/config .git/config
+  cp $BASE/$ID/$FILENAME .git/config
 
   # Tell the user about the backup
   echo "Backup saved at $pwd/.git/config.$DATE"
@@ -124,7 +130,8 @@ find -type d -name .git | while read repo; do
   # Get absolute folder
   PWD="`cd $repo/.. &> /dev/null && pwd`"
 
-  echo -n -e "Processing $PWD...\t"
+  #echo -n -e "Processing $PWD...\t"
+  echo "Processing $PWD..."
 
   (
   cd $PWD