Add git-instaweb, instantly browse the working repo with gitweb
I got tired of having to configure gitweb for every repository I work on. I sometimes prefer gitweb to standard GUIs like gitk or gitview; so this lets me automatically configure gitweb to browse my working repository and also opens my browser to it. Updates from the original patch: Added Apache/mod_perl2 compatibility if Dennis Stosberg's gitweb has been applied, too: <20060621130708.Gcbc6e5c@leonov.stosberg.net> General cleanups in shell code usage. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Jul 1, 2006 at 15:14 UTC
a51d37c1df66386a4d9b7559d64a39241d4e47da
4 files changed
+335
-1
.gitignore
+1
@@ -46,6 +46,7 @@ git-http-push
46
git-imap-send
47
git-index-pack
48
git-init-db
49
+git-instaweb
50
git-local-fetch
51
git-log
52
git-lost-found
Documentation/git-instaweb.txt
new
+84
@@ -0,0 +1,84 @@
1
+git-instaweb(1)
2
+===============
3
+
4
+NAME
5
+----
6
+git-instaweb - instantly browse your working repository in gitweb
7
+
8
+SYNOPSIS
9
+--------
10
+'git-instaweb' [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]
11
+
12
+'git-instaweb' [--start] [--stop] [--restart]
13
+
14
+DESCRIPTION
15
+-----------
16
+A simple script to setup gitweb and a web server for browsing the local
17
+repository.
18
+
19
+OPTIONS
20
+-------
21
+
22
+-l|--local::
23
+ Only bind the web server to the local IP (127.0.0.1).
24
+
25
+-d|--httpd::
26
+ The HTTP daemon command-line that will be executed.
27
+ Command-line options may be specified here, and the
28
+ configuration file will be added at the end of the command-line.
29
+ Currently, lighttpd and apache2 are the only supported servers.
30
+ (Default: lighttpd)
31
+
32
+-m|--module-path::
33
+ The module path (only needed if httpd is Apache).
34
+ (Default: /usr/lib/apache2/modules)
35
+
36
+-p|--port::
37
+ The port number to bind the httpd to. (Default: 1234)
38
+
39
+-b|--browser::
40
+
41
+ The web browser command-line to execute to view the gitweb page.
42
+ If blank, the URL of the gitweb instance will be printed to
43
+ stdout. (Default: 'firefox')
44
+
45
+--start::
46
+ Start the httpd instance and exit. This does not generate
47
+ any of the configuration files for spawning a new instance.
48
+
49
+--stop::
50
+ Stop the httpd instance and exit. This does not generate
51
+ any of the configuration files for spawning a new instance,
52
+ nor does it close the browser.
53
+
54
+--restart::
55
+ Restart the httpd instance and exit. This does not generate
56
+ any of the configuration files for spawning a new instance.
57
+
58
+CONFIGURATION
59
+-------------
60
+
61
+You may specify configuration in your .git/config
62
+
63
+-----------------------------------------------------------------------
64
+[instaweb]
65
+ local = true
66
+ httpd = apache2 -f
67
+ port = 4321
68
+ browser = konqueror
69
+ modulepath = /usr/lib/apache2/modules
70
+
71
+-----------------------------------------------------------------------
72
+
73
+Author
74
+------
75
+Written by Eric Wong <normalperson@yhbt.net>
76
+
77
+Documentation
78
+--------------
79
+Documentation by Eric Wong <normalperson@yhbt.net>.
80
+
81
+GIT
82
+---
83
+Part of the gitlink:git[7] suite
84
+
Makefile
+15
-1
@@ -142,7 +142,7 @@ SCRIPT_PYTHON = \
142
SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)) \
143
$(patsubst %.perl,%,$(SCRIPT_PERL)) \
144
$(patsubst %.py,%,$(SCRIPT_PYTHON)) \
145
- git-cherry-pick git-status
145
+ git-cherry-pick git-status git-instaweb
146
147
# The ones that do not have to link with lcrypto, lz nor xdiff.
148
SIMPLE_PROGRAMS = \
@@ -545,6 +545,20 @@ git-status: git-commit
545
cp $< $@+
546
mv $@+ $@
547
548
+git-instaweb: git-instaweb.sh gitweb/gitweb.cgi gitweb/gitweb.css
549
+ rm -f $@ $@+
550
+ sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
551
+ -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
552
+ -e 's/@@NO_CURL@@/$(NO_CURL)/g' \
553
+ -e 's/@@NO_PYTHON@@/$(NO_PYTHON)/g' \
554
+ $@.sh | sed \
555
+ -e 's|@@GITWEB_CGI@@|#!$(PERL_PATH_SQ)|; T; r gitweb/gitweb.cgi' \
556
+ | sed \
557
+ -e 's|@@GITWEB_CSS@@||; T; r gitweb/gitweb.css' \
558
+ > $@+
559
+ chmod +x $@+
560
+ mv $@+ $@
561
+
562
# These can record GIT_VERSION
563
git$X git.spec \
564
$(patsubst %.sh,%,$(SCRIPT_SH)) \
git-instaweb.sh
new
+235
@@ -0,0 +1,235 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2006 Eric Wong
4
+#
5
+USAGE='[--start] [--stop] [--restart]
6
+ [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]
7
+ [--module-path=<path> (for Apache2 only)]'
8
+
9
+. git-sh-setup
10
+
11
+case "$GIT_DIR" in
12
+/*)
13
+ fqgitdir="$GIT_DIR" ;;
14
+*)
15
+ fqgitdir="$PWD/$GIT_DIR" ;;
16
+esac
17
+
18
+local="`git repo-config --bool --get instaweb.local`"
19
+httpd="`git repo-config --get instaweb.httpd`"
20
+browser="`git repo-config --get instaweb.browser`"
21
+port=`git repo-config --get instaweb.port`
22
+module_path="`git repo-config --get instaweb.modulepath`"
23
+
24
+conf=$GIT_DIR/gitweb/httpd.conf
25
+
26
+# Defaults:
27
+
28
+# if installed, it doens't need further configuration (module_path)
29
+test -z "$httpd" && httpd='lighttpd -f'
30
+
31
+# probably the most popular browser among gitweb users
32
+test -z "$browser" && browser='firefox'
33
+
34
+# any untaken local port will do...
35
+test -z "$port" && port=1234
36
+
37
+start_httpd () {
38
+ httpd_only="`echo $httpd | cut -f1 -d' '`"
39
+ if test "`expr index $httpd_only /`" -eq '1' || \
40
+ which $httpd_only >/dev/null
41
+ then
42
+ $httpd $fqgitdir/gitweb/httpd.conf
43
+ else
44
+ # many httpds are installed in /usr/sbin or /usr/local/sbin
45
+ # these days and those are not in most users $PATHs
46
+ for i in /usr/local/sbin /usr/sbin
47
+ do
48
+ if test -x "$i/$httpd_only"
49
+ then
50
+ # don't quote $httpd, there can be
51
+ # arguments to it (-f)
52
+ $i/$httpd "$fqgitdir/gitweb/httpd.conf"
53
+ return
54
+ fi
55
+ done
56
+ fi
57
+}
58
+
59
+stop_httpd () {
60
+ test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
61
+}
62
+
63
+while case "$#" in 0) break ;; esac
64
+do
65
+ case "$1" in
66
+ --stop|stop)
67
+ stop_httpd
68
+ exit 0
69
+ ;;
70
+ --start|start)
71
+ start_httpd
72
+ exit 0
73
+ ;;
74
+ --restart|restart)
75
+ stop_httpd
76
+ start_httpd
77
+ exit 0
78
+ ;;
79
+ --local|-l)
80
+ local=true
81
+ ;;
82
+ -d|--httpd|--httpd=*)
83
+ case "$#,$1" in
84
+ *,*=*)
85
+ httpd=`expr "$1" : '-[^=]*=\(.*\)'` ;;
86
+ 1,*)
87
+ usage ;;
88
+ *)
89
+ httpd="$2"
90
+ shift ;;
91
+ esac
92
+ ;;
93
+ -b|--browser|--browser=*)
94
+ case "$#,$1" in
95
+ *,*=*)
96
+ browser=`expr "$1" : '-[^=]*=\(.*\)'` ;;
97
+ 1,*)
98
+ usage ;;
99
+ *)
100
+ browser="$2"
101
+ shift ;;
102
+ esac
103
+ ;;
104
+ -p|--port|--port=*)
105
+ case "$#,$1" in
106
+ *,*=*)
107
+ port=`expr "$1" : '-[^=]*=\(.*\)'` ;;
108
+ 1,*)
109
+ usage ;;
110
+ *)
111
+ port="$2"
112
+ shift ;;
113
+ esac
114
+ ;;
115
+ -m|--module-path=*|--module-path)
116
+ case "$#,$1" in
117
+ *,*=*)
118
+ module_path=`expr "$1" : '-[^=]*=\(.*\)'` ;;
119
+ 1,*)
120
+ usage ;;
121
+ *)
122
+ module_path="$2"
123
+ shift ;;
124
+ esac
125
+ ;;
126
+ *)
127
+ usage
128
+ ;;
129
+ esac
130
+ shift
131
+done
132
+
133
+mkdir -p "$GIT_DIR/gitweb/tmp"
134
+GIT_EXEC_PATH="`git --exec-path`"
135
+GIT_DIR="$fqgitdir"
136
+export GIT_EXEC_PATH GIT_DIR
137
+
138
+
139
+lighttpd_conf () {
140
+ cat > "$conf" <<EOF
141
+server.document-root = "$fqgitdir/gitweb"
142
+server.port = $port
143
+server.modules = ( "mod_cgi" )
144
+server.indexfiles = ( "gitweb.cgi" )
145
+server.pid-file = "$fqgitdir/pid"
146
+cgi.assign = ( ".cgi" => "" )
147
+mimetype.assign = ( ".css" => "text/css" )
148
+EOF
149
+ test "$local" = true && echo 'server.bind = "127.0.0.1"' >> "$conf"
150
+}
151
+
152
+apache2_conf () {
153
+ test -z "$module_path" && module_path=/usr/lib/apache2/modules
154
+ mkdir -p "$GIT_DIR/gitweb/logs"
155
+ bind=
156
+ test "$local" = true && bind='127.0.0.1:'
157
+ echo 'text/css css' > $fqgitdir/mime.types
158
+ cat > "$conf" <<EOF
159
+ServerRoot "$fqgitdir/gitweb"
160
+DocumentRoot "$fqgitdir/gitweb"
161
+PidFile "$fqgitdir/pid"
162
+Listen $bind$port
163
+TypesConfig $fqgitdir/mime.types
164
+DirectoryIndex gitweb.cgi
165
+EOF
166
+
167
+ # check to see if Dennis Stosberg's mod_perl compatibility patch
168
+ # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
169
+ if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
170
+ "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
171
+ then
172
+ # favor mod_perl if available
173
+ cat >> "$conf" <<EOF
174
+LoadModule perl_module $module_path/mod_perl.so
175
+PerlPassEnv GIT_DIR
176
+PerlPassEnv GIT_EXEC_DIR
177
+<Location /gitweb.cgi>
178
+ SetHandler perl-script
179
+ PerlResponseHandler ModPerl::Registry
180
+ PerlOptions +ParseHeaders
181
+ Options +ExecCGI
182
+</Location>
183
+EOF
184
+ else
185
+ # plain-old CGI
186
+ cat >> "$conf" <<EOF
187
+LoadModule cgi_module $module_path/mod_cgi.so
188
+AddHandler cgi-script .cgi
189
+<Location /gitweb.cgi>
190
+ Options +ExecCGI
191
+</Location>
192
+EOF
193
+ fi
194
+}
195
+
196
+script='
197
+s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#;
198
+s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#;
199
+s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#;
200
+s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
201
+
202
+gitweb_cgi () {
203
+ cat > "$1.tmp" <<\EOFGITWEB
204
+@@GITWEB_CGI@@
205
+EOFGITWEB
206
+ sed "$script" "$1.tmp" > "$1"
207
+ chmod +x "$1"
208
+ rm -f "$1.tmp"
209
+}
210
+
211
+gitweb_css () {
212
+ cat > "$1" <<\EOFGITWEB
213
+@@GITWEB_CSS@@
214
+EOFGITWEB
215
+}
216
+
217
+gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
218
+gitweb_css $GIT_DIR/gitweb/gitweb.css
219
+
220
+case "$httpd" in
221
+*lighttpd*)
222
+ lighttpd_conf
223
+ ;;
224
+*apache2*)
225
+ apache2_conf
226
+ ;;
227
+*)
228
+ echo "Unknown httpd specified: $httpd"
229
+ exit 1
230
+ ;;
231
+esac
232
+
233
+start_httpd
234
+test -z "$browser" && browser=echo
235
+$browser http://127.0.0.1:$port