Skip to content

Instantly share code, notes, and snippets.

@ia
Created September 18, 2017 12:32
Show Gist options
  • Save ia/33505366420a3ffdcbd77c9496fd2a73 to your computer and use it in GitHub Desktop.
Save ia/33505366420a3ffdcbd77c9496fd2a73 to your computer and use it in GitHub Desktop.
LEGACY: simple script to track config files via git
#!/bin/sh
#*
#* config-keeper - moves configs in dir and makes git repo in it
#* Copyright (C) 2009 ia
#*
#* config-keeper is free software.
#*
#* You may redistribute it and/or modify it under the terms of the
#* GNU General Public License, as published by the Free Software
#* Foundation; either version 3 of the License, or (at your option)
#* any later version.
#*
#* toolchain is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#* See the GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with main.c. If not, write to:
#* The Free Software Foundation, Inc.,
#* 51 Franklin Street, Fifth Floor
#* Boston, MA 02110-1301, USA.
#*
# usage
# ------
# config-keeper command by default:
# 1. creates their own config file ~/.config-keeper.list
# 2. creates ~/.config-keeper dir
# 3. move in dir config files from list (dirs also supported)
# 3. creates symlinks like ".config - ~/.config-keeper/config"
# 4. creates from ~/.config-keeper dir git repo for tracking changes
# If you running config-keeper when dir exists, then it ask:
# 1. to revert configs to their places (move config to their origin places and removes whole ~/.config-keeper dir)
# 2. to rescan config-keeper.list and to add new config files from list in config-keeper dir and starts to track them.
# WARNING:
# Supposing, that names of tracking config files starts with "."; in other case consequences may be unpredictable.
# ----
revert()
{
for i in `seq 1 $lines`; do
current_file="`sed -n ''"$i"'p' "$keep_file" | grep -v "^#"`"
if [ -n "$current_file" ]; then
if [ ! -e "$current_file" ]; then
echo "file "$current_file" doesn't exist!";
sed -i 's,'"$current_file"',\#'"$current_file"',' "$keep_file";
fi;
rm "$current_file"
current_keep_file="`echo "$current_file" | awk -F "/" '{print $NF}' | sed -e 's/^.//'`"
mv "$keep_dir"/"$current_keep_file" "$current_file"
fi;
done
rm -rf "$keep_dir"
}
keep()
{
mkdir "$keep_dir"
for i in `seq 1 $lines`; do
current_file="`sed -n ''"$i"'p' "$keep_file" | grep -v "^#"`"
if [ -n "$current_file" ]; then
if [ ! -e "$current_file" ]; then
echo "file "$current_file" doesn't exist!";
sed -i 's,'"$current_file"',\#'"$current_file"',' "$keep_file";
fi;
current_keep_file="`echo "$current_file" | awk -F "/" '{print $NF}' | sed -e 's/^.//'`"
mv "$current_file" "$keep_dir"/"$current_keep_file"
ln -s "$keep_dir"/"$current_keep_file" "$current_file"
fi;
done
cd "$keep_dir"
git init
git add .
git commit -a -m "Initial commit for local config files"
}
re_add()
{
for i in `seq 1 $lines`; do
current_file="`sed -n ''"$i"'p' "$keep_file" | grep -v "^#"`"
if [ -n "$current_file" ]; then
if [ ! -e "$current_file" ]; then
echo "file "$current_file" doesn't exist!";
sed -i 's,'"$current_file"',\#'"$current_file"',' "$keep_file";
fi;
if [ ! -L "$current_file" ]; then
current_keep_file="`echo "$current_file" | awk -F "/" '{print $NF}' | sed -e 's/^.//'`"
mv "$current_file" "$keep_dir"/"$current_keep_file"
ln -s "$keep_dir"/"$current_keep_file" "$current_file"
fi;
fi;
done
cd "$keep_dir"
git ls-files -o | git update-index --add --stdin
git commit -a -m "Additional commit for local config files"
}
###
keep_file="/home/"$USER"/.config-keeper.list";
keep_dir="/home/"$USER"/.config-keeper";
###
if [ ! -e "$keep_file" ]; then
#creates sample config file for config-keeper
touch "$keep_file";
echo "# generated by config-keeper script" > "$keep_file";
echo "# comments allowed via #" >> "$keep_file";
echo "# example:" >> "$keep_file";
echo "# /home/"$USER"/.gitconfig" >> "$keep_file";
echo "# working example:" >> "$keep_file";
echo "$keep_file" >> "$keep_file";
fi;
lines="`wc -l "$keep_file" | awk '{print $1}'`";
if [ -d "$keep_dir" ]; then
echo "Looks like you've run this script before - just check .config-keeper directory";
while [ "$reverting" != "y" ] && [ "$reverting" != "n" ]; do
read -n1 -p "Revert all configs to their places? (y/n): " reverting; printf "\n";
done;
if [ "$reverting" == "y" ]; then
echo "Reverting...";
revert;
fi;
while [ "$readding" != "y" ] && [ "$readding" != "n" ]; do
read -n1 -p "Re-add configs from "$keep_file" file to "$keep_dir"? (y/n): " readding; printf "\n";
done;
if [ "$readding" == "y" ]; then
echo "Readding...";
re_add;
fi;
else
keep;
fi;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment