Skip to content

Conversation

black-desk
Copy link
Contributor

@black-desk black-desk commented Apr 29, 2025

  • PR Description

Man page of git apply says that:

  --unidiff-zero
      By default, git apply expects that the patch being applied is a
      unified diff with at least one line of context. This provides
      good safety measures, but breaks down when applying a diff
      generated with --unified=0. To bypass these checks use
      --unidiff-zero.

      Note, for the reasons stated above, the usage of context-free
      patches is discouraged.

Lazygit always render custom patch with zero context,
so this --unidiff-zero option is needed when pass such patch to
git apply.

Check https://github.com/black-desk/lazygit-bug-report-2025-04-29
for details.

  • Please check if the PR fulfills these requirements
  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (see here)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (see here)
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

Man page of `git apply` says that:

  --unidiff-zero
      By default, git apply expects that the patch being applied is a
      unified diff with at least one line of context. This provides
      good safety measures, but breaks down when applying a diff
      generated with --unified=0. To bypass these checks use
      --unidiff-zero.

      Note, for the reasons stated above, the usage of context-free
      patches is discouraged.

Lazygit always render custom patch with zero context,
so this --unidiff-zero option is needed when pass such patch to
`git apply`.

Check https://github.com/black-desk/lazygit-bug-report-2025-04-29
for details.
@black-desk black-desk marked this pull request as ready for review April 29, 2025 02:49
@stefanhaller
Copy link
Collaborator

Lazygit always render custom patch with zero context

This is not true; it only does this if you set your diff context size to 0 (by pressing { repeatedly) before starting to create a patch.

I don't think we should use the --unidiff-zero option; there are good reasons why git discourages this. Instead, we should show a better error message when users start to create a custom patch while the diff context is zero, similar to what we did for normal staging in #4235.

@black-desk
Copy link
Contributor Author

Lazygit always render custom patch with zero context

This is not true; it only does this if you set your diff context size to 0 (by pressing { repeatedly) before starting to create a patch.

Oh, but I do not remember I have done this before. Is it defaults to zero?

I don't think we should use the --unidiff-zero option; there are good reasons why git discourages this. Instead, we should show a better error message when users start to create a custom patch while the diff context is zero, similar to what we did for normal staging in #4235.

@stefanhaller
Copy link
Collaborator

This is not true; it only does this if you set your diff context size to 0 (by pressing { repeatedly) before starting to create a patch.

Oh, but I do not remember I have done this before. Is it defaults to zero?

No, that would be very weird. It defaults to 3. It is stored in state.yml, so if you do set it to 0, this gets remembered across restarts of lazygit.

@black-desk
Copy link
Contributor Author

black-desk commented Apr 29, 2025

This is not true; it only does this if you set your diff context size to 0 (by pressing { repeatedly) before starting to create a patch.

Oh, but I do not remember I have done this before. Is it defaults to zero?

No, that would be very weird. It defaults to 3. It is stored in state.yml, so if you do set it to 0, this gets remembered across restarts of lazygit.

I think might we should make sure it to be at least 1, as 0 is discouraged by git and will somehow make git apply do not work.

Or at least print something useful in log? I have been troubled by this issue for months.

@stefanhaller
Copy link
Collaborator

I think might we should make sure it to be at least 1,

It used to be limited to at least 1, but we changed it deliberately to allow 0 as well (in version 0.45, see #4050) because some people wanted this.

Or at least print something useful in log?

Not in the log, but show an error panel; that's what I suggested above. Here's a PR that does this: #4522

@black-desk
Copy link
Contributor Author

I think might we should make sure it to be at least 1,

It used to be limited to at least 1, but we changed it deliberately to allow 0 as well (in version 0.45, see #4050) because some people wanted this.

Or at least print something useful in log?

Not in the log, but show an error panel; that's what I suggested above. Here's a PR that does this: #4522

Thanks.

@black-desk black-desk closed this Apr 29, 2025
stefanhaller added a commit that referenced this pull request Apr 29, 2025
- **PR Description**

This is very similar to what we are doing for staging or discarding
hunks in the Files panel (see #4235). Git doesn't allow applying patches
with a zero context size (unless you use the --unidiff-zero option,
which is discouraged).

Fixes #4521.
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

Successfully merging this pull request may close these issues.

2 participants