Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Author: Fam Zheng <famz@redhat.com> |
| 4 | # |
| 5 | # Archive source tree, including submodules. This is created for test code to |
| 6 | # export the source files, in order to be built in a different environment, |
| 7 | # such as in a docker instance or VM. |
| 8 | # |
| 9 | # This code is licensed under the GPL version 2 or later. See |
| 10 | # the COPYING file in the top-level directory. |
| 11 | |
| 12 | error() { |
| 13 | printf %s\\n "$*" >&2 |
| 14 | exit 1 |
| 15 | } |
| 16 | |
| 17 | if test $# -lt 1; then |
| 18 | error "Usage: $0 <output tarball>" |
| 19 | fi |
| 20 | |
Mao Zhongyi | 934821e | 2018-10-15 17:17:34 +0800 | [diff] [blame] | 21 | tar_file=$(realpath "$1") |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 22 | list_file="${tar_file}.list" |
| 23 | vroot_dir="${tar_file}.vroot" |
Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 24 | |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 25 | # We want a predictable list of submodules for builds, that is |
| 26 | # independent of what the developer currently has initialized |
| 27 | # in their checkout, because the build environment is completely |
| 28 | # different to the host OS. |
Daniel P. Berrange | 9271282 | 2017-09-29 11:11:58 +0100 | [diff] [blame] | 29 | submodules="dtc ui/keycodemapdb" |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 30 | |
| 31 | trap "status=$?; rm -rf \"$list_file\" \"$vroot_dir\"; exit \$status" 0 1 2 3 15 |
| 32 | |
| 33 | if git diff-index --quiet HEAD -- &>/dev/null |
| 34 | then |
| 35 | HEAD=HEAD |
| 36 | else |
Mao Zhongyi | 934821e | 2018-10-15 17:17:34 +0800 | [diff] [blame] | 37 | HEAD=$(git stash create) |
Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 38 | fi |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 39 | git clone --shared . "$vroot_dir" |
| 40 | test $? -ne 0 && error "failed to clone into '$vroot_dir'" |
Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 41 | |
Daniel P. Berrange | 47bb908 | 2017-09-29 11:11:57 +0100 | [diff] [blame] | 42 | cd "$vroot_dir" |
| 43 | test $? -ne 0 && error "failed to change into '$vroot_dir'" |
| 44 | |
| 45 | git checkout $HEAD |
| 46 | test $? -ne 0 && error "failed to checkout $HEAD revision" |
| 47 | |
| 48 | for sm in $submodules; do |
| 49 | git submodule update --init $sm |
| 50 | test $? -ne 0 && error "failed to init submodule $sm" |
| 51 | done |
Fam Zheng | 6b560c7 | 2017-09-05 10:11:52 +0800 | [diff] [blame] | 52 | |
| 53 | if test -n "$submodules"; then |
| 54 | { |
| 55 | git ls-files || error "git ls-files failed" |
| 56 | for sm in $submodules; do |
| 57 | (cd $sm; git ls-files) | sed "s:^:$sm/:" |
| 58 | if test "${PIPESTATUS[*]}" != "0 0"; then |
| 59 | error "git ls-files in submodule $sm failed" |
| 60 | fi |
| 61 | done |
| 62 | } | grep -x -v $(for sm in $submodules; do echo "-e $sm"; done) > "$list_file" |
| 63 | else |
| 64 | git ls-files > "$list_file" |
| 65 | fi |
| 66 | |
| 67 | if test $? -ne 0; then |
| 68 | error "failed to generate list file" |
| 69 | fi |
| 70 | |
| 71 | tar -cf "$tar_file" -T "$list_file" || error "failed to create tar file" |
| 72 | |
| 73 | exit 0 |