git.go

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5package main
 6
 7import (
 8	"fmt"
 9	"os"
10	"os/exec"
11)
12
13func runGitWithStdin(args []string, content string) error {
14	gitCmd := exec.Command("git", args...)
15	gitCmd.Stdout = os.Stdout
16	gitCmd.Stderr = os.Stderr
17
18	stdin, err := gitCmd.StdinPipe()
19	if err != nil {
20		return fmt.Errorf("failed to create stdin pipe: %w", err)
21	}
22
23	if err := gitCmd.Start(); err != nil {
24		return fmt.Errorf("failed to start git command: %w", err)
25	}
26
27	if _, err := stdin.Write([]byte(content)); err != nil {
28		return fmt.Errorf("failed to write to git stdin: %w", err)
29	}
30
31	if err := stdin.Close(); err != nil {
32		return fmt.Errorf("failed to close stdin: %w", err)
33	}
34
35	if err := gitCmd.Wait(); err != nil {
36		return fmt.Errorf("git %s failed: %w", args[0], err)
37	}
38
39	return nil
40}
41
42func validateSubjectLength(subject string) error {
43	length := len(subject)
44	if length > 50 {
45		exceededBy := length - 50
46		truncated := subject[:50] + "…"
47		return fmt.Errorf("subject exceeds 50 character limit by %d:\n%s", exceededBy, truncated)
48	}
49	return nil
50}