From e90114b0122a8b8338b472a0c4d475a9fd3eb66f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 30 Oct 2013 20:45:16 -0400 Subject: rename libregit to gitget, add bare repository support --- Makefile | 4 +- src/gitget/Makefile | 1 + src/gitget/gitget | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/gitget/libregit | 40 +++++++++++++ src/libregit/Makefile | 1 - src/libregit/libregit | 74 ------------------------ 6 files changed, 200 insertions(+), 77 deletions(-) create mode 100644 src/gitget/Makefile create mode 100755 src/gitget/gitget create mode 100755 src/gitget/libregit delete mode 100644 src/libregit/Makefile delete mode 100755 src/libregit/libregit diff --git a/Makefile b/Makefile index dc56eb6..feff83f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ include config.mk ################################################################################ # these are the resulting packages -packages=doc libretools libretools-mips64el librelib libregit +packages=doc libretools libretools-mips64el librelib gitget # and which directories they contain doc=doc libretools=\ @@ -17,7 +17,7 @@ libretools=\ src/toru libretools-mips64el=src/mips64el-tools librelib=src/lib -libregit=src/libregit +gitget=src/gitget ################################################################################ diff --git a/src/gitget/Makefile b/src/gitget/Makefile new file mode 100644 index 0000000..2c76089 --- /dev/null +++ b/src/gitget/Makefile @@ -0,0 +1 @@ +include ../../common.mk diff --git a/src/gitget/gitget b/src/gitget/gitget new file mode 100755 index 0000000..0ec959b --- /dev/null +++ b/src/gitget/gitget @@ -0,0 +1,157 @@ +#!/usr/bin/env bash + +# Copyright (c) 2012-2013 Pacman Development Team +# Copyright (c) 2012-2013 Luke Shumaker +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. libremessages + +# from makepkg +dir_is_empty() { + ( + shopt -s dotglob nullglob + files=("$1"/*) + (( ${#files} == 0 )) + ) +} + +# from makepkg +cd_safe() { + if ! cd "$1"; then + error "Failed to change to directory %s" "$1" + plain "Aborting..." + exit 1 + fi +} + +# from makepkg +download_git_checkout() ( + local url=$1 + local ref=$2 + local dir=$3 + local name=${4:-${dir##*/}} + + if [[ ! -d "$dir/.git" ]] ; then + msg2 "Cloning %s %s repo..." "${name}" "git" + if ! git clone "$url" "$dir"; then + error "Failure while downloading %s %s repo" "${name}" "git" + plain "Aborting..." + exit 1 + fi + cd_safe "$dir" + git checkout "$ref" + else + cd_safe "$dir" + # Make sure we are fetching the right repo + if [[ "$repo" != "$(git config --get remote.origin.url)" ]] ; then + error "%s is not a clone of %s" "$dir" "$url" + plain "Aborting..." + exit 1 + fi + msg2 "Updating %s %s repo..." "${name}" "git" + if ! git pull origin "$ref"; then + # only warn on failure to allow offline builds + warning "Failure while updating %s %s repo" "${repo}" "git" + fi + fi +) + +# from makepkg +download_git_bare() ( + local url=$1 + local dir=$2 + local name=${3:-${dir##*/}} + + if [[ ! -d "$dir" ]] || dir_is_empty "$dir" ; then + msg2 "Cloning %s %s repo..." "${name}" "git" + if ! git clone --mirror "$url" "$dir"; then + error "Failure while downloading %s %s repo" "${name}" "git" + plain "Aborting..." + exit 1 + fi + else + cd_safe "$dir" + # Make sure we are fetching the right repo + if [[ "$url" != "$(git config --get remote.origin.url)" ]] ; then + error "%s is not a clone of %s" "$dir" "$url" + plain "Aborting..." + exit 1 + fi + msg2 "Updating %s %s repo..." "${name}" "git" + if ! git fetch --all -p; then + # only warn on failure to allow offline builds + warning "Failure while updating %s %s repo" "${name}" "git" + fi + fi +) + +usage() { + print 'Usage: %s [OPTIONS] bare URL DIRECTORY' "${0##*/}" + print 'Usage: %s [OPTIONS] checkout URL DIRECTORY' "${0##*/}" + print 'A URL-handler for git urls. Capable of updating or cloning.' + echo + prose "Clones or pulls from the git URL, to a local DIRECTORY. If + \`bare\` is specified, it will create a bare repository; if + \`checkout\` is specified, it will create or update a working tree." + echo + prose 'For a checkout, the tree to checkout is specified by appending + `#ANYTHING=TREE-ISH` to the URL. For example, `#branch=stable`, + or `#tag=v12.3`. Whatever is on the left side of the equal sign + is ignored, this is for compatability with `makepkg` source + URLs.' + echo + prose "The URL may be prefixed with \`git+\`. This is also for + compatability with \`makepkg\` source URLs." + echo + prose "It does safety checks, figures out whether to clone or pull, and + other helpful things. This exists because the same + \`download_git\` function from makepkg was being copied and + pasted again and again." + echo + print "Options:" + flag "-n $(_ NAME)" \ + 'In messages, instead of using the basename of DIRECTORY as the + repository name, use NAME' + flag '-h' 'Show this message' +} + +main() { + local name='' + while getopts 'n:h' flag; do + case "${flag}" in + n) name=$OPTARG;; + h) usage; return 0;; + *) usage >&2; return 1;; + esac + done + [[ $# == 3 ]] || { usage >&2; return 1; } + local mode=$1 + local url=${2#git+} + local dir=$3 + + local urlmain=${url%%#*} + local urlfrag=${url#*#} + local ref=${urlfrag#*=} + + name=${name:-${dir##*/}} + + case "$mode" in + checkout) download_git_checkout "$urlmain" "$ref" "$dir" "$name";; + bare) download_git_bare "$urlmain" "$dir" "$name";; + *) usage >&2; return 1;; + esac +} + +main "$@" diff --git a/src/gitget/libregit b/src/gitget/libregit new file mode 100755 index 0000000..f89a402 --- /dev/null +++ b/src/gitget/libregit @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +# Copyright (c) 2012-2013 Luke Shumaker +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. libremessages + +usage() { + print 'Usage: %s repo ref dir' "${0##*/}" + print 'A compatability wrapper around `gitget checkout`' + echo + prose "This exists because gitget used to be called libregit, and took + the arguments in this format, and I'm sure there are a few + scripts floating around that use it." + echo + prose "Clones or pulls from the git URL 'repo', and checks out the git + ref 'ref' to the directory 'dir'." +} + + +main() { + [[ $# == 3 ]] || { usage >&2; return 1; } + repo=$1 + ref=$2 + dir=$3 + + gitget checkout "${repo}#ref=${ref}" "${dir}" +} diff --git a/src/libregit/Makefile b/src/libregit/Makefile deleted file mode 100644 index 2c76089..0000000 --- a/src/libregit/Makefile +++ /dev/null @@ -1 +0,0 @@ -include ../../common.mk diff --git a/src/libregit/libregit b/src/libregit/libregit deleted file mode 100755 index 8687d2f..0000000 --- a/src/libregit/libregit +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env bash - -# Copyright (c) 2012-2013 Pacman Development Team -# Copyright (c) 2012-2013 Luke Shumaker -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -. libremessages - -cd_safe() { - if ! cd "$1"; then - error "Failed to change to directory %s" "$1" - plain "Aborting..." - exit 1 - fi -} - -download_git() { - if [[ ! -d "$dir/.git" ]] ; then - msg2 "Cloning %s %s repo..." "${repo}" "git" - if ! git clone "$repo" "$dir"; then - error "Failure while downloading %s %s repo" "${repo}" "git" - plain "Aborting..." - exit 1 - fi - else - cd_safe "$dir" - # Make sure we are fetching the right repo - if [[ "$repo" != "$(git config --get remote.origin.url)" ]] ; then - error "%s is not a clone of %s" "$dir" "$repo" - plain "Aborting..." - exit 1 - fi - msg2 "Updating %s %s repo..." "${repo}" "git" - if ! git pull origin "$ref"; then - # only warn on failure to allow offline builds - warning "Failure while updating %s %s repo" "${repo}" "git" - fi - fi -} - -usage() { - print 'Usage: %s repo ref dir' "${0##*/}" - echo - print "Clones or pulls from the git URL 'repo', and checks out the git ref" - print "'ref' to the directory 'dir'." - echo - print "It does safety checks, figures out whether to clone or pull, and other" - print "helpful things. This exists because the same 'download_git' function" - print "from makepkg was being copied again and again." -} - -main() { - [[ $# == 3 ]] || { usage >&2; return 1; } - repo=$1 - ref=$2 - dir=$3 - - [[ -d "${dir%/*}" ]] || mkdir -p "${dir%/*}" - download_git -} - -main "$@" -- cgit v1.2.2