This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env tclsh8.5 | |
| package require Tclx | |
| proc debug {buf} { | |
| # puts $buf | |
| } | |
| proc logmsg {buf} { | |
| puts $buf | |
| } | |
| proc logerr {buf} { | |
| puts $buf | |
| set ::error 1 | |
| } | |
| proc update_serial {filename old new} { | |
| logmsg "** Updating $filename serial $old -> $new" | |
| set buf [read_file $filename] | |
| regsub $old $buf $new newbuf | |
| set fh [open $filename "w"] | |
| puts -nonewline $fh $newbuf | |
| close $fh | |
| set serial_change [exec git add $filename] | |
| set ::action 1 | |
| } | |
| proc main {} { | |
| set changed [exec git diff-index HEAD] | |
| set prefix [clock format [clock seconds] -format "%Y%m%d" -gmt 1] | |
| set ::error 0 | |
| set ::action 0 | |
| foreach file [split $changed "\n"] { | |
| if {[regexp {\s+(\S+)\s+M\s+(.*)$} $file _ commit_id filename]} { | |
| if {$commit_id == "0000000000000000000000000000000000000000"} { | |
| debug "$filename modified but not added" | |
| } else { | |
| set buf [read_file $filename] | |
| if {[regexp {SOA\s+\S+\s+\S+\s+\(\s+(\d\d\d\d\d\d\d\d)(\d\d)\s+} $buf _ date sequence]} { | |
| set serial "$date$sequence" | |
| debug "Found serial $serial in $filename" | |
| if {$prefix < $date} { | |
| logerr "Existing serial number $serial is in the future" | |
| } elseif {$prefix == $date} { | |
| incr sequence | |
| update_serial "$filename" "$serial" "[format "%08d" $prefix][format "%02d" $sequence]" | |
| } else { | |
| set sequence "00" | |
| update_serial "$filename" "$serial" "[format "%08d" $prefix][format "%02d" $sequence]" | |
| } | |
| } else { | |
| debug "No serial number found in $filename" | |
| } | |
| } | |
| } | |
| } | |
| if {$::action || $::error} { | |
| after 5000 | |
| } | |
| if {$::error} { | |
| exit -1 | |
| } | |
| } | |
| if !$tcl_interactive main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment