arnoldssss
arnoldssss

Reputation: 488

Git hook precommit doesn't working

It is a git hook for precommit It is assumed to execute esformatter command for every file modified (staged),

#!/bin/sh

for fil in $(git diff --name-only --staged | grep ".js"); do
 esformatter -i$ fil & git add $fil;
done

git commit -m "Adding format to files"

Actually I wanted that this code is executed in the same commit but I don't know how to do it. The code actually works until the done part. But the files are not added to the commit obviusly, so that's why I need the commit part.

Regards

Upvotes: 1

Views: 167

Answers (2)

Ajk_P
Ajk_P

Reputation: 1874

First off think if this is the route you want to take. Modifying people's commits on the fly, is likely to cause hard to untangle messes down the road.

Secondly, if you want to to leave the changes in the same commit, you can simply remove the git commit -m "Adding format to files" line, so there's no separate commit done by the githook.

If that doesn't work, you could try to add it as a post-commit hook and do git commit --amend

Upvotes: 2

Arne Vogel
Arne Vogel

Reputation: 6676

You are not adding the file after modification.

esformatter -i$ fil & git add $fil

You probably intended to use && here, not &. & will launch the formatter in the background, and, concurrently, stage the current contents of the file (which may not be formatted yet, or be in an unpredictable state even). I.e.:

  • task1 && task2 – execute task1 synchronously, then execute task2 if task1 was successful
  • task1 & task2 – execute task1 asynchronously and also execute task2 (in this case, task2 is executed synchronously because it is followed by ; but newline has the same meaning)

Upvotes: 0

Related Questions