我正在使用gradle-git。我已经定义了两个任务如下:
PrepareTask.groovy
class PrepareTask extends DefaultTask {
public String branch
private Grgit git
@TaskAction
public void executeTask(){
try {
git = Grgit.open(dir: project.rootProject.projectDir)
} catch (RepositoryNotFoundException e) {
println("Git is not available in this project.")
}
if (git){
def currentBranch = git.branch.getCurrent().getName()
if (currentBranch != branch){
git.checkout(branch: branch, createBranch: true)
}
}
}
}
CommitTask.groovy
class CommitTask extends DefaultTask {
public String prev_branch
public String message = "committing changes"
private Grgit git
@TaskAction
public void executeTask(){
try {
git = Grgit.open(dir: project.rootProject.projectDir)
} catch (RepositoryNotFoundException e) {
println("Git is not available in this project.")
}
if (git){
git.add(patterns: ['.'])
def commit = git.commit(message: message, all: true)
if (prev_branch){
def branch = git.branch.getCurrent().getName()
git.checkout(branch: prev_branch, createBranch: false)
git.merge(head: branch, mode: MergeOp.Mode.CREATE_COMMIT, message: "merging")
}
}
}
}
我在gradle中有一个updateSource
任务工作正常,我在 build.gradle 中设置了以下内容:
task prepare(type: PrepareTask){
branch = 'changes-branch'
}
task commit(type: CommitTask){
prev_branch = 'current-branch'
}
updateSource.dependsOn prepare
updateSource.finalizedBy commit
一切都在执行,新分支被创建和检出,源代码得到更新和提交,然后原始分支再次被检出。但是当任务尝试合并分支时,它会失败:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':commit'.
> Problem merging.
添加--stacktrace
选项会显示:
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':commit'.
...
Caused by: org.ajoberstar.grgit.exception.GrgitException: Problem merging.
...
Caused by: org.eclipse.jgit.api.errors.CheckoutConflictException: Checkout conflict with files:
但由于两个原因,这没有任何意义。结账很顺利,2。我从一个干净的分支开始。