gnu-arch-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gnu-arch-users] Re: Moving project to another archive


From: Robin Farine
Subject: Re: [Gnu-arch-users] Re: Moving project to another archive
Date: Wed, 11 Feb 2004 23:02:19 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6b) Gecko/20031221 Thunderbird/0.4

Rodrigo Bernardo Pimentel wrote:
        I checked the source archive and apparently the only place where
it's name is stored, inside the project directory, is in the */log
files. Would simply moving the project directory to the new archive work?

If you you really want to rename your archive and are aware of the consequences, then you could make a backup of it and then run the attached script on it. The usage message should suffice to tell you how to do that.

It's called 'fixarch' because when I started with Arch, I renamed an archive and then commited to it. I learned later the hard way that it was not a good idea to do that.


Robin
#! /bin/bash

set -f

pid=$$
progname="$0"
archname_re='address@hidden'
arch_basefilter='( -path */.arch-ids/* -o -path */{arch}/* )'
backupsuf='.prearchfix'
fixarch_cleanup='fixarch.action'
msg_prefix='+ '

usage() {
    echo "Usage: $progname --commit | --undo | <from archive> <to archive>"
    echo "   Recursively traverse the subtree rooted at '.' and replace the"
    echo "   arch archive name <from archive> with <to archive> in files and"
    echo "   base/patch tarballs. '--commit' deletes the backup files a"
    echo "   normal run creates and '--undo' reverts modified files to their"
    echo "   backuped state."
    exit 64
}

cleanup () {
    if test -f $fixarch_cleanup; then
        action=`cat ${fixarch_cleanup}`
        case "$action" in
            file:*)
                rm -f ${action/#file:}
                ;;
            tardir:*)
                revdir=`dirname ${action/#tardir:}`
                tdir=`basename ${action/#tardir:}`
                (cd $revdir && \
                    rm -rf $tdir new-${tdir}.tar.gz new-${tdir}.src.tar.gz)
                ;;
            *)
                echo "cleanup! unknown action $action"
                ;;
        esac
        rm -f $fixarch_cleanup
    fi
}
trap "cleanup" 0 1 2 3 15

error() {
    echo "Error: $2"
    exit $1
}

message () {
    echo "${msg_prefix}$1"
}

revert_to_backups() {
    find . -type f -name "*$backupsuf" | \
        sed 's,\(.*\)\.prearchfix,\1,' | while read file; do
        message "reverting $file to backup"
        mv -f $file$backupsuf $file
    done || error 69 "revert to backup failed"
}

if [ $# -eq 1 ]; then
    case "$1" in
        --commit)
            msg_prefix='- '
            find . -type f -name "*$backupsuf" -exec rm -f {} \;
            ;;
        --undo)
            msg_prefix='- '
            revert_to_backups
            ;;
        *)
            usage
            ;;
    esac
    exit $?
fi

if [ $# -ne 2 ]; then usage; fi

from_arch="$1"
to_arch="$2"

if ! echo $from_arch | grep -q "$archname_re"; then
    error 64 "$from_arch doesn't match an archive name"
fi
if ! echo $to_arch | grep -q "$archname_re"; then
    error 64 "$to_arch doesn't match an archive name"
fi


# check for existing backup files
if find . -type f -print | grep -q ".*$backupsuf"; then
    error 64 "backup files found"
fi


do_file () {
    test $# -eq 1 || error 64 "do_file <filename>"
    file=$1
    sed -e "s,${from_arch}\([^-a-zA-Z0-9.]\),${to_arch}\1,g" \
        -e "s,${from_arch}\$,${to_arch}\1,g" $file > ${file}.new
    if test $? -ne 0; then
        error 69 "sed"
    fi
    if ! cmp -s ${file}.new $file; then
        mv -f $file $file$backupsuf || error 69 "mv"
        mv -f ${file}.new $file || error 69 "mv"
        message "updated ${file}"
    else
        rm -f ${file}.new
    fi
}

do_tar () {
    test $# -ge 2 -a $# -le 3 || \
        error 64 "do_tar <filename> <extracted root name> [<file find filter>]"
    tarfile=$1
    tardir=$2
    filefilter=${3:-}
    change_stamp='archfix.stamp'

    rm -f $change_stamp
    message "extracting tar file $tarfile"
    tar xzf $tarfile || error 69 "untar $tarfile"

    message "fixing directory names"
    find $tardir -type d -name "$from_arch" -print | while read dir; do
        mv $dir `dirname $dir`/$to_arch || error 69 "rename dir"
        message "renamed $dir"
        touch $change_stamp
    done || exit $?

    message "fixing files"
    find $tardir -type f $filefilter -print | while read file; do
        do_file $file || exit $?
        if test -f $file$backupsuf; then
            touch $change_stamp
            rm -f $file$backupsuf
        fi
    done || exit $?

    if test -f $change_stamp; then
        message "changes detected, creating new tar file"
        tar czf new-$tarfile $tardir || error 69 "new tar"
        mv $tarfile $tarfile$backupsuf || error 69 "move old tar"
        mv new-$tarfile $tarfile || error 69 "move new tar"
    fi
    rm -rf $change_stamp $tardir
}


find . -type f \
       ! \( -name "$fixarch_cleanup" -o -name "*$backupsuf" \) -print | \
while read file; do
    dir=`dirname $file`
    file=`basename $file`
    case $file in
        *--*--*--*.patches.tar.gz)
            msg_prefix='** '
            tardir=`echo $file | sed 's,\.tar\.gz$,,'`
            echo "tardir:${dir}/$tardir" > $fixarch_cleanup
            message "entering $dir"
            ( cd $dir && do_tar $file $tardir ) || exit $?
            message "leaving $dir"
            msg_prefix='+ '
            ;;
        *--*--*--*.tar.gz)
            msg_prefix='** '
            tardir=`echo $file | sed -e 's,\.tar\.gz$,,' -e 's,\.src$,,'`
            echo "tardir:${dir}/$tardir" > $fixarch_cleanup
            message "entering $dir"
            ( cd $dir && do_tar $file $tardir "$arch_basefilter" ) || exit $?
            message "leaving $dir"
            msg_prefix='+ '
            ;;
        *)
            echo "file:${dir}/${file}.new" > $fixarch_cleanup
            do_file ${dir}/${file} || exit $?
            ;;
    esac
    rm -f $fixarch_cleanup
done

exit $?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]