Evgenij Reznik
Evgenij Reznik

Reputation: 18594

Last modification of a file

I'm trying to retrieve as much information of the files on Git with JGit as possible.
With getCommitTime I can get the time, when a files was commited. Is it also possible to get the time when a file has been modified/updated/edited? I haven't found something like getModifiedTime.

Upvotes: 1

Views: 699

Answers (1)

jub0bs
jub0bs

Reputation: 66214

You're out of luck. Git records the contents of a file in an object called "blob", and the filename in a tree object, but it keeps no record of timestamps associated with the file.

When you check a file out, the modification timestamp of the file now in your working tree is actually set to the current date and time shown by your kernel clock.

Why it's not such a bad thing

You probably use the make utility (or the likes of it) to automate your build. As you know, make relies on modification timestamps to figure out which targets (if any) must be built.

Let's imagine what could happen if Git did record (and restore upon checkout) modification timestamps of committed files. Let's assume you have a repository with only one tracked file, foo, which is the only dependency of a target called bar:

foo -> bar

Let's assume further that,

  • your history consists of two commits:

    A -- B [HEAD=master]
    
  • the modification timestamp of foo in commit A is "today", 9 am,

  • the modification timestamp of foo in commit B is "today", 10 am.

You currently have commit B checked out, and you have run make at 10:05 am, so the build (bar) in your working tree is up to date. However, you decide that you want to go back to commit A, and create and check out a branch from there (possibly to take foo in a different direction):

git checkout -b other HEAD^

Your repo now looks as follows

A [HEAD=other]
 \
  B [master]

If you were to run make again, in order to build the version of bar corresponding to commit A, make would compare the modification timestamps of the tracked and currently checked out file foo (9 am) and that of the untracked file bar (10:05 am), and it would conclude that bar is up to date and doesn't need to be built again.

To force make to build bar again, you would have to touch foo. That be a hassle, and of course, the problem would get worse as the number of tracked make dependencies in your project increases.

So it's not such a bad thing that Git doesn't record and restore modification timestamps of tracked files upon checkout.


Relevant reading:

Upvotes: 1

Related Questions