{"thread":{"id":"40377","subject":"BUG: git_work_tree issue with git alias and scripting","startedAt":"2015-09-18T08:09:31Z","lastAt":"2015-09-18T08:09:31Z","messageCount":1,"participants":["Tuomo Tanskanen"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"270279","messageId":"CAE_UwRqMnmgDEUjcuxNDXgx_D3MTFxNRTgA9xGB1OvK6_d-bMw@mail.gmail.com","threadId":"40377","inReplyTo":null,"subject":"BUG: git_work_tree issue with git alias and scripting","fromName":"Tuomo Tanskanen","fromEmail":"tuomo@tanskanen.org","sentAt":"2015-09-18T08:09:31Z","receivedAt":"2015-09-18T08:09:31Z","isPatch":false,"sender":{"key":"tuomo@tanskanen.org","avatar":null},"body":"Hello,\n\nAttached is a simple script exposing a possible bug when git alias is\ncombined with git commands in script. It should be pretty self-explanatory.\nCheck what it does and execute it. On my Linux and OSX, both with git 2.5.1\nbehave the same, ie. git add fails even the file \"test\" is in $PWD.\n\nSummary: $GIT_WORK_TREE persists in env, and then some git commands operate\non repo in $PWD (like git log), while some others (like git add) operate on\nrepo in $GIT_WORK_TREE, making it inconsistent and results in very\nunexpected results.\n\nCheers, Tuomo\n\n\n---\n#!/bin/sh\n\n# setup a test repo\nmkdir bar\ncd bar\ngit init\necho \"test\" > bar\ngit add bar\ngit commit -m \"test commit in bar\"\necho \"adding this will fail later on\" > test\ncd ..\n\n# setup a repo with an alias and script alias will execute\nmkdir foo\ncd foo\ngit init\n\ncat >foo.sh <<EOF\n#!/bin/sh -x\ncd $PWD/../bar\nenv | egrep \"^(GIT_WORK_TREE|PWD)\"\necho \"Git log works (bar log shows up):\"\ngit log --oneline | head -1\necho \"Git add does not (tries to add files in GIT_WORK_TREE=foo, while\nPWD=bar):\"\ngit add test\nEOF\nchmod 755 foo.sh\n\ncat >>.git/config <<EOF\n[alias]\n  foo = !$PWD/foo.sh\nEOF\n\ngit add foo.sh\ngit commit -m \"committing foo.sh\"\n\n# run test via alias\ngit foo\n"}]}