Last active
August 29, 2015 14:15
-
-
Save emlun/dd6e8eb41b1ff2f7617a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Emil Lundberg <[email protected]> <https://keybase.io/emlun> | |
# | |
# This is free and unencumbered software released into the public domain. | |
# See http://unlicense.org/ for the full license. | |
# | |
# Usage: | |
# | |
# $ confgit ls # List git repositories in ~/.config/ | |
# $ confgit foo init # Clone 'foo.git' from remote into ~/.config/foo/ | |
# $ confgit foo log --graph --all # Use any git command in ~/.config/foo/ | |
# $ confgit - update # Fetch and fast-forward all repositories in ~/.config/ | |
CONFIG_ROOT="${HOME}/.config" | |
PROGRAM="$0" | |
PROGRAM_NAME="$(basename $PROGRAM)" | |
namespace="$1" | |
shift | |
command="$1" | |
remotename='origin' | |
remotehost='myserver' | |
remotepath="~/git/config/${namespace}.git" | |
remoteurl="${remotehost}:${remotepath}" | |
branchname="$HOSTNAME" | |
confdir="${CONFIG_ROOT}/${namespace}" | |
git="git -C ${confdir}" | |
if [[ "$namespace" == '-' ]]; then | |
case $command in | |
ls) | |
for d in $(find "${CONFIG_ROOT}"/* -maxdepth 0 -type d); do | |
d=$(basename $d) | |
if git -C "${CONFIG_ROOT}/${d}" rev-parse &>/dev/null; then | |
echo "$d" | |
fi | |
done | |
exit 0 | |
;; | |
push|update) | |
for d in $($PROGRAM - ls); do | |
echo | |
echo "$command $d" | |
$PROGRAM "$d" $command | |
done | |
exit 0 | |
;; | |
*) | |
echo 1>&2 "Cannot perform operation '$command' on all repositories" | |
exit 1 | |
;; | |
esac | |
else | |
case $command in | |
init) | |
git init "$confdir" | |
$git checkout -b $branchname | |
$git branch --set-upstream-to="${remotename}/${branchname}" | |
trap 'exit $?' ERR | |
if $git remote | grep $remotename; then | |
$git remote rm $remotename; | |
fi | |
if $git remote add $remotename "$remoteurl"; then | |
ssh "${remotehost}" "git init --bare ${remotepath}" | |
$git fetch --all | |
fi | |
exit 0 | |
;; | |
update) | |
trap 'exit $?' ERR | |
$git fetch --all | |
$git merge --ff-only "${remotename}/master" | |
;; | |
*) | |
$git "$@" | |
exit $? | |
;; | |
esac | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment