获取 git 目录中文件的最后修改日期

Get last modification dates of files in directory in git

我有一个装满文件的文件夹,我想获取每个文件最后 git 更新的时间戳。

我想在 Gradle 任务中获得这些。

我用 GrGit 尝试了以下操作:

def git = org.ajoberstar.grgit.Grgit.open dir:project.rootDir

task showGit() {
    doFirst {
        file( "$project.rootDir/src/main/java/some/folder" ).listFiles().each{ f ->
            git.log( includes:[ 'HEAD' ], paths:[ f.name ] ).each{
                println "$f.name -> Author: $it.author.name - Date: ${it.date.format( 'dd.MM.yyyy HH:mm' )}"
            }
        }
    }
}

但它什么也不打印。

如果我像这样省略 paths

task showGit() {
    doFirst {
         git.log( includes:[ 'HEAD' ] ).each{
           println "Author: $it.author.name - Date: ${it.date.format( 'dd.MM.yyyy HH:mm' )}"
        }
    }
}

它打印整个目录的所有提交信息。

如何获取每个文件的时间戳?

事实证明这很容易。

受到How to get the last commit date for a bunch of files in Git?的启发,我整理了自己的 GrGit 任务:

def git = org.ajoberstar.grgit.Grgit.open dir:project.rootDir

task lastGitUpdated() {
    doFirst {
        int base = project.rootDir.toURI().toString().size()

        def dates = file( "$project.rootDir/src/main/java/some/dir" ).listFiles().collect{
            git.log( includes:[ 'HEAD' ], paths:[ it.toURI().toString().substring( base ) ], maxCommits:1 )[ 0 ].date
        }
    }
}

它就像一个魅力!

唯一有点令人失望的是,包含 ~150 个文件的目录中的任务需要 ~2 分钟才能完成...