threads / patch / 43444

patchMake git-commit cleverer - have it figure out whether it needs -a automatically

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

## tl;dr

7 messages between Nov 30, 2006 and Dec 1, 2006. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Andy Parkins· Nov 30, 2006, 13:24 UTC · lore
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(-)
Show changes to git-commit.sh +8 −0
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· Nov 30, 2006, 13:32 UTC · re: Andy Parkins · lore

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

Show 13 quoted lines
> 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).

Show 8 quoted lines
> +       all=t
> +fi
> +
>  ################################################################
>  # Sanity check options
>
> --
> 1.4.4.1.g3ece-dirty
Jakub Narebski· Nov 30, 2006, 13:41 UTC · re: Nguyen Thai Ngoc Duy · lore

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

Nguyen Thai Ngoc Duy wrote:
Show 8 quoted lines
>> +# 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· Nov 30, 2006, 15:01 UTC · re: Nguyen Thai Ngoc Duy · lore
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(-)
Show changes to git-commit.sh +10 −0
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· Nov 30, 2006, 15:43 UTC · re: Andy Parkins · lore

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

Andy Parkins wrote:
Show 6 quoted lines
> 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· Nov 30, 2006, 16:28 UTC · re: Andy Parkins · lore

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

Andy Parkins wrote:
Show 14 quoted lines
> 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· Dec 1, 2006, 10:59 UTC · re: Jakub Narebski · lore

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

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

← back to recent threads