Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.licenses
/.push-*
/bin
/_test_tools/lfs-server/lfs-test-server
1 change: 1 addition & 0 deletions Dockerfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ RUN /stage_binaries.sh -o {ARG_STAGING} \
-p coreutils \
-p curl \
-p git \
-p git-lfs \
-p gzip \
-p openssh-client \
-p socat \
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@ test: $(BUILD_DIRS)
TEST_TOOLS := $(shell find _test_tools/* -type d -printf "%f ")
test-tools: $(foreach tool, $(TEST_TOOLS), .container-test_tool.$(tool))

# The git-lfs test server is built from the version pinned in tools/go.mod (so
# it is checksum-verified via tools/go.sum), and then copied into an otherwise
# empty image. It is a static binary, so CGO is off and GOOS is always linux.
_test_tools/lfs-server/lfs-test-server: tools/go.mod tools/go.sum
pushd tools >/dev/null; \
export GOOS=linux GOARCH=$(ARCH) CGO_ENABLED=0; \
go build -o ../$@ github.com/git-lfs/lfs-test-server; \
popd >/dev/null
.container-test_tool.lfs-server: _test_tools/lfs-server/lfs-test-server

.container-test_tool.%: _test_tools/% _test_tools/%/*
docker build --build-arg ALPINE_REGISTRY_PREFIX="$(ALPINE_REGISTRY_PREFIX)" -t $(REGISTRY)/test/$$(basename $<) $<
docker images -q $(REGISTRY)/test/$$(basename $<) > $@
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,20 @@ OPTIONS
- off: Disable explicit git garbage collection, which may be a good
fit when also using --one-time.

--git-lfs, $GITSYNC_GIT_LFS
Enable git-lfs, so that LFS-tracked files are downloaded when git
checks them out (including in submodules) instead of being left as
pointer files. This requires the git-lfs binary to be available
(it is included in the official container image); git-sync exits
at startup if it cannot run git-lfs. The LFS endpoint is
discovered by git-lfs in the usual way (e.g. from the repo URL or
from a .lfsconfig file in the repo). If an LFS object cannot be
fetched, the sync fails and the previously published revision is
kept. Credentials supplied via --username/--password or
--credential are passed to git, so they also apply to an LFS
endpoint on the same host as the repo. If not specified, this
defaults to false.

--github-base-url <string>, $GITSYNC_GITHUB_BASE_URL
The GitHub base URL to use in GitHub requests when GitHub app
authentication is used. If not specified, defaults to
Expand Down
27 changes: 27 additions & 0 deletions _test_tools/lfs-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2026 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A minimal git-lfs server for tests. The binary is built by `make test-tools`
# from the version pinned in tools/go.mod; this image just wraps it.

FROM scratch

COPY lfs-test-server /lfs-test-server

# No users, no auth: anyone can upload and download. This is a test tool.
ENV LFS_PUBLIC=true
# Storage (a bolt DB and content files) is created in the current directory.
WORKDIR /

ENTRYPOINT ["/lfs-test-server"]
32 changes: 32 additions & 0 deletions _test_tools/lfs-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# A simple git-lfs server for tests

DO NOT USE THIS FOR ANYTHING BUT TESTING!!!

This wraps https://github.com/git-lfs/lfs-test-server in an empty (`scratch`)
image. The server is not downloaded at image-build time: the version is pinned
(and checksummed) in `tools/go.mod` / `tools/go.sum`, and `make test-tools`
compiles it into `lfs-test-server` here before `docker build`.

It runs in "public" mode, so there are no users or passwords.

## How to use it

```
$ make test-tools
$ docker run --rm --network=host \
--env LFS_LISTEN=tcp://:9377 --env LFS_HOST=localhost:9377 \
example.com/test/lfs-server
```

`LFS_HOST` is what the server puts in the download/upload URLs it hands to
clients, so it must be an address the client can reach.

To use it from a git repo, put the URL in `.lfsconfig`, and upload objects
without needing any git server:

```
$ git lfs install --local
$ git lfs track '*.bin'
$ printf '[lfs]\n\turl = "http://localhost:9377/"\n' > .lfsconfig
$ git lfs push --all http://localhost:9377/
```
47 changes: 47 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type repoSync struct {
ref string // the ref to sync
depth int // for shallow sync
filter string // for partial clone
gitLFS bool // whether to fetch git-lfs objects
submodules submodulesMode // how to handle submodules
gc gcMode // garbage collection
link absPath // absolute path to the symlink to publish
Expand Down Expand Up @@ -196,6 +197,9 @@ func main() {
flFilter := pflag.String("filter",
envString("", "GITSYNC_FILTER"),
"use partial clone with the specified filter (e.g. 'blob:none', 'tree:0')")
flGitLFS := pflag.Bool("git-lfs",
envBool(false, "GITSYNC_GIT_LFS"),
"enable git-lfs, so that LFS-tracked files are fetched when checked out (requires git-lfs to be installed)")
flSubmodules := pflag.String("submodules",
envString("recursive", "GITSYNC_SUBMODULES", "GIT_SYNC_SUBMODULES"),
"git submodule behavior: one of 'recursive', 'shallow', or 'off'")
Expand Down Expand Up @@ -783,6 +787,7 @@ func main() {
ref: *flRef,
depth: *flDepth,
filter: *flFilter,
gitLFS: *flGitLFS,
submodules: submodulesMode(*flSubmodules),
gc: gcMode(*flGitGC),
link: absLink,
Expand Down Expand Up @@ -823,6 +828,15 @@ func main() {
os.Exit(1)
}

// If requested, enable the git-lfs filters in our private gitconfig, so
// that LFS-tracked files are downloaded whenever git checks them out.
if *flGitLFS {
if err := git.SetupGitLFS(ctx); err != nil {
log.Error(err, "can't set up git-lfs")
os.Exit(1)
}
}

// If the --repo or any submodule uses SSH, we need to know which keys.
if err := git.SetupGitSSH(*flSSHKnownHosts, *flSSHKeyFiles, *flSSHKnownHostsFile); err != nil {
log.Error(err, "can't set up git SSH", "keyFiles", *flSSHKeyFiles, "useKnownHosts", *flSSHKnownHosts, "knownHostsFile", *flSSHKnownHostsFile)
Expand Down Expand Up @@ -2316,6 +2330,25 @@ func (git *repoSync) SetupDefaultGitConfigs(ctx context.Context) error {
return nil
}

// SetupGitLFS enables the git-lfs clean/smudge filters in the (private) global
// git config. With the filters in place, any `git checkout`/`git reset` that
// writes an LFS-tracked file (including inside submodules) downloads the real
// content instead of leaving a pointer file. This fails if git-lfs is not
// installed, which is what we want: better to refuse to start than to quietly
// publish pointer files.
//
// Note that just running `git lfs pull` is not sufficient, because it will
// download objects but skip replacing the pointer files unless the filters are
// already configured.
func (git *repoSync) SetupGitLFS(ctx context.Context) error {
git.log.V(1).Info("enabling git-lfs")
// --skip-repo: only touch the global config, not hooks in any repo.
if _, _, err := git.Run(ctx, "", "lfs", "install", "--skip-repo"); err != nil {
return fmt.Errorf("error enabling git-lfs: %w", err)
}
return nil
}

// SetupExtraGitConfigs configures the global git environment with user-provided
// override settings.
func (git *repoSync) SetupExtraGitConfigs(ctx context.Context, configsFlag string, flagName string) error {
Expand Down Expand Up @@ -2743,6 +2776,20 @@ OPTIONS
- off: Disable explicit git garbage collection, which may be a good
fit when also using --one-time.

--git-lfs, $GITSYNC_GIT_LFS
Enable git-lfs, so that LFS-tracked files are downloaded when git
checks them out (including in submodules) instead of being left as
pointer files. This requires the git-lfs binary to be available
(it is included in the official container image); git-sync exits
at startup if it cannot run git-lfs. The LFS endpoint is
discovered by git-lfs in the usual way (e.g. from the repo URL or
from a .lfsconfig file in the repo). If an LFS object cannot be
fetched, the sync fails and the previously published revision is
kept. Credentials supplied via --username/--password or
--credential are passed to git, so they also apply to an LFS
endpoint on the same host as the repo. If not specified, this
defaults to false.

--github-base-url <string>, $GITSYNC_GITHUB_BASE_URL
The GitHub base URL to use in GitHub requests when GitHub app
authentication is used. If not specified, defaults to
Expand Down
132 changes: 132 additions & 0 deletions test_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,51 @@ function GIT_SYNC() {
"$@"
}

# git-lfs tests need a git-lfs server. We run a minimal one (see
# _test_tools/lfs-server) on the host network, so it is "localhost" to the host
# (which uploads objects) and to git-sync (which downloads them).
LFS_PORT=9377

function lfs_server_start() {
if ! git lfs version >/dev/null 2>&1; then
fail "git-lfs must be installed on the host to run this test"
fi
docker_run \
--network=host \
--env LFS_LISTEN="tcp://:$LFS_PORT" \
--env LFS_HOST="localhost:$LFS_PORT" \
e2e/test/lfs-server \
>/dev/null
}

# Commit a git-lfs tracked file (big.bin) with the given content to $REPO. The
# first call also sets up LFS in the repo. Unless $2 is "no-upload", upload
# the LFS object to the server (there is no git server involved, only the LFS
# server). Note that the LFS server URL comes from .lfsconfig in the repo.
#
# The object is uploaded BEFORE the commit is created: a running git-sync polls
# $REPO, and as soon as the commit exists it tries to download the object.
function lfs_commit() {
local content="$1"
local upload="${2:-upload}"

if [[ ! -f "$REPO/.lfsconfig" ]]; then
git -C "$REPO" lfs install --local >/dev/null
git -C "$REPO" lfs track '*.bin' >/dev/null
printf '[lfs]\n\turl = "http://localhost:%s/"\n' "$LFS_PORT" > "$REPO/.lfsconfig"
git -C "$REPO" add .gitattributes .lfsconfig
fi
echo "$content" > "$REPO/big.bin"
git -C "$REPO" add big.bin
if [[ "$upload" == "upload" ]]; then
# The staged blob is the LFS pointer, which holds the object's ID.
local oid
oid=$(git -C "$REPO" show :big.bin | sed -n 's/^oid sha256://p')
git -C "$REPO" lfs push --object-id "http://localhost:$LFS_PORT/" "$oid" >/dev/null
fi
git -C "$REPO" commit -qm "$content"
}

function remove_containers() {
sleep 2 # Let docker finish saving container metadata
docker ps --filter label="git-sync-e2e=$RUNID" --format="{{.ID}}" \
Expand Down Expand Up @@ -3963,6 +4008,93 @@ function e2e::init_max_failures_exceeded() {
assert_file_absent "$ROOT/link/file"
}

##############################################
# Test git-lfs
##############################################

# Without --git-lfs, LFS-tracked files are left as pointer files. This also
# shows that the other tests really depend on --git-lfs doing something.
function e2e::git_lfs_disabled() {
lfs_server_start
lfs_commit "${FUNCNAME[0]}"

GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link"
assert_link_exists "$ROOT/link"
assert_file_contains "$ROOT/link/big.bin" "https://git-lfs.github.com/spec/v1"
}

function e2e::git_lfs() {
lfs_server_start
lfs_commit "${FUNCNAME[0]}"

GIT_SYNC \
--one-time \
--git-lfs \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link"
assert_link_exists "$ROOT/link"
assert_file_eq "$ROOT/link/big.bin" "${FUNCNAME[0]}"
}

# LFS objects are fetched on demand for the checked-out commit, so --depth does
# not interfere.
function e2e::git_lfs_depth() {
lfs_server_start
lfs_commit "${FUNCNAME[0]}-1"
lfs_commit "${FUNCNAME[0]}-2"

GIT_SYNC \
--one-time \
--git-lfs \
--depth=1 \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link"
assert_link_exists "$ROOT/link"
assert_file_eq "$ROOT/link/big.bin" "${FUNCNAME[0]}-2"
}

function e2e::git_lfs_updates() {
lfs_server_start
lfs_commit "${FUNCNAME[0]}-1"

GIT_SYNC \
--period=100ms \
--git-lfs \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link" \
&
wait_for_sync "${MAXWAIT}"
assert_link_exists "$ROOT/link"
assert_file_eq "$ROOT/link/big.bin" "${FUNCNAME[0]}-1"

lfs_commit "${FUNCNAME[0]}-2"
wait_for_sync "${MAXWAIT}"
assert_file_eq "$ROOT/link/big.bin" "${FUNCNAME[0]}-2"
}

# If an LFS object can't be fetched, the sync must fail rather than publish a
# pointer file.
function e2e::git_lfs_missing_object() {
lfs_server_start
lfs_commit "${FUNCNAME[0]}" "no-upload"

assert_fail \
GIT_SYNC \
--one-time \
--git-lfs \
--repo="file://$REPO" \
--root="$ROOT" \
--link="link"
assert_file_absent "$ROOT/link/big.bin"
}

#
# main
#
Expand Down
4 changes: 4 additions & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ go 1.26.10

require (
github.com/estesp/manifest-tool/v2 v2.2.0
github.com/git-lfs/lfs-test-server v0.4.1-0.20251217053250-d27ad23fddc1
github.com/google/go-licenses/v2 v2.0.0-alpha.1
)

require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/containerd/containerd v1.6.6 // indirect
github.com/containerd/containerd/v2 v2.0.4 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
Expand All @@ -26,6 +28,8 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/licenseclassifier/v2 v2.0.0 // indirect
github.com/gorilla/context v1.1.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/Microsoft/hcsshim v0.12.9 h1:2zJy5KA+l0loz1HzEGqyNnjd3fyZA31ZBCGKacp6
github.com/Microsoft/hcsshim v0.12.9/go.mod h1:fJ0gkFAna6ukt0bLdKB8djt4XIJhF/vEPuoIWYVvZ8Y=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -62,6 +64,8 @@ github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/git-lfs/lfs-test-server v0.4.1-0.20251217053250-d27ad23fddc1 h1:kHrYPw90YRy3kGYJsMH98dNb1nvcATArSjfKw5NaYDQ=
github.com/git-lfs/lfs-test-server v0.4.1-0.20251217053250-d27ad23fddc1/go.mod h1:vwmiNBvHSN7jmuH6FvFPouJcEZfmce0Uk98CE0xK2lQ=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand Down Expand Up @@ -106,6 +110,10 @@ github.com/google/licenseclassifier/v2 v2.0.0/go.mod h1:cOjbdH0kyC9R22sdQbYsFkto
github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw=
github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o=
github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand Down
Loading