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

git apply --unsafe-paths only works in certain cases! #1430

Closed
DenizThatMenace opened this issue Nov 21, 2022 · 3 comments
Closed

git apply --unsafe-paths only works in certain cases! #1430

DenizThatMenace opened this issue Nov 21, 2022 · 3 comments

Comments

@DenizThatMenace
Copy link

Introduction

I am trying to use git apply --unsafe-paths as a "better GNU patch" (aka as replacement for the GNU patch executable).
However, this is not really working out as expected and stated by its documentation.

What did you do before the bug happened? (Steps to reproduce your issue)

Follow these steps to reproduce the issue:

  1. git clone a Git repository which contains a Git submodule, e.g., the Git repository itself:

     git clone --depth 1 https://github.com/git/git.git source
    
  2. Change into the cloned source directory and initialize the submodule(s), e.g., the single submodule sha1collisiondetection of the Git repository:

     cd source && git submodule update --init
    
  3. Change into the directory of that Git submodule and create a patch for a file in it.
    (Afterwards change into the root source directory again.)

     cd sha1collisiondetection
     echo "Some additional text to append to the readme file." >> README.md
     git diff -u README.md > README.md.patch
     git checkout README.md
     cd ..
    
  4. Now we have 4 different cases to consider:

    1. Case 1: Copy the directory of this Git submodule into another directory which itself is not located in or beneath a directory under Git control.

       cp -r sha1collisiondetection ../case-1
      
    2. Case 2: Same as for case 1, but additionally remove the .git file in that copied directory.

       cp -r sha1collisiondetection ../case-2 && rm ../case-2/.git
      
    3. Case 3: Rename the directory of this submodule but do not inform Git about it.

       mv sha1collisiondetection case-3
      
    4. Case 4: Same as for case 3, but additionally remove the .git file in that moved directory.

       mv sha1collisiondetection case-4 && rm case-4/.git
      
  5. For each directory created under the sub-steps of 4 change into it and run git apply with --unsafe-paths option:

     git apply --unsafe-paths -v README.md.patch
    

What did you expect to happen? (Expected behavior)

I expected all four different scenarios to succeed and git apply to successfully patch README.md in step 5 similar to GNU patch executable.

What happened instead? (Actual behavior)

  • Case 1 failed in step 5 with the following message:

      fatal: not a git repository: /path/to/case-1/../.git/modules/sha1collisiondetection
    
  • Case 2 succeeded in step 5 with the following message:

      Checking patch README.md...
      Applied patch README.md cleanly.
    
  • Case 3 failed in step 5 with the following message:

      fatal: cannot chdir to '../../../sha1collisiondetection': No such file or directory
    
  • Case 4 failed in step 5 with the following message:

      Skipped patch 'README.md'.
    

What's different between what you expected and what actually happened?

I would have expected that option --unsafe-paths allows me to patch any file using git apply no matter where the directory is located and whether there is a .git file or directory in any of its parent directories or not.
That would have allowed me to use git apply --unsafe-paths as an alternative to GNU patch (which might not be available on all platforms) as written in git apply's man page.

Anything else you want to add:

If this is behavior as designed, the documentation then really needs clarification. Additionally, git apply should then provide another option --simulate-patch (or similar called) which really works as a GNU patch replacement in all situations (and does not get distracted by the existence of a file or directory with called .git).

This described problem with git apply --unsafe-paths occurs on Windows as well as on Linux (and probably any other platform for which Git is available).

[System Info]
git version:
git version 2.38.1.windows.1
cpu: x86_64
built from commit: b85c8f6
sizeof-long: 4
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon
uname: Windows 10.0 22000
compiler info: gnuc: 12.2
libc info: no libc information available
$SHELL (typically, interactive shell): C:\Program Files\Git\usr\bin\bash.exe

[Enabled Hooks]
not run from a git repository - no hooks to show

@dscho
Copy link
Member

dscho commented Nov 22, 2022

@Bagira80 Thank you for writing up a bug report. However, this is not the place the Git projects expects bug reports: please send it to the Git mailing list. See https://git-scm.com/community for more details.

@dscho dscho closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2022
@DenizThatMenace
Copy link
Author

Thank you, @dscho, but sending this bug report directly via the Git mailing list is what I already tried almost a year ago. Nobody seemed to have noticed.
So, I thought, maybe reporting it here might bring it to the attention of Git developers that feel responsible for git apply. I assumed the message might automatically be put on the Git mailing list (as I understand is the case with pull requests)?

@DenizThatMenace
Copy link
Author

My original bug-report to the Git mailing list can be seen here.

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