1-- The following hooks are run as soon as revisions or certs to existing revisions
2-- are arriving via netsync. Then they note if a particular, pre-defined branch
3-- (_branch) is touched and if so, a script (_updater) is run at the end of the
4-- netsync session.
5--
6-- A sample script which updates a particular workspace automatically could look
7-- like this. This is particularily useful if you manage a website with monotone
8-- and want that your just committed changes popup on the web server:
9--
10-- #!/bin/bash
11-- branch="my.target.branch"
12-- workspace="/path/to/workspace"
13-- cd $workspace
14-- # only update if there is no divergency
15-- heads=`mtn heads -b $branch 2>/dev/null | wc -l`
16-- if [ "$heads" != "1" ]; then exit; fi
17-- mtn up -r "h:$branch" >/dev/null 2>&1
18--
19-- Copy the following lua hooks into your monotonerc file or load them with
20-- --rcfile for the monotone process which serves your database.
21--
22-- License: GPL
23--
24-- Version history:
25-- ----------------
26--
27-- 0.1 (2007-01-29) Thommas Keller <me@thomaskeller.biz>
28--     - initial version
29--
30
31_branch = "my.target.branch"
32_updater = "/path/to/update.sh"
33_sessions = {}
34
35-- fixme: only session_id is set, so we can't check the server's role or sync type here!
36-- this seems to be some weird bug with monotone (tested with 0.31)
37function note_netsync_start (session_id, my_role, sync_type, remote_host, remote_keyname, includes, excludes)
38        print("netsync_start: starting netsync communication")
39        _sessions[session_id] = 0
40end
41
42function note_netsync_revision_received (new_id, revision, certs, session_id)
43        if _sessions[session_id] == nil then
44                print("revision_received: no session present")
45                return
46        end
47
48        for i,cert in ipairs(certs) do
49                if cert["name"] == "branch" and cert["value"] == _branch then
50                        print("revision_received: found another interesting revision")
51                        _sessions[session_id] = _sessions[session_id] + 1
52                end
53        end
54end
55
56-- check if an interesting cert has arrived due to propagate
57function note_netsync_cert_received (rev_id, key, name, value, session_id)
58        if _sessions[session_id] == nil then
59                print("cert_received: no session present")
60                return
61        end
62
63        if name == branch and value == _branch then
64                print("cert_received: found another interesting cert")
65                _sessions[session_id] = _sessions[session_id] + 1
66        end
67end
68
69function note_netsync_end (session_id, status, bytes_in, bytes_out, certs_in, certs_out, revs_in, revs_out, keys_in, keys_out)
70        if _sessions[session_id] == nil then
71                print("netsync_end: no session present")
72                return
73        end
74
75        -- fixme: we should check status for being != 200, but as above, it seems as this is not set
76
77        -- if no interesting revisions arrived, skip the update
78        if _sessions[session_id] == 0 then
79                print("netsync_end: no interesting revisions/certs received")
80                return
81        end
82
83        _sessions[session_id] = nil
84
85        print("netsync_end: running update script")
86        spawn(_updater)
87end
88