File: git-archive-file

package info (click to toggle)
git-extras 7.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,120 kB
  • sloc: sh: 4,312; python: 994; makefile: 146
file content (37 lines) | stat: -rwxr-xr-x 1,149 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash

# extract current branch name
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

# get name of the most top folder of current directory, used for the
# output filename
ARCHIVE_NAME=$(basename "$(git rev-parse --show-toplevel)")

if [[ $BRANCH = tags* ]]; then
    BRANCH=$(git describe)
    echo Building for tag "\"$BRANCH\""
    FILENAME=$ARCHIVE_NAME.$BRANCH.zip
else
    echo Building archive on branch "\"$BRANCH\""
    # get a version string, so archives will not be overwritten when creating
    # many of them
    VERSION=$(git describe --always --long)
    # if not on master append branch name into the filename
    if [ "$BRANCH" = "$(git_extra_default_branch)" ]; then
        FILENAME=$ARCHIVE_NAME.$VERSION.zip
    else
        FILENAME=$ARCHIVE_NAME.$VERSION.$BRANCH.zip
    fi
fi

# rename invalid chars for the file path
FILENAME=${FILENAME//\//-}
FILENAME=${FILENAME//\\/-}
# combine path and filename
OUTPUT=$PWD/$FILENAME

# building archive
git archive --format zip --output "$OUTPUT" "$BRANCH"

# also display size of the resulting file
echo Saved to \""$FILENAME"\" \("$(du -h "$OUTPUT" | cut -f1)"\)