|
| 1 | +/* |
| 2 | + * GradleUtils |
| 3 | + * Copyright (C) 2021 Forge Development LLC |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + * USA |
| 19 | + */ |
| 20 | + |
| 21 | +package net.minecraftforge.gradleutils |
| 22 | + |
| 23 | +import org.eclipse.jgit.api.Git |
| 24 | +import org.eclipse.jgit.errors.RepositoryNotFoundException |
| 25 | +import org.eclipse.jgit.lib.ObjectId |
| 26 | +import org.eclipse.jgit.lib.Repository |
| 27 | + |
| 28 | +class GradleUtils { |
| 29 | + static { |
| 30 | + String.metaClass.rsplit = { String del, int limit = -1 -> |
| 31 | + def lst = new ArrayList() |
| 32 | + def x = 0, idx |
| 33 | + def tmp = delegate |
| 34 | + while ((idx = tmp.lastIndexOf(del)) != -1 && (limit == -1 || x++ < limit)) { |
| 35 | + lst.add(0, tmp.substring(idx + del.length(), tmp.length())) |
| 36 | + tmp = tmp.substring(0, idx) |
| 37 | + } |
| 38 | + lst.add(0, tmp) |
| 39 | + return lst |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static gitInfo(dir) { |
| 44 | + def git = null |
| 45 | + try { |
| 46 | + git = Git.open(dir) |
| 47 | + } catch (RepositoryNotFoundException e) { |
| 48 | + return [ |
| 49 | + tag: '0.0', |
| 50 | + offset: '0', |
| 51 | + hash: '00000000', |
| 52 | + branch: 'master', |
| 53 | + commit: '0000000000000000000000', |
| 54 | + abbreviatedId: '00000000' |
| 55 | + ] |
| 56 | + } |
| 57 | + def desc = git.describe().setLong(true).setTags(true).call().rsplit('-', 2) |
| 58 | + def head = git.repository.exactRef('HEAD') |
| 59 | + def longBranch = head.symbolic ? head?.target?.name : null // matches Repository.getFullBranch() but returning null when on a detached HEAD |
| 60 | + |
| 61 | + def ret = [:] |
| 62 | + ret.tag = desc[0] |
| 63 | + ret.offset = desc[1] |
| 64 | + ret.hash = desc[2] |
| 65 | + ret.branch = longBranch != null ? Repository.shortenRefName(longBranch) : null |
| 66 | + ret.commit = ObjectId.toString(head.objectId) |
| 67 | + ret.abbreviatedId = head.objectId.abbreviate(8).name() |
| 68 | + |
| 69 | + return ret |
| 70 | + } |
| 71 | + |
| 72 | + static String getSimpleVersion(info) { |
| 73 | + return "${info.tag}.${info.offset}" |
| 74 | + } |
| 75 | +} |
0 commit comments