n0mer / gradle-git-properties

Gradle plugin for `git.properties` file generation
https://plugins.gradle.org/plugin/com.gorylenko.gradle-git-properties
Apache License 2.0
321 stars 54 forks source link

`generateGitProperties` fails when Git's `core.fsmonitor` is enabled #230

Open henrik242 opened 7 months ago

henrik242 commented 7 months ago

The generateGitProperties task will fail if .git/config contains core.fsmonitor = true:

[core]
    fsmonitor = true

The error is:

Execution failed for task ':my-project:generateGitProperties'.
> Cannot access input property 'source' of task ':fhh-integration:generateGitProperties'. Accessing 
  unreadable inputs or outputs is not supported. Declare the task as untracked by using
  Task.doNotTrackState(). For more information, please refer to
  https://docs.gradle.org/8.6/userguide/incremental_build.html#sec:disable-state-tracking
  in the Gradle documentation.
   > java.io.IOException: Cannot snapshot /Users/me/src/my-project/.git/fsmonitor--daemon.ipc:
     not a regular file

Should be solved by ignoring .git/fsmonitor--daemon.ipc (or generally all sockets) by default.

henrik242 commented 7 months ago

@n0mer Is there a way to exclude .git/fsmonitor--daemon.ipc in the current implementation?

henrik242 commented 2 months ago

I have worked around this by ditching this plugin and using an Exec task with a bash script instead.

Bash script:

#!/usr/bin/env bash

# Replacement for the gradle-git-properties plugin since it clashes with git.fsmonitor,
# see https://github.com/n0mer/gradle-git-properties/issues/230

branch=$(git branch --show-current)
host=$(hostname)
user_email=$(git config user.email)
user_name=$(git config user.name)
version="1.0.0-SNAPSHOT"
describe=$(git describe --tags --long)
closest_tag_name=$(git describe --tags)
closest_tag_commit_count=$(git rev-list $(git describe --tags --abbrev=0)..HEAD --count)
commit_id=$(git rev-parse HEAD)
commit_id_abbrev=$(git rev-parse --short HEAD)
commit_message_full=$(git log -1 --pretty=%B | cut -c1-100 | jq -Rsa)
commit_message_short=$(git log -1 --pretty=%s)
commit_time=$(git log -1 --date=format-local:%Y-%m-%dT%H:%M:%S%z --pretty=format:%cd)
commit_user_email=$(git log -1 --pretty=%ce)
commit_user_name=$(git log -1 --pretty=%cn)
dirty=$(git diff-index --quiet HEAD -- || echo "true")
[ -z "$dirty" ] && dirty="false"
remote_origin_url=$(git config --get remote.origin.url)
tags=$(git tag --points-at HEAD)
total_commit_count=$(git rev-list --count HEAD)

cat <<EOL > git.properties
git.branch=${branch}
git.build.host=${host}
git.build.user.email=${user_email}
git.build.user.name=${user_name}
git.build.version=${version}
git.closest.tag.commit.count=${closest_tag_commit_count}
git.closest.tag.name=${closest_tag_name}
git.commit.id=${commit_id}
git.commit.id.abbrev=${commit_id_abbrev}
git.commit.id.describe=${describe}
git.commit.message.full=${commit_message_full}
git.commit.message.short=${commit_message_short}
git.commit.time=${commit_time}
git.commit.user.email=${commit_user_email}
git.commit.user.name=${commit_user_name}
git.dirty=${dirty}
git.remote.origin.url=${remote_origin_url}
git.tags=${tags}
git.total.commit.count=${total_commit_count}
EOL

Gradle task:

val generateGitProperties by tasks.registering(Exec::class) {
  description = "Generates git.properties file with git information"
  group = "Build"
  workingDir = project.layout.buildDirectory.dir("resources/main").get().asFile
  outputs.file(project.layout.buildDirectory.file("resources/main/git.properties"))
  commandLine("$rootDir/tools/utils/generate-git-properties.sh")
}
named("assemble") { dependsOn(generateGitProperties) }