view tools/patch-tool-mxe @ 2489:2f3353b7a313

Patch tool: Move environment variable setup to a function. This keeps us from doing this in case of an incorrect call, making error messages much more sane.
author Ryan Pavlik <rpavlik@iastate.edu>
date Mon, 07 May 2012 12:41:32 -0500
parents d9f6c4ade221
children 0db1b21402e9
line wrap: on
line source

#!/usr/bin/env bash

# Tool for converting between MXE patch files and git repos
# Imports and exports patch files in "git format-patch" format.

cmd=$1
pkg=$2

setupEnv() {
  # MXE directory
  export mxedir=$(cd $(dirname $0) && cd .. && pwd)

  # directory for unpacked tarballs/git repos
  export gitsdir=${mxedir}/gits

  mkdir -p ${gitsdir}


  # John Doe <John Doe@acme.org>
  export author=`git var GIT_AUTHOR_IDENT | sed 's/^\(.* [<].*[>]\).*$/\1/'`

  export pkg_version=$(sed -n "s/^.* id=\"${pkg}-version\">\([^<]*\)<.*$/\1/p" "${mxedir}/index.html")

  export pkg_short_version=`echo $pkg_version | sed s/'\(.*\)\.[^.]*$'/'\1'/`

  export pkg_subdir=`grep '^$(PKG)_SUBDIR' $mxedir/src/$pkg.mk  | \
    sed 's/.*:= \(.*\)/\1/' | \
    sed s/'$($(PKG)_VERSION)'/$pkg_version/ | \
    sed s/'$(call SHORT_PKG_VERSION,$(PKG))'/$pkg_short_version/ | \
    sed s/'$(PKG)'/$pkg/;`

  export pkg_file=`grep '^$(PKG)_FILE' $mxedir/src/$pkg.mk  | \
    sed 's/.*:= \(.*\)/\1/' | \
    sed s/'$($(PKG)_VERSION)'/$pkg_version/ | \
    sed s/'$(call SHORT_PKG_VERSION,$(PKG))'/$pkg_short_version/ | \
    sed s/'$($(PKG)_SUBDIR)'/$pkg_subdir/ | \
    sed s/'$(PKG)'/$pkg/;`

  #echo $pkg
  #echo $pkg_version
  #echo $pkg_subdir
  #echo $pkg_file

}
# init
function init_git {
  setupEnv
  cd $gitsdir
  echo $pkg_file | grep "\.tar\.gz"  >> /dev/null && tar xf $mxedir/pkg/$pkg_file
  echo $pkg_file | grep "\.tar\.bz2" >> /dev/null && tar xf $mxedir/pkg/$pkg_file
  echo $pkg_file | grep "\.tar\.xz"  >> /dev/null && xz -dc $mxedir/pkg/$pkg_file | tar xf -
  echo $pkg_file | grep "\.zip"      >> /dev/null && unzip  $mxedir/pkg/$pkg_file >> /dev/null
  cd $gitsdir/$pkg_subdir && \
  (git init; git add -A; git commit -m "init") > /dev/null
  git tag dist
}

function export_patch {
  setupEnv
  cd $gitsdir/$pkg_subdir && \
  (
    echo 'This file is part of MXE.'
    echo 'See index.html for further information.'
    echo ''
    echo 'Contains ad hoc patches for cross building.'
    echo ''
    git format-patch -p --stdout dist..HEAD | \
    sed 's/^From: .*/From: MXE/g;'
  ) > $mxedir/src/$pkg-1-fixes.patch
}

function import_patch {
  setupEnv
  cd $gitsdir/$pkg_subdir && \
  cat $mxedir/src/$pkg-1-fixes.patch | \
  sed '/^From/,$  !d' | \
  sed s/'^From: .*'/"From: $author"/'g;' | \
  git am --keep-cr
}

case "$cmd" in
  init)
    init_git $pkg
    ;;
  import)
    import_patch $pkg
    ;;
  export)
    export_patch $pkg
    ;;
  *)
    echo "Unrecognized command '${cmd}'" >&2
    cat <<EOS
    Usage: $0 COMMAND PACKAGENAME
    where COMMAND is one of:
      init - create a git directory for the package with the raw source
      import - apply the "pkgname-1-fixes.patch" patch commits
      export - create/replace the "pkgname-1-fixes.patch" patch with a patch of all commits since init.
EOS
    exit 1
    ;;
esac