Repository: fluxcd/flux
Pull request #3179 (2020-07-08 16:03:56)

Put git messages into tmp files
Unfortunately #3140 was not enough to fix problem.

This PR will partially revert #3140 and also put git message for `git commit` and `git note` into TMP files. Preventing `args too long` error.


Patch:
diff --git a/pkg/git/operations.go b/pkg/git/operations.go
index 02324fd7..eb3db7fb 100644
--- a/pkg/git/operations.go
+++ b/pkg/git/operations.go
@@ -8,6 +8,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"io/ioutil"
 	"os"
 	"os/exec"
 	"strings"
@@ -141,7 +142,16 @@ func secretUnseal(ctx context.Context, workingDir string) error {
 }
 
 func commit(ctx context.Context, workingDir string, commitAction CommitAction) error {
-	args := []string{"commit", "--no-verify", "-a", "-m", commitAction.Message}
+	message, err := ioutil.TempFile("", "flux-commit-*.txt")
+	if err != nil {
+		return err
+	}
+	defer os.Remove(message.Name())
+	if _, err := message.WriteString(commitAction.Message); err != nil {
+		return err
+	}
+
+	args := []string{"commit", "--no-verify", "-a", "--file", message.Name()}
 	var env []string
 	if commitAction.Author != "" {
 		args = append(args, "--author", commitAction.Author)
@@ -205,7 +215,17 @@ func addNote(ctx context.Context, workingDir, rev, notesRef string, note interfa
 	if err != nil {
 		return err
 	}
-	args := []string{"notes", "--ref", notesRef, "add", "-m", string(b), rev}
+
+	message, err := ioutil.TempFile("", "flux-note-*.json")
+	if err != nil {
+		return err
+	}
+	defer os.Remove(message.Name())
+	if _, err := message.Write(b); err != nil {
+		return err
+	}
+
+	args := []string{"notes", "--ref", notesRef, "add", "--file", message.Name(), rev}
 	return execGitCmd(ctx, args, gitCmdConfig{dir: workingDir})
 }
 
diff --git a/pkg/update/automated.go b/pkg/update/automated.go
index e4bc129e..5a093e1c 100644
--- a/pkg/update/automated.go
+++ b/pkg/update/automated.go
@@ -69,19 +69,10 @@ func (a *Automated) CommitMessage(result Result) string {
 		fmt.Fprintf(buf, "Auto-release %s", images[0])
 
 	default:
-		limit := 10
-
 		fmt.Fprintf(buf, "Auto-release multiple (%d) images\n\n", total)
-		if total > limit {
-			// Take first 10 images to keep commit message size in bounds
-			images = images[:limit]
-		}
 		for _, im := range images {
 			fmt.Fprintf(buf, " - %s\n", im)
 		}
-		if total > limit {
-			fmt.Fprintln(buf, "   ...")
-		}
 	}
 	return buf.String()
 }


Test patch:
diff --git a/pkg/update/automated_test.go b/pkg/update/automated_test.go
index 740eab6e..501c3b0a 100644
--- a/pkg/update/automated_test.go
+++ b/pkg/update/automated_test.go
@@ -7,32 +7,6 @@ import (
 )
 
 func TestCommitMessage(t *testing.T) {
-	automated := Automated{}
-	result := Result{
-		resource.MakeID("ns", "kind", "1"): {
-			Status: ReleaseStatusSuccess,
-			PerContainer: []ContainerUpdate{
-				{Target: mustParseRef("docker.io/image:v1")},
-				{Target: mustParseRef("docker.io/image:v2")},
-				{Target: mustParseRef("docker.io/image:v3")},
-			},
-		},
-	}
-	result.ChangedImages()
-
-	actual := automated.CommitMessage(result)
-	expected := `Auto-release multiple (3) images
-
- - docker.io/image:v1
- - docker.io/image:v2
- - docker.io/image:v3
-`
-	if actual != expected {
-		t.Fatalf("Expected git commit message: '%s', was '%s'", expected, actual)
-	}
-}
-
-func TestCommitMessage10Max(t *testing.T) {
 	automated := Automated{}
 	result := Result{
 		resource.MakeID("ns", "kind", "1"): {
@@ -67,7 +41,7 @@ func TestCommitMessage10Max(t *testing.T) {
  - docker.io/image:v6
  - docker.io/image:v7
  - docker.io/image:v8
-   ...
+ - docker.io/image:v9
 `
 	if actual != expected {
 		t.Fatalf("Expected git commit message: '%s', was '%s'", expected, actual)