import subprocess

db.define_table(
    'Meta',
    Field('creation', 'datetime', default=request.now),
    Field('git_commit', 'string'),
    Field('git_date', 'string')
)

def getGitCommit(_record):
    try:
        commit = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=request.folder, encoding="utf-8")[:-1]
    except:
        commit = "Failed to get git commit"
    return(commit)

def getGitDate(_record):
    try:
        date = subprocess.check_output(["git", "log", "-1", "--format=%cd", "--date=iso8601"], cwd=request.folder, encoding="utf-8")[:-7]
    except:
        date = "Failed to get git date"
    return(date)

db.Meta.git_commit.compute = getGitCommit
db.Meta.git_date.compute = getGitDate

if db(db.Meta).count() == 0:
    db.Meta.insert()