So if anyone is interested I came up with a solution after looking at the comments below. I've written a bash script that publishes files contained within the $gitFiles
folder for me using git archive
to the $publishFolder
location. The script then gets info for each file using git log
and prints to each file header using sed s/findstr/repstr/ $file
.
#!/bin/bash
publishFolder="./Release"
gitFiles="./PythonFiles"
updateFindStr="\\\$Last Update[^\\$]*\\$"
commentFindStr="\\\$Comment[^\\$]*\\$"
hashFindStr="\\\$Hash[^\\$]*\\$"
# Archive the whole folder
folderHash=$(git log -1 --pretty=format:"%H" $gitFiles)
git archive $folderHash --format tar $gitFiles > $publishFolder/archive.tar
# Unpack the tar file and delete it
tar xf $publishFolder/archive.tar -C $publishFolder
rm $publishFolder/archive.tar
# Loop through all files in the gitfiles folder and write git info to the published files
for file in $(ls $gitFiles/*.*)
do
echo "Publishing $(basename $file)..."
# Grab the git info and save as string in format date;author;comment;hash
gitInfo=$(git log -1 --pretty=format:"%cd;%an;%s;%H" --date=short $file)
# Split the string and save to separate variables
IFS=";" read date author comment hash <<< "$gitInfo"
# build update, comment and hash strings
updateRepStr="\\\$Last Update: ${date} by ${author} \\$"
commentRepStr="\\\$Comment: ${comment} \\$"
hashRepStr="\\\$Hash: ${hash} \\$"
# Write git hash and info into the new file
sed -i "s/${updateFindStr}/${updateRepStr}/; s/${commentFindStr}/${commentRepStr}/; s/${hashFindStr}/${hashRepStr}/" $publishFolder${file:1}
done
So I start with a file header like this:
# ------------------------------------------------------------------------------
# PYTHONSCRIPT: myfile.py
# ------------------------------------------------------------------------------
# Project: n/a
# Created by: user on 22/7/2015
# $Last Update: $
# $Comment: $
# $Hash: $
# ------------------------------------------------------------------------------
And I end up with a published file with a header looking like this, which is what I wanted :)
# ------------------------------------------------------------------------------
# PYTHONSCRIPT: myfile.py
# ------------------------------------------------------------------------------
# Project: n/a
# Created by: user on 22/7/2015
# $Last Update: 2015-09-09 by user $
# $Comment: Added comment keyword $
# $Hash: ec56f527333c0c0af98c2ed3ab3395c6c8a50624 $
# ------------------------------------------------------------------------------