|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Minecrell <https://github.com/Minecrell> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | +package net.minecrell.gitpatcher |
| 23 | + |
| 24 | +class Git { |
| 25 | + |
| 26 | + File repo |
| 27 | + |
| 28 | + Git(File repo) { |
| 29 | + setRepo(repo) |
| 30 | + } |
| 31 | + |
| 32 | + void setRepo(File repo) { |
| 33 | + this.repo = repo |
| 34 | + assert repo.exists() |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + Command invokeMethod(String name, Object input) { |
| 39 | + return new Command(['git', name.replace('_' as char, '-' as char), *input].execute(null as String[], repo)) |
| 40 | + } |
| 41 | + |
| 42 | + static class Command { |
| 43 | + |
| 44 | + final Process process |
| 45 | + |
| 46 | + private Command(Process process) { |
| 47 | + this.process = process; |
| 48 | + } |
| 49 | + |
| 50 | + int run() { |
| 51 | + def result = process.waitFor() |
| 52 | + return result |
| 53 | + } |
| 54 | + |
| 55 | + void execute() { |
| 56 | + def result = run() |
| 57 | + assert result == 0, 'Process returned error code' |
| 58 | + } |
| 59 | + |
| 60 | + void setup(OutputStream out, OutputStream err) { |
| 61 | + if (out) { |
| 62 | + process.consumeProcessOutputStream(out) |
| 63 | + } |
| 64 | + if (err) { |
| 65 | + process.consumeProcessErrorStream(err) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + void writeTo(OutputStream out) { |
| 70 | + setup(out, System.err) |
| 71 | + execute() |
| 72 | + } |
| 73 | + |
| 74 | + void forceWriteTo(OutputStream out) { |
| 75 | + setup(out, out) |
| 76 | + run() |
| 77 | + } |
| 78 | + |
| 79 | + def rightShift = this.&writeTo |
| 80 | + def rightShiftUnsigned = this.&forceWriteTo |
| 81 | + |
| 82 | + Object asType(Class type) { |
| 83 | + if (type == String) { |
| 84 | + setup(null, System.err) |
| 85 | + execute() |
| 86 | + return process.getInputStream().text |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments