Skip to content

Instantly share code, notes, and snippets.

@arteymix
Last active March 15, 2024 10:55
Show Gist options
  • Select an option

  • Save arteymix/03702e3eb05c2c161a86b49d4626d21f to your computer and use it in GitHub Desktop.

Select an option

Save arteymix/03702e3eb05c2c161a86b49d4626d21f to your computer and use it in GitHub Desktop.
Archive HEAD and its submodules recursively
#!/usr/bin/env sh
if [ -z $1 ]; then
echo "You must specify a super-archive name."
exit 1
fi
git archive --prefix "$1/" -o "$1.tar" HEAD
git submodule foreach --recursive "git archive --prefix=$1/\$path/ --output=\$sha1.tar HEAD && tar --concatenate --file=$(pwd)/$1.tar \$sha1.tar && rm \$sha1.tar"
gzip "$1.tar"
@krypty
Copy link

krypty commented Oct 10, 2019

Thank you !

I found a "typo":
git submodule foreach --recursive "git archive --prefix=\$path/ --output=\$sha1.tar HEAD && tar --concatenate --file=$(pwd)/$1.tar \$sha1.tar && rm \$sha1.tar" <-- Note the missing .tar in --file=$(pwd)/$1.tar

@arteymix
Copy link
Author

I just fixed it. It will effectively create two archive instead of concatenating into the main TAR.

@tx0c
Copy link

tx0c commented Feb 24, 2023

found a problem when using this on https://github.com/jaiminpan/pg_jieba which has a recursive submodule,

pg_jieba
  libjieba @ 391121d        <= submodule here which has another submodule under
    deps/
      limonp @ a269e34    

the current using of \$path would cause deps/libmonp being put at top directory instead of libjieba/ 's subdirectory,

debugging showing of these variables as:

ubuntu@Ubuntu:~/src/github.com/jaiminpan/pg_jieba$ git submodule foreach --recursive 'echo variables $name, $sm_path, $displaypath, $sha1 and $toplevel `git rev-parse HEAD`'
Entering 'libjieba'
variables libjieba, libjieba, libjieba, 391121d5db0f31dd5ce9795d4d34812f20eeb25c and /home/ubuntu/src/github.com/jaiminpan/pg_jieba 391121d5db0f31dd5ce9795d4d34812f20eeb25c
Entering 'libjieba/deps/limonp'
variables deps/limonp, deps/limonp, libjieba/deps/limonp, a269e34dc4948d5a9209e21a7887b52daa0d3e78 and /home/ubuntu/src/github.com/jaiminpan/pg_jieba/libjieba a269e34dc4948d5a9209e21a7887b52daa0d3e78

you can see $displaypath showing the correct libjieba/deps/limonp with $path would mean deps/limonp cause being put to the top directory,

Changing your $path to $displaypath resolved this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment