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

Make Badge newline configurable or only update if content changed #40

Closed
s-weigand opened this issue Aug 2, 2020 · 4 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@s-weigand
Copy link
Contributor

Describe the feature you'd like

First of all thanks for the awesome tool 😄
I would love to use the badge creation feature as a pre-push hook in my projects, so the badge is always up to date.
But since each major OS has its own newline (Linux \n, MacOs '\r' and Windows \r\n), creating a new badge on push can lead to a diff, which basically is empty, blocking the push.
Since my projects (as most others I guess) are setup to have \n as newline, it would be nice to have badge_newline as a configurable option (this can be done with a kwarg in open).

Alternative solution

Another alternative would be to check if the text in interrogate_badge.svg did change and only write changes if that is the case.
That way the new line cofiguration would be left to the user (i.e. via .gitattributes), an not clutter the CLI and config of interrogate.

Is your feature request related to a problem?

Badge creation blocks usage with pre-commit.

Your Environment

  • interrogate version(s) (interrogate --version: 1.2.0
  • Operating System(s): Win10
  • Python version(s): 3.7.6
@s-weigand s-weigand added enhancement New feature or request needs triage Issue needs triaging labels Aug 2, 2020
@econchick
Copy link
Owner

Hey @s-weigand ! My apologies for not following up on this issue earlier.

This seems like a common issue when folks collaborate using different OSes, not necessarily something that interrogate should deal with specifically. But maybe I'm confused: if git does CRLF normalization (via .gitattributes or globally as you pointed out), why would a tool like interrogate need to adapt?

@s-weigand
Copy link
Contributor Author

The problem I have is, when I want to run it with pre-commit, to keep my badge always up to date.

My use case is on Windows with gitbash.
To me it looks like the end of line replacement done by git does happen when you stage the files.
So when pre-commit runs again the lines have a \n ending, but since interrogate always writes the badge I end up with a new file with \r\n ending which git sees a diff. Thus, I'm stuck in a cycle never being able to push or commit (depending on which point you set the badge creation).

def save_badge(badge, output):
"""Save badge to the specified path.
:param str badge: SVG contents of badge.
:param str output: path to output badge file.
:return: path to output badge file.
:rtype: str
"""
if os.path.isdir(output):
output = os.path.join(output, DEFAULT_FILENAME)
with open(output, "w") as f:
f.write(badge)
return output

IMHO the most elegant way to solve this, would be an "only update if needed"/"lazy" strategy and since this package is already python 3.5+ one could use pathlib and the check could (... I guess) be as nice and clean as:

if bage_path.read_text() == badge:
    bage_path.write_text(badge)

If you like the idea of the "lazy" strategy, I can give it a try in a PR.

Reproducing the problem

To reproduce this you will need a Windows or MacOs system (vm for windows/no easy way to spin up MacOs in a VM for testing I know) or change your line ending in .gitattributes.

  • Clone a git repo
  • Install pre-commit and add a .pre-commit-config.yaml file:
    repos:
    - repo: https://github.com/econchick/interrogate
      rev: 1.2.0
      hooks:
        - id: interrogate
          entry: interrogate -g <badge_path> <project_name>
          exclude: "docs|tests"
  • Run pre-commit (simmulating commit/push)
    $ pre-commit run --all
    interrogate..............................................................Failed
    - hook id: interrogate
    - files were modified by this hook
    ...
    <interrogate PASS messages for all files>
  • Add the new files with git add . (just for example sake)
    $ git add .
    warning: CRLF will be replaced by LF in docs/_static/interrogate_badge.svg.
    The file will have its original line endings in your working directory
  • Check the status
    $ git status
    On branch add-precommit-hooks
    Your branch is up to date with 'origin/add-precommit-hooks'.
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
          modified:   .pre-commit-config.yaml
          modified:   docs/_static/interrogate_badge.svg
  • Run pre-commit again (simmulating commit/push)
    $ pre-commit run --all
    interrogate..............................................................Failed
    - hook id: interrogate
    - files were modified by this hook
    ...
    <interrogate PASS messages for all files>
  • Check the status again
    $ git status                                                         
    On branch add-precommit-hooks                                        
    Your branch is up to date with 'origin/add-precommit-hooks'.         
                                                                       
    Changes to be committed:                                             
      (use "git restore --staged <file>..." to unstage)                  
          modified:   .pre-commit-config.yaml                          
          modified:   docs/_static/interrogate_badge.svg               
                                                                       
    Changes not staged for commit:                                       
      (use "git add <file>..." to update what will be committed)         
      (use "git restore <file>..." to discard changes in working director
          modified:   docs/_static/interrogate_badge.svg               

@econchick
Copy link
Owner

Hmm yeah that does make sense now; thank you for the explanation. It didn't connect with me first with the fact that pre-commit will run before git and therefore rendering .gitattributes useless in this case.

I like the "lazy" idea better - adding another CLI arg seems a bit more clumsy IMO. But I will definitely appreciate a PR if you have some time to spare!

Thanks!

@s-weigand s-weigand mentioned this issue Aug 25, 2020
7 tasks
econchick added a commit that referenced this issue Sep 3, 2020
Instead of always generating a badge, first check to see if there’s an existing badge, and if so, do not generate a badge if the existing badge contains the same results. Addresses #40
@econchick econchick removed the needs triage Issue needs triaging label Sep 3, 2020
@econchick
Copy link
Owner

Hopefully is addressed in #47 in version 1.3.1. Please reopen if not!

Digitalintf added a commit to Digitalintf/interrogate that referenced this issue Aug 4, 2024
Instead of always generating a badge, first check to see if there’s an existing badge, and if so, do not generate a badge if the existing badge contains the same results. Addresses econchick/interrogate#40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants