2

I have about 30 older *.ipynb files which I want to start tracking in git.

I want to save as much metadata as possible, e.g., what is the best way to record their original timestamp in git?

The way this timestamp (in fact, just the mtime of the file) would be used is figuring out which experimental approaches were tried in what order (and, thus, which are more likely to be useful). Thus, I might want to order the files by their original timestamp.

PS. In this context the file mtime becomes, in fact, git ctime. :-)

sds
  • 58,617
  • 29
  • 161
  • 278
  • 1
    If you want `git checkout` do do something extra, like set timestamps, write a `post-checkout` hook that does what you want. – jthill Dec 03 '16 at 23:33
  • Keep in mind that Unix filesystems do not store file creation time. – Keith Thompson Dec 03 '16 at 23:40
  • I would propose a pre-commit hook which examines changes being committed and adds/updates mtime in `notes` in a format suitable for further analyzing as @PatrykObara suggests. Then use `touch -d ...` in the post-checkout hook proposed by @jthill – user3159253 Dec 03 '16 at 23:50

2 Answers2

2

By default git stores only file mode, but you can attach textual data to any object you like - this feature is called "notes". You can attach note to any object (including blob), but probably best way is to attach it to commit introducing your file, as you will be able to easily see note content with git log.

$ git notes add <hash>

See manual page to read about all the options :)

Patryk Obara
  • 1,847
  • 14
  • 19
1

Method

I can store the timestamp as the AuthorDate of the first commit. Since the files are *.ipynb notebooks, they are not going to be mentioned in any Makefile, and I want to do it just once, on the initial checkin, so Linus's vehement objection quoted in What's the equivalent of use-commit-times for git? is not applicable in any form.

Store

1 - get the list of files to operate on:

LEGACY_IPYNB=$(git ls-files --others --exclude-standard | grep ipynb)

2 - add the files with git add $LEGACY_IPYNB.

3 - commit each file separately (YUK! but I cannot do a single commit because the author time is per commit, not per file!):

for f in $LEGACY_IPYNB; do
  git commit --date=$(stat -f "%Sm" -t "%FT%T" $f) \
      --author="the actual author" -m 'initial commit' $f
done

Recover

1 - extract the initial commit ID (How to reference the initial commit?):

# FIXME: will break if `$f` has been since renamed!
COMMIT=$(git rev-list --max-parents=0 HEAD -- $f)

2 - extract the initial author time:

MTIME=$(git log --pretty=format:%ad --date=format:%Y%m%d%H%M $COMMIT)

3 - modify mtime of the checked out file:

touch -m -t $MTIME $f
Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278