{"thread":{"id":"15980","subject":"git add --patch newfile doesn't add newfile to cache ?","startedAt":"2008-10-20T14:36:36Z","lastAt":"2008-10-22T13:29:46Z","messageCount":4,"participants":["Marc Weber","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"93519","messageId":"20081020143636.GB3988@gmx.de","threadId":"15980","inReplyTo":null,"subject":"git add --patch newfile doesn't add newfile to cache ?","fromName":"Marc Weber","fromEmail":"marco-oweber@gmx.de","sentAt":"2008-10-20T14:36:36Z","receivedAt":"2008-10-20T14:36:36Z","isPatch":false,"sender":{"key":"marco-oweber@gmx.de","avatar":null},"body":"Is this desired behaviour?\n\nTestcase:\n\n        #!/bin/sh\n        # this script creates testGitAddPatch and will run the testcase there\n         \n        rm -fr testGitAddPatch\n        mkdir testGitAddPatch\n        cd testGitAddPatch\n         \n        git init\n        echo test > test\n        git add --patch test\n        echo \"running status, nothing has been added\"\n        git status\n        git --version\n \nMy output\n \n        marc@mail: /tmp/git ]$ sh test.sh\n        Initialized empty Git repository in /tmp/git/testGitAddPatch/.git/\n        No changes.\n        running status, nothing has been added\n        # On branch master\n        #\n        # Initial commit\n        #\n        # Untracked files:\n        #   (use \"git add <file>...\" to include in what will be committed)\n        #\n        #       test\n        nothing added to commit but untracked files present (use \"git add\" to\n        track)\n        git version 1.6.0.2.GIT\n\nMarc Weber\n"},{"id":"93563","messageId":"20081020235049.GA23120@coredump.intra.peff.net","threadId":"15980","inReplyTo":"20081020143636.GB3988@gmx.de","subject":"Re: git add --patch newfile doesn't add newfile to cache ?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-10-20T23:50:50Z","receivedAt":"2008-10-20T23:50:50Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Oct 20, 2008 at 04:36:36PM +0200, Marc Weber wrote:\n\n> Is this desired behaviour?\n> [...]\n>         git init\n>         echo test > test\n>         git add --patch test\n>         echo \"running status, nothing has been added\"\n>         git status\n\nI think your example makes sense, but nobody ever really tried it\nbefore. I use \"git add -p\" all the time, but almost always when I am\nadding a new file, I add the whole contents.\n\nI think there are two ways to go about fixing it:\n\n  - in git-add--interactive.perl, the function patch_update_cmd\n    explicitly looks at the list of modified files. It would have to\n    also check for untracked files, which is easy. But we also need to\n    keep track of which files are modified and which are untracked\n    through the whole patching procedure, which is a bit more invasive.\n\n  - the recently-added \"git add -N\" adds an empty file into the index,\n    at which point we could add content in the normal way. So:\n\n      git add -N test\n      git add -p test\n\n    should just work (but obviously requires two steps from the user).\n    You could do something more automatic like the patch below, but I\n    think the semantics aren't quite right. If you stage nothing for a\n    newly added file, then you still end up with an empty version of the\n    staged file in the index. I would expect the semantics to be:\n\n      1. if you stage any content, then the file is added to the index\n         with that content\n\n      2. if you stage no content, then the file remains untracked\n\n---\ndiff --git a/git-add--interactive.perl b/git-add--interactive.perl\nindex da768ee..72f8a67 100755\n--- a/git-add--interactive.perl\n+++ b/git-add--interactive.perl\n@@ -811,6 +811,12 @@ EOF\n }\n \n sub patch_update_cmd {\n+\tmy @new = list_untracked();\n+\tif (@new) {\n+\t\tsystem(qw(git add -N), @new)\n+\t\t\tand die \"git add reported failure\";\n+\t}\n+\n \tmy @mods = grep { !($_->{BINARY}) } list_modified('file-only');\n \tmy @them;\n \n"},{"id":"93681","messageId":"20081022131232.GL3988@gmx.de","threadId":"15980","inReplyTo":"20081020235049.GA23120@coredump.intra.peff.net","subject":"Re: git add --patch newfile doesn't add newfile to cache ?","fromName":"Marc Weber","fromEmail":"marco-oweber@gmx.de","sentAt":"2008-10-22T13:12:32Z","receivedAt":"2008-10-22T13:12:32Z","isPatch":false,"sender":{"key":"marco-oweber@gmx.de","avatar":null},"body":"On Mon, Oct 20, 2008 at 07:50:50PM -0400, Jeff King wrote:\n> On Mon, Oct 20, 2008 at 04:36:36PM +0200, Marc Weber wrote:\n> \n> > Is this desired behaviour?\n> > [...]\n> >         git init\n> >         echo test > test\n> >         git add --patch test\n> >         echo \"running status, nothing has been added\"\n> >         git status\n> \n> I think your example makes sense, but nobody ever really tried it\n> before. [..]\nI came across this use case because I'm lazy. eg\n        git add --patch file-to-be-committed-partly  newfile\n\nis shorter than\n        git add --patch file-to-be-committed-partly\n        git add newfile\n\n\n\n> I use \"git add -p\" all the time, but almost always when I am\n> adding a new file, I add the whole contents.\n> \n> I think there are two ways to go about fixing it:\n> \n>   - in git-add--interactive.perl, the function patch_update_cmd\n>     explicitly looks at the list of modified files. It would have to\n>     also check for untracked files, which is easy. But we also need to\n>     keep track of which files are modified and which are untracked\n>     through the whole patching procedure, which is a bit more invasive.\n> \n>   - the recently-added \"git add -N\" adds an empty file into the index,\n>     at which point we could add content in the normal way. So:\n> \n>       git add -N test\n>       git add -p test\n> \n>     should just work (but obviously requires two steps from the user).\n>     You could do something more automatic like the patch below, but I\n>     think the semantics aren't quite right. If you stage nothing for a\n>     newly added file, then you still end up with an empty version of the\n>     staged file in the index. I would expect the semantics to be:\n> \n>       1. if you stage any content, then the file is added to the index\n>          with that content\n> \n>       2. if you stage no content, then the file remains untracked\n\n> ---\n> diff --git a/git-add--interactive.perl b/git-add--interactive.perl\n> [...]\n>  sub patch_update_cmd {\n> +\tmy @new = list_untracked();\n> +\tif (@new) {\n> +\t\tsystem(qw(git add -N), @new)\n> +\t\t\tand die \"git add reported failure\";\n> +\t}\n> +\n \n\nI've tried the patch. However I'm not fully satisified.\nI often use --patch to have another second look at each change to be\ncommitted. Your patch adds new files to the cache silently without\ngiving the user the change to omit or edit the patch. But exatly that's\nthe reason I'm using --patch. So maybe I can work on this in some days..\nMaybe I've also injected those lines into the wrong git version \n(1.6.0.2.GIT)\n\nMay I try rewriting your semantics proposal to this ?\n\n      1) when using git add --patch untracked-file the user should be\n         given the default patch view (only containing + lines)\n         so that he can use edit to only commit parts of the file in the\n         usual way. (I guess this is similar to having used git add -N\n         before, I haven't tried yet)\n\n      2) if he wants to skip the entire patch / file nothing should be\n         added to the index.\n\nThanks for your reply.\n\nSincerly\nMarc Weber\n"},{"id":"93685","messageId":"20081022132946.GB17393@coredump.intra.peff.net","threadId":"15980","inReplyTo":"20081022131232.GL3988@gmx.de","subject":"Re: git add --patch newfile doesn't add newfile to cache ?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-10-22T13:29:46Z","receivedAt":"2008-10-22T13:29:46Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Oct 22, 2008 at 03:12:32PM +0200, Marc Weber wrote:\n\n> >  sub patch_update_cmd {\n> > +\tmy @new = list_untracked();\n> > +\tif (@new) {\n> > +\t\tsystem(qw(git add -N), @new)\n> > +\t\t\tand die \"git add reported failure\";\n> > +\t}\n> > +\n>  \n> \n> I've tried the patch. However I'm not fully satisified.\n> I often use --patch to have another second look at each change to be\n> committed. Your patch adds new files to the cache silently without\n> giving the user the change to omit or edit the patch. But exatly that's\n> the reason I'm using --patch. So maybe I can work on this in some days..\n> Maybe I've also injected those lines into the wrong git version \n> (1.6.0.2.GIT)\n\nYes, you need to use the current 'master' branch for the \"-N\" feature.\nThe point of \"-N\" is to say \"this is a file I want to track, but don't\nadd any contents yet.\" So it does your part 1:\n\n>       1) when using git add --patch untracked-file the user should be\n>          given the default patch view (only containing + lines)\n>          so that he can use edit to only commit parts of the file in the\n>          usual way. (I guess this is similar to having used git add -N\n>          before, I haven't tried yet)\n\nBut not your part 2:\n\n>       2) if he wants to skip the entire patch / file nothing should be\n>          added to the index.\n\nIf you add _no_ contents, you still end up with the \"this is a file I\nwant to track\" part, but with no contents. And I agree it should stage\nnothing, which is why this is an unsatisfactory solution (and why I\ndidn't clean up the patch and send it to Junio for inclusion).\n\n-Peff\n"}]}