Document how to use ikiwiki with Gerrit Code Review
[ikiwiki] / doc / rcs / git.mdwn
1 [[!meta title="Git"]]
2
3 [Git][git] is a distributed revison control system originally developed for
4 the Linux kernel. Ikiwiki supports storing a wiki in git.
5
6 [git]: http://git.or.cz/
7
8 Ikiwiki can run as a git `post-update` hook to update a wiki
9 whenever commits come in. When running as a [[cgi]],
10 ikiwiki automatically commits edited pages, and uses the
11 git history to generate the [[RecentChanges]] page.
12
13 Normally you can just follow the instructions in [[setup]] to create
14 the git repositories and get started. To understand the details, read on.
15
16 ## git repository setup
17
18 [[!img wiki_edit_flow.svg size=490x align=right]]
19
20 The suggested setup for git has a bare repository, and various
21 working clones (with working directories). The bare
22 repository is pushed to and pulled from the various working clones.
23
24 One of the clones is special; it is the srcdir
25 which is used to compile the wiki, and is also used by the
26 [[cgi]] to commit changes made via the web interface. It is special
27 since the `post-update` hook for the bare root repository is used to
28 trigger an update of this repository, and then an ikiwiki refresh
29 updates the published wiki itself.
30
31 The other (optional) clones are meant for you to work
32 on, and commit to, changes should then be pushed to the bare root
33 repository.
34
35 Using three or more repositories isn't the most obvious set up, but
36 it works the best for typical ikiwiki use. [[ikiwiki-makerepo]] can
37 automate setting this up for the common case where there is no
38 pre-existing wiki. [[tips/Laptop_wiki_with_git]] describes a different
39 way to set up ikiwiki and git.
40
41 ## git repository with multiple committers
42
43 It can be tricky to get the permissions right to allow multiple people to
44 commit to an ikiwiki git repository. As the [[security]] page mentions,
45 for a secure ikiwiki installation, only one person should be able to write
46 to ikiwiki's srcdir. When other committers make commits, their commits
47 should be pushed to the bare repository, which has a `post-update` hook
48 that uses ikiwiki to pull the changes to the srcdir.
49
50 One setup that will work is to put all committers in a group (say,
51 "ikiwiki"), and use permissions to allow that group to commit to the bare git
52 repository. Make both the post-update hook and ikiwiki.cgi be setgid
53 to the group, as well as suid to the user who admins the wiki. The
54 `wrappergroup` [[setup_file_option|usage]] can be used to make the wrappers
55 be setgid to the right group. Then the srcdir, including its git
56 repository, should only be writable by the wiki's admin, and *not* by the
57 group. Take care that ikiwiki uses a umask that does not cause files in
58 the srcdir to become group writable. (umask 022 will work.)
59
60 ## git repository with untrusted committers
61
62 By default, anyone who can commit to the git repository can modify any file
63 on the wiki however they like. A `pre-receive` hook can be set up to limit
64 incoming commits from untrusted users. Then the same limits that are placed
65 on edits via the web will be in effect for commits to git for the users.
66 They will not be allowed to edit locked pages, they will only be able to
67 delete pages that the [[plugins/remove]] configuration allows them to
68 remove, and they will only be allowed to add non-page attachments that the
69 [[plugins/attachment]] configuration allows.
70
71 To enable this, you need to set up the git repository to have multiple
72 committers. Trusted committers, including the user that ikiwiki runs as, 
73 will not have their commits checked by the `pre-receive` hook. Untrusted
74 committers will have their commits checked. The configuration settings to
75 enable are `git_test_receive_wrapper`, which enables generation of a
76 `pre-receive` hook, and `untrusted_committers`, which is a list of
77 usernames of the untrusted committers.
78
79 Note that when the `pre-receive` hook is checking incoming changes, it
80 ignores the git authorship information, and uses the username of the unix
81 user who made the commit. Then tests including the `locked_pages`
82 [[ikiwiki/PageSpec]]
83 are checked to see if that user can edit the pages in the commit.
84
85 You can even set up an [[anonymous_user|tips/untrusted_git_push]], to allow
86 anyone to push changes in via git rather than using the web interface.
87
88 ## Optionally using a local wiki to preview changes
89
90 When working on your wiki,
91 it is common (but optional) practice to preview your changes using a
92 private wiki on the local host before publishing the updates by
93 sending it to the root repository. If you do want to setup a private
94 wiki, you will have to have another setup file and and an ikiwiki
95 installation on your local machine. You will need all the packages
96 this implies -- a web server, git, ikiwiki, etc.  However, there is a
97 _caveat_: by default, ikiwiki pulls and pushes from `origin`. This is
98 not ideal for the working clones on the local machine, since you might
99 go through several iterations of a page before pushing to the bare
100 root of the repository tree (and thus publishing it on your public wiki).
101 You do not want the action of refreshing the local wiki in order to
102 review your work to accidentally publish the
103 contents before you are ready. In order to prevent the git push that
104 is the normal behaviour of ikiwiki, set the configuration of the local wiki:
105
106       gitorigin_branch => "",
107       ## git post-commit wrapper
108       git_wrapper => "/working/dir/.git/hooks/post-commit",
109
110 Then just committing should refresh the private ikiwiki on the local
111 host.  Now just run `ikiwiki -setup localwiki.setup -gettime` and
112 you should be good to go. (You only need the slow `-gettime` option
113 the first time you run setup.)  Use standard git commands to handle
114 pulling from and pushing to the server.  **Note**: After
115 pulling changes from the bare root repository, you will need to
116 manually update the local wiki, with a command such as `ikiwiki
117 -setup localwiki.setup -refresh`. You could use git's `post-merge` hook
118 to automate that command.
119
120 ## Using ikiwiki with Gerrit
121
122 [Gerrit Code Review](https://code.google.com/p/gerrit/) manages a set of Git
123 repositories and provides a web interface to review and merge commits.  You can
124 configure ikiwiki to work with a Gerrit-managed repository, allowing you to
125 review and merge commits to your wiki.
126
127 First, create your initial wiki repository with Gerrit.  On the server, as the
128 user that will own the wiki, clone that repository to create a working
129 directory for ikiwiki, such as /srv/wiki/ikiwiki-checkout.  Create a setup file
130 and target directory as usual, referencing that working directory path, and
131 creating a post-update hook in Gerrit's repository.  You'll need to set
132 appropriate permissions on the hook directory for the repository so that the
133 user running ikiwiki can compile and install the post-update hook.  Also note
134 that you must disable web editing by disabling the editpage plugin, and you
135 must not enable any other plugin that commits to the repository, since ikiwiki
136 will not have permission to push to the repository.  (Allowing web edits to
137 have such permission would bypass Gerrit's code review, defeating the purpose.)
138
139 Gerrit does not run per-repository hooks, such as the post-update hook ikiwiki
140 installs to update the wiki after pushes.  However, Gerrit has site-wide hooks,
141 including a ref-updated hook that runs whenever a ref changes.  You can use
142 that hook to trigger ikiwiki's post-update hook.  The following script,
143 installed as Gerrit's ref-updated hook, will run the post-update hook on any
144 repository that has a "gerrit-run-post-update-hook" file in it:
145
146     #!/bin/sh
147     if [ -e "$GIT_DIR/gerrit-run-post-update-hook" ] ; then
148         exec "$GIT_DIR/hooks/post-update"
149     fi
150
151 Then just create gerrit-run-post-update-hook in the wiki repository, run
152 ikiwiki --setup on the setup file, add your wiki to /etc/ikiwiki/wikilist, and
153 start reviewing and committing wiki changes via Gerrit.