web-logs/Makefile: Minor bug fixes.
[utils/utils.git] / gitar
1 #!/bin/bash
2 # usage: gitar foo-dir >foo.gitar
3
4 set -e
5 trap 'echo "Unexpected error!
6 I am leaving the .git subdirectory around so you can troubleshoot;
7 delete the subdirectory before trying to gitar again." 1>&2' ERR
8 cd "$1"
9
10 if [ -e '.git' ]; then
11         echo 'The source directory is already a git repository!' 1>&2
12         exit 1
13 fi
14
15 if ! 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
17 fi
18
19 # Make repository.
20 git init-db >/dev/null
21
22 # Make a dummy commit to hold all the files.
23 function list-files-to-add {
24         find . -wholename './.git' -prune -or '(' -type f -or -type l ')' -printf '%P\n'
25 }
26 list-files-to-add | git update-index --add --stdin >/dev/null
27 tree=$(git write-tree)
28 function 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 }
31 clean-commit $tree >.git/refs/heads/master
32
33 # Pack things up nicely.
34 git repack -a >/dev/null
35 for i in pack idx; do
36         mv .git/objects/pack/{pack*.$i,pack.$i}
37 done
38 git prune >/dev/null
39
40 # Write out git repository as a Matt-style file tree.
41 function write_file {
42         echo -n "+ ${#2} $2 "
43         stat --format=$'f %s' -- "$1/$2"
44         cat -- "$1/$2"
45         echo
46 }
47 echo '{'
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 '}'
62 echo '}'
63
64 rm -rf .git