From cc1dbd1b5d610bd5e626f54d310f11cf47684ea1 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Thu, 10 May 2007 11:25:12 +0200 Subject: 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 --- .gitmodules | 5 ++ Makefile | 49 +++++++++++----- submodules.sh | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+), 13 deletions(-) create mode 100644 .gitmodules create mode 100755 submodules.sh diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..51dd1ef --- /dev/null +++ b/.gitmodules @@ -0,0 +1,5 @@ +# This file maps a submodule path to an url from where the submodule +# can be obtained. The script "submodules.sh" finds the url in this file +# when invoked with -i to clone the submodules. + +git git://git.kernel.org/pub/scm/git/git.git diff --git a/Makefile b/Makefile index 644914c..138f261 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,11 @@ CGIT_VERSION = 0.2 prefix = /var/www/htdocs/cgit -gitsrc = git SHA1_HEADER = CACHE_ROOT = /var/cache/cgit -EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto +EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ ui-summary.o ui-log.o ui-view.o ui-tree.o ui-commit.o ui-diff.o \ ui-snapshot.o ui-blob.o @@ -17,28 +16,52 @@ ifdef DEBUG CFLAGS += -g endif -CFLAGS += -I$(gitsrc) -DSHA1_HEADER='$(SHA1_HEADER)' +CFLAGS += -Igit -DSHA1_HEADER='$(SHA1_HEADER)' + + + +# +# basic build rules +# all: cgit +cgit: cgit.c cgit.h $(OBJECTS) + $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \ + $(OBJECTS) $(EXTLIBS) + +$(OBJECTS): cgit.h git/libgit.a + +git/libgit.a: + ./submodules.sh -i + $(MAKE) -C git + +# +# phony targets +# install: all clean-cache mkdir -p $(prefix) install cgit $(prefix)/cgit.cgi install cgit.css $(prefix)/cgit.css -cgit: cgit.c cgit.h $(OBJECTS) $(gitsrc)/libgit.a - $(CC) $(CFLAGS) -DCGIT_VERSION='"$(CGIT_VERSION)"' cgit.c -o cgit \ - $(OBJECTS) $(EXTLIBS) - -$(OBJECTS): cgit.h +clean-cgit: + rm -f cgit *.o -$(gitsrc)/libgit.a: - $(MAKE) -C $(gitsrc) +distclean-cgit: clean-cgit + git clean -d -x +clean-sub: + $(MAKE) -C git clean -.PHONY: clean -clean: - rm -f cgit *.o +distclean-sub: clean-sub + $(shell cd git && git clean -d -x) clean-cache: rm -rf $(CACHE_ROOT)/* + +clean: clean-cgit clean-sub + +distclean: distclean-cgit distclean-sub + +.PHONY: all install clean clean-cgit clean-sub clean-cache \ + distclean distclean-cgit distclean-sub diff --git a/submodules.sh b/submodules.sh new file mode 100755 index 0000000..1d7b13f --- /dev/null +++ b/submodules.sh @@ -0,0 +1,181 @@ +#!/bin/sh +# +# submodules.sh: init, update or list git submodules +# +# Copyright (C) 2006 Lars Hjemli +# +# Licensed under GNU General Public License v2 +# (see COPYING for full license text) +# + + +usage="submodules.sh [-i | -u] [-q] [--cached] [path...]" +init= +update= +quiet= +cached= + + +say() +{ + if test -z "$quiet" + then + echo -e "$@" + fi +} + + +die() +{ + echo >&2 -e "$@" + exit 1 +} + + + +# +# Silently checkout specified submodule revision, return exit status of git-checkout +# +# $1 = local path +# $2 = requested sha1 +# +module_checkout() +{ + $(cd "$1" && git checkout "$2" 1>/dev/null 2>/dev/null) +} + + +# +# Find all (requested) submodules, run clone + checkout on missing paths +# +# $@ = requested paths (default to all) +# +modules_init() +{ + git ls-files --stage -- $@ | grep -e '^160000 ' | + while read mode sha1 stage path + do + test -d "$path/.git" && continue + + if test -d "$path" + then + rmdir "$path" 2>/dev/null || + die "Directory '$path' exist, but not as a submodule" + fi + + test -e "$path" && die "A file already exist at path '$path'" + + url=$(sed -nre "s/^$path[ \t]+//p" .gitmodules) + test -z "$url" && die "No url found for $path in .gitmodules" + + git clone "$url" "$path" || die "Clone of submodule '$path' failed" + module_checkout "$path" "$sha1" || die "Checkout of submodule '$path' failed" + say "Submodule '$path' initialized" + done +} + +# +# Checkout correct revision of each initialized submodule +# +# $@ = requested paths (default to all) +# +modules_update() +{ + git ls-files --stage -- $@ | grep -e '^160000 ' | + while read mode sha1 stage path + do + if ! test -d "$path/.git" + then + say "Submodule '$path' not initialized" + continue; + fi + subsha1=$(cd "$path" && git rev-parse --verify HEAD) || + die "Unable to find current revision of submodule '$path'" + if test "$subsha1" != "$sha1" + then + module_checkout "$path" "$sha1" || + die "Unable to checkout revision $sha1 of submodule '$path'" + say "Submodule '$path' reset to revision $sha1" + fi + done +} + +# +# List all registered submodules, prefixed with: +# - submodule not initialized +# + different version checked out +# +# If --cached was specified the revision in the index will be printed +# instead of the currently checked out revision. +# +# $@ = requested paths (default to all) +# +modules_list() +{ + git ls-files --stage -- $@ | grep -e '^160000 ' | + while read mode sha1 stage path + do + if ! test -d "$path/.git" + then + say "-$sha1 $path" + continue; + fi + revname=$(cd "$path" && git describe $sha1) + if git diff-files --quiet -- "$path" + then + say " $sha1 $path\t($revname)" + else + if test -z "$cached" + then + sha1=$(cd "$path" && git rev-parse HEAD) + revname=$(cd "$path" && git describe HEAD) + fi + say "+$sha1 $path\t($revname)" + fi + done +} + + +while case "$#" in 0) break ;; esac +do + case "$1" in + -i) + init=1 + ;; + -u) + update=1 + ;; + -q) + quiet=1 + ;; + --cached) + cached=1 + ;; + --) + break + ;; + -*) + echo "Usage: $usage" + exit 1 + ;; + --*) + echo "Usage: $usage" + exit 1 + ;; + *) + break + ;; + esac + shift +done + + +if test "$init" = "1" +then + modules_init $@ +elif test "$update" = "1" +then + modules_update $@ +else + modules_list $@ +fi -- cgit 1.4.1-21-gabe81