Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre-commit hooks #62

Open
mauvilsa opened this issue Sep 1, 2023 · 8 comments
Open

pre-commit hooks #62

mauvilsa opened this issue Sep 1, 2023 · 8 comments

Comments

@mauvilsa
Copy link
Contributor

mauvilsa commented Sep 1, 2023

In bump2version, when bumping and commit is enabled, if the repository has a pre-commit hook setup, then it is run as part of the bump process. I was trying to change from bump2version to bump-my-version a repo that relies on the pre-commit hook being run. However, when testing with bump-my-version, the pre-commit hook was not run.

Is it a design decision that pre-commit hooks are not run during bump? Or does it need to be configured so that they get run?

The use case that I have is updating the release date in the changelog. So maybe with bump-my-version I don't need the pre-commit hook. The exact behavior that I want is:

  • Change ^v{new-version} (.*) to v{new-version} (YYYY-MM-DD).
  • If ^v{new-version} (.*) not found in the changelog, then abort bump with an appropriate message.

Is this behavior possible with bump-my-version?

Though, independent of the changelog date, the pre-commit hook is also needed. More specifically, when a bump is run, then a python package is built and then checked with twine check dist/*.whl. There is no reason to run this check for every commit that will not be a release. In the pre-commit hook, the BUMPVERSION_NEW_VERSION environment variable is used to determine whether the check needs to be executed or not.

@coordt
Copy link
Member

coordt commented Sep 4, 2023

Pre-commit hooks are run during the bump process unless you have:

[tool.bumpversion]
commit_args = "--no-verify"

I usually suggest this to avoid getting into strange states because of an error in the pre-commit.

If you need to change the date and also not fail during a missing version:

As for triggering the package build, You should be able to accomplish it by leaving commit_args out of your configuration. I would suggest looking at automating it with a GitHub action that triggers whenever a tag is pushed to the repo.

@mauvilsa
Copy link
Contributor Author

mauvilsa commented Sep 4, 2023

@coordt thank you for the response!

I don't have commit_args = "--no-verify" in my config. And still, the pre-commit hook did not run. Since it is supposed to run, how can I figure out the reason why it doesn't?

@coordt
Copy link
Member

coordt commented Sep 4, 2023

@mauvilsa That depends on how your pre-commit hooks are set up. Are you using pre-commit?

@mauvilsa
Copy link
Contributor Author

mauvilsa commented Sep 4, 2023

Yes, it is using pre-commit. The repo I am trying this out with is jsonargparse.

@coordt
Copy link
Member

coordt commented Sep 5, 2023

I can only make more guesses.

Looking at the repo, I didn't find where you might be running the bumpversion command. During automation, you have to make sure you initialize pre-commit on the local checkout for it to work.

The only reason I can think of for pre-commit not running on a commit is that it isn't initialized.

@mauvilsa
Copy link
Contributor Author

mauvilsa commented Sep 6, 2023

Version bumping is not automatized. I run it locally. And for sure pre-commit is initialized.

Is the output of pre-commit supposed to be shown in the output? What I get when I try to bump for example is:

$ bump-my-version bump --verbose minor
Starting BumpVersion 0.10.0                                                                                                                                           
Reading config file pyproject.toml:                                                                                                                                   
Parsing version '4.24.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                
Parsed the following values: major=4, minor=24, patch=1                                                                                                               
Attempting to increment part 'minor'                                                                                                                                  
Values are now: major=4, minor=25, patch=0                                                                                                                            
New version will be '4.25.0'                                                                                                                                          
Asserting files .sonarcloud.properties, jsonargparse/__init__.py contain the version string...                                                                        
Found 're.compile('4\\.24\\.1', re.MULTILINE|re.DOTALL)' in jsonargparse/__init__.py at line 71: 4.24.1                                                               
Found 're.compile('4\\.24\\.1', re.MULTILINE|re.DOTALL)' in .sonarcloud.properties at line 2: 4.24.1                                                                  
...
Preparing Git commit                                                                                                                                                  
Adding changes in file 'pyproject.toml' to Git                                                                                                                        
Adding changes in file '.sonarcloud.properties' to Git                                                                                                                
Adding changes in file 'jsonargparse/__init__.py' to Git                                                                                                              
Committing to Git with message 'Bump version: 4.24.1 → 4.25.0'                                                                                                        
Would not tag

It seems that pre-commit is run, but its normal output is not shown. And most problematic is that if pre-commit fails, then instead of seeing which hook fails, there is a quite unfriendly message. For example one test that I did was to modify some file and then:

$ bump-my-version bump --allow-dirty --verbose minor
Starting BumpVersion 0.10.0                                                                                                                                           
Reading config file pyproject.toml:                                                                                                                                   
Parsing version '4.24.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                
Parsed the following values: major=4, minor=24, patch=1                                                                                                               
Attempting to increment part 'minor'                                                                                                                                  
Values are now: major=4, minor=25, patch=0                                                                                                                            
New version will be '4.25.0'                                                                                                                                          
Asserting files .sonarcloud.properties, jsonargparse/__init__.py contain the version string...                                                                        
Found 're.compile('4\\.24\\.1', re.MULTILINE|re.DOTALL)' in jsonargparse/__init__.py at line 71: 4.24.1                                                               
Found 're.compile('4\\.24\\.1', re.MULTILINE|re.DOTALL)' in .sonarcloud.properties at line 2: 4.24.1                                                                  
...
Preparing Git commit                                                                                                                                                  
Adding changes in file 'pyproject.toml' to Git                                                                                                                        
Adding changes in file '.sonarcloud.properties' to Git                                                                                                                
Adding changes in file 'jsonargparse/__init__.py' to Git                                                                                                              
Committing to Git with message 'Bump version: 4.24.1 → 4.25.0'                                                                                                        
Failed to run ['git', 'commit', '-F', '/tmp/tmpdqtqmkf6']: return code 1, output: b''                                                                                 
╭──────────────────────────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────────────────────────╮
│ /home/mvillegas/repos/github/jsonargparse/venv-bump-my-version/lib/python3.10/site-packages/bumpversion/scm.py:66 in commit                                        │
│                                                                                                                                                                    │
│    63 │   │                                                                                                                                                        │
│    64 │   │   try:                                                                                                                                                 │
│    65 │   │   │   cmd = [*cls._COMMIT_COMMAND, f.name, *extra_args]                                                                                                │
│ ❱  66 │   │   │   subprocess.run(cmd, env=env, capture_output=True, check=True)  # noqa: S603                                                                      │
│    67 │   │   except subprocess.CalledProcessError as exc:  # pragma: no-coverage                                                                                  │
│    68 │   │   │   err_msg = f"Failed to run {exc.cmd}: return code {exc.returncode}, output:                                                                       │
│       {exc.output}"                                                                                                                                                │
│    69 │   │   │   logger.exception(err_msg)                                                                                                                        │
│                                                                                                                                                                    │
│ /usr/lib/python3.10/subprocess.py:526 in run                                                                                                                       │
│                                                                                                                                                                    │
│    523 │   │   │   raise                                                                                                                                           │
│    524 │   │   retcode = process.poll()                                                                                                                            │
│    525 │   │   if check and retcode:                                                                                                                               │
│ ❱  526 │   │   │   raise CalledProcessError(retcode, process.args,                                                                                                 │
│    527 │   │   │   │   │   │   │   │   │    output=stdout, stderr=stderr)                                                                                          │
│    528 │   return CompletedProcess(process.args, retcode, stdout, stderr)                                                                                          │
│    529                                                                                                                                                             │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
CalledProcessError: Command '['git', 'commit', '-F', '/tmp/tmpdqtqmkf6']' returned non-zero exit status 1.                                                            
Traceback (most recent call last):
  File "/.../venv-bump-my-version/bin/bump-my-version", line 8, in <module>
    sys.exit(cli())
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/rich_click/rich_group.py", line 21, in main
    rv = super().main(*args, standalone_mode=False, **kwargs)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/bumpversion/cli.py", line 309, in bump
    do_bump(version_part, new_version, config, found_config_file, dry_run)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/bumpversion/bump.py", line 88, in do_bump
    commit_and_tag(config, config_file, configured_files, ctx, dry_run)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/bumpversion/bump.py", line 117, in commit_and_tag
    config.scm_info.tool.commit_to_scm(list(commit_files), config, ctx, extra_args, dry_run)
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/bumpversion/scm.py", line 156, in commit_to_scm
    cls.commit(
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/bumpversion/scm.py", line 70, in commit
    raise exc
  File "/.../venv-bump-my-version/lib/python3.10/site-packages/bumpversion/scm.py", line 66, in commit
    subprocess.run(cmd, env=env, capture_output=True, check=True)  # noqa: S603
  File "/usr/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['git', 'commit', '-F', '/tmp/tmpdqtqmkf6']' returned non-zero exit status 1.

@coordt
Copy link
Member

coordt commented Sep 9, 2023

The output of the git command isn't supposed to be output to the user. However a better error message is necessary.

How about this: we divide this into two different feature requests.

First, better error messages on git command failures.

Second, expand verbosity to include output from git commands.

How does that sound?

@mauvilsa
Copy link
Contributor Author

If the git commit output isn't supposed to be seen, at least the output should say something like "running git commit (output only expected on failure)". Otherwise, users will not know what is expected, the same as happened to me. The only way for me to know was to force a fail in pre-commit. And if the git command fails, then show the output exactly as if the same command would have been run without bump-my-version.

If verbosity is enabled, always showing the git output would be good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants