git apply: Not reliably working as `patch` replacement when `.git` file/directory exists
From: Deniz Bahadir <hidden>
Date: 2021-12-08 17:40:08
Hello Git developers, thank your for that great developer tool! I noticed a behavior with git apply which I assume is a bug. So I am going to report it here. Thanks for reading, Deniz Bahadir ## What did you do before the bug happened? (Steps to reproduce your issue) 1. `git clone` a Git repository which contains a Git submodule. 2. Initialize the submodule by using `git submodule update --init`. 3. Change into that Git submodule directory and create a patch for a file in it. (Note: Here we assume the directory is called `submodule` and the to-be-patched file is called `file.txt`.)
cd submodule
echo "extra-tokens" >> file.txt
git diff -u file.txt > file.txt.patch
git checkout file.txt
4.a) Copy the directory of this Git submodule into another directory
which itself is not located in or beneath a directory under Git
control.
4.b) Same as 4.a) but additionally remove the `.git` file in that
copied directory.
4.c) Rename the directory of this submodule but do not inform Git
about it (e.g. do a `mv submodule submodule_renamed`).
4.d) Same as 4.c) but additionally remove the `.git` file in that
moved directory.
5. For each directory created under step 4 change into it and run: git apply --unsafe-paths -v file.txt.patch
## What did you expect to happen? (Expected behavior) I expected all four different scenarios to succeed and to successfully patch `file.txt` in step 5. ## What happened instead? (Actual behavior) Case 5.a) fails with the following message:
fatal: not a git repository: /tmp/submodule/../.git/modules/submodule
Case 5.b) succeeds with the following message:
Checking patch file.txt...
Applied patch file.txt cleanly.
Case 5.c) fails with the following message:
fatal: cannot chdir to '../../../submodule': No such file or directory
Case 5.d) fails with the following message:
Skipped patch 'file.txt'.
## 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` not 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 `patch` (which might not be available on all platforms) as written on `git apply`'s man page. ## Anything else you want to add: Alternatively, maybe it would be better if `git apply` provides an extra option `--simulate-patch` or something similar which really works as a `patch` replacement in all situations (and does not consider `.git` files or directories at all). ## Additional system info. [System Info] git version: git version 2.33.1.windows.1 and 2.25.1 (ubuntu) cpu: x86_64 sizeof-long: 4 sizeof-size_t: 8 shell-path: /bin/sh