|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# commun sub program to generate keyword content |
| 4 | +sub generate_keyword { |
| 5 | + # pattern as in the raw source file |
| 6 | + $pattern = $_[0]; |
| 7 | + # keyword value from git log |
| 8 | + $value = $_[1]; |
| 9 | + |
| 10 | + # extract pattern start |
| 11 | + $start = substr($pattern,0,2); |
| 12 | + if ($start eq "::") { |
| 13 | + # if it start with "::", the pattern length must be preserved |
| 14 | + return ":: " . $value . (' ' x (length($pattern) - length($value) - 3)); |
| 15 | + } else { |
| 16 | + # return default content without length concern |
| 17 | + return ": " . $value . " "; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +# get first argument (full path name) |
| 22 | +$path = shift; |
| 23 | + |
| 24 | +if (0 < length($path)) { |
| 25 | + # extract file name |
| 26 | + $path =~ /.*\/(.*)/; |
| 27 | + $filename = $1; |
| 28 | + |
| 29 | + # not a full path, return the entire path as file name |
| 30 | + if (0 == length($filename)) { |
| 31 | + $filename = $path; |
| 32 | + } |
| 33 | + |
| 34 | + # Need to grab filename and to use git log for this to be accurate. |
| 35 | + use IPC::Open3; |
| 36 | + $pid = open3(0,\*READ,\*ERROR,"git", "log", "-n", "1", "--format=%H%n%an%n%ai%n", "--", "$path"); |
| 37 | + waitpid( $pid, 0 ); |
| 38 | + |
| 39 | + # read error pipe |
| 40 | + chomp($error = <ERROR>); |
| 41 | + |
| 42 | + # if there is no error message |
| 43 | + if (0 == length($error)) { |
| 44 | + # read the 3 output lines of svn log |
| 45 | + chomp($revision = <READ>); # 1st line is revision hash |
| 46 | + chomp($author = <READ>); # 2nd line is author name |
| 47 | + chomp($date = <READ>); # 3rd line is date |
| 48 | + |
| 49 | + # some usefull debug stuff |
| 50 | + # print("$revision\n$author\n$date\n"); |
| 51 | + |
| 52 | + # main processing loop |
| 53 | + |
| 54 | + # read from stdin |
| 55 | + while (<STDIN>) { |
| 56 | + # replace keyword "Date" |
| 57 | + s/\$([Dd][Aa][Tt][Te])([^\$]*)\$/"\$" . $1 . generate_keyword($2,$date) . "\$"/eg; |
| 58 | + |
| 59 | + # replace keyword "Author" |
| 60 | + s/\$([Aa][Uu][Tt][Hh][Oo][Rr])([^\$]*)\$/"\$" . $1 . generate_keyword($2,$author) . "\$"/eg; |
| 61 | + |
| 62 | + # replace keyword "Id" |
| 63 | + s/\$([Ii][Dd])([^\$]*)\$(.*)/"\$" . $1 . generate_keyword($2,$path . " " . $author . " " . date) . "\$"/eg; |
| 64 | + |
| 65 | + # replace keyword "Url" |
| 66 | + s/\$([Uu][Rr][Ll])([^\$]*)\$/"\$" . $1 . generate_keyword($2,$path) . "\$"/eg; |
| 67 | + |
| 68 | + # replace keyword "File" |
| 69 | + s/\$([Ff][Ii][Ll][Ee])([^\$]*)\$/"\$" . $1 . generate_keyword($2,$filename) . "\$"/eg; |
| 70 | + |
| 71 | + # replace keywords "Revision" and "Rev" |
| 72 | + s/\$([Rr][Ee][Vv][Ii][Ss][Ii][Oo][Nn])([^\$]*)\$/"\$" . $1 . generate_keyword($2,$revision) . "\$"/eg; |
| 73 | + s/\$([Rr][Ee][Vv])([^Ii][^\$]*|)\$/"\$" . $1 . generate_keyword($2,$revision) . "\$"/eg; |
| 74 | + |
| 75 | + } continue { |
| 76 | + # output generated text to stdout |
| 77 | + print; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments