-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgitCommands.gradle
More file actions
112 lines (93 loc) · 3.39 KB
/
gitCommands.gradle
File metadata and controls
112 lines (93 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.regex.Matcher
import java.util.regex.Pattern
/**
* Provide git commands which can be used in gradle tasks.
* @author Marcel Schlegel (schlegel11)
*/
ext.git = [
checkoutBranch : { branch ->
gitCommand("checkout", branch)
},
createBranch : { branch ->
gitCommand("checkout", "-b", branch)
},
createEmptyBranch : { branch ->
gitCommand("checkout", "--orphan", branch)
gitCommand("rm", "-rf", '.')
},
switchCreateBranch : { branch, closure = {} ->
def lastBranch = git.getCurrentBranch()
git.createOrCheckoutBranch(branch)
def returnValue = closure(lastBranch)
git.checkoutBranch(lastBranch)
returnValue
},
switchCreateEmptyBranch : { branch, closure = {} ->
def lastBranch = git.getCurrentBranch()
git.createEmptyOrCheckoutBranch(branch)
def returnValue = closure(lastBranch)
git.checkoutBranch(lastBranch)
returnValue
},
createOrCheckoutBranch : { branch ->
git.hasBranch(branch) ? git.checkoutBranch(branch) : git.createBranch(branch)
},
createEmptyOrCheckoutBranch: { branch ->
git.hasBranch(branch) ? git.checkoutBranch(branch) : git.createEmptyBranch(branch)
},
add : { ... files ->
gitCommand("add", files.join(','))
},
commit : { message ->
gitCommand("commit", "-m", message)
},
getCurrentBranch : { ->
gitCommand("branch").find('(?<=[*]{1}).*').trim()
},
hasBranch : { branch ->
def result = gitCommand("branch")
Pattern p = Pattern.compile("(${branch}\\b)")
Matcher m = p.matcher(result)
m.find()
},
hasFiles : { ... files ->
try {
gitCommand("ls-files", "--error-unmatch", files.join(','))
} catch (ignored) {
return false
}
return true
},
getModifiedFiles : { ... paths ->
gitCommand("ls-files", "--modified", paths.join(','))
},
hasFilesUpToDate : { ... files ->
git.hasFiles(files) && git.getModifiedFiles(files).isEmpty()
},
createTag : { name, message ->
gitCommand('tag', '-a', name, '-m', message)
},
hasTag : { tag ->
def result = gitCommand("tag")
Pattern p = Pattern.compile("(${tag}\\b)")
Matcher m = p.matcher(result)
m.find()
}
]
private String gitCommand(String... commands) {
runCommands(git.processDir, (['git'] + commands).flatten() as String[])
}
/**
* Credits to Oleksandr Bondarchuk
*/
private static String runCommands(File processDir, String... commands) {
def process = new ProcessBuilder(commands).directory(processDir).redirectErrorStream(true).start()
process.waitFor()
def result = ''
process.inputStream.eachLine { result += it + '\n' }
def errorResult = process.exitValue() == 0
if (!errorResult) {
throw new IllegalStateException(result)
}
return result
}