|
| 1 | +#!/usr/bin/perl |
| 2 | +# |
| 3 | +# @brief Git filter to implement rcs keyword expansion as seen in cvs and svn. |
| 4 | +# @author Martin Turon |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# .git_filter/rcs-keywords.smudge file_path < file_contents |
| 8 | +# |
| 9 | +# To add keyword expansion: |
| 10 | +# <project>/.gitattributes - *.c filter=rcs-keywords |
| 11 | +# <project>/.git_filters/rcs-keywords.smudge - copy this file to project |
| 12 | +# <project>/.git_filters/rcs-keywords.clean - copy companion to project |
| 13 | +# ~/.gitconfig - add [filter] lines below |
| 14 | +# |
| 15 | +# [filter "rcs-keywords"] |
| 16 | +# clean = .git_filters/rcs-keywords.clean |
| 17 | +# smudge = .git_filters/rcs-keywords.smudge %f |
| 18 | +# |
| 19 | +# Copyright (c) 2009-2011 Turon Technologies, Inc. All rights reserved. |
| 20 | + |
| 21 | +$path = shift; |
| 22 | +$path =~ /.*\/(.*)/; |
| 23 | +$filename = $1; |
| 24 | + |
| 25 | +if (0 == length($filename)) { |
| 26 | + $filename = $path; |
| 27 | +} |
| 28 | + |
| 29 | +# Need to grab filename and to use git log for this to be accurate. |
| 30 | +$rev = `git log --date=iso -- | head -n 3`; |
| 31 | +$rev =~ /^Author:\s*(.*)\s*$/m; |
| 32 | +$author = $1; |
| 33 | +$author =~ /\s*(.*)\s*<.*/; |
| 34 | +$name = $1; |
| 35 | +$rev =~ /^Date:\s*(.*)\s*$/m; |
| 36 | +$date = $1; |
| 37 | +$rev =~ /^commit (.*)$/m; |
| 38 | +$ident = $1; |
| 39 | +$shortident = substr($ident,0, 6); |
| 40 | + |
| 41 | +while (<STDIN>) { |
| 42 | + s/\$Date(((::)*)[^\$]*)\$/TR("\$Date",$1,$date,$2,"\$")/eo; |
| 43 | + s/\$Author(((::)*)[^\$]*)\$/TR("\$Author",$1,$author,$2,"\$")/eo; |
| 44 | + s/\$Id(((::)*)[^\$]*)\$/TR("\$Id",$1,"$filename | $date | $name",$2,"\$")/eo; |
| 45 | + s/\$File(((::)*)[^\$]*)\$/TR("\$File",$1,$filename,$2,"\$")/eo; |
| 46 | + s/\$Source(((::)*)[^\$]*)\$/TR("\$Source",$1,$path,$2,"\$")/eo; |
| 47 | + s/\$Rev(((::)*)[^\$]*)\$/TR("\$Rev",$1,$shortident,$2,"\$")/eo; |
| 48 | + s/\$Revision(((::)*)[^\$]*)\$/TR("\$Revision",$1,$ident,$2,"\$")/eo; |
| 49 | +} continue { |
| 50 | + print or die "-p destination: $!\n"; |
| 51 | +} |
| 52 | + |
| 53 | +sub TR{ |
| 54 | + my ($pre,$from,$to,$fix,$post)=@_; |
| 55 | + return $pre.$to.$post if($fix eq ''); |
| 56 | + |
| 57 | + $pre.=$fix; |
| 58 | + if(length($from)<length($to)+length($fix)){ |
| 59 | + $to=substr($to,0,length($from)-length($fix)); |
| 60 | + }else{ |
| 61 | + $to.=' ' x (length($from)-length($to)-length($fix)); |
| 62 | + } |
| 63 | + return $pre.$to.$post; |
| 64 | +} |
0 commit comments