'Get last modification dates of files in directory in git

I have a folder full of files and I want to get the timestamps of last git update for each of those files.

I'd like to get those in a Gradle task.

I tried the following with 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' )}"
            }
        }
    }
}

but it prints nothing.

If I ommit the paths like so:

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

it prints all commit infos for the whole dir.

How to get the timestamps for each file?



Solution 1:[1]

It turns out it was fairly easy.

Inspired by How to get the last commit date for a bunch of files in Git? I put together my own GrGit task:

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
        }
    }
}

and it works like a charm!

The only thing which is slightly dissappointing is, that the tasks on a dir with ~150 files takes ~2 mins to complete...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 injecteer