Blame Scripts/Bash/Functions/cli_commitRepoChanges.sh

d627b6
#!/bin/bash
d627b6
#
4e4549
# cli_commitRepoChanges.sh -- This function looks for revision changes
640f58
# inside absolute path passed as action value and ask you to commit
640f58
# them up to central repository. Use this function before or after
640f58
# whatever function that makes changes to files inside the working
640f58
# copy. It is better to commit small changes than long ones.
d627b6
#
2a50d0
# Copyright (C) 2009-2011 Alain Reguera Delgado
d627b6
# 
7cd8e9
# This program is free software; you can redistribute it and/or
7cd8e9
# modify it under the terms of the GNU General Public License as
7cd8e9
# published by the Free Software Foundation; either version 2 of the
7cd8e9
# License, or (at your option) any later version.
d627b6
# 
d627b6
# This program is distributed in the hope that it will be useful, but
d627b6
# WITHOUT ANY WARRANTY; without even the implied warranty of
d627b6
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d627b6
# General Public License for more details.
d627b6
#
d627b6
# You should have received a copy of the GNU General Public License
d627b6
# along with this program; if not, write to the Free Software
d627b6
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
d627b6
# USA.
d627b6
# 
d627b6
# ----------------------------------------------------------------------
d627b6
# $Id$
d627b6
# ----------------------------------------------------------------------
d627b6
d627b6
function cli_commitRepoChanges {
d627b6
4e4549
    local -a FILES
4e4549
    local -a INFO
4e4549
    local -a FILESNUM
d627b6
    local COUNT=0
4e4549
    local UPDATEOUT=''
4e4549
    local PREDICATE=''
58a639
    local LOCALFILES=''
58a639
    local CHNGDIRECTION=''
b3ea32
    local CHNGTOTAL=0
55612a
    local LOCATIONS=''
d627b6
55612a
    # Define location. If function arguments are provided to
55612a
    # cli_commitRepoChanges assume them as path location to work with.
55612a
    # Function arguments can contain more than one path enclosed by
55612a
    # quotation mark. In that case all paths are used as location to
55612a
    # work with.  If function arguments are not provided, the
55612a
    # ACTIONVAL variable default value is used instead.
55612a
    if [[ "$@" != '' ]];then
55612a
        LOCATIONS="$@"
ae2995
    else
55612a
        LOCATIONS="$ACTIONVAL"
640f58
    fi
640f58
55612a
    # Verify locations. The location value may point to the working
55612a
    # copy in the workstation, or to a URL in the central repository.
55612a
    # The cli_commitRepoChanges function performs actions over working
55612a
    # copy files only. So, we need to exclude all URL from location
55612a
    # inside this function. Whatever action you perform over URL will
55612a
    # take place immediatly through direct commit.
55612a
    LOCATIONS=$(echo -e "$LOCATIONS" | sed -r "s! +!\n!g" \
55612a
        | egrep '^/home/centos/artwork/(trunk|branches|tags)/.+$')
55612a
4e4549
    echo '----------------------------------------------------------------------'
55612a
    # Update working copy.
58a639
    cli_printMessage "`gettext "Bringing changes from the repository into the working copy"`" 'AsResponseLine'
55612a
    UPDATEOUT=$(svn update ${LOCATIONS})
58a639
55612a
    # Check working copy.
58a639
    cli_printMessage "`gettext "Checking changes in the working copy"`" 'AsResponseLine'
55612a
    STATUSOUT=$(svn status ${LOCATIONS})
4e4549
    echo '----------------------------------------------------------------------'
d627b6
4e4549
    # Define path of files considered recent modifications from
4e4549
    # central repository to working copy.
55612a
    FILES[0]=$(echo "$UPDATEOUT" | egrep "^A.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
55612a
    FILES[1]=$(echo "$UPDATEOUT" | egrep "^D.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
55612a
    FILES[2]=$(echo "$UPDATEOUT" | egrep "^U.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
55612a
    FILES[3]=$(echo "$UPDATEOUT" | egrep "^C.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
55612a
    FILES[4]=$(echo "$UPDATEOUT" | egrep "^G.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
58a639
58a639
    # Define path fo files considered recent modifications from
58a639
    # working copy up to central repository.
55612a
    FILES[5]=$(echo "$STATUSOUT" | egrep "^M.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
55612a
    FILES[6]=$(echo "$STATUSOUT" | egrep "^\?.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
55612a
    FILES[7]=$(echo "$STATUSOUT" | egrep "^D.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
55612a
    FILES[8]=$(echo "$STATUSOUT" | egrep "^A.+$(cli_getRepoTLDir "${LOCATIONS}").+$" | sed -r "s,^.+($(cli_getRepoTLDir "${LOCATIONS}").+)$,\1,")
d627b6
4e4549
    # Define description of files considered recent modifications from
4e4549
    # central repository to working copy.
4e4549
    INFO[0]="`gettext "Added"`"
4e4549
    INFO[1]="`gettext "Deleted"`"
4e4549
    INFO[2]="`gettext "Updated"`"
4e4549
    INFO[3]="`gettext "Conflicted"`"
4e4549
    INFO[4]="`gettext "Merged"`"
58a639
58a639
    # Define description of files considered recent modifications from
58a639
    # working copy up to central repository.
4e4549
    INFO[5]="`gettext "Modified"`"
640f58
    INFO[6]="`gettext "Unversioned"`"
58a639
    INFO[7]=${INFO[1]}
640f58
    INFO[8]=${INFO[0]}
d627b6
4e4549
    while [[ $COUNT -ne ${#FILES[*]} ]];do
d627b6
4e4549
        # Get total number of files. Avoid counting empty line.
58a639
        if [[ "${FILES[$COUNT]}" == '' ]];then
4e4549
            FILESNUM[$COUNT]=0
4e4549
        else
4e4549
            FILESNUM[$COUNT]=$(echo "${FILES[$COUNT]}" | wc -l)
4e4549
        fi
4e4549
b3ea32
        # Calculate total amount of changes.
b3ea32
        CHNGTOTAL=$(($CHNGTOTAL + ${FILESNUM[$COUNT]}))
b3ea32
4e4549
        # Build report predicate. Use report predicate to show any
4e4549
        # information specific to the number of files found. For
4e4549
        # example, you can use this section to show warning messages,
58a639
        # notes, and so on. By default we use the word `file' or
58a639
        # `files' at ngettext's consideration followed by change
58a639
        # direction.
4e4549
        if [[ ${FILESNUM[$COUNT]} -lt 1 ]];then
58a639
            PREDICATE[$COUNT]=`gettext "file"`
4e4549
        else
4e4549
            PREDICATE[$COUNT]=`ngettext "file" "files" ${FILESNUM[$COUNT]}`
4e4549
        fi
4e4549
58a639
        # Redefine report predicate to add direction of changes.
58a639
        if [[ $COUNT -le 4 ]]; then
58a639
            # Consider recent modifications from central repository
58a639
            # down to working copy.
58a639
            CHNGDIRECTION="`gettext "from the repository."`"
58a639
        elif [[ $COUNT -gt 4 ]];then
58a639
            # Consider recent modifications from working copy up to
58a639
            # central repository.
58a639
            CHNGDIRECTION="`gettext "from the working copy."`"
58a639
        fi
58a639
58a639
        # Redefine report predicate using change direction.
58a639
        PREDICATE[$COUNT]="${PREDICATE[$COUNT]} ${CHNGDIRECTION}"
58a639
4e4549
        # Output report line.
4e4549
        cli_printMessage "${INFO[$COUNT]}: ${FILESNUM[$COUNT]} ${PREDICATE[$COUNT]}" 'AsRegularLine'
4e4549
4e4549
        # Increase counter.
4e4549
        COUNT=$(($COUNT + 1))
4e4549
4e4549
    done
4e4549
4e4549
    echo '----------------------------------------------------------------------'
4e4549
b3ea32
    # Check total amount of changes. If there is no change, there is
b3ea32
    # nothing more to do here except to return flow control to this
b3ea32
    # function caller.
b3ea32
    if [[ $CHNGTOTAL -eq 0 ]];then
b3ea32
       return 0 
b3ea32
    fi
b3ea32
2ce3a5
    # In case new unversioned files exist, ask user to add them into
2ce3a5
    # the repository. This may happen when new documentation entries
2ce3a5
    # are created.
2ce3a5
    if [[ ${FILESNUM[6]} -gt 0 ]];then
00f591
        cli_printMessage "`ngettext "The following file is unversioned" \
00f591
            "The following files are unversioned" ${FILESNUM[6]}`:"
00f591
        for FILE in ${FILES[6]};do
2ce3a5
            cli_printMessage $FILE 'AsResponseLine'
2ce3a5
        done
c4b35f
        cli_printMessage "`ngettext "Do you want to add it now?" \
c4b35f
            "Do you want to add them now?" ${FILESNUM[6]}`" 'AsYesOrNoRequestLine'
52155a
        svn add ${FILES[6]} --quiet
2ce3a5
    fi
2ce3a5
b3ea32
    # Reset counter.
b3ea32
    COUNT=0
58a639
58a639
    # Unify local changes into a common variable so common actions can
58a639
    # be applied to them.
58a639
    while [[ $COUNT -ne ${#FILES[*]} ]];do
58a639
        LOCALFILES="$LOCALFILES ${FILES[$COUNT]}"
58a639
        COUNT=$(($COUNT + 1 ))
58a639
    done
58a639
4e4549
    # Check list of changed files. If there are changes in the working
4e4549
    # copy, ask the user to verify, and later, commit them up to
4e4549
    # central repository.
58a639
    if [[ $LOCALFILES != '' ]];then
d627b6
d627b6
        # Verify changes.
4e4549
        cli_printMessage "`gettext "Do you want to see changes now?"`" "AsYesOrNoRequestLine"
58a639
        svn diff $LOCALFILES | less
d627b6
d627b6
        # Commit changes.
58a639
        cli_printMessage "`gettext "Do you want to commit changes now?"`" "AsYesOrNoRequestLine"
58a639
        svn commit $LOCALFILES
4e4549
d627b6
    fi
d627b6
d627b6
}