Skip to content

Commit

Permalink
Revert "Remove the fallback logic for CommitBuildTimeProvider (#7631)" (
Browse files Browse the repository at this point in the history
#7638)

This reverts commit a7cd8b7.
  • Loading branch information
jesong authored Sep 27, 2021
1 parent 5fe7672 commit d663a3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
20 changes: 19 additions & 1 deletion docs/specs/git.yml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,24 @@ outputs:
"updated_at": "2018-10-30 12:00 AM",
}
---
# Use commit build time as updated_at when `updateTimeAsCommitBuildTime` is true and fallback to old style build_history
repos:
https://docs.com/update_time_as_commit_build_time#master:
- files:
docfx.yml: |
updateTimeAsCommitBuildTime: true
docs/index.md: Alice creates this.
state:
history/build_history_1026df13-852a-b3da-a226-492390c1c82e_17190aeb-6297-d34d-a48f-a681d3061212.json: |
{
"commits": [{ "sha": "7e6344c7275cccec2334ca04b73017bdeb7ecc2a", "built_at": "2019-04-23T00:00:00Z" }]
}
outputs:
docs/index.json: |
{
"updated_at": "2019-04-23 12:00 AM",
}
---
# Use commit build time as updated_at when `updateTimeAsCommitBuildTime` is true
repos:
https://docs.com/update_time_as_commit_build_time#master:
Expand Down Expand Up @@ -647,7 +665,7 @@ repos:
b.md: |
Alice creates this.
state:
build-history.json: |
history/build_history_95ac9e0f-677b-d6a6-13b5-a7a3118624ab_cd6b8f09-2146-73d3-cade-4e832627b4f6.json: |
{ "commits":[
{"sha":"62f395b3259c14e5b081a2b327da21b84bd2464d","built_at":"2020-02-12T00:00:00Z"},
{"sha":"42364b93484c710e471827b81bb375e1e1dd8eb7","built_at":"2020-02-11T00:00:00Z"},
Expand Down
24 changes: 20 additions & 4 deletions src/docfx/lib/git/CommitBuildTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,29 @@ public CommitBuildTimeProvider(Config config, Repository repo)
_repo = repo;
_config = config;
_commitBuildTimePath = AppData.BuildHistoryStatePath;

// Fallback to load previous path with MD5 Hash in path
var commitBuildTimePath = _commitBuildTimePath;
var potentialBuildHistoryDirectory = Path.Combine(AppData.StateRoot, "history");
if (!File.Exists(commitBuildTimePath) && Directory.Exists(potentialBuildHistoryDirectory))
{
var potentialCommitBuildTimePath = Directory.GetFiles(potentialBuildHistoryDirectory)
.Where(path => Path.GetFileName(path).StartsWith("build_history"))
.FirstOrDefault(x => Path.HasExtension(x) && string.Equals(Path.GetExtension(x), ".json", StringComparison.OrdinalIgnoreCase));

if (potentialCommitBuildTimePath != null)
{
commitBuildTimePath = potentialCommitBuildTimePath;
}
}

_buildTime = config.BuildTime ?? DateTime.UtcNow;

var exists = File.Exists(_commitBuildTimePath);
Log.Write($"{(exists ? "Using" : "Missing")} git commit build time cache file: '{_commitBuildTimePath}'");
var exists = File.Exists(commitBuildTimePath);
Log.Write($"{(exists ? "Using" : "Missing")} git commit build time cache file: '{commitBuildTimePath}'");

var commitBuildTime = exists
? ProcessUtility.ReadJsonFile<CommitBuildTime>(_commitBuildTimePath)
? ProcessUtility.ReadJsonFile<CommitBuildTime>(commitBuildTimePath)
: new CommitBuildTime();

_buildTimeByCommit = commitBuildTime.Commits.ToDictionary(item => item.Sha, item => item.BuiltAt);
Expand All @@ -38,7 +54,7 @@ public DateTime GetCommitBuildTime(string commitId)

public void Save()
{
if (!_config.UpdateCommitBuildTime || _buildTimeByCommit.ContainsKey(_repo.Commit))
if (!_config.UpdateCommitBuildTime)
{
return;
}
Expand Down

0 comments on commit d663a3c

Please sign in to comment.