get the last modification data of a file in git repo

前端 未结 3 2072
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 06:59

Im trying to find the command to get the last modified date of a file in a local git repo.

Created the repo and have done one commit. Had to edit one file and will commi

3条回答
  •  误落风尘
    2021-02-04 07:14

    MitchellK's answer exactly fit my needs, setting my local files' last written times to what's in git. Here's a little C# LinqPad script to automate the process:

    var root = new DirectoryInfo(@"C:\gitlab\mydirectory\");
    
    Directory.SetCurrentDirectory(root.FullName); // Give git some context
    
    var files = root.GetFiles("*.*", SearchOption.AllDirectories);
    
    foreach (var file in files)
    {
      var results = Util.Cmd("git", 
                             $"log -1 --pretty=\"format:%ci\" \"{file.FullName}\"",
                             true);
      
      var lastUpdatedString = results.FirstOrDefault();
      if (lastUpdatedString == null)
      {
        Console.WriteLine($"{file.FullName} did not have a last updated date!!");
        continue;
      }
      
      var dt = DateTimeOffset.Parse(lastUpdatedString);
      if (file.LastWriteTimeUtc != dt.UtcDateTime)
      {
        Console.WriteLine($"{file.FullName}, {file.LastWriteTimeUtc} => {dt.UtcDateTime}");
        file.LastWriteTimeUtc = dt.UtcDateTime;
      }
      else
      {
        Console.WriteLine($"{file.FullName} already updated.");
      }
    }
    

提交回复
热议问题