| 1 | #!/bin/sh |
| 2 | # |
| 3 | # get-vcs-id |
| 4 | # |
| 5 | # Copyright © 2009-2024 Guillem Jover <[email protected]> |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | |
| 21 | error() |
| 22 | { |
| 23 | echo "get-vcs-id: error: $*" 1>&2 |
| 24 | exit 1 |
| 25 | } |
| 26 | |
| 27 | if [ -f .dist-vcs-id ]; then |
| 28 | # Get the VCS id from the file distributed in the tarball. |
| 29 | id="$(cat .dist-vcs-id)" |
| 30 | elif [ -d .git ] || [ -f .git ]; then |
| 31 | if ! command -v git >/dev/null; then |
| 32 | error "cannot get project VCS id, git checkout but git program not found" |
| 33 | fi |
| 34 | |
| 35 | # Get the VCS id from the git repository. |
| 36 | id="$(git rev-list -1 HEAD 2>/dev/null)" |
| 37 | |
| 38 | # Check if we are on a dirty checkout. |
| 39 | git update-index --refresh -q >/dev/null |
| 40 | dirty=$(git diff-index --name-only HEAD 2>/dev/null) |
| 41 | if [ -n "$dirty" ]; then |
| 42 | id="$id-dirty" |
| 43 | fi |
| 44 | else |
| 45 | error "cannot get project VCS id, not a git checkout nor a distribution tarball." |
| 46 | fi |
| 47 | |
| 48 | # Use printf to avoid the trailing new line that m4_esyscmd would not handle. |
| 49 | printf "%s" "$id" |