Blame Scripts/Bash/Functions/Vcs/Git/git_commitRepoChanges.sh

123ee8
#!/bin/bash
123ee8
#
123ee8
# git_commitRepoChanges.sh -- This function commits all the changes
123ee8
# found in files under version control.
123ee8
#
123ee8
# Copyright (C) 2009-2013 The CentOS Project
123ee8
#
123ee8
# This program is free software; you can redistribute it and/or modify
123ee8
# it under the terms of the GNU General Public License as published by
123ee8
# the Free Software Foundation; either version 2 of the License, or (at
123ee8
# your option) any later version.
123ee8
#
123ee8
# This program is distributed in the hope that it will be useful, but
123ee8
# WITHOUT ANY WARRANTY; without even the implied warranty of
123ee8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
123ee8
# General Public License for more details.
123ee8
#
123ee8
# You should have received a copy of the GNU General Public License
123ee8
# along with this program; if not, write to the Free Software
123ee8
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
123ee8
#
123ee8
# ----------------------------------------------------------------------
123ee8
# $Id$
123ee8
# ----------------------------------------------------------------------
123ee8
123ee8
function git_commitRepoChanges {
123ee8
123ee8
    local -a FILES
123ee8
    local -a INFO
123ee8
    local -a FILESNUM
123ee8
    local COUNT=0
123ee8
    local STATUSOUT=''
123ee8
    local PREDICATE=''
123ee8
    local CHNGTOTAL=0
123ee8
    local LOCATION=$(cli_checkRepoDirSource "$1")
123ee8
123ee8
    # Verify source location absolute path. It should point to
123ee8
    # existent files or directories. They don't need to be under
123ee8
    # version control.
123ee8
    cli_checkFiles ${LOCATION} -e
123ee8
123ee8
    # Print action message.
123ee8
    cli_printMessage "`gettext "Checking changes in the working copy"`" --as-banner-line
123ee8
123ee8
    # Build list of files that have received changes in its version
123ee8
    # status.  Be sure to keep output files off from this list.
123ee8
    # Remember, output files are not version inside the working copy,
123ee8
    # so they are not considered for evaluation here. But take care,
123ee8
    # sometimes output files are in the same format of source files,
123ee8
    # so we need to differentiate them using their locations.
123ee8
    STATUSOUT="$(${COMMAND} status --porcelain ${LOCATION})"
123ee8
123ee8
    # Process location based on its path information. Both
123ee8
    # by-extension and by-location exclusions are no longer needed
123ee8
    # here.  They are already set in the `.git/info/exclude' file.
123ee8
123ee8
    # Define path to files considered recent modifications from
123ee8
    # working copy up to local repository.
123ee8
    FILES[0]=$(echo "$STATUSOUT" | egrep "^.M"  | sed -r "s,^.+${TCAR_WORKDIR}/,,")
123ee8
    FILES[1]=$(echo "$STATUSOUT" | egrep "^.\?" | sed -r "s,^.+${TCAR_WORKDIR}/,,")
123ee8
    FILES[2]=$(echo "$STATUSOUT" | egrep "^.D"  | sed -r "s,^.+${TCAR_WORKDIR}/,,")
123ee8
    FILES[3]=$(echo "$STATUSOUT" | egrep "^.A"  | sed -r "s,^.+${TCAR_WORKDIR}/,,")
123ee8
123ee8
    # Define description of files considered recent modifications from
123ee8
    # working copy up to local repository.
123ee8
    INFO[0]="`gettext "Modified"`"
123ee8
    INFO[1]="`gettext "Unversioned"`"
123ee8
    INFO[2]="`gettext "Deleted"`"
123ee8
    INFO[3]="`gettext "Added"`"
123ee8
123ee8
    while [[ $COUNT -ne ${#FILES[*]} ]];do
123ee8
123ee8
        # Define total number of files. Avoid counting empty line.
123ee8
        if [[ "${FILES[$COUNT]}" == '' ]];then
123ee8
            FILESNUM[$COUNT]=0
123ee8
        else
123ee8
            FILESNUM[$COUNT]=$(echo "${FILES[$COUNT]}" | wc -l)
123ee8
        fi
123ee8
123ee8
        # Calculate total amount of changes.
123ee8
        CHNGTOTAL=$(($CHNGTOTAL + ${FILESNUM[$COUNT]}))
123ee8
123ee8
        # Build report predicate. Use report predicate to show any
123ee8
        # information specific to the number of files found. For
123ee8
        # example, you can use this section to show warning messages,
123ee8
        # notes, and so on. By default we use the word `file' or
123ee8
        # `files' at ngettext's consideration followed by change
123ee8
        # direction.
123ee8
        PREDICATE[$COUNT]=`ngettext "file in the working copy" \
123ee8
            "files in the working copy" $((${FILESNUM[$COUNT]} + 1))`
123ee8
123ee8
        # Output report line.
123ee8
        cli_printMessage "${INFO[$COUNT]}: ${FILESNUM[$COUNT]} ${PREDICATE[$COUNT]}" --as-stdout-line
123ee8
123ee8
        # Increase counter.
123ee8
        COUNT=$(($COUNT + 1))
123ee8
123ee8
    done
123ee8
123ee8
    # When files have changed in the target location, show which these
123ee8
    # files are and request user to see such changes and then, for
123ee8
    # committing them up to the local repository.
123ee8
    if [[ ${FILESNUM[0]} -gt 0 ]];then
123ee8
123ee8
        # Print action message.
123ee8
        cli_printMessage "`gettext "Do you want to see changes now?"`" --as-yesornorequest-line
123ee8
123ee8
        # Show differences.
123ee8
        ${COMMAND} diff ${LOCATION} | less
123ee8
123ee8
        # Print action message.
123ee8
        cli_printMessage "`gettext "Do you want to commit changes now?"`" --as-yesornorequest-line
123ee8
123ee8
        # Add changes for next commit.
123ee8
        ${COMMAND} add ${LOCATION}
123ee8
123ee8
        # Commit changes up to local repository.
123ee8
        ${COMMAND} commit ${LOCATION}
123ee8
123ee8
    fi
123ee8
123ee8
    # When there are unversioned files in the target location, show
123ee8
    # which these files are and request user to add such files into
123ee8
    # the working copy.
123ee8
    if [[ ${FILESNUM[1]} -gt 0 ]];then
123ee8
123ee8
        # Print action message.
123ee8
        cli_printMessage '-' --as-separator-line
123ee8
        cli_printMessage "`gettext "Do you want to add unversioned files now?"`" --as-yesornorequest-line
123ee8
123ee8
        # Add unversioned files to be considered in the next commit.
123ee8
        for FILE in ${FILES[1]};do
123ee8
            ${COMMAND} add "${TCAR_WORKDIR}/$FILE"
123ee8
        done
123ee8
123ee8
        # Print action message.
123ee8
        cli_printMessage "`gettext "Do you want to commit changes now?"`" --as-yesornorequest-line
123ee8
123ee8
        # Commit changes up to local repository.
123ee8
        ${COMMAND} commit ${LOCATION}
123ee8
123ee8
    fi
123ee8
123ee8
    # When there are added files in the target location, show which
123ee8
    # these files are and request user to commit them up to local
123ee8
    # repository.
123ee8
    if [[ ${FILESNUM[3]} -gt 0 ]];then
123ee8
        cli_printMessage '-' --as-separator-line
123ee8
        cli_printMessage "`gettext "Do you want to commit changes now?"`" --as-yesornorequest-line
123ee8
        ${COMMAND} commit ${LOCATION}
123ee8
    fi
123ee8
123ee8
}