#!/bin/bash #------------------------------------------------------------------------------# # vi: set sw=4 ts=4 ai: ("set modeline" in ~/.exrc) # #------------------------------------------------------------------------------# # Program : vigit # # # # Author : Ton Kersten Ton.Kersten@ATComputing.nl # # AT Computing Toernooiveld 104 # # 6525 EC Nijmegen The Netherlands # # Fax: +31-24 3527292 Tel: +31-24 3527282 # # # # Date : 23-01-2009 Time : 12:18 # # # # Description : Program to edit files and commit them to git # # # # Parameters : The files # # # # Pre reqs : Git should be installed # # # # Remarks : Stolen from and based on an idea of Miek Gieben # # # # Exit codes : 0 -> OK # # <> 0 -> !OK # # # # Updates : None (yet) # #------------------------------------------------------------------------------# # (c) Copyright 2009 by AT Computing, The Netherlands # #------------------------------------------------------------------------------# #------------------------------------------------------------------------------# # V e r s i o n i n f o r m a t i o n # #------------------------------------------------------------------------------# # $Id:: vigit 3 2009-01-26 09:36:01Z tonk $: # # $Revision:: 3 $: # # $Author:: Ton Kersten $: # # $Date:: 2009-01-26 10:36:13 +0100 (Mon, 26 Jan 2009) $: # #------------------------------------------------------------------------------# # E n d o f v e r s i o n i n f o r m a t i o n # #------------------------------------------------------------------------------# #------------------------------------------------------------------------------# # Determine the program name and the 'running directory' # #------------------------------------------------------------------------------# IAM="${0##*/}" CRD="$( [[ "$(printf "${0}" | cut -c 1 )" = "." ]] && { printf "${PWD}/${0}" } || { printf "${0}" })" CRD="${CRD%/*}" CUR="${PWD}" #------------------------------------------------------------------------------# # Save the shell settings # #------------------------------------------------------------------------------# SETA=0; [[ ${-} = *a* ]] && SETA=1 SETE=0; [[ ${-} = *e* ]] && SETE=1 SETU=0; [[ ${-} = *u* ]] && SETU=1 SETX=0; [[ ${-} = *x* ]] && SETX=1 #------------------------------------------------------------------------------# # Set and unset the needed shell settings # #------------------------------------------------------------------------------# set +o noclobber # Overwrite existing files, if needed # set -o nounset # Don't allow uninitialized variables # set +o errexit # No returncode checking # #------------------------------------------------------------------------------# # No git, no glory # #------------------------------------------------------------------------------# [[ x"$(which git 2>/dev/null)" = x"" ]] && { echo "No 'git' found. Please use plain vi(m)" exit 1 } spc=" " #------------------------------------------------------------------------------# # Who are we # #------------------------------------------------------------------------------# who=${SUDO_USER:-${LOGNAME}} #------------------------------------------------------------------------------# # Try to get the user and email info from git # #------------------------------------------------------------------------------# if [[ -f ${HOME}/.gitconfig ]] then full=$( git-config -f ${HOME}/.gitconfig --get user.name) email=$(git-config -f ${HOME}/.gitconfig --get user.email) fi #------------------------------------------------------------------------------# # If no info was found, make up your own # #------------------------------------------------------------------------------# if [[ x"${full:-}" = x"" ]] then full=$(getent passwd ${who} | awk -F: '{ gsub(/,*/, ""); print $5 }') email="${who}@ATComputing.nl" fi author="${full} <${email}>" #------------------------------------------------------------------------------# # Try to find the .git directory # #------------------------------------------------------------------------------# function search_git_dir { gpath="${1}" [[ -d "${gpath}/.git" ]] && echo "${gpath}" && return [[ -z "${gpath}" ]] && echo "" && return #--------------------------------------------------------------------------# # Strip that last path component and try again # #--------------------------------------------------------------------------# search_git_dir "${gpath%/*}" } #------------------------------------------------------------------------------# # Proces all files # #------------------------------------------------------------------------------# for file in "${@}" do dir="$(dirname "${file}")" cd "${dir}" base="$(basename "${file}")" if [[ x"$(search_git_dir "${PWD}")" = x"" ]] then #----------------------------------------------------------------------# # Make a new one in ${PWD} # #----------------------------------------------------------------------# git init || exit 1 fi chmod +w "${base}" 2> /dev/null if ${EDITOR:-/usr/bin/vi} "${base}" then #----------------------------------------------------------------------# # Make sure we do not check ourselfs in # #----------------------------------------------------------------------# [[ x"${base}" = x"${IAM}" ]] && { echo "Cannot commit the program itself." echo "Use 'git commit ${IAM}'" continue } #----------------------------------------------------------------------# # Did we put something usefull in it # #----------------------------------------------------------------------# [[ ! -s "${base}" ]] && exit 0 #----------------------------------------------------------------------# # Check if the file is already in git (get the current hash) # #----------------------------------------------------------------------# initial=0 hash=$(git-show -s --pretty=format:"${base} %h %ci ${who}" -- "${base}" 2>/dev/null) [[ x"${hash}" = x"" ]] && { git add "${base}" git commit --author "${author}" -m "Automatic initial checkin by '${IAM}'" "${base}" initial=1 } #----------------------------------------------------------------------# # Do we have a Hash and what type is it # #----------------------------------------------------------------------# longhash=0 havehash=$(grep -c "\$Hash[\$\:]" "${base}") [[ ${havehash} != 0 ]] && longhash=$(grep -c "\$Hash::" "${base}") #----------------------------------------------------------------------# # Collapse the $Hash$ line (Keep the .bck, we still might need it) # #----------------------------------------------------------------------# if [[ ${havehash} != 0 ]] then if [[ ${longhash} != 0 ]] then sed -i.bck -e 's!^\([[:space:]]*\$Hash::\).*\$:!\1 '"${spc:0:66}"'\$:!' "${base}" else sed -i.bck 's/\$Hash:.*\$/\$Hash\$/' "${base}" fi fi #----------------------------------------------------------------------# # If we didn't make any changes, the repo and this one are the same # #----------------------------------------------------------------------# diff=$(git diff "${base}" 2>&1) [[ x"${diff}" = x"" ]] && { if [[ ${initial} = 0 ]] then [[ ${havehash} != 0 ]] && mv "${base}.bck" "${base}" continue fi } rm -f "${base}.bck" #----------------------------------------------------------------------# # Give the file a new version number (while we are at it) # #----------------------------------------------------------------------# if [[ $(grep -c "[[:space:]]*\$Revision::.*\$:" "${base}") != 0 ]] then datim=$(perl -e ' my $t = (stat("'"${base}"'"))[9]; my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime($t); printf "%04d-%02d-%02d %02d:%02d:%02dZ", $year+1900, $mon+1, $mday, $hour, $min, $sec; ') newrev="$(awk '/[[:space:]]*\$Revision::/ { print $3; exit }' "${base}")" [[ x"${newrev}" = x"\$:" ]] && newrev=0 newrev="$(( ${newrev} + 1 ))" f="${base##*/} ${newrev} ${datim} ${USER} ${spc}" newrev="${newrev}${spc}" nam="${author}${spc}" now="$(date '+%F %T %z (%a, %d %b %Y)') ${spc}" sed -i.bck \ -e 's!\([[:space:]]*\$Id::\).*\$:!\1 '"${f:0:68}"'\$:!' \ -e 's!\([[:space:]]*\$Revision::\).*\$:!\1 '"${newrev:0:62}"'\$:!' \ -e 's!\([[:space:]]*\$Author::\).*\$:!\1 '"${nam:0:64}"'\$:!' \ -e 's!\([[:space:]]*\$Date::\).*\$:!\1 '"${now:0:66}"'\$:!' \ "${base}" rm -f "${base}.bck" fi #----------------------------------------------------------------------# # Commit the file to git # #----------------------------------------------------------------------# git commit --author "${author}" "${base}" fi #--------------------------------------------------------------------------# # Get the current git id # #--------------------------------------------------------------------------# id=$(git-show -s --pretty=format:"${base} %h %ci ${who}" -- "${base}") [[ -z ${id} ]] && continue #--------------------------------------------------------------------------# # Re-add the $Hash$ line # #--------------------------------------------------------------------------# if [[ ${havehash} != 0 ]] then if [[ ${longhash} != 0 ]] then id="${id}${spc}" sed -i.bck -e 's!\([[:space:]]*\$Hash::\).*\$:!\1 '"${id:0:66}"'\$:!' "${base}" else sed -i.bck "s/\\\$Hash\\\$/\$Hash: ${id} \$/" "${base}" fi rm -f "${base}.bck" fi chmod a-w "${base}" 2> /dev/null cd - > /dev/null 2>&1 done