Skip to content

Commit

Permalink
fix(tasks): Push task also handles empty repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mulier-p committed May 15, 2024
1 parent 539c5f3 commit 7314918
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
40 changes: 30 additions & 10 deletions src/main/java/io/kestra/plugin/git/AbstractPushTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.Edit;
import org.eclipse.jgit.diff.EditList;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.slf4j.Logger;

import java.io.*;
Expand Down Expand Up @@ -87,10 +90,10 @@ private Path createGitDirectory(RunContext runContext) throws IllegalVariableEva
private void deleteOutdatedResources(Git git, Path basePath, Map<Path, Supplier<InputStream>> contentByPath, List<String> regexes) throws IOException, GitAPIException {
try (Stream<Path> paths = Files.walk(basePath)) {
Stream<Path> filteredPathsStream = paths.filter(path ->
!contentByPath.containsKey(path) &&
!path.getFileName().toString().equals(".git") &&
!path.equals(basePath)
);
!contentByPath.containsKey(path) &&
!path.getFileName().toString().equals(".git") &&
!path.equals(basePath)
);

if (regexes != null) {
filteredPathsStream = filteredPathsStream.filter(path -> regexes.stream().anyMatch(regex ->
Expand Down Expand Up @@ -125,12 +128,15 @@ protected void writeResourceFile(Path path, InputStream inputStream) throws IOEx

private static URI createDiffFile(RunContext runContext, Git git) throws IOException, GitAPIException {
File diffFile = runContext.tempFile(".ion").toFile();
try(DiffFormatter diffFormatter = new DiffFormatter(null);
BufferedWriter diffWriter = new BufferedWriter(new FileWriter(diffFile))) {
try (DiffFormatter diffFormatter = new DiffFormatter(null);
BufferedWriter diffWriter = new BufferedWriter(new FileWriter(diffFile))) {
diffFormatter.setRepository(git.getRepository());

git.diff().setCached(true).call().stream()
.sorted(Comparator.comparing(AbstractPushTask::getPath))
git.diff()
.setOldTree(treeIterator(git, "HEAD~1"))
.setNewTree(treeIterator(git, "HEAD"))
.call()
.stream().sorted(Comparator.comparing(AbstractPushTask::getPath))
.map(throwFunction(diffEntry -> {
EditList editList = diffFormatter.toFileHeader(diffEntry).toEditList();
int additions = 0;
Expand Down Expand Up @@ -168,6 +174,16 @@ private static URI createDiffFile(RunContext runContext, Git git) throws IOExcep
return runContext.storage().putFile(diffFile);
}

private static CanonicalTreeParser treeIterator(Git git, String ref) throws IOException {
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser treeIter = new CanonicalTreeParser();
ObjectId oldTree = git.getRepository().resolve(ref + "^{tree}");
if (oldTree != null) {
treeIter.reset(reader, oldTree);
}
return treeIter;
}

private static String getPath(DiffEntry diffEntry) {
return diffEntry.getChangeType() == DiffEntry.ChangeType.DELETE ? diffEntry.getOldPath() : diffEntry.getNewPath();
}
Expand All @@ -194,12 +210,16 @@ private Output push(Git git, RunContext runContext, GitService gitService) throw
);

String message = runContext.render(this.getCommitMessage());
ObjectId head = git.getRepository().resolve(Constants.HEAD);
commit = git.commit()
.setAllowEmpty(false)
.setMessage(message)
.setAuthor(author(runContext))
.call()
.getId();
if (head == null) {
git.branchRename().setNewName(renderedBranch).call();
}
authentified(git.push(), runContext).call();

commitId = commit.getName();
Expand Down Expand Up @@ -265,10 +285,10 @@ public O run(RunContext runContext) throws Exception {
add.addFilepattern(runContext.render(this.getGitDirectory()));
add.call();

URI diffFileStorageUri = AbstractPushTask.createDiffFile(runContext, git);

Output pushOutput = this.push(git, runContext, gitService);

URI diffFileStorageUri = AbstractPushTask.createDiffFile(runContext, git);

git.close();

return output(pushOutput, diffFileStorageUri);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/kestra/plugin/git/services/GitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.kestra.plugin.git.Clone;
import lombok.AllArgsConstructor;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -43,7 +44,7 @@ public Git cloneBranch(RunContext runContext, String branch, Boolean withSubmodu


Git git = Git.open(runContext.tempDir().toFile());
if (!branchExists) {
if (!branchExists && git.getRepository().resolve(Constants.HEAD) != null) {
git.checkout()
.setName(branch)
.setCreateBranch(true)
Expand Down

0 comments on commit 7314918

Please sign in to comment.