Hi,

I modified the remove-unused-sources script for my own use, 
so it can remove unused sources even when the project is not 
compacted. I also added the possibility to traverse a git repository,
and collect unused sources for the repository as a whole.

Comments are welcome.

Greetings,

lieven


#!/bin/sh

# Copyright (C) 2008 Jonathan Moore Liles                                     #
#                                                                             #
# 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; see the file COPYING.  If not,write to the Free Software #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  #

DRY_RUN=
MOVE=1
COMPACTED=
SCAN_GIT_REPO=
TEMP="/tmp"

# Store awk script in variable

awk_script=$(cat <<'EOF'
BEGIN { i=0 }
/!(Audio_Region)/{ next }
/create/ {
    match($0, ":source \".*\"");
    sources[i++]=substr($0, (RSTART + 9), (RLENGTH - 10));
}
/destroy/ {
    match($0, ":source \".*\"");
    source=substr($0, (RSTART + 9), (RLENGTH - 10));
    for( d in sources ) {
        if( sources[d] == source ) {
            delete sources[d];
            break;
        }
    }
}
END {for(s in sources){
        if(sources[s] != ""){
            printf "%s\n", sources[s]
        }
    }
}
EOF
)

fatal ()
{
    echo Error: "$1"
    echo 'Aborting!'
    cleanup
    exit 1
}

cleanup ()
{
    rm -f "${TEMP}/all-sources" "${TEMP}/used-sources" 
"${TEMP}/used-sources-tmp"
}

set_diff ()
{
    diff --new-line-format '' --old-line-format '%L' --unchanged-line-format '' 
"$1" "$2"
}


remove_sources ()
{
    local TOTAL=0
    local FILE
    local SIZE
    local PSIZE
    while read FILE
    do

        PSIZE=`stat -c '%s' "${FILE}.peak" 2>/dev/null`
        SIZE=`stat -c '%s' "${FILE}" 2>/dev/null`

        PSIZE=${PSIZE:-0}

        if ! [ -f "${FILE}" ]
        then
            echo "Would remove \"${FILE}\", if it existed."
        else
            if [ "$DRY_RUN" = 1 ]
            then
                echo "Would remove: ${FILE}"
            else
                if [ "$MOVE" = 1 ]
                then
                    echo "Moving unused source \"${FILE}\"..."
                    mv -f ./"${FILE}" ./"${FILE}".peak ../unused-sources
                else
                    echo "Removing unused source \"${FILE}\"..."
                    rm -f ./"${FILE}" ./"${FILE}".peak
                fi
            fi

            TOTAL=$(( $TOTAL + $SIZE + $PSIZE ))
        fi

    done

    echo "...Freeing a total of $(($TOTAL / ( 1024 * 1024 ) ))MB"
}

used_sources_awk ()
{
    [ -f "${1}" ] && awk "${awk_script}" "${1}"
}

usage ()
{
    fatal "Usage: $0 [-n] [-g] [-m|-d] path/to/project"
}

while getopts "dmncg" o
do
    case "$o" in
        d) MOVE= ;;
        m) MOVE=1 ;;
        n) DRY_RUN=1 ;;
        g) SCAN_GIT_REPO=1 ;;
        \?) usage ;;
        *) echo "$o" && usage ;;
    esac
done

shift $(( $OPTIND - 1 ))
PROJECT="$1"

[ $# -eq 1 ] || usage

cd "$PROJECT" || fatal "No such project"

[ -f history ] && [ -f info ] || fatal "Not a Non-DAW project?"

[ -f .lock ] && fatal "Project appears to be in use"

if ! grep -v '\(^\{\|\}$\)\|create' history &>/dev/null
then
    COMPACTED=1
fi

echo "Scanning \"${PROJECT}\"..."

if [ "$SCAN_GIT_REPO" = 0 ]
then
    if [ "$COMPACTED" = 1 ]
    then
        sed -n 's/^\s*Audio_Region.* :source "\([^"]\+\)".*$/\1/p' history | 
sort | uniq > "${TEMP}/used-sources"
    else
        awk "${awk_script}" "history" > "${TEMP}/used-sources"
    fi
else
    git status &>/dev/null || fatal "No git repository for this project"
    git checkout -q master || fatal "Could not checkout master branch"
    [ c=$(git rev-list --count HEAD) > 1 ] && { git checkout -q HEAD^ || fatal 
"Couldn't checkout git commit" ;}

    git checkout -q master
    used_sources_awk "history" >> "${TEMP}/used-sources-tmp"

    while git checkout -q HEAD^ 2>/dev/null
    do
        used_sources_awk "history" >> "${TEMP}/used-sources-tmp"
    done

    cat "${TEMP}/used-sources-tmp" | sort | uniq > "${TEMP}/used-sources"
fi


cd sources || fatal "Can't change to source directory"

[ "$MOVE" = 1 ] && mkdir ../unused-sources 2>/dev/null

ls -1 | grep -v '\.peak$' | sort > "${TEMP}/all-sources"

set_diff "${TEMP}/all-sources" "${TEMP}/used-sources" | remove_sources

git checkout -q master

cleanup

echo "Done."

exit 0



Reply via email to