@@ -30,6 +30,9 @@ import static org.eclipse.jgit.submodule.SubmoduleWalk.getSubmoduleRepository
30
30
31
31
import net.minecrell.gitpatcher.git.MailPatch
32
32
import org.eclipse.jgit.api.Git
33
+ import org.eclipse.jgit.api.errors.InvalidRemoteException
34
+ import org.eclipse.jgit.transport.RemoteConfig
35
+ import org.eclipse.jgit.transport.URIish
33
36
import org.gradle.api.tasks.InputFiles
34
37
import org.gradle.api.tasks.OutputDirectory
35
38
import org.gradle.api.tasks.OutputFile
@@ -65,12 +68,15 @@ class ApplyPatchesTask extends PatchTask {
65
68
.call()
66
69
}
67
70
71
+ def gitDir = new File (repo, ' .git' )
68
72
Git git
69
- if (repo . isDirectory()) {
73
+ if (gitDir . isDirectory() && gitDir . list() . length > 0 ) {
70
74
git = Git . open(repo)
71
75
} else {
72
76
logger. lifecycle ' Creating {} repository...' , repo
73
77
78
+ assert repo. deleteDir()
79
+
74
80
git = Git . cloneRepository()
75
81
.setURI(source. toURI(). toString())
76
82
.setDirectory(repo)
@@ -80,7 +86,21 @@ class ApplyPatchesTask extends PatchTask {
80
86
withGit(git) {
81
87
logger. lifecycle ' Resetting {}...' , repo
82
88
83
- fetch(). setRemote(' origin' ). call()
89
+ try {
90
+ fetch(). setRemote(' origin' ). call()
91
+ } catch (InvalidRemoteException e) {
92
+ // Reset remote (maybe it was changed or something)
93
+ def config = new RemoteConfig (repository. getConfig(), ' origin' )
94
+ for (URIish uri : config. getURIs(). toArray()) {
95
+ config. removeURI(uri)
96
+ }
97
+ config. addURI(new URIish (source. toURI(). toString()))
98
+ config. update(repository. getConfig())
99
+
100
+ // Fetch again
101
+ fetch(). setRemote(' origin' ). call()
102
+ }
103
+
84
104
85
105
if (repository. getRef(' master' ) == null ) {
86
106
// Create the master branch
0 commit comments