# [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically

7 messages from 2006-11-30 to 2006-12-01. Participants: Andy Parkins, Salikh Zakirov, Nguyen Thai Ngoc Duy, Jakub Narebski.
Thread: https://gitlist.dev/t/43444

## Andy Parkins, 2006-11-30 13:24

Subject: [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically
Message-ID: <200611301324.04993.andyparkins@gmail.com>
URL: https://gitlist.dev/e/200611301324.04993.andyparkins%40gmail.com
In-Reply-To: <ekmlar$ask$2@sea.gmane.org>

```
Raimund Bauer offered this suggestion (paraphrased):

"Maybe we could do git-commit -a  _only_ if the index matches HEAD, and
otherwise keep current behavior?  So people who don't care about the
index won't get tripped up, and when you do have a dirty index, you get
told about it?"

Johannes Schindelin pointed out that this isn't the right thing to do for
an --amend, so that is checked for.

Additionally, it's probably not the right thing to do if any files are
specified with "--only" or "--include", so they turn this behaviour off
as well.

I've also output a message as suggested by Andreas Ericsson.

Signed-off-by: Andy Parkins <andyparkins@gmail.com>
---
 git-commit.sh |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/git-commit.sh b/git-commit.sh
index 81c3a0c..fabfeae 100755
--- a/git-commit.sh
+++ b/git-commit.sh
@@ -265,6 +265,14 @@ $1"
 done
 case "$edit_flag" in t) no_edit= ;; esac
 
+# Clever commit - if this commit would do nothing, then make it an "all"
+# commit
+if [ -z "$(git-diff-index --cached --name-only HEAD)" \
+	-a -z "$amend" -a -z "$only" -a -z "$also" ]; then
+	echo "Nothing to commit but changes in working tree. Assuming 'git commit -a'"
+	all=t
+fi
+
 ################################################################
 # Sanity check options
 
-- 
1.4.4.1.g3ece-dirty

```

## Nguyen Thai Ngoc Duy, 2006-11-30 13:32

Subject: Re: [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically
Message-ID: <fcaeb9bf0611300532x77c7fc8aq2ba77ff57b81cc05@mail.gmail.com>
URL: https://gitlist.dev/e/fcaeb9bf0611300532x77c7fc8aq2ba77ff57b81cc05%40mail.gmail.com
In-Reply-To: <200611301324.04993.andyparkins@gmail.com>

```
> diff --git a/git-commit.sh b/git-commit.sh
> index 81c3a0c..fabfeae 100755
> --- a/git-commit.sh
> +++ b/git-commit.sh
> @@ -265,6 +265,14 @@ $1"
>  done
>  case "$edit_flag" in t) no_edit= ;; esac
>
> +# Clever commit - if this commit would do nothing, then make it an "all"
> +# commit
> +if [ -z "$(git-diff-index --cached --name-only HEAD)" \
> +       -a -z "$amend" -a -z "$only" -a -z "$also" ]; then
> +       echo "Nothing to commit but changes in working tree. Assuming 'git commit -a'"

This is hardly seen as the editor will immediately pop up. Better
pause a second or put it in commit template (I'd prefer the latter).

> +       all=t
> +fi
> +
>  ################################################################
>  # Sanity check options
>
> --
> 1.4.4.1.g3ece-dirty
-- 

```

## Jakub Narebski, 2006-11-30 13:41

Subject: Re: [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically
Message-ID: <ekmmvf$hsr$1@sea.gmane.org>
URL: https://gitlist.dev/e/ekmmvf%24hsr%241%40sea.gmane.org
In-Reply-To: <fcaeb9bf0611300532x77c7fc8aq2ba77ff57b81cc05@mail.gmail.com>

```
Nguyen Thai Ngoc Duy wrote:

>> +# Clever commit - if this commit would do nothing, then make it an "all"
>> +# commit
>> +if [ -z "$(git-diff-index --cached --name-only HEAD)" \
>> +       -a -z "$amend" -a -z "$only" -a -z "$also" ]; then
>> +       echo "Nothing to commit but changes in working tree. Assuming 'git commit -a'"
> 
> This is hardly seen as the editor will immediately pop up. Better
> pause a second or put it in commit template (I'd prefer the latter).

Well, if it is VISUAL editor, you would see this. But adding this
to template is certainly good idea.
-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git


```

## Andy Parkins, 2006-11-30 15:01

Subject: [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically
Message-ID: <200611301501.43436.andyparkins@gmail.com>
URL: https://gitlist.dev/e/200611301501.43436.andyparkins%40gmail.com
In-Reply-To: <fcaeb9bf0611300532x77c7fc8aq2ba77ff57b81cc05@mail.gmail.com>

```
Raimund Bauer offered this suggestion (paraphrased):

"Maybe we could do git-commit -a  _only_ if the index matches HEAD, and
otherwise keep current behavior?  So people who don't care about the
index won't get tripped up, and when you do have a dirty index, you get
told about it?"

Johannes Schindelin pointed out that this isn't the right thing to do for
an --amend, so that is checked for. Additionally, it's probably not the
right thing to do if any files are specified with "--only" or
"--include", so they turn this behaviour off as well.

Nguyen Thai Ngoc Duy asked that git-commit let you know it's done this
by adding an extra comment to the commit message.

Signed-off-by: Andy Parkins <andyparkins@gmail.com>
---
 git-commit.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/git-commit.sh b/git-commit.sh
index 81c3a0c..b391257 100755
--- a/git-commit.sh
+++ b/git-commit.sh
@@ -265,6 +265,16 @@ $1"
 done
 case "$edit_flag" in t) no_edit= ;; esac
 
+# Clever commit - if this commit would do nothing, then make it an "all"
+# commit
+if [ -z "$(git-diff-index --cached --name-only HEAD)" \
+	-a -z "$amend" -a -z "$only" -a -z "$also" ]; then
+	echo "# There was nothing to commit but changes were detected in the" > $GIT_DIR/SQUASH_MSG
+	echo "# working tree. 'git commit -a' mode activated." >> $GIT_DIR/SQUASH_MSG
+	echo "#" >> $GIT_DIR/SQUASH_MSG
+	all=t
+fi
+
 ################################################################
 # Sanity check options
 
-- 
1.4.4.1.g3ece-dirty

```

## Salikh Zakirov, 2006-11-30 15:43

Subject: Re: [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically
Message-ID: <ekmu7u$ddh$1@sea.gmane.org>
URL: https://gitlist.dev/e/ekmu7u%24ddh%241%40sea.gmane.org
In-Reply-To: <200611301501.43436.andyparkins@gmail.com>

```
Andy Parkins wrote:
> Raimund Bauer offered this suggestion (paraphrased):
> 
> "Maybe we could do git-commit -a  _only_ if the index matches HEAD, and
> otherwise keep current behavior?  So people who don't care about the
> index won't get tripped up, and when you do have a dirty index, you get
> told about it?"

Brilliant solution!

```

## Jakub Narebski, 2006-11-30 16:28

Subject: Re: [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically
Message-ID: <ekn0ne$nuk$1@sea.gmane.org>
URL: https://gitlist.dev/e/ekn0ne%24nuk%241%40sea.gmane.org
In-Reply-To: <200611301501.43436.andyparkins@gmail.com>

```
Andy Parkins wrote:

> Raimund Bauer offered this suggestion (paraphrased):
> 
> "Maybe we could do git-commit -a  _only_ if the index matches HEAD, and
> otherwise keep current behavior?  So people who don't care about the
> index won't get tripped up, and when you do have a dirty index, you get
> told about it?"
> 
> Johannes Schindelin pointed out that this isn't the right thing to do for
> an --amend, so that is checked for. Additionally, it's probably not the
> right thing to do if any files are specified with "--only" or
> "--include", so they turn this behaviour off as well.
> 
> Nguyen Thai Ngoc Duy asked that git-commit let you know it's done this
> by adding an extra comment to the commit message.

Insount on #git pointed out fragility of this solution with respect
to adding/removing/moving files, which dirties index (which might not be
understood by newbie user: "git commit" used to work, but doesn't work the
same when I added some files).
-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git


```

## Andy Parkins, 2006-12-01 10:59

Subject: Re: [PATCH] Make git-commit cleverer - have it figure out whether it needs -a automatically
Message-ID: <200612011059.01659.andyparkins@gmail.com>
URL: https://gitlist.dev/e/200612011059.01659.andyparkins%40gmail.com
In-Reply-To: <ekn0ne$nuk$1@sea.gmane.org>

```
On Thursday 2006 November 30 16:28, Jakub Narebski wrote:

> Insount on #git pointed out fragility of this solution with respect
> to adding/removing/moving files, which dirties index (which might not be
> understood by newbie user: "git commit" used to work, but doesn't work the
> same when I added some files).

It does, provided they only added files and didn't change anything else.  If 
they did then we're out of it for this patch anyway.

Anyway, this is only meant to help ease people into the index.  As discussed 
elsewhere, hiding the index is a silly policy.

This patch isn't /just/ for the newbies by the way (who are already confused, 
so that hasn't changed), I know about the index, but I still like it.  
Remember, all it's really saying is "when commit would do nothing, do 
something".  So it only takes away an option that you can't have been using 
anyway because it didn't do anything.


Andy
-- 
Dr Andy Parkins, M Eng (hons), MIEE

```
