Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use last commit as a date front matter option #142

Closed
zachleat opened this issue Jun 14, 2018 · 12 comments
Closed

Use last commit as a date front matter option #142

zachleat opened this issue Jun 14, 2018 · 12 comments
Labels
enhancement: favorite Vanity label! The maintainer likes this enhancement request a lot. enhancement

Comments

@zachleat
Copy link
Member

This will alleviate the problem of deploying on a remote server and losing all of your file creation/updated dates and also make it unnecessary to hard code dates.

Inspired by vuepress https://vuepress.vuejs.org/default-theme-config/#last-updated

@zachleat zachleat added enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. labels Jun 14, 2018
@zachleat
Copy link
Member Author

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

The enhancement backlog can be found here: https://github.com/11ty/eleventy/issues?utf8=%E2%9C%93&q=label%3Aneeds-votes+sort%3Areactions-%2B1-desc+

@zachleat zachleat added the enhancement: favorite Vanity label! The maintainer likes this enhancement request a lot. label Jul 6, 2018
@BrentARitchie
Copy link

I just wanted to point out that the discussion is mostly about the last commit date so far. There are considerations around first commit date as well, such as if this should operate as if the follow option was specified or not. This would have implications if a file was renamed. The correct behaviour is non-obvious as it would likely need to be dealt with on a case-by-case basis.

Should there be a new config section to specify some of these options? or should it be specified in the front matter of each page?

@adamduncan
Copy link

We currently have Last Modified, which is super handy when building locally. Although have noticed it retruns a possibly misleading value in CI environments.

Would it be helpful to include Last Changed as an additional value available for use with Content dates?

I expect it might break the existing understanding of Last Modified (using ctime under the hood) vs. mtime 🤔

Or will we still need some git log wizardry to get a file's true Last modified (committed) date?

@BrentARitchie
Copy link

I've been playing with some ideas and so far the most promising is by using git-date-extractor in a prebuild script that drops the created and modified dates into a global datafile. The problem is that the build times do take quite a long time, even after filtering to only files in the sitemap. 24 files takes about 40-ish seconds to crawl and generate in the repo.

May have to look into caching results and truncating the search to recent commits.

@Ryuno-Ki
Copy link
Contributor

Thanks, @BrentARitchie.
I looked into the source code. I think, it can be simplified of our use case.
Basically, applying How to retrieve the last modification date of all files in a git repository with git log RETTY FORMATS yields:

git ls-tree -r --name-only HEAD | while read filename; do echo "$(git log -n1 --format=%aI $filename) $filename"; done

In case you can't read bash:

  • git ls-tree: List the contents of a tree object
  • -r: Do so recursively
  • --name-only: Only output the path
  • HEAD: The latest commit (on main/master)

This yields a list of file names git knows about.
You pipe this into a while loop in bash, where the item is assigned to a variable filename in each iteration.

  • git log: Show information
  • n1 (or -1): Limit to top one (the latest)
  • --format=%aI: Use strict ISO format (see the docs)
  • $filename: Apply to the current file path

I'd suggest to replace the git ls-tree and while loop in Node to increase the cross-platform compatibility.
Then, trigger a exec form child_process for each file (or use the git ls-tree to compile the list to be sure?).

zachleat added a commit that referenced this issue Feb 17, 2022
Adds `date: "git Last Modified"` support to use last git commit as `page.date` variable.
@zachleat zachleat added this to the Eleventy 1.0.1 milestone Feb 17, 2022
@zachleat zachleat removed the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label Feb 17, 2022
@zachleat
Copy link
Member Author

zachleat commented Feb 17, 2022

This adds a date: "git Last Modified" option to populate page.date with the last git commit.

Importantly, if a file is not checked into git, returns Date.now() instead.

See https://github.com/11ty/eleventy/blob/8f35e553f80a9a75da3b2eb1a923ecce87f65573/src/Util/DateGitLastUpdated.js and thanks to VuePress for the implementation!

This is a little bit expensive so maybe use sparingly (or only in production?). Will pair nicely with ENV variables and directory data files:

e.g. _posts/_posts.11tydata.js

if(process.env.CONTEXT === "production") {
  module.exports = {
    date: "git Last Modified"
  };
}

module.exports = {};

This is slated to ship with 1.0.1

@nhoizey
Copy link
Contributor

nhoizey commented Feb 17, 2022

On a project, I would need a "git First Commited", do you think it would possible to add it with a similar command?

Here's how I currently get it in a Bash script:

fileDate=$(git log --all --diff-filter=A --pretty=%x0a%ci --name-only | awk '
/^$/        { dateline=!dateline; next }
dateline    { date=$0; next }
!seen[$0]++ { print date,$0 }
' | grep "$fileBasename" | head -1)

Found it in https://stackoverflow.com/a/32894198/717195

@zachleat
Copy link
Member Author

@nhoizey can you file this as a separate issue? I’d be open to adding an config point for folks to add their own callbacks too

@nhoizey
Copy link
Contributor

nhoizey commented Feb 17, 2022

Done: #2224

zachleat added a commit to 11ty/11ty-website that referenced this issue Feb 17, 2022
zachleat added a commit to 11ty/11ty-website that referenced this issue Feb 18, 2022
@kkga
Copy link

kkga commented Feb 20, 2022

Has anyone tried this with Vercel?
It creates a shallow clone of the git repo with only latest 10 timestamps of git history (using --depth 10, source), so the timestamp returned by git log would not actually represent the last commit date for given file in most cases.

@zachleat
Copy link
Member Author

Good callout @kkga, probably worth adding a warning to the docs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: favorite Vanity label! The maintainer likes this enhancement request a lot. enhancement
Projects
None yet
Development

No branches or pull requests

6 participants