mulle-bootstrap-tag.sh
5de2fbd6
 #! /bin/sh
 #
 #   Copyright (c) 2015 Nat! - Mulle kybernetiK
 #   All rights reserved.
 #
 #   Redistribution and use in source and binary forms, with or without
 #   modification, are permitted provided that the following conditions are met:
 #
 #   Redistributions of source code must retain the above copyright notice, this
 #   list of conditions and the following disclaimer.
 #
 #   Redistributions in binary form must reproduce the above copyright notice,
 #   this list of conditions and the following disclaimer in the documentation
 #   and/or other materials provided with the distribution.
 #
 #   Neither the name of Mulle kybernetiK nor the names of its contributors
 #   may be used to endorse or promote products derived from this software
 #   without specific prior written permission.
 #
 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 #   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 #   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 #   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 #   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 #   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 #   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 #   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 #   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 #   POSSIBILITY OF SUCH DAMAGE.
 
 
 # tag this project, and all cloned dependencies
 # the dependencies will get a different vendor tag
 # based on the tag
 #
 
 . mulle-bootstrap-local-environment.sh
d7f96955
 . mulle-bootstrap-scripts.sh
3be635de
 
422c21da
 
 name=`basename "${PWD}"`
 
 
40fe269d
 usage()
3be635de
 {
40fe269d
    cat <<EOF
422c21da
 usage: tag [tag] [vendortag] [vendorprefix]
40fe269d
 
422c21da
    tag          : the tag for your repository ($name)
    vendortag    : the tag used for tagging the fetched repositories
    vendorprefix : prefix for the vendortag, if vendortag is "-"
40fe269d
 EOF
3be635de
 }
 
40fe269d
 
 check_and_usage_and_help()
 {
    if [ "$TAG" = "" -o "$TAG" = "-h" -o "$TAG" = "--help" -o "$VENDOR_TAG" = "" ]
    then
       usage >&2
       exit 1
    fi
 }
3be635de
 
 
5de2fbd6
 project=`find_xcodeproj "${name}"`
 AGVTAG=
0a0f03f4
 
5de2fbd6
 if [ "${project}" != "" ]
 then
0a0f03f4
    log_fluff "Trying agvtool to figure out current version"
5de2fbd6
    dir=`dirname "${project}"`
0a0f03f4
    [ -x "${dir}" ] || fail "${dir} is not accesible"
 
5de2fbd6
    AGVTAG=`(cd "${dir}" ; agvtool what-version -terse ) 2> /dev/null`
    if [ $? -ne 0 ]
    then
0a0f03f4
       log_fluff "agvtool failed"
5de2fbd6
       AGVTAG=
0a0f03f4
    else
       log_info "Current version: ${AGVTAG}"
5de2fbd6
    fi
 fi
 
e3e80f18
 
0a0f03f4
 TAG=${1:-"$AGVTAG"}
5de2fbd6
 shift
40fe269d
 
 VENDOR_TAG="$1"
5de2fbd6
 shift
 
422c21da
 VENDOR_PREFIX="$1"
 shift
 
 
 if [ -z "${VENDOR_TAG}" -o "${VENDOR_TAG}" = "-" ]
5de2fbd6
 then
422c21da
    if [ -z "${VENDOR_PREFIX}" ]
    then
       VENDOR_PREFIX=`basename "${PWD}"`
       VENDOR_PREFIX="${VENDOR_PREFIX%%.*}"  # remove vile extension :)
    fi
40fe269d
 
422c21da
    if [ "${VENDOR_PREFIX}" = "-" ]
    then
       VENDOR_TAG="${TAG}"
    else
       VENDOR_TAG="${VENDOR_PREFIX}-${TAG}"
    fi
5de2fbd6
 
422c21da
    check_and_usage_and_help
5de2fbd6
 
422c21da
    log_info "Set vendortag to \"${VENDOR_TAG}\""
 else
    check_and_usage_and_help
 fi
40fe269d
 
 REPO="."
5de2fbd6
 
 
 git_must_be_clean()
 {
    local name
    local clean
 
    name="${1:-${PWD}}"
 
    if [ ! -d .git ]
    then
0a0f03f4
       fail "\"${name}\" is not a git repository"
5de2fbd6
    fi
 
    clean=`git status -s`
0a0f03f4
    if [ "${clean}" != "" ]
5de2fbd6
    then
       fail "repository $name is tainted"
    fi
 }
 
e3e80f18
 
5de2fbd6
 ensure_repos_clean()
 {
    #
    # Make sure that tagging is OK
    # all git repos must be clean
    #
    (cd "${REPO}" ; git_must_be_clean "${REPO}" ) || exit 1
 
    if  dir_has_files "${CLONES_SUBDIR}"
    then
       for i in "${CLONES_SUBDIR}"/*
       do
          # only tag what looks like a git repo
          if [ -d "${i}/.git" -o -d "${i}/refs" ]
          then
             (cd "${i}" ; git_must_be_clean "${i}" ) || exit 1
          fi
       done
    fi
 }
 
 
 tag()
 {
    local i
e3e80f18
    local script
5de2fbd6
 
d7f96955
    run_fetch_settings_script "pre-tag"
 
    script=`find_fetch_setting_file "bin/tag.sh"`
e3e80f18
    if [ -x "$script" ]
5de2fbd6
    then
d7f96955
       run_script "$script" "${TAG}" "${REPO}" || exit 1
e3e80f18
    else
aedda0b2
       log_info "Tagging \"`basename "${REPO}"`\" with \"${TAG}\""
       ( cd "${REPO}" ; exekutor git tag "${TAG}" ) || exit 1
e3e80f18
 
       if  dir_has_files "${CLONES_SUBDIR}"
       then
          for i in "${CLONES_SUBDIR}"/*
          do
             if [ -d "$i" ]
5de2fbd6
             then
e3e80f18
                if [ -d "${i}/.git" -o -d "${i}/refs" ]
                then
aedda0b2
                   log_info "Tagging \"`basename "${i}"`\" with \"${VENDOR_TAG}\""
                   (cd "$i" ; exekutor git tag "${VENDOR_TAG}" ) || fail "tag failed"
e3e80f18
                fi
5de2fbd6
             fi
e3e80f18
          done
       fi
5de2fbd6
    fi
 
d7f96955
    run_fetch_settings_script "pre-tag"
5de2fbd6
 }
 
 
 main()
 {
3c8ec4d2
    log_fluff "::: tag :::"
40fe269d
 
5de2fbd6
    ensure_repos_clean
 
aedda0b2
    echo "Will tag `basename "${PWD}"` with ${TAG}" >&2
5de2fbd6
    if  dir_has_files "${CLONES_SUBDIR}"
    then
aedda0b2
       echo "Will tag clones with ${VENDOR_TAG}" >&2
5de2fbd6
    fi
 
aedda0b2
    user_say_yes "Is this OK ?"
    if [ $? -eq 0 ]
    then
       tag
    fi
5de2fbd6
 }
 
40fe269d
 main "$@"