From bc5269f187db9c05d96a3073d127f477b69cd9b7 Mon Sep 17 00:00:00 2001
From: Daniel J Walsh <dwalsh@redhat.com>
Date: Fri, 23 Jun 2023 07:55:12 -0400
Subject: [PATCH] Trim whitespace from unit files while parsing

Fixes: https://github.com/containers/podman/issues/18979

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
---
 .pre-commit-config.yaml                   | 2 +-
 Makefile                                  | 2 +-
 pkg/systemd/parser/unitfile.go            | 2 +-
 test/e2e/quadlet/remap-keep-id2.container | 3 ++-
 4 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index ca023d18a1..0f31ebc5fe 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -12,7 +12,7 @@ repos:
       - id: end-of-file-fixer
         exclude: test/buildah-bud/buildah-tests.diff
       - id: trailing-whitespace
-        exclude: test/buildah-bud/buildah-tests.diff
+        exclude: test/buildah-bud/buildah-tests.diff|test/e2e/quadlet/remap-keep-id2.container
       - id: mixed-line-ending
       - id: check-byte-order-marker
       - id: check-executables-have-shebangs
diff --git a/Makefile b/Makefile
index cc173f1a04..da8e73c342 100644
--- a/Makefile
+++ b/Makefile
@@ -253,7 +253,7 @@ help: ## (Default) Print listing of key targets with their descriptions
 .PHONY: .gitvalidation
 .gitvalidation:
 	@echo "Validating vs commit '$(call err_if_empty,EPOCH_TEST_COMMIT)'"
-	GIT_CHECK_EXCLUDE="./vendor:./test/tools/vendor:docs/make.bat:test/buildah-bud/buildah-tests.diff" ./test/tools/build/git-validation -run DCO,short-subject,dangling-whitespace -range $(EPOCH_TEST_COMMIT)..$(HEAD)
+	GIT_CHECK_EXCLUDE="./vendor:./test/tools/vendor:docs/make.bat:test/buildah-bud/buildah-tests.diff:test/e2e/quadlet/remap-keep-id2.container" ./test/tools/build/git-validation -run DCO,short-subject,dangling-whitespace -range $(EPOCH_TEST_COMMIT)..$(HEAD)
 
 .PHONY: lint
 lint: golangci-lint
diff --git a/pkg/systemd/parser/unitfile.go b/pkg/systemd/parser/unitfile.go
index 56fa1888fb..963909f9d8 100644
--- a/pkg/systemd/parser/unitfile.go
+++ b/pkg/systemd/parser/unitfile.go
@@ -373,7 +373,7 @@ func (p *UnitFileParser) flushPendingComments(toComment bool) {
 func nextLine(data string, afterPos int) (string, string) {
 	rest := data[afterPos:]
 	if i := strings.Index(rest, "\n"); i >= 0 {
-		return data[:i+afterPos], data[i+afterPos+1:]
+		return strings.TrimSpace(data[:i+afterPos]), data[i+afterPos+1:]
 	}
 	return data, ""
 }
diff --git a/test/e2e/quadlet/remap-keep-id2.container b/test/e2e/quadlet/remap-keep-id2.container
index e382f65418..e35af2ba29 100644
--- a/test/e2e/quadlet/remap-keep-id2.container
+++ b/test/e2e/quadlet/remap-keep-id2.container
@@ -2,6 +2,7 @@
 
 [Container]
 Image=localhost/imagename
-RemapUsers=keep-id
+# The added three spaces to keep-id are necessary for test of trimwhitespace
+RemapUsers=keep-id   
 RemapUid=200
 RemapGid=210