Bug 577333 - CommitCommand with option 'all' should add files before executing the preCommit hooks
Summary: CommitCommand with option 'all' should add files before executing the preComm...
Status: NEW
Alias: None
Product: JGit
Classification: Technology
Component: JGit (show other bugs)
Version: 5.13   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-18 08:38 EST by Reda Housni Alaoui CLA
Modified: 2021-11-18 08:38 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Reda Housni Alaoui CLA 2021-11-18 08:38:49 EST
I am the author of a precommit formatting library (https://github.com/Cosium/git-code-format-maven-plugin/). The hook formats the staged files.

Everything works fine except when using commit 'all' option.
With the classic git cli 'git commit -a' works fine. But with jGit, the hook is triggered before adding the files to staging:

```
if (!noVerify) {
				Hooks.preCommit(repo, hookOutRedirect.get(PreCommitHook.NAME),
						hookErrRedirect.get(PreCommitHook.NAME))
						.call();
			}

			processOptions(state, rw);

			if (all && !repo.isBare()) {
				try (Git git = new Git(repo)) {
					git.add().addFilepattern(".") //$NON-NLS-1$
							.setUpdate(true).call();
				} catch (NoFilepatternException e) {
					// should really not happen
					throw new JGitInternalException(e.getMessage(), e);
				}
			}
```

Because of that, the hook does not see any staged file and therefore does not format them.

I think that the files should be added to staging before hooks precommit execution.