Skip to content

Instantly share code, notes, and snippets.

@arrdem
Created February 6, 2015 05:04
Show Gist options
  • Save arrdem/d52c36bf4a94907648c4 to your computer and use it in GitHub Desktop.
Save arrdem/d52c36bf4a94907648c4 to your computer and use it in GitHub Desktop.
Git bits
#!/bin/bash
# This is a quick little git extension designed to allow me to manage
# all the git repos that I keep around better. The idea is that a
# "root" directory, $GIT_DAT/$OWNER/$REPO, and then
# symlink that directory to wherever you are now.
if ( [[ "--help" = $1 ]] ||
[[ "help" = $1 ]] ||
[[ -z $1 ]] )
then
echo "git-get"
echo "Usage: git get [help | --help | GIT_ADDR | GIT_ADDR USER/NAME]"
echo " Clones a git repo to the $GIT_DAT dir, creating a symlink"
echo " into the current directory."
exit 0
elif [ ! -z $2 ]
then
USER=$(echo $2 | awk "{match(\$1,\"([^/]+)/.*\",a)}END{print(a[1])}")
REPO=$(echo $2 | awk "{match(\$1,\".*?/(.*?)(.git)?\",a)}END{print(a[1])}")
elif [ ! -z $1 ]
then
USER=$(echo $1 | awk "{match(\$1,\".*?:([^/]+)/.*\",a)}END{print(a[1])}")
REPO=$(echo $1 | awk "{match(\$1,\".*?:[^/]+/(.*?).git\",a)}END{print(a[1])}")
fi
[ ! -z $USER ] && [ ! -z $REPO ] || exit 1
# Ensure the target dir exists
([ -d "$GIT_DAT/$USER" ] || mkdir -p "$GIT_DAT/$USER" ) &&
# Get the data
([ -d "$GIT_DAT/$USER/$REPO/.git/" ] || git clone $1 "$GIT_DAT/$USER/$REPO") &&
# Create a link to it
ln -s "$GIT_DAT/$USER/$REPO" "./$REPO" &&
exit 0
#!/usr/bin/env bash
function show_contents {
test -f "$2" && echo "$1 gitignore: $2" && cat "$2"
}
function show_global {
show_contents Global `git config --global core.excludesfile`
}
function add_global {
add_patterns `git config --global core.excludesfile` "$@"
}
function show_local {
show_contents Local .gitignore
}
function add_local {
add_patterns .gitignore "$@"
}
function add_patterns {
echo "Adding pattern(s) to: $1"
for pattern in "${@:2}"; do
echo "... adding '$pattern'"
(test -f "$1" && test "$pattern" && grep -q "$pattern" "$1") || echo "$pattern" >> "$1"
done
}
if test $# -eq 0; then
show_global
echo "---------------------------------"
show_local
else
case "$1" in
-l|--local)
test $# -gt 1 && add_local "${@:2}" && echo
show_local
;;
-g|--global)
test $# -gt 1 && add_global "${@:2}" && echo
show_global
;;
*)
add_local "$@"
;;
esac
fi
#!/bin/sh
file="$1"
test -z "$file" && echo "file required." 1>&2 && exit 1
git filter-branch -f --index-filter "git rm -r --cached '$file' --ignore-unmatch" --prune-empty --tag-name-filter cat -- --all
#!/bin/bash
if ( [[ "--help" = $1 ]] ||
[[ "help" = $1 ]] ||
[[ -z $1 ]] )
then
echo "git-rename"
echo "Usage: git rename [help | --help | {src dst}]"
echo " Moves the source to the destination, comitting."
exit 0
elif [ -e "$1" -a ! -z "$2" ]
then
git stash &&
git mv "$1" "$2" &&
git commit -m rename &&
git stash apply
fi
#!/bin/bash
# This is a quick little git extension designed to allow me to stow a
# git repo created in place to a named repo stored elsewhere in my git
# data dir.
if ( [[ "--help" = $1 ]] ||
[[ "help" = $1 ]] ||
[[ -z $1 ]] )
then
echo "git-stow"
echo "Usage: git stow [help | --help | PATH USER/NAME]"
echo " Moves a git repo to USER/NAME in GIT_DAT, creating a symlink back"
echo " into the current directory."
exit 0
elif [ ! -z $2 ]
then
USER=$(echo $2 | awk "{match(\$1,\"([^/]+)/.*\",a)}END{print(a[1])}")
REPO=$(echo $2 | awk "{match(\$1,\".*?/(.*?)(.git)?\",a)}END{print(a[1])}")
fi
[ ! -z $USER ] && [ ! -z $REPO ] || exit 1
# Ensure the target dir exists
([ -d "$1" ] && mv "$1" "$GIT_DAT/$USER/$REPO") &&
# Create a link to it
ln -s "$GIT_DAT/$USER/$REPO" "./$1" &&
exit 0
#!/bin/bash
# Git repo updater script
#
# Iterates over the $GIT_DAT/$USER/$REPO dir set, checking to see if a
# file ".ignore-updates" exists. If it does, then the project is
# skipped. Otherwise, git-fetch is used to pull down all new refs from
# all remotes of the project. Note that the actual updates are done in
# parallel for performance.
#
# This provides a quick and dirty way to update all the projects which
# I maintain local coppies of in my dat directory.
pushd .
cd $GIT_DAT
for USER in ./*; do
if [ -d $USER ]
then
pushd . &> /dev/null
echo "Updating $USER"
cd $USER
for PROJECT in ./*; do
if [ -d $PROJECT ]
then
pushd . &> /dev/null
cd $PROJECT
if [ ! -f ./.ignore-updates ]
then
echo "Updating $PROJECT"
(git fetch --all --tags &> /dev/null) &
fi
popd &> /dev/null
fi
done
popd &> /dev/null
fi
done
popd &> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment