GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: fix incoherent beginning whitespace by cardoeng · Pull Request #1933 · gitpython-developers/GitPython

Conversation

@cardoeng

@cardoeng cardoeng commented Jun 18, 2024

Copy link
Copy Markdown
Contributor

Some repositories contain files / directories beginning by a space in their history (example : react-component/field-form@0e81dc0).

The current version of GitPython returns this space sometimes when doing a diff (depending on whether the space is in a_path or b_path).

Python 3.12.3 [...] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import git
>>> git.Repo.clone_from("https://github.com/react-component/field-form", "field-form") # example repository
<git.repo.base.Repo '/tmp/field-form/.git'>
>>> r = git.Repo("field-form")
>>> c = r.commit("0e81dc0d69d198d644b44eb4f84d875777c03581") # commit where there is a space
>>> d1 = c.diff(c.parents[0])[0] # first diff
>>> d1.a_path
'.github/workflows/main.yml'
>>> d1.b_path
' .github/workflows/main.yml' # note the space in the beginning
>>> d2 = c.parents[0].diff(c)[0] # same diff but inverted commits
>>> d2.a_path
'.github/workflows/main.yml' # there is no space
>>> d2.b_path
'.github/workflows/main.yml'

This is due to a strip being done to the path (see the only line changed in diff.py). This PR make a simple change by not stripping the spaces, but only new lines (we can also add other characters if needed) so the behavior stays coherent and the white space is given when doing a diff. An alternative would be to remove the strip (some tests started failing when doing so...).

>>> r = git.Repo("field-form")
>>> c = r.commit("0e81dc0d69d198d644b44eb4f84d875777c03581")
>>> d1 = c.diff(c.parents[0])[0] 
>>> d1.a_path
'.github/workflows/main.yml'
>>> d1.b_path
' .github/workflows/main.yml' # note the space
>>> d2 = c.parents[0].diff(c)[0]
>>> d2.a_path
' .github/workflows/main.yml' # the space is there
>>> d2.b_path
'.github/workflows/main.yml'

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
cardoeng added 3 commits June 17, 2024 09:28
@cardoeng
fix: fix beginning whitespace error
d35998f
@cardoeng
test: add test for diff with beginning whitespace
9910a88
@cardoeng
style: ruff
97fad9c
Byron
Byron approved these changes Jun 18, 2024
View reviewed changes

@Byron Byron left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this fix, that's better!

I wonder if the strip("\n") has to happen at all, it's unusual to have newlines in paths anyway, and if they are there, they should not be removed.

In other words, what happens if there is no strip() call?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Byron Byron added the waiting for feedback label Jun 18, 2024
@cardoeng

cardoeng commented Jun 18, 2024

Copy link
Copy Markdown
Contributor Author

I tried removing the strip() call, and it seems two tests related to copied or renamed files during diff started failing :

FAILED test/test_diff.py::TestDiff::test_diff_with_copied_file - AssertionError: 'test2.txt\n' != 'test2.txt'
FAILED test/test_diff.py::TestDiff::test_diff_with_rename - AssertionError: 'that\n' != 'that'

A \n seems to be added at the end of a_path and b_path in certain cases if we remove completely the strip(). (I am not fully familiar with the internal working of GitPython, so I cannot really give more details as to why...)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Byron Byron removed the waiting for feedback label Jun 18, 2024
@Byron

Byron commented Jun 18, 2024

Copy link
Copy Markdown
Member

Thanks for trying it out, and it seems there is nothing left to do here but to hit merge.

Thanks again for contributing :)!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Byron
Byron merged commit 4c21e51 into gitpython-developers:main Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Byron Byron Byron approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@cardoeng @Byron
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.