diff --git a/kicad-git-setup b/kicad-git-setup index 4db7bc1458975657e1651e08179a0c0ac04bcd33..04d07ba2caeb4c9092c40b3933e3a93bbdeefff1 100755 --- a/kicad-git-setup +++ b/kicad-git-setup @@ -2,26 +2,27 @@ shopt -s nullglob +# Symlink git filters mkdir -p ~/.config/git for s in kicad-pro-filter kicad-sch-filter ; do - FILTER=$(realpath $(dirname $0)/kicad-sch-filter) - if [ -L ~/.config/git/${s} ] ; then - if [ $(readlink ~/.config/git/${s}) != ${FILTER} ] ; then + FILTER=$(realpath $(dirname $0)/${s}) + CONFIG_PATH=~/.config/git/${s} + if [ -L ${CONFIG_PATH} ] ; then + if [ $(readlink ${CONFIG_PATH}) != ${FILTER} ] ; then echo REPLACE link - rm ~/.config/git/${s} - fi - elif [ -f ~/.config/git/${s} ] ; then - if diff -q <($s) <(cat ~/.config/git/${s}) ; then - echo REPLACE file - rm ~/.config/git/${s} + rm ${CONFIG_PATH} fi + elif [ -f ${CONFIG_PATH} ] ; then + echo REPLACE file + rm ${CONFIG_PATH} fi - if [ ! -L ~/.config/git/${s} ] ; then - ln -s $FILTER ~/.config/git/${s} + if [ ! -L ${CONFIG_PATH} ] ; then + ln -s ${FILTER} ${CONFIG_PATH} fi done +# Sanity check kicad project SCHEMATICS=( *.sch ) if [ ${#SCHEMATICS[*]} -eq 0 ] ; then echo "No schematic file(s) in directory" >&2 @@ -35,6 +36,7 @@ if [ ${#PROJECT[*]} -ne 1 ] ; then exit 1 fi +# Setup git if [ ! -d .git ] ; then # Initialize git git init @@ -42,7 +44,7 @@ fi # Fill in .gitignore GITIGNORE=( "*~" "*.net" "*.kicad_pcb-bak" "*.bak" "_autosave*" ) -GITIGNORE+=( "*.csv" "*.tsv" "*.xml" ) +GITIGNORE+=( "*.sch-bak" "*.csv" "*.tsv" "*.xml" ) for s in ${PROJECT[@]} ; do if [[ "$s" =~ (.*)\.pro ]] ; then BASE=${BASH_REMATCH[1]} @@ -102,4 +104,22 @@ EOF fi done +# Install hooks +for s in post-commit post-checkout ; do + HOOK=$(realpath $(dirname $0)/${s}) + HOOK_PATH=.git/hooks/${s} + if [ -L ${HOOK_PATH} ] ; then + if [ $(readlink ${HOOK_PATH}) != ${HOOK} ] ; then + echo REPLACE link + rm ${HOOK_PATH} + fi + elif [ -f ${HOOK_PATH} ] ; then + echo REPLACE file + rm ${HOOK_PATH} + fi + if [ ! -L ${HOOK_PATH} ] ; then + ln -s ${HOOK} ${HOOK_PATH} + fi +done + git status diff --git a/kicad-sch-filter b/kicad-sch-filter index 83b67d77bc55e9b7380dc9729b5f6aa3b7603538..9ccabd1f20e781ac807b4ed6cc78b9db4a2dcf11 100755 --- a/kicad-sch-filter +++ b/kicad-sch-filter @@ -1,20 +1,23 @@ #!/bin/bash +set -e + clean() { IFS= read -r LINE - if [[ "${LINE}" =~ ^(EESchema.*[^\ ])[\ ]*\#(\$Id.*\$)$ ]] ; then - echo "${BASH_REMATCH[1]} #${BASH_REMATCH[2]}" - elif [[ "${LINE}" =~ ^(EESchema[^\#\$]*[^\ ])[\ ]*$ ]] ; then - echo "${BASH_REMATCH[1]} #\$Id\$" + if [[ "${LINE}" =~ ^EESchema.*$ ]] ; then + echo "${LINE}" else echo "Unknown schema type '${LINE}'" 1>&2 exit 1 fi ( while IFS= read -r LINE ; do - if [[ "${LINE}" =~ ^((Rev|Date)\ \").*(\") ]] ; then + if [[ "${LINE}" =~ ^((Rev|Date|Comment4)\ \").*(\") ]] ; then # Clean out revision and date echo "${BASH_REMATCH[1]}${BASH_REMATCH[3]}" + elif [[ "${LINE}" =~ ^((Comment[4])\ \").*(\") ]] ; then + # Clean out auto comment + echo "${BASH_REMATCH[1]}${BASH_REMATCH[3]}" else echo "${LINE}" fi @@ -23,22 +26,39 @@ clean() { } smudge() { - local REV="Unknown" - local DATE="Unknown" - local HASH= + local REV + local DATE + local AUTHOR + local HASH + if [[ "$(git --no-pager status --short -- "$1")" =~ ^(.)(.)\ $1 ]] ; then + STATUS=${BASH_REMATCH[2]} + case "${STATUS}" in + "?" ) + REV="UNTRACKED" + DATE=$(date -r "$1" +"%Y-%m-%d %H:%M:%S %z") + AUTHOR=$(grep $(id -u) /etc/passwd | cut -d ':' -f 5) + ;; + "D" ) + REV="DELETED" + DATE="" + AUTHOR=$(grep $(id -u) /etc/passwd | cut -d ':' -f 5) + ;; + "M" | "A" | "R" | "C" | "U" ) + REV="MODIFIED:${STATUS}" + DATE=$(date -r "$1" +"%Y-%m-%d %H:%M:%S %z") + AUTHOR=$(grep $(id -u) /etc/passwd | cut -d ':' -f 5) + ;; + esac + else + # File is unchanged + HASH=$(git --no-pager hash-object "$1") + REV="g$(git describe --always ${HASH} | sed -e 's/:.*//')" + DATE=$(git --no-pager log -n 1 --format="%ai" -- "$1") + AUTHOR=$(git --no-pager log -n 1 --format="%aN" -- "$1") + fi + IFS= read -r LINE - if [[ "${LINE}" =~ ^EESchema.*\#\$Id:(.*)\$ ]] ; then - local HASH=${BASH_REMATCH[1]} - REV=$(git describe ${HASH} 2>/dev/null | sed -e 's/:.*//') - if [ -z "${REV}" ] ; then - REV="g$(git describe --always ${HASH} | sed -e 's/:.*//')" - fi - DATE=$(git log --format="%ai" -- "$1" | head -1) - if [ -z "${DATE}" ] ; then - DATE="Unknown2" - fi - echo "${LINE}" - elif [[ "${LINE}" =~ ^EESchema.* ]] ; then + if [[ "${LINE}" =~ ^EESchema.* ]] ; then echo "${LINE}" else echo "Unknown schema type '${LINE}'" 1>&2 @@ -50,12 +70,15 @@ smudge() { echo "${BASH_REMATCH[1]}${REV}${BASH_REMATCH[2]}" elif [[ "${LINE}" =~ ^(Date\ \").*(\") ]] ; then echo "${BASH_REMATCH[1]}${DATE}${BASH_REMATCH[2]}" + elif [[ "${LINE}" =~ ^(Comment4\ \").*(\") ]] ; then + echo "${BASH_REMATCH[1]}Last edit by ${AUTHOR}${BASH_REMATCH[2]}" else echo "${LINE}" fi done } +#echo $1 $(stat $2) 1>&2 case "$1" in clean) clean $2 @@ -63,7 +86,15 @@ case "$1" in smudge) smudge $2 ;; + post-checkout) + smudge $2 + ;; + post-commit) + smudge $2 + ;; *) echo "unknown command '$1'" 1>&2 + exit 1 ;; esac +#stat $2 1>&2 diff --git a/post-checkout b/post-checkout new file mode 100755 index 0000000000000000000000000000000000000000..6da965d00035349dcf307787d05a881475306664 --- /dev/null +++ b/post-checkout @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e +shopt -s nullglob + +for f in *.sch ; do + TMP=$(mktemp -p .) + + cat "$f" | $(dirname $(realpath $0))/kicad-sch-filter post-checkout "$f" > ${TMP} + if ! diff -q "$f" "${TMP}" > /dev/null ; then + touch -r "$f" ${TMP} + mv ${TMP} "$f" + else + rm "${TMP}" + fi +done diff --git a/post-commit b/post-commit new file mode 100755 index 0000000000000000000000000000000000000000..a8a14cf7112a6df89bc01f0305213969e401d474 --- /dev/null +++ b/post-commit @@ -0,0 +1,17 @@ +#!/bin/bash + +set -e +shopt -s nullglob + + +for f in *.sch ; do + TMP=$(mktemp -p .) + + cat "$f" | $(dirname $(realpath $0))/kicad-sch-filter post-commit "$f" > ${TMP} + if ! diff -q "$f" "${TMP}" > /dev/null ; then + touch -r "$f" ${TMP} + mv ${TMP} "$f" + else + rm "${TMP}" + fi +done