server-side hook to require a bookmark?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

server-side hook to require a bookmark?

Lee Nave
I'm trying to write a server-side hook that would reject any changegroup that doesn't come in with a bookmark specified.  Have looked at a ton of code samples and stackoverflow posts as well as the mercurial source code and I'm still not seeing how to do this.

Is it possible?

Thanks!
Lee


_______________________________________________
Mercurial mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial
Reply | Threaded
Open this post in threaded view
|

Re: server-side hook to require a bookmark?

Bob Hood
On 9/28/2017 3:42 PM, Lee Nave wrote:
> I'm trying to write a server-side hook that would reject any changegroup
> that doesn't come in with a bookmark specified.  Have looked at a ton of
> code samples and stackoverflow posts as well as the mercurial source code
> and I'm still not seeing how to do this.
>
> Is it possible?

I've only written "pretxnchangegroup" hook types so far, but this may be the
case for others types.

I usually use this page as a reference while writing hooks:
https://www.mercurial-scm.org/wiki/MercurialApi

Each incoming changeset is represented by a "Node" object, and that object
provides a plethora of information regarding the changeset.  Each Node object
provides access to a list of the tags (I'm pretty sure these would be the
"bookmarks" you're asking after) associated with the changeset it represents. 
You retrieve this by calling Node.tags().  That would probably be where you'd
want to start your enforcement; e.g.:

     ...
     ctx = repo[kwargs['node']]
     start = ctx.rev()
     end = len(repo)

     for rev in xrange(start, end):
         node = repo[rev]

         if "MyRequiredBookmark" not in node.tags():
             ui.warn("Error: Changeset %d:%s is missing a required bookmark;
rejecting!\n" % (rev, node.hex()[:12]))
             return True  # reject the changegroup
     ...

_______________________________________________
Mercurial mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial
Reply | Threaded
Open this post in threaded view
|

Re: server-side hook to require a bookmark?

Lee Nave
On Thu, Sep 28, 2017 at 3:34 PM, Bob Hood <[hidden email]> wrote:
On 9/28/2017 3:42 PM, Lee Nave wrote:
I'm trying to write a server-side hook that would reject any changegroup that doesn't come in with a bookmark specified.  Have looked at a ton of code samples and stackoverflow posts as well as the mercurial source code and I'm still not seeing how to do this.

Is it possible?

I've only written "pretxnchangegroup" hook types so far, but this may be the case for others types.

I usually use this page as a reference while writing hooks: https://www.mercurial-scm.org/wiki/MercurialApi

Each incoming changeset is represented by a "Node" object, and that object provides a plethora of information regarding the changeset.  Each Node object provides access to a list of the tags (I'm pretty sure these would be the "bookmarks" you're asking after) associated with the changeset it represents.  You retrieve this by calling Node.tags().  That would probably be where you'd want to start your enforcement; e.g.:
[snip]

I appreciate the suggestion, but bookmarks and tags are different beasts.  From my reading of the mercurial source I'm inclined to think that the bookmark is sent after the changegroup in a separate transaction (even though to the user it appears to be a single operation) and there may be no way to do this.

_______________________________________________
Mercurial mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial
Reply | Threaded
Open this post in threaded view
|

Re: server-side hook to require a bookmark?

Bob Hood
On 9/28/2017 4:40 PM, Lee Nave wrote:
On Thu, Sep 28, 2017 at 3:34 PM, Bob Hood <[hidden email]> wrote:
On 9/28/2017 3:42 PM, Lee Nave wrote:
I'm trying to write a server-side hook that would reject any changegroup that doesn't come in with a bookmark specified.  Have looked at a ton of code samples and stackoverflow posts as well as the mercurial source code and I'm still not seeing how to do this.

Is it possible?

I've only written "pretxnchangegroup" hook types so far, but this may be the case for others types.

I usually use this page as a reference while writing hooks: https://www.mercurial-scm.org/wiki/MercurialApi

Each incoming changeset is represented by a "Node" object, and that object provides a plethora of information regarding the changeset.  Each Node object provides access to a list of the tags (I'm pretty sure these would be the "bookmarks" you're asking after) associated with the changeset it represents.  You retrieve this by calling Node.tags().  That would probably be where you'd want to start your enforcement; e.g.:
[snip]

I appreciate the suggestion, but bookmarks and tags are different beasts.  From my reading of the mercurial source I'm inclined to think that the bookmark is sent after the changegroup in a separate transaction (even though to the user it appears to be a single operation) and there may be no way to do this.

Sorry, I assumed the tags were the bookmarks--I get confused between Mercurial and git.  :)

Because the "pretxnchangegroup" hook applies the changesets to the repository, but has not yet committed them (transaction), you might be able to directly query the state of the repository at that point to review the impact of the changesets, and see if the bookmarks are present.  Reject (and roll back) if not.

Or, again, I could be wrong.  :)


_______________________________________________
Mercurial mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial
Reply | Threaded
Open this post in threaded view
|

Re: server-side hook to require a bookmark?

Lee Nave
In reply to this post by Lee Nave
On Fri, Sep 29, 2017 at 9:10 AM, Pierre-Yves David <[hidden email]> wrote:


On 09/28/2017 11:40 PM, Lee Nave wrote:
On Thu, Sep 28, 2017 at 3:34 PM, Bob Hood <[hidden email] <mailto:[hidden email]>> wrote:

    On 9/28/2017 3:42 PM, Lee Nave wrote:

        I'm trying to write a server-side hook that would reject any
        changegroup that doesn't come in with a bookmark specified.         Have looked at a ton of code samples and stackoverflow posts as
        well as the mercurial source code and I'm still not seeing how
        to do this.

        Is it possible?


    I've only written "pretxnchangegroup" hook types so far, but this
    may be the case for others types.

    I usually use this page as a reference while writing hooks:
    https://www.mercurial-scm.org/wiki/MercurialApi
    <https://www.mercurial-scm.org/wiki/MercurialApi>

    Each incoming changeset is represented by a "Node" object, and that
    object provides a plethora of information regarding the changeset.     Each Node object provides access to a list of the tags (I'm pretty
    sure these would be the "bookmarks" you're asking after) associated
    with the changeset it represents.  You retrieve this by calling
    Node.tags().  That would probably be where you'd want to start your
    enforcement; e.g.:
    [snip]


I appreciate the suggestion, but bookmarks and tags are different beasts.  From my reading of the mercurial source I'm inclined to think that the bookmark is sent after the changegroup in a separate transaction (even though to the user it appears to be a single operation) and there may be no way to do this.

Changesets and bookmarks are handled in the same transaction for push and pull. You need to use the pretxnclose hooks for that,

Thanks!  pretxnclose does the trick:

def require_bookmark_on_release(ui, repo, **kwargs):
    if repo[None].branch() == 'release' and not repo.currenttransaction().changes['bookmarks']:
        ui.warn('A bookmark is required when committing to the release branch\n')
        return True
    else:
        return False

_______________________________________________
Mercurial mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial