Skip past introduction.

WolfBlog

Reflections from a Swede in Norway.

Here you will find my small graffiti board where I write about things I come to think about.

In my blog, I write about the general unfairness of life, about spam mail, vintage computers, board games, Norwegians, current and not-so-current affairs, technology and whatever else occurs to me — in other words, a glorious mess. All opinion expressed here are of course my own, and all similarities with any living people is of course intentional.

These pages are using automatic language selection, if you prefer to read in Swedish, please use this page instead. See the help for info on how to make your web browser select the correct language automatically. A monthly overview is also available.

All text and pictures © copyright Peter Krefting, unless otherwise noted. More about the author.

Expanding keywords in Git

Published: Thursday 2009-08-13.

A common migration problem with Git is that Git does not have support for expanding RCS-like keywords. Several other systems have support for, for instance, inserting the time of the last check-in into a document by adding a place-holder on the form $Date$. Git does not, and this is considered to be by design.

I understand the arguments they give and in most cases I can live without this particular functionality. But when particularly considering having my web site in Git I would very much like to have it. Most of my pages have a date stamp at the end of the page which describes the date it was last updated, and I am much too forgetful to keep it updated by myself. I first tried a method that uses filters, but that did not work out very well. After testing various options, I have solved it by using a so called pre-commit hook:

#!/bin/bash -e

# Find base commit
if git-rev-parse --verify HEAD >/dev/null 2>&1
then
  against=HEAD
else
  # Initial commit: diff against an empty tree object
  against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Set up Date substitution
export NOW=$(date +"%Y-%m-%d %H:%M:%S")
for file in $(git diff-index --cached --diff-filter=AM --name-only $against); do
  if git check-attr datereplace -- "$file" | grep 'datereplace: true' > /dev/null; then
    perl -w -i.bak -e 'my $now = $ENV{"NOW"};
$now =~ s/[^-:0-9 ]//g;
while (<>)
{
 if (/\$Date:?[^\$]*\$/)
 {
   s/\$Date:?[^\$]*\$/\$Date: ${now} \$/;
 }
 print
}' "$file"
    git update-index --add "$file"
  fi
done

exit 0

I have no doubt that it can be done in a cleaner fashion, but this works to my satisfaction for my use-case, so I am happy with it for the time being.

Categories: software. Share: Facebook, Google+, email

Comments

The article is older than a fortnight and has been closed for new comments.

Disclaimer: The comments are copyrighted by their respective authors. The web site owner takes no responsibility for the contents of the comments. Improper comments will be deleted.

  • Datum: 2009-08-13 19.27.14 CEST
  • Namn: Anders Carlsson
  • Sänt från: ******************.business.telia.com

Du kanske också skulle kunna frilansa på Utbildningsradion?

-"Välj den fil du vill kopiera och skriv sedan chmod ifs /dev/null, vc. Vc ska sedan pipa till vg print $1, fpath ls -la $param, exec tempfile trap. Här kommer processen att forka, och child kommer sen att gå vidare till rm -f, optparse rmm typeset l1 -r .current, scanf grep .sh .dflag. Sen forkar processen igen och skickar resultatet till en pipe, som du sedan kopplar till den fil som du vill kopiera!"

(få se om ditt antispamfilter släpper igenom det här?)

  • Datum: 2009-08-14 10.02.16 CEST
  • Namn: Peter Krefting
  • Sänt från: *******.opera.com

Jag har tänkt lyfta ut de miniguider jag skrivit här till en egen avdelning på vävplatsen, men som allt så kräver det att jag sätter mig ner och faktiskt gör det :-)

| | Latest postings | This month | All months and categories

This page is best read on the Internet.

peter@softwolves.pp.se