Import updates to some utilities that were sitting in my personal bin
[utils/utils.git] / gitar
... / ...
CommitLineData
1#!/bin/bash
2# usage: gitar foo-dir >foo.gitar
3
4set -e
5trap 'echo "Unexpected error!
6I am leaving the .git subdirectory around so you can troubleshoot;
7delete the subdirectory before trying to gitar again." 1>&2' ERR
8cd "$1"
9
10if [ -e '.git' ]; then
11 echo 'The source directory is already a git repository!' 1>&2
12 exit 1
13fi
14
15if ! find . -type d -empty | cmp /dev/null - >/dev/null; then
16 echo 'WARNING: The source directory contains empty directories, and git will drop them.' 1>&2
17fi
18
19# Make repository.
20git init-db >/dev/null
21
22# Make a dummy commit to hold all the files.
23function list-files-to-add {
24 find . -wholename './.git' -prune -or '(' -type f -or -type l ')' -printf '%P\n'
25}
26list-files-to-add | git update-index --add --stdin >/dev/null
27tree=$(git write-tree)
28function clean-commit {
29 GIT_AUTHOR_NAME='reproducible' GIT_AUTHOR_EMAIL='' GIT_AUTHOR_DATE='946684801 +0000' GIT_COMMITTER_NAME='reproducible' GIT_COMMITTER_EMAIL='' GIT_COMMITTER_DATE='946684801 +0000' git commit-tree "$@" </dev/null
30}
31clean-commit $tree >.git/refs/heads/master
32
33# Pack things up nicely.
34git repack -a >/dev/null
35for i in pack idx; do
36 mv .git/objects/pack/{pack*.$i,pack.$i}
37done
38git prune >/dev/null
39
40# Write out git repository as a Matt-style file tree.
41function write_file {
42 echo -n "+ ${#2} $2 "
43 stat --format=$'f %s' -- "$1/$2"
44 cat -- "$1/$2"
45 echo
46}
47echo '{'
48 echo '+ 4 HEAD f 23'
49 echo 'ref: refs/heads/master'
50 echo
51 echo '+ 4 refs {'
52 echo '+ 5 heads {'
53 write_file .git/refs/heads master
54 echo '}'
55 echo '}'
56 echo '+ 7 objects {'
57 echo '+ 4 pack {'
58 write_file .git/objects/pack pack.pack
59 write_file .git/objects/pack pack.idx
60 echo '}'
61 echo '}'
62echo '}'
63
64rm -rf .git