4

I'am using git archive to create a zip file with latest version/HEAD but would like to add the branch name and the commit to the zip filename. How can I achieve this?

Orlando
  • 368
  • 2
  • 9

2 Answers2

3

You can run this script:

#!/bin/sh
sha1=`git rev-parse --short --verify HEAD`
branch=`git symbolic-ref -q --short HEAD`

git archive -o latest_${branch}_${sha1}.zip HEAD

Call it git-auto-archive, for example, make it executable, put in your path, and run it with

git auto-archive
CharlesB
  • 86,532
  • 28
  • 194
  • 218
0

In addition of CharlesB's script, make sure yo use Git 2.20+ (Q4 201), because git archive -o latest_${branch}_${sha1}.zip can produce a tar file instead of a zip one (bug which has been fixed), if use for a --remote repo.

See commit 00436bf (25 Oct 2018) by Josh Steadmon (``).
Helped-by: Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit a5ab66e, 06 Nov 2018)

archive: initialize archivers earlier

Initialize archivers as soon as possible when running git archive.
Various non-obvious behavior depends on having the archivers initialized, such as determining the desired archival format from the provided filename.

Since 08716b3 ("archive: refactor file extension format-guessing", 2011-06-21, Git v1.7.7-rc0), archive_format_from_filename() has used the registered archivers to match filenames (provided via --output) to archival formats.

However, when git archive is executed with --remote, format detection happens before the archivers have been registered.
This causes archives from remotes to always be generated as TAR files, regardless of the actual filename (unless an explicit --format is provided).

This patch fixes that behavior; archival format is determined properly from the output filename, even when --remote is used.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250