Blame Automation/centos-art.sh-mods/Vcs/Git/git_commitRepoChanges.sh

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