]> sipb.mit.edu Git - ikiwiki.git/blob - doc/patchqueue/darcs.mdwn
fix broken format string, manual unfuzzy
[ikiwiki.git] / doc / patchqueue / darcs.mdwn
1 Here's Thomas Schwinge unfinished darcs support for ikiwiki.
2
3 > I haven't been working on this for months and also won't in the near
4 > future.  Feel free to use what I have done so
5 > far and bring it into an usable state!  Also, feel free to contact me
6 >  if there are questions.
7
8 -- [Thomas Schwinge](mailto:tschwinge@gnu.org)
9
10         # Support for the darcs rcs, <URL:http://darcs.net/>.
11         # Copyright (C) 2006  Thomas Schwinge <tschwinge@gnu.org>
12         #
13         # This program is free software; you can redistribute it and/or modify it
14         # under the terms of the GNU General Public License as published by the
15         # Free Software Foundation; either version 2 of the License, or (at your
16         # option) any later version.
17         #
18         # This program is distributed in the hope that it will be useful, but
19         # WITHOUT ANY WARRANTY; without even the implied warranty of
20         # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21         # General Public License for more details.
22         #
23         # You should have received a copy of the GNU General Public License along
24         # with this program; if not, write to the Free Software Foundation, Inc.,
25         # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26         
27         
28         # We're guaranteed to be the only instance of ikiwiki running at a given
29         # time.  It is essential that only ikiwiki is working on a particular
30         # repository.  That means one instance of ikiwiki and it also means that
31         # you must not `darcs push' into this repository, as this might create
32         # race conditions, as I understand it.
33         
34         
35         use warnings;
36         use strict;
37         use IkiWiki;
38         
39         package IkiWiki;
40         
41         
42         # Which darcs executable to use.
43         my $darcs = ($ENV{DARCS} or 'darcs');
44         
45         
46         # Internal functions.
47         
48         sub darcs_info ($$$) {
49             my $field = shift;
50             my $repodir = shift;
51             my $file = shift; # Relative to the repodir.
52         
53             my $child = open(DARCS_CHANGES, "-|");
54             if (! $child) {
55                 exec($darcs, 'changes', '--repo=' . $repodir, '--xml-output', $file) or
56                     error('failed to run `darcs changes\'');
57             }
58         
59             # Brute force for now.  :-/
60             while (<DARCS_CHANGES>) {
61                 last if /^<\/created_as>$/;
62             }
63             ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
64             $field eq 'hash' and s/\.gz//; # Strip away the `.gz' from `hash'es.
65         
66             close(DARCS_CHANGES) or error('`darcs changes\' exited ' . $?);
67         
68             return $_;
69         }
70         
71         
72         # Exported functions.
73         
74         sub rcs_update () {
75             # Not needed.
76         }
77         
78         sub rcs_prepedit ($) {
79             # Prepares to edit a file under revision control.  Returns a token that
80             # must be passed to rcs_commit() when the file is to be commited.  For us,
81             # this token the hash value of the latest patch that modifies the file,
82             # i.e. something like its current revision.  If the file is not yet added
83             # to the repository, we return TODO: the empty string.
84         
85             my $file = shift; # Relative to the repodir.
86         
87             my $hash = darcs_info('hash', $config{srcdir}, $file);
88             return defined $hash ? $hash : "";
89         }
90         
91         sub rcs_commit ($$$) {
92             # Commit the page.  Returns `undef' on success and a version of the page
93             # with conflict markers on failure.
94         
95             my $file = shift; # Relative to the repodir.
96             my $message = shift;
97             my $rcstoken = shift;
98         
99             # Compute if the ``revision'' of $file changed.
100             my $changed = darcs_info('hash', $config{srcdir}, $file) ne $rcstoken;
101         
102             # Yes, the following is a bit convoluted.
103             if ($changed) {
104                 # TODO.  Invent a better, non-conflicting name.
105                 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
106                     error("failed to rename $file to $file.save: $!");
107         
108                 # Roll the repository back to $rcstoken.
109         
110                 # TODO.  Can we be sure that no changes are lost?  I think that
111                 # we can, if we make sure that the `darcs push' below will always
112                 # succeed.
113         
114                 # We need to revert everything as `darcs obliterate' might choke
115                 # otherwise.
116                 # TODO: `yes | ...' needed?  Doesn't seem so.
117                 system($darcs, "revert", "--repodir=" . $config{srcdir}, "--all") and
118                     error("`darcs revert' failed");
119                 # Remove all patches starting at $rcstoken.
120                 # TODO.  Something like `yes | darcs obliterate ...' seems to be needed.
121                 system($darcs, "obliterate", "--quiet", "--repodir" . $config{srcdir},
122                        "--match", "hash " . $rcstoken) and
123                            error("`darcs obliterate' failed");
124                 # Restore the $rcstoken one.
125                 system($darcs, "pull", "--quiet", "--repodir=" . $config{srcdir},
126                        "--match", "hash " . $rcstoken, "--all") and
127                            error("`darcs pull' failed");
128         
129                 # We're back at $rcstoken.  Re-install the modified file.
130                 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
131                     error("failed to rename $file.save to $file: $!");
132             }
133         
134             # Record the changes.
135             # TODO: What if $message is empty?
136             writefile("$file.log", $config{srcdir}, $message);
137             system($darcs, 'record', '--repodir=' . $config{srcdir}, '--all',
138                    '--logfile=' . "$config{srcdir}/$file.log",
139                    '--author=' . 'web commit <web-hurd@gnu.org>', $file) and
140                        error('`darcs record\' failed');
141         
142             # Update the repository by pulling from the default repository, which is
143             # master repository.
144             system($darcs, "pull", "--quiet", "--repodir=" . $config{srcdir},
145                    "--all") and error("`darcs pull' failed\n");
146         
147             # If this updating yields any conflicts, we'll record them now to resolve
148             # them.  If nothing is recorded, there are no conflicts.
149             $rcstoken = darcs_info('hash', $config{srcdir}, $file);
150             # TODO: Use only the first line here, i.e. only the patch name?
151             writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
152             system($darcs, 'record', '--repodir=' . $config{srcdir}, '--all',
153                    '--logfile=' . "$config{srcdir}/$file.log",
154                    '--author=' . 'web commit <web-hurd@gnu.org>', $file) and
155                        error('`darcs record\' failed');
156             my $conflicts = darcs_info('hash', $config{srcdir}, $file) ne $rcstoken;
157             unlink("$config{srcdir}/$file.log") or
158                 error("failed to remove `$file.log'");
159         
160             # Push the changes to the main repository.
161             system($darcs, 'push', '--quiet', '--repodir=' . $config{srcdir}, '--all')
162                 and error('`darcs push\' failed');
163             # TODO: darcs send?
164         
165             if ($conflicts) {
166                 my $document = readfile("$config{srcdir}/$file");
167                 # Try to leave everything in a consistent state.
168                 # TODO: `yes | ...' needed?  Doesn't seem so.
169                 system($darcs, "revert", "--repodir=" . $config{srcdir}, "--all") and
170                     warn("`darcs revert' failed.\n");
171                 return $document;
172             } else {
173                 return undef;
174             }
175         }
176         
177         sub rcs_add ($) {
178             my $file = shift; # Relative to the repodir.
179         
180             # Intermediate directories will be added automagically.
181             system($darcs, 'add', '--quiet', '--repodir=' . $config{srcdir},
182                    '--boring', $file) and error('`darcs add\' failed');
183         }
184         
185         sub rcs_recentchanges ($) {
186             warn('rcs_recentchanges() is not implemented');
187             return 'rcs_recentchanges() is not implemented';
188         }
189         
190         sub rcs_notify () {
191             warn('rcs_notify() is not implemented');
192         }
193         
194         sub rcs_getctime () {
195             warn('rcs_getctime() is not implemented');
196         }
197         
198         1