Do not run hooks in diff mode

Also made most functions script-level scope.