From fd67e7033b36d4a0704cf86c90c38b943a65a5d9 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 25 Feb 2021 20:19:44 -0600 Subject: [PATCH] making a big file --- config/config.go | 3 + versions/max_file_size.go | 116 ++++++++++++++++++++++++++++++++++++++ versions/versions.go | 2 + 3 files changed, 121 insertions(+) create mode 100644 versions/max_file_size.go diff --git a/config/config.go b/config/config.go index 1963587..7cb90cb 100755 --- a/config/config.go +++ b/config/config.go @@ -20,6 +20,7 @@ var ( OAuthServer string VersionInterval time.Duration ReadOnly bool + MaxSizeMB int ) func init() { @@ -38,6 +39,7 @@ func Refresh() { as.Append(args.STRING, "oauth", "oauth URL", "") as.Append(args.DURATION, "version", "duration to mark versions", "0s") 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 { panic(err) } @@ -67,6 +69,7 @@ func Refresh() { OAuthServer = as.GetString("oauth") VersionInterval = as.GetDuration("version") ReadOnly = as.GetBool("ro") + MaxSizeMB = as.GetInt("max-v-size") } const defaultWrapper = ` diff --git a/versions/max_file_size.go b/versions/max_file_size.go new file mode 100644 index 0000000..740904d --- /dev/null +++ b/versions/max_file_size.go @@ -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 +# 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: " \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 +` diff --git a/versions/versions.go b/versions/versions.go index a61b5f4..81a53bf 100755 --- a/versions/versions.go +++ b/versions/versions.go @@ -15,6 +15,8 @@ func New() (*Versions, error) { v.cmd("git", "init") v.cmd("git", "config", "user.email", "user@user.user") v.cmd("git", "config", "user.name", "user") + s := getScript() + panic(s) return v, nil }