Skip to content

Add dates to git util #1

@DavidWells

Description

@DavidWells

Created:

git log --follow --pretty=format:%at -- ./src/posts/hello-world.md

Modified:

// https://github.com/joshuatz/git-date-extractor/blob/4903ef2f64733371b1b8912a5f2f2ba4a378bcd0/src/stamp-handler.js#L72-L140
promiseArr.push((async () => {
  const fileMeta = filesToGet[f];
  const {fullFilePath, cacheKey} = fileMeta;
  // Lookup values in cache
  /**
  * @type {import('./types').StampObject}
  */
  let dateVals = timestampsCache[cacheKey];
  dateVals = typeof (dateVals) === 'object' ? dateVals : {
    created: 0,
    modified: 0
  };

  try {
    /* istanbul ignore else */
    if (!dateVals.created || ignoreCreatedCache) {
      // Get the created stamp by looking through log and following history
      // Remember - for created, we want the very **first** commit, which in the git log, is actually the *oldest* and *last* commit

      // NEED to either wrap with try/catch, or create helper utility to wrap any promise with return null if fail, etc.

      let createdStamp = null;
      const createdStampsLog = await failSafePromise(spawnPromise(`git`, [`log`, `--follow`, `--pretty=format:%at`, `--`, fullFilePath], execOptions), null);
      if (createdStampsLog) {
      // Need to basically run `tail -n 1`, grab last line
        const createdStamps = createdStampsLog.split(/[\r\n]+/m);
        createdStamp = Number(createdStamps[createdStamps.length - 1]);
      }
      if (!getIsValidStampVal(createdStamp) && gitCommitHook.toString() !== 'post') {
        // During pre-commit, a file could be being added for the first time, so it wouldn't show up in the git log. We'll fall back to OS stats here
        // createdStamp = Math.floor(fse.statSync(fullFilePath).birthtimeMs / 1000);
        createdStamp = (await getFsBirth(fullFilePath)).birthtime;
      }
      if (Number.isNaN(createdStamp) === false) {
        dateVals.created = createdStamp;
      }
    }

    // Always update modified stamp regardless
    let modifiedStamp = null;
    if (gitCommitHook === 'none' || gitCommitHook === 'post') {
      // If this is running after the commit that modified the file, we can use git log to pull the modified time out
      // Modified should be the most recent and at the top of the log
      modifiedStamp = await failSafePromise(spawnPromise(`git`, [`log`, `-1`, `--pretty=format:%at`, `--follow`, `--`, fullFilePath], execOptions), null);
    }
    modifiedStamp = Number(modifiedStamp);
    if (gitCommitHook === 'pre' || !getIsValidStampVal(modifiedStamp)) {
      // If this is running before the changed files have actually be commited, they either won't show up in the git log, or the modified time in the log will be from one commit ago, not the current
      // Pull modified time from file itself
      const fsStats = await statPromise(fullFilePath);
      modifiedStamp = Math.floor(fsStats.mtimeMs / 1000);
    }
    if (Number.isNaN(modifiedStamp) === false) {
      dateVals.modified = modifiedStamp;
    }
    // Check for zero values - this might be the case if there is no git history - new file
    // If there is a zero, replace with current Unix stamp, but make sure to convert from JS MS to regular S
    dateVals = replaceZeros(dateVals, Math.floor((new Date()).getTime() / 1000));
  } catch (error) {
    /* istanbul ignore next */
    console.log(`getting git dates failed for ${fullFilePath}`, error);
  }

  return {
    fileMeta,
    stamps: dateVals
  };
})());
}

https://serverfault.com/questions/401437/how-to-retrieve-the-last-modification-date-of-all-files-in-a-git-repository

git ls-tree -r --name-only HEAD -z | TZ=UTC xargs -0n1 -I_ git --no-pager log -1 --date=iso-local --format="%ad _" -- _

2021-01-14 06:19:57 +0000 README.md
2021-01-15 11:26:47 +0000 gatsby-config.js
2021-01-15 13:43:00 +0000 gatsby-node.js
2021-01-15 13:49:26 +0000 package-lock.json
2021-01-15 15:55:52 +0000 package.json
2021-01-15 11:26:47 +0000 src/components/layout.js
2021-01-14 06:19:57 +0000 src/images/icon.png
2021-01-14 06:19:57 +0000 src/pages/404.js
2021-01-15 11:26:47 +0000 src/pages/index.js
2021-01-15 11:26:47 +0000 src/posts/hello-world.md
2021-01-15 11:26:47 +0000 src/styles/main.css
2021-01-15 11:26:47 +0000 src/templates/markdown.js
2021-01-15 13:43:00 +0000 timestamps.json
2021-01-15 15:55:52 +0000 update-stamps.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    auto-generatedAutomatically generated contentclaude-codeItems created or modified by Claude Code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions