{"thread":{"id":"57054","subject":"git apply: Not reliably working as `patch` replacement when `.git` file/directory exists","startedAt":"2021-12-08T17:40:08Z","lastAt":"2021-12-08T17:40:08Z","messageCount":1,"participants":["Deniz Bahadir"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"443487","messageId":"CAFhJ7yv4F6gwXh3PgKaYQGOcAq7ryqVhXhGdBTBjGRScHUo9=A@mail.gmail.com","threadId":"57054","inReplyTo":null,"subject":"git apply: Not reliably working as `patch` replacement when `.git` file/directory exists","fromName":"Deniz Bahadir","fromEmail":"deniz@code.bahadir.email","sentAt":"2021-12-08T17:32:56Z","receivedAt":"2021-12-08T17:40:08Z","isPatch":false,"sender":{"key":"deniz@code.bahadir.email","avatar":null},"body":"Hello Git developers,\n\nthank your for that great developer tool!\nI noticed a behavior with git apply which I assume is a bug. So I am\ngoing to report it here.\n\nThanks for reading,\nDeniz Bahadir\n\n\n## What did you do before the bug happened? (Steps to reproduce your issue)\n\n1. `git clone` a Git repository which contains a Git submodule.\n2. Initialize the submodule by using `git submodule update --init`.\n3. Change into that Git submodule directory and create a patch for a\n   file in it.\n   (Note: Here we assume the directory is called `submodule` and the\n   to-be-patched file is called `file.txt`.)\n   ```\n   cd submodule\n   echo \"extra-tokens\" >> file.txt\n   git diff -u file.txt > file.txt.patch\n   git checkout file.txt\n   ```\n4.a) Copy the directory of this Git submodule into another directory\n     which itself is not located in or beneath a directory under Git\n     control.\n4.b) Same as 4.a) but additionally remove the `.git` file in that\n     copied directory.\n4.c) Rename the directory of this submodule but do not inform Git\n     about it (e.g. do a `mv submodule submodule_renamed`).\n4.d) Same as 4.c) but additionally remove the `.git` file in that\n     moved directory.\n5. For each directory created under step 4 change into it and run:\n   ```\n   git apply --unsafe-paths -v file.txt.patch\n   ```\n\n\n## What did you expect to happen? (Expected behavior)\n\nI expected all four different scenarios to succeed and to successfully\npatch `file.txt` in step 5.\n\n\n## What happened instead? (Actual behavior)\n\nCase 5.a) fails with the following message:\n```\nfatal: not a git repository: /tmp/submodule/../.git/modules/submodule\n```\n\nCase 5.b) succeeds with the following message:\n```\nChecking patch file.txt...\nApplied patch file.txt cleanly.\n```\n\nCase 5.c) fails with the following message:\n```\nfatal: cannot chdir to '../../../submodule': No such file or directory\n```\n\nCase 5.d) fails with the following message:\n```\nSkipped patch 'file.txt'.\n```\n\n\n## What's different between what you expected and what actually happened?\n\nI would have expected that option `--unsafe-paths` allows me to patch\nany file using `git apply` not matter where the directory is located\nand whether there is a `.git` file or directory in any of its parent\ndirectories or not.\nThat would have allowed me to use `git apply --unsafe-paths` as an\nalternative to `patch` (which might not be available on all platforms)\nas written on `git apply`'s man page.\n\n\n## Anything else you want to add:\n\nAlternatively, maybe it would be better if `git apply` provides an\nextra option `--simulate-patch` or something similar which really\nworks as a `patch` replacement in all situations (and does not\nconsider `.git` files or directories at all).\n\n\n## Additional system info.\n\n[System Info]\ngit version:\ngit version 2.33.1.windows.1 and 2.25.1 (ubuntu)\ncpu: x86_64\nsizeof-long: 4\nsizeof-size_t: 8\nshell-path: /bin/sh\n"}]}