Skip to content

Instantly share code, notes, and snippets.

@kuri65536
Last active December 6, 2023 14:56
Show Gist options
  • Save kuri65536/acf44b5b63fe56c8534c89a051949bb2 to your computer and use it in GitHub Desktop.
Save kuri65536/acf44b5b63fe56c8534c89a051949bb2 to your computer and use it in GitHub Desktop.
Keywords expansion with Git

git keyword expansions with staging minimum lines

this scripts applies keyword expansions to git cache with minimum lines.

the program of keyword expansions

a bash script to patch keywords expansions with git apply .

you can save this into your .bashrc and use as bash functions.

function gitapplykw_one() {
    if [[ "$1" == -f ]] ; then
        shift 1
    elif [[ "$(file $1)" != *text ]] ; then
        return
    fi
    kw_date=$(date --rfc-3339=seconds)
    kw_rev=$(git count HEAD)-$(git rev-parse --short=10 HEAD)
    #tmp=".tmp/$(echo $1 | sed 's/\////g').$(date '+%Y%m%d-%H%M%S')"
    tmp=".tmp/$(echo $1 | sed 's/\////g')"
    mkdir -p .tmp
    git show ":$1" > "$tmp"
    sed -i "s/\\\$Date:[-_+ :A-Za-z0-9]*\\\$/\$Date: ${kw_date} \$/" "$tmp"
    sed -i "s/\\\$Rev:[-_+ :A-Za-z0-9]*\\\$/\$Rev: ${kw_rev} \$/" "$tmp"
    git show ":$1" | diff -u /dev/stdin "$tmp" \
    | (echo diff --git a/$1 b/$1
       echo "index 0000..0000 100644"
       echo "--- a/$1"
       echo "+++ b/$1"
       tail -n +3
    ) | tee $tmp.patch | git apply --index  # --cached
}

a sample of commit

search staged files and patch keywords expansions to them.

function gitapplykw() {
    git st -s --ignore-submodules | sed -n 's/^M \+//p' \
    | while read line; do
        gitapplykw_one "$line"
    done
}

function gitcommit() {
    gitapplykw
    git commit $*
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment