From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jon Seymour Subject: [PATCH 2/3] Reorganization of git-commit-script [rev 4] Date: Thu, 16 Jun 2005 00:38:11 +1000 Message-ID: <20050615143811.27248.qmail@blackcubes.dyndns.org> Cc: jon.seymour@gmail.com X-From: git-owner@vger.kernel.org Wed Jun 15 16:35:21 2005 Return-path: Received: from vger.kernel.org ([12.107.209.244]) by ciao.gmane.org with esmtp (Exim 4.43) id 1DiYyY-0004au-A2 for gcvg-git@gmane.org; Wed, 15 Jun 2005 16:34:42 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S261525AbVFOOjh (ORCPT ); Wed, 15 Jun 2005 10:39:37 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S261464AbVFOOjc (ORCPT ); Wed, 15 Jun 2005 10:39:32 -0400 Received: from 203-166-247-224.dyn.iinet.net.au ([203.166.247.224]:26497 "HELO blackcubes.dyndns.org") by vger.kernel.org with SMTP id S261519AbVFOOiN (ORCPT ); Wed, 15 Jun 2005 10:38:13 -0400 Received: (qmail 27258 invoked by uid 500); 15 Jun 2005 14:38:11 -0000 To: git@vger.kernel.org Sender: git-owner@vger.kernel.org Precedence: bulk X-Mailing-List: git@vger.kernel.org This change re-organizes git-commit-script to break out the different phases of execution into separate bash functions. The main body is now: if ! print_status > .editmsg; then # error handling fi if edit_message .editmsg .cmitmsg; then exec_commit .cmitmsg # cleanup fi The subsequent patch in this series adds support for an optional .nextmsg file to enable the pre-population of commit message text by external tools. Signed-off-by: Jon Seymour --- git-commit-script | 110 ++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 75 insertions(+), 35 deletions(-) diff --git a/git-commit-script b/git-commit-script --- a/git-commit-script +++ b/git-commit-script @@ -1,44 +1,84 @@ #!/bin/sh + +initial_commit() { +cat <&2 + return 1 + fi + initial_commit + else + if [ -f $GIT_DIR/MERGE_HEAD ]; then + merge_commit + fi + git-status-script + fi +} + +edit_message() { + status=$1 + msg=$2 + + :> $msg + ${VISUAL:-${EDITOR:-vi}} "$status" + grep -v '^#' < $status | git-stripspace >$msg + [ -s $msg ] || exit 1 +} + +commit_parents() { + [ -f $GIT_DIR/HEAD ] && + echo -n "-p HEAD" && + [ -f $GIT_DIR/MERGE_HEAD ] && + echo -n " -p MERGE_HEAD" + echo "" +} + +exec_commit() { + msg=$1 + tree=$(git-write-tree) || exit 1 + parents=$(commit_parents) || exit 1 + commit=$(cat $msg | git-commit-tree $tree $parents) || exit 1 + echo $commit > $GIT_DIR/HEAD + rm -f -- $GIT_DIR/MERGE_HEAD +} + +SENTINEL="#SENTINEL - delete this line to confirm the commit" : ${GIT_DIR=.git} if [ ! -d $GIT_DIR ]; then echo Not a git directory 1>&2 exit 1 fi -PARENTS="-p HEAD" -if [ ! -r $GIT_DIR/HEAD ]; then - if [ -z "$(git-ls-files)" ]; then - echo Nothing to commit 1>&2 - exit 1 - fi - ( - echo "#" - echo "# Initial commit" - echo "#" - git-ls-files | sed 's/^/# New file: /' - echo "#" - ) > .editmsg - PARENTS="" -else - if [ -f $GIT_DIR/MERGE_HEAD ]; then - echo "#" - echo "# It looks like your may be committing a MERGE." - echo "# If this is not correct, please remove the file" - echo "# $GIT_DIR/MERGE_HEAD" - echo "# and try again" - echo "#" - PARENTS="-p HEAD -p MERGE_HEAD" - fi > .editmsg - git-status-script >> .editmsg -fi -if [ "$?" != "0" ] -then + +if ! print_status > .editmsg; then cat .editmsg + rm .editmsg exit 1 fi -${VISUAL:-${EDITOR:-vi}} .editmsg -grep -v '^#' < .editmsg | git-stripspace > .cmitmsg -[ -s .cmitmsg ] || exit 1 -tree=$(git-write-tree) || exit 1 -commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) || exit 1 -echo $commit > $GIT_DIR/HEAD -rm -f -- $GIT_DIR/MERGE_HEAD + +if edit_message .editmsg .cmitmsg; then + exec_commit .cmitmsg + [ -f .editmsg ] && rm .editmsg + [ -f .cmitmsg ] && rm .cmitmsg + exit 0 +fi ------------