{"thread":{"id":"21237","subject":"[PATCH] checkout: add 'pre-checkout' hook","startedAt":"2009-10-14T04:45:25Z","lastAt":"2009-10-14T07:04:18Z","messageCount":9,"participants":["Sam Vilain","Jeff King","Junio C Hamano","Bert Wesarg"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"124926","messageId":"1255495525-11254-1-git-send-email-sam.vilain@catalyst.net.nz","threadId":"21237","inReplyTo":null,"subject":"[PATCH] checkout: add 'pre-checkout' hook","fromName":"Sam Vilain","fromEmail":"sam.vilain@catalyst.net.nz","sentAt":"2009-10-14T04:45:25Z","receivedAt":"2009-10-14T04:45:25Z","isPatch":true,"sender":{"key":"sam@vilain.net","avatar":"https://gravatar.com/avatar/8fc840ca854dbf6f7065b4335e3b934951c1dca3b11db688e95e471901f8f4a8?d=mp&s=160"},"body":"Add a simple hook that will run before checkouts.\n\nSigned-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>\n---\n Documentation/githooks.txt |   20 +++++++++++++++-----\n builtin-checkout.c         |   25 ++++++++++++++++++++++---\n 2 files changed, 37 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/githooks.txt b/Documentation/githooks.txt\nindex 06e0f31..8dc3fbf 100644\n--- a/Documentation/githooks.txt\n+++ b/Documentation/githooks.txt\n@@ -143,21 +143,31 @@ pre-rebase\n This hook is called by 'git-rebase' and can be used to prevent a branch\n from getting rebased.\n \n+pre-checkout\n+-----------\n \n-post-checkout\n-~~~~~~~~~~~~~\n-\n-This hook is invoked when a 'git-checkout' is run after having updated the\n+This hook is invoked when a 'git-checkout' is run after before updating the\n worktree.  The hook is given three parameters: the ref of the previous HEAD,\n the ref of the new HEAD (which may or may not have changed), and a flag\n indicating whether the checkout was a branch checkout (changing branches,\n flag=1) or a file checkout (retrieving a file from the index, flag=0).\n-This hook cannot affect the outcome of 'git-checkout'.\n+This hook can prevent the checkout from proceeding by exiting with an\n+error code.\n \n It is also run after 'git-clone', unless the --no-checkout (-n) option is\n used. The first parameter given to the hook is the null-ref, the second the\n ref of the new HEAD and the flag is always 1.\n \n+This hook can be used to perform any clean-up deemed necessary before\n+checking out the new branch/files.\n+\n+post-checkout\n+-----------\n+\n+This hook is invoked when a 'git-checkout' is run after having updated the\n+worktree.  It takes the same arguments as the 'pre-checkout' hook.\n+This hook cannot affect the outcome of 'git-checkout'.\n+\n This hook can be used to perform repository validity checks, auto-display\n differences from the previous HEAD if different, or set working dir metadata\n properties.\ndiff --git a/builtin-checkout.c b/builtin-checkout.c\nindex d050c37..b72a3cb 100644\n--- a/builtin-checkout.c\n+++ b/builtin-checkout.c\n@@ -36,6 +36,17 @@ struct checkout_opts {\n \tenum branch_track track;\n };\n \n+static int pre_checkout_hook(struct commit *old, struct commit *new,\n+\t\t\t      int changed)\n+{\n+\treturn run_hook(NULL, \"pre-checkout\",\n+\t\t\tsha1_to_hex(old ? old->object.sha1 : null_sha1),\n+\t\t\tsha1_to_hex(new ? new->object.sha1 : null_sha1),\n+\t\t\tchanged ? \"1\" : \"0\", NULL);\n+\t/* \"new\" can be NULL when checking out from the index before\n+\t   a commit exists. */\n+}\n+\n static int post_checkout_hook(struct commit *old, struct commit *new,\n \t\t\t      int changed)\n {\n@@ -256,6 +267,13 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,\n \tif (errs)\n \t\treturn 1;\n \n+\t/* Run the pre-checkout hook */\n+\tresolve_ref(\"HEAD\", rev, 0, &flag);\n+\thead = lookup_commit_reference_gently(rev, 1);\n+\terrs = pre_checkout_hook(head, head, 0);\n+\tif (errs)\n+\t\treturn 1;\n+\n \t/* Now we are committed to check them out */\n \tmemset(&state, 0, sizeof(state));\n \tstate.force = 1;\n@@ -279,9 +297,6 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,\n \t    commit_locked_index(lock_file))\n \t\tdie(\"unable to write new index file\");\n \n-\tresolve_ref(\"HEAD\", rev, 0, &flag);\n-\thead = lookup_commit_reference_gently(rev, 1);\n-\n \terrs |= post_checkout_hook(head, head, 0);\n \treturn errs;\n }\n@@ -543,6 +558,10 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)\n \t\tparse_commit(new->commit);\n \t}\n \n+\tret = pre_checkout_hook(old.commit, new->commit, 1);\n+\tif (ret)\n+\t\treturn ret;\n+\n \tret = merge_working_tree(opts, &old, new);\n \tif (ret)\n \t\treturn ret;\n-- \n1.6.3.3\n"},{"id":"124934","messageId":"20091014051319.GF31810@coredump.intra.peff.net","threadId":"21237","inReplyTo":"1255495525-11254-1-git-send-email-sam.vilain@catalyst.net.nz","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-10-14T05:13:19Z","receivedAt":"2009-10-14T05:13:19Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Oct 14, 2009 at 05:45:25PM +1300, Sam Vilain wrote:\n\n> Add a simple hook that will run before checkouts.\n\nWhat is the use case that makes it useful as a hook, and not simply as\nsomething people can do before running checkout?\n\nI guess you can use it to block a checkout, but only after finding out\n_what_ you are going to checkout, but an exact use case escapes me.\n\n> -post-checkout\n> -~~~~~~~~~~~~~\n> -\n> -This hook is invoked when a 'git-checkout' is run after having updated the\n> +This hook is invoked when a 'git-checkout' is run after before updating the\n\nDid you mean \"before having\" here?\n\n>  worktree.  The hook is given three parameters: the ref of the previous HEAD,\n>  the ref of the new HEAD (which may or may not have changed), and a flag\n>  indicating whether the checkout was a branch checkout (changing branches,\n>  flag=1) or a file checkout (retrieving a file from the index, flag=0).\n> -This hook cannot affect the outcome of 'git-checkout'.\n> +This hook can prevent the checkout from proceeding by exiting with an\n> +error code.\n>  \n>  It is also run after 'git-clone', unless the --no-checkout (-n) option is\n>  used. The first parameter given to the hook is the null-ref, the second the\n>  ref of the new HEAD and the flag is always 1.\n\nShould this \"after\" in the bottom paragraph perhaps become \"during\"?\n\n-Peff\n"},{"id":"124935","messageId":"7vr5t6lfr0.fsf@alter.siamese.dyndns.org","threadId":"21237","inReplyTo":"1255495525-11254-1-git-send-email-sam.vilain@catalyst.net.nz","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-10-14T05:13:39Z","receivedAt":"2009-10-14T05:13:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Sam Vilain <sam.vilain@catalyst.net.nz> writes:\n\n> Add a simple hook that will run before checkouts.\n>\n> Signed-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>\n> ---\n>  Documentation/githooks.txt |   20 +++++++++++++++-----\n>  builtin-checkout.c         |   25 ++++++++++++++++++++++---\n>  2 files changed, 37 insertions(+), 8 deletions(-)\n>\n> diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt\n> index 06e0f31..8dc3fbf 100644\n> --- a/Documentation/githooks.txt\n> +++ b/Documentation/githooks.txt\n> @@ -143,21 +143,31 @@ pre-rebase\n>  This hook is called by 'git-rebase' and can be used to prevent a branch\n>  from getting rebased.\n>  \n> +pre-checkout\n> +-----------\n>  \n> -post-checkout\n> -~~~~~~~~~~~~~\n> -\n> -This hook is invoked when a 'git-checkout' is run after having updated the\n> +This hook is invoked when a 'git-checkout' is run after before updating the\n\n\"after before\"?\n\n>  worktree.  The hook is given three parameters: the ref of the previous HEAD,\n>  the ref of the new HEAD (which may or may not have changed), and a flag\n>  indicating whether the checkout was a branch checkout (changing branches,\n>  flag=1) or a file checkout (retrieving a file from the index, flag=0).\n> -This hook cannot affect the outcome of 'git-checkout'.\n> +This hook can prevent the checkout from proceeding by exiting with an\n> +error code.\n>  \n>  It is also run after 'git-clone', unless the --no-checkout (-n) option is\n>  used. The first parameter given to the hook is the null-ref, the second the\n>  ref of the new HEAD and the flag is always 1.\n>  \n> +This hook can be used to perform any clean-up deemed necessary before\n> +checking out the new branch/files.\n> +\n> +post-checkout\n> +-----------\n\nThis is not about your patch, but the patch text shows that our diff\nalgorithm seems to have a room for improvement.  I expected to see a\nstraight insersion of block of text, not touching anything in the original\nsection on post-checkout hook.\n"},{"id":"124936","messageId":"4AD5601A.8090503@catalyst.net.nz","threadId":"21237","inReplyTo":"7vr5t6lfr0.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Sam Vilain","fromEmail":"sam.vilain@catalyst.net.nz","sentAt":"2009-10-14T05:22:34Z","receivedAt":"2009-10-14T05:22:34Z","isPatch":true,"sender":{"key":"sam@vilain.net","avatar":"https://gravatar.com/avatar/8fc840ca854dbf6f7065b4335e3b934951c1dca3b11db688e95e471901f8f4a8?d=mp&s=160"},"body":"Junio C Hamano wrote:\n> Sam Vilain <sam.vilain@catalyst.net.nz> writes:\n> \n>> Add a simple hook that will run before checkouts.\n>>\n>> Signed-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>\n>> ---\n>>  Documentation/githooks.txt |   20 +++++++++++++++-----\n>>  builtin-checkout.c         |   25 ++++++++++++++++++++++---\n>>  2 files changed, 37 insertions(+), 8 deletions(-)\n>>\n>> diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt\n>> index 06e0f31..8dc3fbf 100644\n>> --- a/Documentation/githooks.txt\n>> +++ b/Documentation/githooks.txt\n>> @@ -143,21 +143,31 @@ pre-rebase\n>>  This hook is called by 'git-rebase' and can be used to prevent a branch\n>>  from getting rebased.\n>>  \n>> +pre-checkout\n>> +-----------\n>>  \n>> -post-checkout\n>> -~~~~~~~~~~~~~\n>> -\n>> -This hook is invoked when a 'git-checkout' is run after having updated the\n>> +This hook is invoked when a 'git-checkout' is run after before updating the\n> \n> \"after before\"?\n\n*ahem* whoops :).  I think I got the heading style wrong too...\n\n> This is not about your patch, but the patch text shows that our diff\n> algorithm seems to have a room for improvement.  I expected to see a\n> straight insersion of block of text, not touching anything in the original\n> section on post-checkout hook.\n\nCorrect.  This is because the paragraph explaining when the hook runs\nhas been moved to the pre-checkout paragraph, which appears before the\npost-checkout section.  I just compared the output to 'diff -du' and it\nseems to be the same, so I wouldn't worry too much.\n-- \nSam Vilain, Perl Hacker, Catalyst IT (NZ) Ltd.\nphone: +64 4 499 2267        PGP ID: 0x66B25843\n"},{"id":"124938","messageId":"20091014052527.GH31810@coredump.intra.peff.net","threadId":"21237","inReplyTo":"7vr5t6lfr0.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-10-14T05:25:27Z","receivedAt":"2009-10-14T05:25:27Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Oct 13, 2009 at 10:13:39PM -0700, Junio C Hamano wrote:\n\n> >  worktree.  The hook is given three parameters: the ref of the previous HEAD,\n> >  the ref of the new HEAD (which may or may not have changed), and a flag\n> >  indicating whether the checkout was a branch checkout (changing branches,\n> >  flag=1) or a file checkout (retrieving a file from the index, flag=0).\n> > -This hook cannot affect the outcome of 'git-checkout'.\n> > +This hook can prevent the checkout from proceeding by exiting with an\n> > +error code.\n> >  \n> >  It is also run after 'git-clone', unless the --no-checkout (-n) option is\n> >  used. The first parameter given to the hook is the null-ref, the second the\n> >  ref of the new HEAD and the flag is always 1.\n> >  \n> > +This hook can be used to perform any clean-up deemed necessary before\n> > +checking out the new branch/files.\n> > +\n> > +post-checkout\n> > +-----------\n> \n> This is not about your patch, but the patch text shows that our diff\n> algorithm seems to have a room for improvement.  I expected to see a\n> straight insersion of block of text, not touching anything in the original\n> section on post-checkout hook.\n\nI think it's right as-is. He changed the title of the section, made a\nfew tweaks in the text to make it appropriate for \"pre-checkout\", and\nthen made a new post-checkout section that says \"This is just like\npre-checkout\". So most of the lines were left untouched. Short of our\ndiff understanding the block-formatting of asciidoc, I think it's as\ngood as we can get.\n\n-Peff\n"},{"id":"124937","messageId":"4AD560C8.8010108@catalyst.net.nz","threadId":"21237","inReplyTo":"20091014051319.GF31810@coredump.intra.peff.net","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Sam Vilain","fromEmail":"sam.vilain@catalyst.net.nz","sentAt":"2009-10-14T05:25:28Z","receivedAt":"2009-10-14T05:25:28Z","isPatch":true,"sender":{"key":"sam@vilain.net","avatar":"https://gravatar.com/avatar/8fc840ca854dbf6f7065b4335e3b934951c1dca3b11db688e95e471901f8f4a8?d=mp&s=160"},"body":"Jeff King wrote:\n>> Add a simple hook that will run before checkouts.\n> \n> What is the use case that makes it useful as a hook, and not simply as\n> something people can do before running checkout?\n> \n> I guess you can use it to block a checkout, but only after finding out\n> _what_ you are going to checkout, but an exact use case escapes me.\n\nRight.  Yes, this could be explained more.\n\nActually the use case is submodules.  When switching branches, the user\nwants to add a hook to remove submodules.  However post-checkout is too\nlate, because the index and the .gitmodules file do not record the\nsubmodule locations.\n\nOf course the best explanation is a test case :) I'll look at cooking\none up.\n-- \nSam Vilain, Perl Hacker, Catalyst IT (NZ) Ltd.\nphone: +64 4 499 2267        PGP ID: 0x66B25843\n"},{"id":"124944","messageId":"36ca99e90910132349o25322021l266124bd8b0d30b3@mail.gmail.com","threadId":"21237","inReplyTo":"1255495525-11254-1-git-send-email-sam.vilain@catalyst.net.nz","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Bert Wesarg","fromEmail":"bert.wesarg@googlemail.com","sentAt":"2009-10-14T06:49:10Z","receivedAt":"2009-10-14T06:49:10Z","isPatch":true,"sender":{"key":"bert.wesarg@googlemail.com","avatar":"https://avatars.githubusercontent.com/u/111934?v=4"},"body":"On Wed, Oct 14, 2009 at 06:45, Sam Vilain <sam.vilain@catalyst.net.nz> wrote:\n> diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt\n> index 06e0f31..8dc3fbf 100644\n> --- a/Documentation/githooks.txt\n> +++ b/Documentation/githooks.txt\n> @@ -143,21 +143,31 @@ pre-rebase\n>  This hook is called by 'git-rebase' and can be used to prevent a branch\n>  from getting rebased.\n>\n> +pre-checkout\n> +-----------\n>\n> -post-checkout\n> -~~~~~~~~~~~~~\n> -\nWhy do you change the caption from subsection (Ie. ~~~) to section (Ie. ---)?\n\n> -This hook is invoked when a 'git-checkout' is run after having updated the\n> +This hook is invoked when a 'git-checkout' is run after before updating the\n>  worktree.  The hook is given three parameters: the ref of the previous HEAD,\n>  the ref of the new HEAD (which may or may not have changed), and a flag\n>  indicating whether the checkout was a branch checkout (changing branches,\n>  flag=1) or a file checkout (retrieving a file from the index, flag=0).\n> -This hook cannot affect the outcome of 'git-checkout'.\n> +This hook can prevent the checkout from proceeding by exiting with an\n> +error code.\n>\n>  It is also run after 'git-clone', unless the --no-checkout (-n) option is\n>  used. The first parameter given to the hook is the null-ref, the second the\n>  ref of the new HEAD and the flag is always 1.\n>\n> +This hook can be used to perform any clean-up deemed necessary before\n> +checking out the new branch/files.\n> +\n> +post-checkout\n> +-----------\nDitto.\n\n> +\n> +This hook is invoked when a 'git-checkout' is run after having updated the\n> +worktree.  It takes the same arguments as the 'pre-checkout' hook.\n> +This hook cannot affect the outcome of 'git-checkout'.\n> +\n>  This hook can be used to perform repository validity checks, auto-display\n>  differences from the previous HEAD if different, or set working dir metadata\n>  properties.\n\nBert\n"},{"id":"124945","messageId":"4AD574CA.8010900@catalyst.net.nz","threadId":"21237","inReplyTo":"36ca99e90910132349o25322021l266124bd8b0d30b3@mail.gmail.com","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Sam Vilain","fromEmail":"sam.vilain@catalyst.net.nz","sentAt":"2009-10-14T06:50:50Z","receivedAt":"2009-10-14T06:50:50Z","isPatch":true,"sender":{"key":"sam@vilain.net","avatar":"https://gravatar.com/avatar/8fc840ca854dbf6f7065b4335e3b934951c1dca3b11db688e95e471901f8f4a8?d=mp&s=160"},"body":"Bert Wesarg wrote:\n>> +pre-checkout\n>> +-----------\n>>\n>> -post-checkout\n>> -~~~~~~~~~~~~~\n>> -\n> Why do you change the caption from subsection (Ie. ~~~) to section (Ie. ---)?\n\nPossibly the headings changed from the version I originally patched to\nthe current 'master'.  More likely, I just made a simple mistake :-}\n-- \nSam Vilain, Perl Hacker, Catalyst IT (NZ) Ltd.\nphone: +64 4 499 2267        PGP ID: 0x66B25843\n"},{"id":"124946","messageId":"36ca99e90910140004y275cbb86he03d790ec7a388d4@mail.gmail.com","threadId":"21237","inReplyTo":"4AD574CA.8010900@catalyst.net.nz","subject":"Re: [PATCH] checkout: add 'pre-checkout' hook","fromName":"Bert Wesarg","fromEmail":"bert.wesarg@googlemail.com","sentAt":"2009-10-14T07:04:18Z","receivedAt":"2009-10-14T07:04:18Z","isPatch":true,"sender":{"key":"bert.wesarg@googlemail.com","avatar":"https://avatars.githubusercontent.com/u/111934?v=4"},"body":"On Wed, Oct 14, 2009 at 08:50, Sam Vilain <sam.vilain@catalyst.net.nz> wrote:\n> Bert Wesarg wrote:\n>>> +pre-checkout\n>>> +-----------\n>>>\n>>> -post-checkout\n>>> -~~~~~~~~~~~~~\n>>> -\n>> Why do you change the caption from subsection (Ie. ~~~) to section (Ie. ---)?\n>\n> Possibly the headings changed from the version I originally patched to\n> the current 'master'.\nThat was me one month ago ;-)\n"}]}