cogito: understand permissions written as "100755"
[cogito.git] / cg-export
blob40ed1f295251c9f402fe5f256042ac911c5687e0
1 #!/usr/bin/env bash
3 # Export contents of a particular revision
4 # Copyright (c) Johannes E. Schindelin, 2005
6 # Takes a destination and optionally a tree ID as a parameter,
7 # defaulting to 'HEAD'.
9 # The destination can be either a `.tar`, `.tar.gz`, `.tar.bz2` or `.tgz`
10 # for generating a tarball, or `.zip` for generating a zip file. Destination
11 # specifiers not ending by any of these extensions are assumed to be
12 # directory names, and the tree is exported to the given directory.
14 # OPTIONS
15 # -------
16 # -r TREE_ID:: Specify the tree version to export
17 # Base the export on the given tree.
19 # Testsuite: TODO
21 USAGE="cg-export [-r TREE_ID] DESTFILE"
22 _git_requires_root=1
23 _git_wc_unneeded=1
25 . "${COGITO_LIB}"cg-Xlib || exit 1
27 id=
28 while optparse; do
29 if optparse -r=; then
30 # We do not resolve to tree id since git-tar-tree can
31 # utilize some commit information.
32 id="$(cg-object-id -c "$OPTARG" 2>/dev/null)" || id="$OPTARG"
33 else
34 optfail
36 done
38 if [ -z "$id" ]; then
39 id="$(cg-object-id -c)"
42 dest="${ARGS[0]}"
44 ([ -n "$dest" ] && [ -n "$id" ]) || usage
46 [ -e "$dest" ] && die "$dest already exists."
48 case "$dest" in
49 *.tar|*.tar.gz|*.tar.bz2|*.tgz|*.zip)
50 base="${dest%.tar*}"
51 base="${base%.tgz}"
52 base="${base%.zip}"
53 ext="${dest#$base}"
54 base="${base##*/}/"
55 case "$ext" in
56 .tar.gz|.tgz)
57 git-archive --format=tar --prefix="$base" "$id" | gzip -c9 >"$dest"
59 .tar.bz2)
60 git-archive --format=tar --prefix="$base" "$id" | bzip2 -c >"$dest"
62 .tar)
63 git-archive --format=tar --prefix="$base" "$id" >"$dest"
65 .zip)
66 git-archive --format=zip --prefix="$base" "$id" >"$dest"
68 esac
71 mkdir -p "$dest" || die "cannot create $dest"
72 export GIT_INDEX_FILE="$dest/.git-index"
73 id="$(cg-object-id -t "$id")"
74 git-read-tree "$id"
75 git-checkout-index "--prefix=$dest/" -a
76 rm "$GIT_INDEX_FILE"
78 esac