From 695dd3162e02d03fa116fa39969ee6921debdd9f Mon Sep 17 00:00:00 2001 From: Andrew Oberstar Date: Fri, 21 Apr 2023 19:35:26 -0500 Subject: [PATCH] Support shallow clone/fetch Uses the new property in grgit to allow shallow fetch. Fixes #57 --- gradle.lockfile | 4 +-- .../gradle/git/publish/BaseCompatTest.groovy | 36 ++++++++++++++++++- .../gradle/git/publish/GitPublication.java | 6 ++++ .../git/publish/GitPublishExtension.java | 4 +++ .../gradle/git/publish/GitPublishPlugin.java | 1 + .../git/publish/tasks/GitPublishReset.java | 10 ++++++ 6 files changed, 58 insertions(+), 3 deletions(-) diff --git a/gradle.lockfile b/gradle.lockfile index f2eaa91..6fd7e95 100644 --- a/gradle.lockfile +++ b/gradle.lockfile @@ -4,8 +4,8 @@ cglib:cglib-nodep:3.3.0=compatTestCompileClasspath,compatTestRuntimeClasspath com.googlecode.javaewah:JavaEWAH:1.1.13=compatTestCompileClasspath,compatTestRuntimeClasspath,compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath net.bytebuddy:byte-buddy:1.11.0=compatTestCompileClasspath,compatTestRuntimeClasspath -org.ajoberstar.grgit:grgit-core:5.1.0=compatTestCompileClasspath,compatTestRuntimeClasspath,compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath -org.ajoberstar.grgit:grgit-gradle:5.1.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.ajoberstar.grgit:grgit-core:5.2.0=compatTestCompileClasspath,compatTestRuntimeClasspath,compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath +org.ajoberstar.grgit:grgit-gradle:5.2.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath org.apiguardian:apiguardian-api:1.1.0=compatTestCompileClasspath,compatTestRuntimeClasspath org.assertj:assertj-core:3.16.1=compatTestCompileClasspath,compatTestRuntimeClasspath org.codehaus.groovy:groovy:3.0.17=compatTestCompileClasspath,compatTestRuntimeClasspath,compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath diff --git a/src/compatTest/groovy/org/ajoberstar/gradle/git/publish/BaseCompatTest.groovy b/src/compatTest/groovy/org/ajoberstar/gradle/git/publish/BaseCompatTest.groovy index c36aea0..daaef94 100644 --- a/src/compatTest/groovy/org/ajoberstar/gradle/git/publish/BaseCompatTest.groovy +++ b/src/compatTest/groovy/org/ajoberstar/gradle/git/publish/BaseCompatTest.groovy @@ -92,6 +92,41 @@ gitPublish { remoteFile('content.txt').text == 'published content here' } + def 'publish with fetchDepth 1 adds to history if branch already exists'() { + given: + remote.checkout(branch: 'gh-pages') + remoteFile('index.md') << 'And has great content' + remote.add(patterns: ['.']) + remote.commit(message: 'second pages commit') + remote.checkout(branch: 'master') + + projectFile('src/content.txt') << 'published content here' + + buildFile << """ +plugins { + id 'org.ajoberstar.git-publish' +} + +gitPublish { + repoUri = '${remote.repository.rootDir.toURI()}' + branch = 'gh-pages' + fetchDepth = 1 + contents.from 'src' +} +""" + when: + def result = build() + and: + remote.checkout(branch: 'gh-pages') + then: + result.task(':gitPublishPush').outcome == TaskOutcome.SUCCESS + remote.log().size() == 3 + remoteFile('content.txt').text == 'published content here' + def working = Grgit.open(dir: "${projectDir}/build/gitPublish/main") + working.head().parentIds.collect { working.resolve.toCommit(it).parentIds == [] } + working.close() + } + def 'reset pulls from reference repo if available before pulling from remote'() { given: def referenceDir = new File(tempDir, 'reference') @@ -339,7 +374,6 @@ task hello { notThrown(UnexpectedBuildFailure) } - @IgnoreIf({ Integer.parseInt(System.properties['compat.gradle.version'].split('\\.')[0]) < 5 }) def 'commit message can be changed'() { given: projectFile('src/content.txt') << 'published content here' diff --git a/src/main/java/org/ajoberstar/gradle/git/publish/GitPublication.java b/src/main/java/org/ajoberstar/gradle/git/publish/GitPublication.java index d994705..f232b19 100644 --- a/src/main/java/org/ajoberstar/gradle/git/publish/GitPublication.java +++ b/src/main/java/org/ajoberstar/gradle/git/publish/GitPublication.java @@ -16,6 +16,7 @@ public class GitPublication implements Named { private final Property repoUri; private final Property referenceRepoUri; private final Property branch; + private final Property fetchDepth; private final Property commitMessage; private final Property sign; private final CopySpec contents; @@ -27,6 +28,7 @@ public GitPublication(String name, Project project, ObjectFactory objectFactory) this.repoUri = objectFactory.property(String.class); this.referenceRepoUri = objectFactory.property(String.class); this.branch = objectFactory.property(String.class); + this.fetchDepth = objectFactory.property(Integer.class); this.commitMessage = objectFactory.property(String.class); this.sign = objectFactory.property(Boolean.class); @@ -56,6 +58,10 @@ public Property getBranch() { return branch; } + public Property getFetchDepth() { + return fetchDepth; + } + public Property getCommitMessage() { return commitMessage; } diff --git a/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishExtension.java b/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishExtension.java index 6434840..d87b30d 100644 --- a/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishExtension.java +++ b/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishExtension.java @@ -43,6 +43,10 @@ public Property getBranch() { return publications.getByName("main").getBranch(); } + public Property getFetchDepth() { + return publications.getByName("main").getFetchDepth(); + } + public Property getCommitMessage() { return publications.getByName("main").getCommitMessage(); } diff --git a/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishPlugin.java b/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishPlugin.java index 2157506..58b1d6d 100644 --- a/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishPlugin.java +++ b/src/main/java/org/ajoberstar/gradle/git/publish/GitPublishPlugin.java @@ -73,6 +73,7 @@ private TaskProvider createResetTask(Project project, GitPublic task.getRepoUri().set(publication.getRepoUri()); task.getReferenceRepoUri().set(publication.getReferenceRepoUri()); task.getBranch().set(publication.getBranch()); + task.getFetchDepth().set(publication.getFetchDepth()); task.setPreserve(publication.getPreserve()); }); } diff --git a/src/main/java/org/ajoberstar/gradle/git/publish/tasks/GitPublishReset.java b/src/main/java/org/ajoberstar/gradle/git/publish/tasks/GitPublishReset.java index ea27655..c44224d 100644 --- a/src/main/java/org/ajoberstar/gradle/git/publish/tasks/GitPublishReset.java +++ b/src/main/java/org/ajoberstar/gradle/git/publish/tasks/GitPublishReset.java @@ -28,6 +28,7 @@ public class GitPublishReset extends DefaultTask { private final Property repoUri; private final Property referenceRepoUri; private final Property branch; + private final Property fetchDepth; private PatternFilterable preserve; private final ObjectFactory objectFactory; @@ -37,6 +38,7 @@ public GitPublishReset(ObjectFactory objectFactory) { this.repoUri = objectFactory.property(String.class); this.referenceRepoUri = objectFactory.property(String.class); this.branch = objectFactory.property(String.class); + this.fetchDepth = objectFactory.property(Integer.class); this.objectFactory = objectFactory; } @@ -60,6 +62,12 @@ public Property getBranch() { return branch; } + @Input + @Optional + public Property getFetchDepth() { + return fetchDepth; + } + @Internal public PatternFilterable getPreserve() { return preserve; @@ -118,6 +126,7 @@ public void reset() throws IOException { git.fetch(op -> { op.setRefSpecs(Arrays.asList(String.format("+refs/heads/%s:refs/remotes/reference/%s", pubBranch, pubBranch))); op.setTagMode("none"); + op.setDepth(getFetchDepth().getOrNull()); }); } } @@ -136,6 +145,7 @@ public void reset() throws IOException { getLogger().info("Fetching from remote repo: " + repoUri.get()); op.setRefSpecs(Arrays.asList(String.format("+refs/heads/%s:refs/remotes/origin/%s", pubBranch, pubBranch))); op.setTagMode("none"); + op.setDepth(getFetchDepth().getOrNull()); }); // make sure local branch exists