Question

Does anybody know way to change mtime to repo commit time (or any other, but depends on commit metadata) for added/updated files?

We have some logic, which tests files mtime, but backend servers have different mtime on files which were changed, because of this we have some bugs.

Was it helpful?

Solution

Assuming you are getting updating/adding files when you do a git fetch, you can create a git-rebase-and-touch script file that does the rebase for you along with touching all files/directories in each new revision.

The script would look like:

#!/bin/bash

saveIFS=${IFS}
IFS=$'\n'
startrev=$(git rev-parse HEAD)
git rebase
for rev in $(git rev-list --reverse ${startrev}..HEAD); do
    stamp=$(git log --pretty="%aD" ${rev}~..${rev})
    IFS=$'\n'
    for filename in $(git diff --name-only ${rev}~..${rev}); do
        file=""
        IFS='/'
        for part in ${filename}; do
            file=${file}/${part}
            file=${file#/}
            touch -c --date="${stamp}" "${file}"
        done
    done
done
IFS=${saveIFS}

If you currently use git pull now, use git fetch instead.

OTHER TIPS

It's bloody dangerous tweaking file timestamps, and it's even more dangerous to assume, as you're doing here, that a timestamp means something other than what it ordinarily means. With anything, not just timestamps, doing that hurts reliability and maintainability, it makes comprehension and auditing difficult. The files changed for a legitimate reason, and your system broke.

The timestamps you want to check are recorded in commit metadata, and getting to them isn't efficient enough. Switch to extracting the timestamps to an index file or some such and check them there. Otherwise you're reduced to telling people learning your setup that "not everything is what it seems to be".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top