Skip to content

Commit

Permalink
Support shallow clone/fetch
Browse files Browse the repository at this point in the history
Uses the new property in grgit to allow shallow fetch.

Fixes #57
  • Loading branch information
ajoberstar committed Apr 22, 2023
1 parent 16a8de9 commit 73370cf
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ gitPublish {
// branch will be created if it doesn't exist
branch = 'gh-pages'
// if set, a shallow clone will be performed instead of pulling all history
fetchDepth = null
// generally, you don't need to touch this
repoDir = file("$buildDir/somewhereelse") // defaults to $buildDir/gitPublish
Expand Down
4 changes: 2 additions & 2 deletions gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class GitPublication implements Named {
private final Property<String> repoUri;
private final Property<String> referenceRepoUri;
private final Property<String> branch;
private final Property<Integer> fetchDepth;
private final Property<String> commitMessage;
private final Property<Boolean> sign;
private final CopySpec contents;
Expand All @@ -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);

Expand Down Expand Up @@ -56,6 +58,10 @@ public Property<String> getBranch() {
return branch;
}

public Property<Integer> getFetchDepth() {
return fetchDepth;
}

public Property<String> getCommitMessage() {
return commitMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public Property<String> getBranch() {
return publications.getByName("main").getBranch();
}

public Property<Integer> getFetchDepth() {
return publications.getByName("main").getFetchDepth();
}

public Property<String> getCommitMessage() {
return publications.getByName("main").getCommitMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private TaskProvider<GitPublishReset> 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());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class GitPublishReset extends DefaultTask {
private final Property<String> repoUri;
private final Property<String> referenceRepoUri;
private final Property<String> branch;
private final Property<Integer> fetchDepth;
private PatternFilterable preserve;
private final ObjectFactory objectFactory;

Expand All @@ -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;
}

Expand All @@ -60,6 +62,12 @@ public Property<String> getBranch() {
return branch;
}

@Input
@Optional
public Property<Integer> getFetchDepth() {
return fetchDepth;
}

@Internal
public PatternFilterable getPreserve() {
return preserve;
Expand Down Expand Up @@ -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());
});
}
}
Expand All @@ -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
Expand Down

0 comments on commit 73370cf

Please sign in to comment.