making a big file
parent
cbd287e20e
commit
fd67e7033b
|
|
@ -20,6 +20,7 @@ var (
|
||||||
OAuthServer string
|
OAuthServer string
|
||||||
VersionInterval time.Duration
|
VersionInterval time.Duration
|
||||||
ReadOnly bool
|
ReadOnly bool
|
||||||
|
MaxSizeMB int
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -38,6 +39,7 @@ func Refresh() {
|
||||||
as.Append(args.STRING, "oauth", "oauth URL", "")
|
as.Append(args.STRING, "oauth", "oauth URL", "")
|
||||||
as.Append(args.DURATION, "version", "duration to mark versions", "0s")
|
as.Append(args.DURATION, "version", "duration to mark versions", "0s")
|
||||||
as.Append(args.BOOL, "ro", "read-only mode", false)
|
as.Append(args.BOOL, "ro", "read-only mode", false)
|
||||||
|
as.Append(args.INT, "max-v-size", "max size in mb for versioning", 2)
|
||||||
if err := as.Parse(); err != nil {
|
if err := as.Parse(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
@ -67,6 +69,7 @@ func Refresh() {
|
||||||
OAuthServer = as.GetString("oauth")
|
OAuthServer = as.GetString("oauth")
|
||||||
VersionInterval = as.GetDuration("version")
|
VersionInterval = as.GetDuration("version")
|
||||||
ReadOnly = as.GetBool("ro")
|
ReadOnly = as.GetBool("ro")
|
||||||
|
MaxSizeMB = as.GetInt("max-v-size")
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultWrapper = `
|
const defaultWrapper = `
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,116 @@
|
||||||
|
package versions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"local/notes-server/config"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getScript() string {
|
||||||
|
return strings.ReplaceAll(script, "{{{MAXSIZE}}}", fmt.Sprint(config.MaxSizeMB<<20))
|
||||||
|
}
|
||||||
|
|
||||||
|
const script = `
|
||||||
|
#!/bin/bash -u
|
||||||
|
# https://github.com/mgit-at/git-max-filesize/blob/master/pre-receive.sh
|
||||||
|
# git-max-filesize
|
||||||
|
#
|
||||||
|
# git pre-receive hook to reject large files that should be commited
|
||||||
|
# via git-lfs (large file support) instead.
|
||||||
|
#
|
||||||
|
# Author: Christoph Hack <chack@mgit.at>
|
||||||
|
# Copyright (c) 2017 mgIT GmbH. All rights reserved.
|
||||||
|
# Distributed under the Apache License. See LICENSE for details.
|
||||||
|
#
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
# "5242880" # 5MB
|
||||||
|
readonly DEFAULT_MAXSIZE={{{MAXSIZE}}}
|
||||||
|
readonly CONFIG_NAME="hooks.maxfilesize"
|
||||||
|
readonly NULLSHA="0000000000000000000000000000000000000000"
|
||||||
|
readonly EXIT_SUCCESS="0"
|
||||||
|
readonly EXIT_FAILURE="1"
|
||||||
|
|
||||||
|
# main entry point
|
||||||
|
function main() {
|
||||||
|
local status="$EXIT_SUCCESS"
|
||||||
|
|
||||||
|
# get maximum filesize (from repository-specific config)
|
||||||
|
local maxsize
|
||||||
|
maxsize="$(get_maxsize)"
|
||||||
|
if [[ "$?" != 0 ]]; then
|
||||||
|
echo "failed to get ${CONFIG_NAME} from config"
|
||||||
|
exit "$EXIT_FAILURE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# skip this hook entirely if maxsize is 0.
|
||||||
|
if [[ "$maxsize" == 0 ]]; then
|
||||||
|
cat > /dev/null
|
||||||
|
exit "$EXIT_SUCCESS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# read lines from stdin (format: "<oldref> <newref> <refname>\n")
|
||||||
|
local oldref
|
||||||
|
local newref
|
||||||
|
local refname
|
||||||
|
while read oldref newref refname; do
|
||||||
|
# skip branch deletions
|
||||||
|
if [[ "$newref" == "$NULLSHA" ]]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# find large objects
|
||||||
|
# check all objects from $oldref (possible $NULLSHA) to $newref, but
|
||||||
|
# skip all objects that have already been accepted (i.e. are referenced by
|
||||||
|
# another branch or tag).
|
||||||
|
local target
|
||||||
|
if [[ "$oldref" == "$NULLSHA" ]]; then
|
||||||
|
target="$newref"
|
||||||
|
else
|
||||||
|
target="${oldref}..${newref}"
|
||||||
|
fi
|
||||||
|
local large_files
|
||||||
|
large_files="$(git rev-list --objects "$target" --not --branches=\* --tags=\* | \
|
||||||
|
git cat-file $'--batch-check=%(objectname)\t%(objecttype)\t%(objectsize)\t%(rest)' | \
|
||||||
|
awk -F '\t' -v maxbytes="$maxsize" '$3 > maxbytes' | cut -f 4-)"
|
||||||
|
if [[ "$?" != 0 ]]; then
|
||||||
|
echo "failed to check for large files in ref ${refname}"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
IFS=$'\n'
|
||||||
|
for file in $large_files; do
|
||||||
|
if [[ "$status" == 0 ]]; then
|
||||||
|
echo ""
|
||||||
|
echo "-------------------------------------------------------------------------"
|
||||||
|
echo "Your push was rejected because it contains files larger than $(numfmt --to=iec "$maxsize")."
|
||||||
|
echo "Please use https://git-lfs.github.com/ to store larger files."
|
||||||
|
echo "-------------------------------------------------------------------------"
|
||||||
|
echo ""
|
||||||
|
echo "Offending files:"
|
||||||
|
status="$EXIT_FAILURE"
|
||||||
|
fi
|
||||||
|
echo " - ${file} (ref: ${refname})"
|
||||||
|
done
|
||||||
|
unset IFS
|
||||||
|
done
|
||||||
|
|
||||||
|
exit "$status"
|
||||||
|
}
|
||||||
|
|
||||||
|
# get the maximum filesize configured for this repository or the default
|
||||||
|
# value if no specific option has been set. Suffixes like 5k, 5m, 5g, etc.
|
||||||
|
# can be used (see git config --int).
|
||||||
|
function get_maxsize() {
|
||||||
|
local value;
|
||||||
|
value="$(git config --int "$CONFIG_NAME")"
|
||||||
|
if [[ "$?" != 0 ]] || [[ -z "$value" ]]; then
|
||||||
|
echo "$DEFAULT_MAXSIZE"
|
||||||
|
return "$EXIT_SUCCESS"
|
||||||
|
fi
|
||||||
|
echo "$value"
|
||||||
|
return "$EXIT_SUCCESS"
|
||||||
|
}
|
||||||
|
|
||||||
|
main
|
||||||
|
`
|
||||||
|
|
@ -15,6 +15,8 @@ func New() (*Versions, error) {
|
||||||
v.cmd("git", "init")
|
v.cmd("git", "init")
|
||||||
v.cmd("git", "config", "user.email", "user@user.user")
|
v.cmd("git", "config", "user.email", "user@user.user")
|
||||||
v.cmd("git", "config", "user.name", "user")
|
v.cmd("git", "config", "user.name", "user")
|
||||||
|
s := getScript()
|
||||||
|
panic(s)
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue