Life is simpler on a git branch.
[ikiwiki.git] / ikiwiki-makerepo
1 #!/bin/sh
2 set -e
3
4 rcs="$1"
5 srcdir="$2"
6 repository="$3"
7
8 usage () {
9         echo "usage: ikiwiki-makerepo cvs|svn|git|monotone|darcs srcdir repository" >&2
10         echo "       ikiwiki-makerepo bzr|mercurial srcdir" >&2
11         exit 1
12 }
13
14 if [ -z "$rcs" ] || [ -z "$srcdir" ]; then
15         usage
16 fi
17
18 if [ ! -d "$srcdir" ]; then
19         echo "srcdir $srcdir not found" >&2 
20         exit 1
21 fi
22
23 if [ "$rcs" != mercurial ] && [ "$rcs" != bzr ]; then
24         if [ -z "$repository" ]; then
25                 echo "you need to specify both a srcdir and a repository for $rcs" >&2
26                 usage
27         fi
28         if [ -e "$repository" ]; then
29                 echo "repository $repository already exists, aborting" >&2 
30                 exit 1
31         fi
32         repository="$(perl -e 'use Cwd; $r=shift; $r=getcwd.q{/}.$r if $r!~m!^/!; print $r' "$repository")"
33         if [ -z "$repository" ]; then
34                 echo "internal error finding repository abs_path" >&2
35                 exit 1
36         fi
37 fi
38
39 echo "Importing $srcdir into $rcs"
40
41 case "$rcs" in
42 cvs)
43         if [ -e "$srcdir/CVS" ]; then
44                 echo "$srcdir already seems to be a cvs working copy" >&2
45                 exit 1
46         fi
47         cvs -Q -d "$repository" init
48         cat > "$repository/CVSROOT/post-commit-wrapper" <<EOF
49 #!/bin/sh
50
51 IKIWIKI_POST_COMMIT_HOOK="$repository/CVSROOT/post-commit"
52
53 exists_ikiwiki_post_commit_hook() {
54         [ -x \$IKIWIKI_POST_COMMIT_HOOK ];
55 }
56
57 called_with_exactly_one_dir() {
58         echo "\$@" | grep 'New directory' >/dev/null 2>&1
59 }
60
61 main() {
62         exists_ikiwiki_post_commit_hook || exit 0
63         called_with_exactly_one_dir "\$@" && exit 0
64         # Return from commit and relinquish write lock. ikiwiki post-commit
65         # wants to "cvs update", which wants to take a read lock.
66         \$IKIWIKI_POST_COMMIT_HOOK &
67         return 0
68 }
69
70 main "\$@"
71 exit \$?
72 EOF
73         chmod +x "$repository/CVSROOT/post-commit-wrapper"
74         cd "$srcdir"/..
75         cvs -Q -d "$repository" get -P CVSROOT
76         cd CVSROOT
77         echo .ikiwiki >> cvsignore
78         cvs -Q add cvsignore
79         echo "^ikiwiki $repository/CVSROOT/post-commit-wrapper %{sVv}" >> loginfo
80         cvs -Q commit -m "ikiwiki-makerepo setup" cvsignore loginfo
81         cd ..
82         rm -rf CVSROOT
83         cd "$srcdir"
84         cvs -Q -d "$repository" import -m "initial import" ikiwiki IKIWIKI PRE_CVS
85         cd ..
86         mv "$srcdir" "$srcdir.orig"
87         cvs -Q -d "$repository" get -P -d "$(basename "$srcdir")" ikiwiki
88         [ -d "$srcdir.orig/.ikiwiki" ] && mv "$srcdir.orig/.ikiwiki" "$srcdir"
89         rm -rf "$srcdir.orig"
90         echo "Directory $srcdir is now a checkout of $rcs repository $repository"
91 ;;
92 svn)
93         if [ -e "$srcdir/.svn" ]; then
94                 echo "$srcdir already seems to be a svn working copy" >&2
95                 exit 1
96         fi
97         svnadmin create "$repository"
98         svn mkdir "file://$repository/trunk" -m "create trunk directory"
99         cd "$srcdir"
100         svn co "file://$repository/trunk" .
101         svn propset svn:ignore ".ikiwiki" .
102         svn add *
103         svn commit -m "initial import"
104         echo "Directory $srcdir is now a checkout of $rcs repository $repository"
105 ;;
106 git)
107         # There are better ways to do this, but this works with older
108         # versions of git.)
109         mkdir -p "$repository"
110         (cd "$repository" && git --bare init --shared)
111
112         cd "$srcdir"
113         git init
114         echo /.ikiwiki > .gitignore
115         echo /recentchanges >> .gitignore
116         git add .
117         git commit -m "initial commit"
118         git remote add origin "$repository"
119         git config branch.master.merge refs/heads/master
120         git config branch.master.remote origin
121         git push --all
122         echo "Directory $srcdir is now a clone of $rcs repository $repository"
123 ;;
124 mercurial)
125         hg init "$srcdir"
126         cd "$srcdir"
127         echo .ikiwiki > .hgignore
128         hg add
129         hg commit -m "initial import"
130         echo "Directory $srcdir is now set up as a mercurial repository"
131 ;;
132 bzr)
133         bzr init "$srcdir"
134         cd "$srcdir"
135         echo .ikiwiki > .bzrignore
136         bzr add
137         bzr commit -m "initial import"
138         echo "Directory $srcdir is now set up as a bzr repository"
139 ;;
140 monotone)
141         if [ -e "$srcdir/_MTN" ]; then
142                 echo "$srcdir already seems to be a monotone working copy" >&2
143                 exit 1
144         fi
145
146         mkdir -p "$(dirname "$repository")"
147         mtn db init -d "$repository"
148
149         cleaned_srcdir=$(basename "$srcdir" | tr -s "[:space:]" "_" | sed 's/_$//g')
150         reverse_hostname=$( (hostname -f 2>/dev/null || hostname) |\
151                 tr  "." "\n" | ( tac 2>/dev/null || tail -r ) | tr "\n" "." )
152         branch_name="$reverse_hostname$cleaned_srcdir"
153         mtn setup -d "$repository" -b "$branch_name" "$srcdir"
154
155         cd "$srcdir"
156         echo \.ikiwiki$ > .mtn-ignore
157         mtn add -R .
158         # this expects that you already have a working mtn environment
159         # with a default key floating around...
160         mtn ci -m "initial import"
161         echo "Directory $srcdir is now set up as a monotone repository"
162         echo ""
163         echo "Note: If your monotone key has a passphrase, you need to configure"
164         echo "monotone to automatically use it. Otherwise, web commits to ikiwiki"
165         echo "will fail."
166         echo ""
167         echo "You can create a $srcdir/_MTN/monotonerc"
168         echo "containing the passphrase:"
169         echo ""
170         echo "function get_passphrase (branchname)"
171         echo '    return "passphrasehere"'
172         echo "end"
173 ;;
174 darcs)
175         if [ -e "$srcdir/_darcs" ]; then
176                 echo "$srcdir already seems to be a darcs repository" >&2
177                 exit 1
178         fi
179
180         mkdir -p "$repository"
181         (cd "$repository" && darcs initialize)
182
183         mkdir -p "$srcdir"
184         cd "$srcdir"
185         darcs initialize
186         echo .ikiwiki >> _darcs/prefs/boring
187         darcs record -a -l -q -m "initial import"
188         darcs pull -a -q "$repository"
189         darcs push -a -q "$repository"
190         echo "Directory $srcdir is now a branch of darcs repo $repository"
191
192         # set up master repo's apply hook and tell user to adjust it if desired
193         darcsdefaults="$repository/_darcs/prefs/defaults"
194         echo "Preconfiguring apply hook in $darcsdefaults - adjust as desired!"
195         echo "apply posthook $repository/_darcs/ikiwiki-wrapper" >> "$darcsdefaults"
196         echo "apply run-posthook" >> "$darcsdefaults"
197 ;;
198 *)
199         echo "Unsupported revision control system $rcs" >&2
200         usage
201 ;;
202 esac