summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2007-05-10 09:25:12 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2007-05-11 09:03:52 (UTC)
commitcc1dbd1b5d610bd5e626f54d310f11cf47684ea1 (patch) (unidiff)
treeceede74f95d643b085232aa66a7d69d04e255208
parente743443811f5816f02a9ca67404613be97bfa615 (diff)
downloadcgit-cc1dbd1b5d610bd5e626f54d310f11cf47684ea1.zip
cgit-cc1dbd1b5d610bd5e626f54d310f11cf47684ea1.tar.gz
cgit-cc1dbd1b5d610bd5e626f54d310f11cf47684ea1.tar.bz2
Add submodules.sh and use it during builds
This adds a shell script which can be be used to initialize, list and update submodules in a git repository. It reads the file .gitmodules to find a mapping between submodule path and repository url for the initial clone of all submodules. The script is used during cgit builds to enable automatic download and checkout of the git git repository. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--.gitmodules5
-rw-r--r--Makefile49
-rwxr-xr-xsubmodules.sh181
3 files changed, 222 insertions, 13 deletions
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..51dd1ef
--- a/dev/null
+++ b/.gitmodules
@@ -0,0 +1,5 @@
1# This file maps a submodule path to an url from where the submodule
2# can be obtained. The script "submodules.sh" finds the url in this file
3# when invoked with -i to clone the submodules.
4
5 git git://git.kernel.org/pub/scm/git/git.git
diff --git a/Makefile b/Makefile
index 644914c..138f261 100644
--- a/Makefile
+++ b/Makefile
@@ -3,3 +3,2 @@ CGIT_VERSION = 0.2
3prefix = /var/www/htdocs/cgit 3prefix = /var/www/htdocs/cgit
4gitsrc = git
5 4
@@ -8,3 +7,3 @@ SHA1_HEADER = <openssl/sha.h>
8CACHE_ROOT = /var/cache/cgit 7CACHE_ROOT = /var/cache/cgit
9EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto 8EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto
10OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ 9OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \
@@ -19,6 +18,25 @@ endif
19 18
20CFLAGS += -I$(gitsrc) -DSHA1_HEADER='$(SHA1_HEADER)' 19CFLAGS += -Igit -DSHA1_HEADER='$(SHA1_HEADER)'
21 20
21
22
23
24#
25# basic build rules
26#
22all: cgit 27all: cgit
23 28
29cgit: cgit.c cgit.h $(OBJECTS)
30 $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \
31 $(OBJECTS) $(EXTLIBS)
32
33$(OBJECTS): cgit.h git/libgit.a
34
35git/libgit.a:
36 ./submodules.sh -i
37 $(MAKE) -C git
38
39#
40# phony targets
41#
24install: all clean-cache 42install: all clean-cache
@@ -28,15 +46,13 @@ install: all clean-cache
28 46
29cgit: cgit.c cgit.h $(OBJECTS) $(gitsrc)/libgit.a 47clean-cgit:
30 $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \ 48 rm -f cgit *.o
31 $(OBJECTS) $(EXTLIBS)
32
33$(OBJECTS): cgit.h
34 49
35$(gitsrc)/libgit.a: 50distclean-cgit: clean-cgit
36 $(MAKE) -C $(gitsrc) 51 git clean -d -x
37 52
53clean-sub:
54 $(MAKE) -C git clean
38 55
39.PHONY: clean 56distclean-sub: clean-sub
40clean: 57 $(shell cd git && git clean -d -x)
41 rm -f cgit *.o
42 58
@@ -44 +60,8 @@ clean-cache:
44 rm -rf $(CACHE_ROOT)/* 60 rm -rf $(CACHE_ROOT)/*
61
62clean: clean-cgit clean-sub
63
64distclean: distclean-cgit distclean-sub
65
66.PHONY: all install clean clean-cgit clean-sub clean-cache \
67 distclean distclean-cgit distclean-sub
diff --git a/submodules.sh b/submodules.sh
new file mode 100755
index 0000000..1d7b13f
--- a/dev/null
+++ b/submodules.sh
@@ -0,0 +1,181 @@
1#!/bin/sh
2#
3# submodules.sh: init, update or list git submodules
4#
5# Copyright (C) 2006 Lars Hjemli
6#
7# Licensed under GNU General Public License v2
8# (see COPYING for full license text)
9#
10
11
12usage="submodules.sh [-i | -u] [-q] [--cached] [path...]"
13init=
14update=
15quiet=
16cached=
17
18
19say()
20{
21 if test -z "$quiet"
22 then
23 echo -e "$@"
24 fi
25}
26
27
28die()
29{
30 echo >&2 -e "$@"
31 exit 1
32}
33
34
35
36#
37# Silently checkout specified submodule revision, return exit status of git-checkout
38#
39# $1 = local path
40# $2 = requested sha1
41#
42module_checkout()
43{
44 $(cd "$1" && git checkout "$2" 1>/dev/null 2>/dev/null)
45}
46
47
48#
49# Find all (requested) submodules, run clone + checkout on missing paths
50#
51# $@ = requested paths (default to all)
52#
53modules_init()
54{
55 git ls-files --stage -- $@ | grep -e '^160000 ' |
56 while read mode sha1 stage path
57 do
58 test -d "$path/.git" && continue
59
60 if test -d "$path"
61 then
62 rmdir "$path" 2>/dev/null ||
63 die "Directory '$path' exist, but not as a submodule"
64 fi
65
66 test -e "$path" && die "A file already exist at path '$path'"
67
68 url=$(sed -nre "s/^$path[ \t]+//p" .gitmodules)
69 test -z "$url" && die "No url found for $path in .gitmodules"
70
71 git clone "$url" "$path" || die "Clone of submodule '$path' failed"
72 module_checkout "$path" "$sha1" || die "Checkout of submodule '$path' failed"
73 say "Submodule '$path' initialized"
74 done
75}
76
77#
78# Checkout correct revision of each initialized submodule
79#
80# $@ = requested paths (default to all)
81#
82modules_update()
83{
84 git ls-files --stage -- $@ | grep -e '^160000 ' |
85 while read mode sha1 stage path
86 do
87 if ! test -d "$path/.git"
88 then
89 say "Submodule '$path' not initialized"
90 continue;
91 fi
92 subsha1=$(cd "$path" && git rev-parse --verify HEAD) ||
93 die "Unable to find current revision of submodule '$path'"
94 if test "$subsha1" != "$sha1"
95 then
96 module_checkout "$path" "$sha1" ||
97 die "Unable to checkout revision $sha1 of submodule '$path'"
98 say "Submodule '$path' reset to revision $sha1"
99 fi
100 done
101}
102
103#
104# List all registered submodules, prefixed with:
105# - submodule not initialized
106# + different version checked out
107#
108# If --cached was specified the revision in the index will be printed
109# instead of the currently checked out revision.
110#
111# $@ = requested paths (default to all)
112#
113modules_list()
114{
115 git ls-files --stage -- $@ | grep -e '^160000 ' |
116 while read mode sha1 stage path
117 do
118 if ! test -d "$path/.git"
119 then
120 say "-$sha1 $path"
121 continue;
122 fi
123 revname=$(cd "$path" && git describe $sha1)
124 if git diff-files --quiet -- "$path"
125 then
126 say " $sha1 $path\t($revname)"
127 else
128 if test -z "$cached"
129 then
130 sha1=$(cd "$path" && git rev-parse HEAD)
131 revname=$(cd "$path" && git describe HEAD)
132 fi
133 say "+$sha1 $path\t($revname)"
134 fi
135 done
136}
137
138
139while case "$#" in 0) break ;; esac
140do
141 case "$1" in
142 -i)
143 init=1
144 ;;
145 -u)
146 update=1
147 ;;
148 -q)
149 quiet=1
150 ;;
151 --cached)
152 cached=1
153 ;;
154 --)
155 break
156 ;;
157 -*)
158 echo "Usage: $usage"
159 exit 1
160 ;;
161 --*)
162 echo "Usage: $usage"
163 exit 1
164 ;;
165 *)
166 break
167 ;;
168 esac
169 shift
170done
171
172
173if test "$init" = "1"
174then
175 modules_init $@
176elif test "$update" = "1"
177then
178 modules_update $@
179else
180 modules_list $@
181fi