#!/bin/bash # Copyright 2009 Kenshi Muto # # Usage: webwml-patch-commit [last-revision] skipauthor="webwml sync agent" lastcommitfile=$(pwd)/lastcommit gitdir=$(pwd)/webwml cvsdir=$(pwd)/webwml-cvs patchesdir=$(pwd)/patches patcheddir=$(pwd)/patched targetlang=japanese patchedprefix=$(date +'%Y%m%d')- if [ "$1" ]; then lastrev=$1 else lastrev=$(cat $lastcommitfile) fi cd $gitdir git pull git format-patch -o $patchesdir $lastrev cd $cvsdir cvs -q update -d -P english $targetlang cd $patchesdir for pf in $(ls *.patch); do sha1=$(head -n 1 $pf | sed -e "s/From //" -e "s/ .*//") if [ "$(head -n 2 $pf | grep "From: $skipauthor")" ]; then # This patch should be already applied echo "[SKIP] $pf by agent" mv $pf $patcheddir/$patchedprefix$pf echo $sha1 > $lastcommitfile else # Test cd $cvsdir patch --dry-run -p1 -f < ${patchesdir}/$pf if [ $? != 0 ]; then while true; do echo -n "[ERROR] $pf failed to apply! Exit now? [y/n] " read yn if [ "$yn" = "y" ]; then exit elif [ "$yn" = "n" ]; then break fi done else logmsg=$(head -n 4 $patchesdir/$pf | grep Subject: | sed -e "s/Subject: \[PATCH\] //") while true; do echo -n "[COMMIT] $pf as '$logmsg'. Apply? [y/n/p/s] " read yn if [ "$yn" = "y" ]; then # apply patch -p1 -f < ${patchesdir}/$pf addfiles=$( cd $gitdir && git whatchanged -1 --pretty=format:"" $sha1 | grep "$targetlang/" | awk '{if ($5 == "A") print $6}' | xargs ) rmfiles=$( cd $gitdir && git whatchanged -1 --pretty=format:"" $sha1 | grep "$targetlang/" | awk '{if ($5 == "D") print $6}' | xargs ) if [ "$addfiles" ]; then # mkdir adddirs=$(echo $addfiles|ruby -e 'D=[];ARGF.each{|l|l.split(/ /).each{|f|D.push(File.dirname(f)) unless File.exists?(File.dirname(f))}};puts D.sort.uniq.join(" ")') if [ "$adddirs"]; then eval mkdir $adddirs eval cvs add $adddirs fi eval cvs add $addfiles fi if [ "$rmfiles" ]; then eval cvs remove $rmfiles fi cd $targetlang # fool proof cvs commit -m "$logmsg" mv $patchesdir/$pf $patcheddir/$patchedprefix$pf echo $sha1 > $lastcommitfile break elif [ "$yn" = "n" ]; then # keep and skip break elif [ "$yn" = "p" ]; then # pager if [ -z "$PAGER" ]; then PAGER=lv fi $PAGER $patchesdir/$pf elif [ "$yn" = "s" ]; then # skip and move to patched mv $patchesdir/$pf $patcheddir/$patchedprefix$pf echo $sha1 > $lastcommitfile break fi done fi cd $patchesdir fi done