{"thread":{"id":"12632","subject":"Is a given file known to git?","startedAt":"2008-03-11T07:15:09Z","lastAt":"2008-03-11T09:45:08Z","messageCount":7,"participants":["Christoph Duelli","Shawn O. Pearce","Johannes Sixt","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"71659","messageId":"47D6317D.7030700@melosgmbh.de","threadId":"12632","inReplyTo":null,"subject":"Is a given file known to git?","fromName":"Christoph Duelli","fromEmail":"duelli@melosgmbh.de","sentAt":"2008-03-11T07:15:09Z","receivedAt":"2008-03-11T07:15:09Z","isPatch":false,"sender":{"key":"duelli@melosgmbh.de","avatar":null},"body":"Given a repository and a path p to a file in it:\nIs it possible (how?) to detect (in a bash script) if the file pointed \nto by p is \"known\" to git?\nSomething along the line:\nif `git knows p?\nthen\n...\nfi\n\nThanks and keep up the good work!\n-- \nChristoph Duelli\n"},{"id":"71660","messageId":"20080311072412.GP8410@spearce.org","threadId":"12632","inReplyTo":"47D6317D.7030700@melosgmbh.de","subject":"Re: Is a given file known to git?","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-03-11T07:24:12Z","receivedAt":"2008-03-11T07:24:12Z","isPatch":false,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Christoph Duelli <duelli@melosgmbh.de> wrote:\n> Given a repository and a path p to a file in it:\n> Is it possible (how?) to detect (in a bash script) if the file pointed \n> to by p is \"known\" to git?\n> Something along the line:\n> if `git knows p?\n> then\n> ...\n> fi\n\nDepends on your definition of \"known to git\".  You can look at\nthe current index, which may have files newly added but not yet\ncommitted:\n\n\tif test $(git ls-files \"$p\" | wc -l) -gt 0\n\tthen\n\t...\n\tfi\n\nyou can look at a specific committed state too:\n\n\tif git cat-file -e \"$commit:$p\" 2>/dev/null\n\tthen\n\t...\n\tfi\n\nwhere $commit can be any tree-ish, so a tag, a branch, a commit,\nthe symbolic-ref HEAD... etc.\n\n-- \nShawn.\n"},{"id":"71663","messageId":"47D636FC.2060203@viscovery.net","threadId":"12632","inReplyTo":"47D6317D.7030700@melosgmbh.de","subject":"Re: Is a given file known to git?","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2008-03-11T07:38:36Z","receivedAt":"2008-03-11T07:38:36Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Christoph Duelli schrieb:\n> Given a repository and a path p to a file in it:\n> Is it possible (how?) to detect (in a bash script) if the file pointed\n> to by p is \"known\" to git?\n> Something along the line:\n> if `git knows p?\n> then\n> ...\n> fi\n\nFor simplicity, I'll assume with \"known to git\" you mean whether the\ncurrent branch's tip has some content at the given path. Then you could write:\n\n\tif git rev-parse HEAD:\"$p\" >/dev/null 2>&1\n\tthen\n\t\t# git knows about $p\n\telse\n\t\t# git does not know about $p\n\tfi\n\n-- Hannes\n"},{"id":"71674","messageId":"47D65074.4000505@melosgmbh.de","threadId":"12632","inReplyTo":"47D636FC.2060203@viscovery.net","subject":"Re: Is a given file known to git?","fromName":"Christoph Duelli","fromEmail":"duelli@melosgmbh.de","sentAt":"2008-03-11T09:27:16Z","receivedAt":"2008-03-11T09:27:16Z","isPatch":false,"sender":{"key":"duelli@melosgmbh.de","avatar":null},"body":"Johannes Sixt schrieb:\n> Christoph Duelli schrieb:\n>> Given a repository and a path p to a file in it:\n>> Is it possible (how?) to detect (in a bash script) if the file pointed\n>> to by p is \"known\" to git?\n>> Something along the line:\n>> if `git knows p?\n>> then\n>> ...\n>> fi\n> \n> For simplicity, I'll assume with \"known to git\" you mean whether the\n> current branch's tip has some content at the given path. Then you could write:\n> \n> \tif git rev-parse HEAD:\"$p\" >/dev/null 2>&1\n> \tthen\n> \t\t# git knows about $p\n> \telse\n> \t\t# git does not know about $p\n> \tfi\n> \n> -- Hannes\nYes, thank you, Johannes and Shawn, this works.\n(A bit slow, though: with this test enabled my script takes 77 secs; \nwithout it it takes 0.3 secs. The time is spent in 9000 calls to the \nabove test (the rev-parse version). (ok, the fact that there is a Perl \nsystem call around it might take some time, too).)\n\n[For the record:\nShawn's ls-files variant takes: 148 secs\nShawn's cat-file variant takes: 251 secs\nTime taken by time; roughly half user, half system.\n]\n\nAs this script won't run often, that not too big a deal.\nStill I would have hoped for a quicker way.\n\nThanks\n-- \nChristoph Duelli\n"},{"id":"71675","messageId":"47D652A9.4090304@viscovery.net","threadId":"12632","inReplyTo":"47D65074.4000505@melosgmbh.de","subject":"Re: Is a given file known to git?","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2008-03-11T09:36:41Z","receivedAt":"2008-03-11T09:36:41Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Christoph Duelli schrieb:\n> Yes, thank you, Johannes and Shawn, this works.\n> (A bit slow, though: with this test enabled my script takes 77 secs;\n> without it it takes 0.3 secs. The time is spent in 9000 calls to the\n> above test (the rev-parse version). (ok, the fact that there is a Perl\n> system call around it might take some time, too).)\n\nOh, you said \"a particular path from a bash script\". If you had said \"9000\nparticular paths, and I have perl wrapped around it\", then there might be\n(much) better solutions. Like parsing the output of \"git ls-files\n--others\" or \"git ls-tree -r\" into a hash and then lookup therein.\n\n-- Hannes\n"},{"id":"71679","messageId":"47D653CC.3070306@melosgmbh.de","threadId":"12632","inReplyTo":"47D652A9.4090304@viscovery.net","subject":"Re: Is a given file known to git?","fromName":"Christoph Duelli","fromEmail":"duelli@melosgmbh.de","sentAt":"2008-03-11T09:41:32Z","receivedAt":"2008-03-11T09:41:32Z","isPatch":false,"sender":{"key":"duelli@melosgmbh.de","avatar":null},"body":"Johannes Sixt schrieb:\n> Christoph Duelli schrieb:\n>> Yes, thank you, Johannes and Shawn, this works.\n>> (A bit slow, though: with this test enabled my script takes 77 secs;\n>> without it it takes 0.3 secs. The time is spent in 9000 calls to the\n>> above test (the rev-parse version). (ok, the fact that there is a Perl\n>> system call around it might take some time, too).)\n> \n> Oh, you said \"a particular path from a bash script\". If you had said \"9000\n> particular paths, and I have perl wrapped around it\", then there might be\n> (much) better solutions. Like parsing the output of \"git ls-files\n> --others\" or \"git ls-tree -r\" into a hash and then lookup therein.\n> \n> -- Hannes\nRight you are ;-)\nI just assumed that such a test would be quick, so I didn't worry about \nspeed back then. But yes, parsing the output of \"igt ls-files\" would \nsolve that nicely.\n\nThank you.\n\n-- \nChristoph Duelli\n"},{"id":"71680","messageId":"7vy78pbnmj.fsf@gitster.siamese.dyndns.org","threadId":"12632","inReplyTo":"47D65074.4000505@melosgmbh.de","subject":"Re: Is a given file known to git?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-03-11T09:45:08Z","receivedAt":"2008-03-11T09:45:08Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christoph Duelli <duelli@melosgmbh.de> writes:\n\n> Yes, thank you, Johannes and Shawn, this works.\n> (A bit slow, though: with this test enabled my script takes 77 secs;\n> without it it takes 0.3 secs. The time is spent in 9000 calls to the\n> above test (the rev-parse version). (ok, the fact that there is a Perl\n> system call around it might take some time, too).)\n>\n> [For the record:\n> Shawn's ls-files variant takes: 148 secs\n> Shawn's cat-file variant takes: 251 secs\n> Time taken by time; roughly half user, half system.\n> ]\n>\n> As this script won't run often, that not too big a deal.\n\nI suspect that you asked a wrong question.  If the original question were\n\"I have a git managed project whose tip has about 8000 paths in it, and I\nhave 9000 paths that may or may not belong to the tip.  How do I find the\npaths that are not part of the tip\" (or \"How do I find the ones that are\npart of the tip\"), the answer would have been quite different.\n\n\tgit ls-files | sort >known\n        \t(or \"git ls-tree --name-only -r HEAD | sort >known\")\n        $list_your_paths_one_per_line | sort | comm known -\n\nwould compare the set of paths in the index (or HEAD) and the set of the\npaths you are interested in, and give you the list of ones that are only\npresent in one side, the other, or both.  Say \"comm -13\" instead of \"comm\"\nif you are only interested in untracked ones, and say \"comm -12\" if you\nare only interested in tracked ones.  Since you are working inside Perl,\nprobably you would not pipe to sort/comm but do the comm part yourself, I\nwould imagine.\n\nIt is as if you asked \"how do I add one to a given number\", without saying\nthat \"I have 9000 paths and I want to repeatedly add one for each path I\nhave\", people taught you the answer to your original question, and you\nended up looping \"n := n + 1\" 9000 times.  If you told them the real\nproblem you were trying to solve upfront, they would have taught you how\nto multiply instead ;-).\n"}]}