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

123ee8
#!/bin/bash
123ee8
#
de6ddf
# git_commitRepoChanges.sh -- This function standardizes the way local
de6ddf
# changes are committed up to central repository.
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
de6ddf
    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.
de6ddf
    FILES[0]=$(echo "$STATUSOUT" | egrep "^[[:space:]]M")
de6ddf
    FILES[1]=$(echo "$STATUSOUT" | egrep "^\?\?")
de6ddf
    FILES[2]=$(echo "$STATUSOUT" | egrep "^[[:space:]]D")
de6ddf
    FILES[3]=$(echo "$STATUSOUT" | egrep "^[[:space:]]A")
de6ddf
    FILES[4]=$(echo "$STATUSOUT" | egrep "^(A|M|R|C)( |M|D)")
123ee8
123ee8
    # Define description of files considered recent modifications from
123ee8
    # working copy up to local repository.
123ee8
    INFO[0]="`gettext "Modified"`"
de6ddf
    INFO[1]="`gettext "Untracked"`"
123ee8
    INFO[2]="`gettext "Deleted"`"
123ee8
    INFO[3]="`gettext "Added"`"
de6ddf
    INFO[4]="`gettext "Staged"`"
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
de6ddf
    # Stage files
de6ddf
    cli_printMessage "`gettext "Do you want to stage files?"`" --as-yesornorequest-line
de6ddf
    ${COMMAND} add ${LOCATION}
123ee8
de6ddf
    # See staged differences.
de6ddf
    cli_printMessage "`gettext "Do you want to see staged files differences?"`" --as-yesornorequest-line
de6ddf
    ${COMMAND} diff --staged ${LOCATION} | less
123ee8
de6ddf
    # Commit staged files.
de6ddf
    cli_printMessage "`gettext "Do you want to commit staged files differences?"`" --as-yesornorequest-line
de6ddf
    ${COMMAND} commit ${LOCATION}
123ee8
de6ddf
    # Push committed files.
de6ddf
    cli_printMessage "`gettext "Do you want to push committed files?"`" --as-yesornorequest-line
de6ddf
    ${COMMAND} push 
123ee8
123ee8
}