| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os/exec" |
| 7 | "strings" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // DefaultMaintTimeout is de maximale duur van onderhoud op één repository. |
| 12 | const DefaultMaintTimeout = 30 * time.Minute |
| 13 | |
| 14 | // MaintenanceArgs is het voorkeurscommando (modern git). Geen --prune=now. |
| 15 | func MaintenanceArgs() []string { |
| 16 | return []string{"maintenance", "run", "--task=gc", "--task=loose-objects", "--task=incremental-repack"} |
| 17 | } |
| 18 | |
| 19 | // GCAutoArgs is de veilige kern: git's eigen auto-drempels, standaard prune-termijn, locking. |
| 20 | func GCAutoArgs() []string { |
| 21 | return []string{"gc", "--auto"} |
| 22 | } |
| 23 | |
| 24 | // extraMaintTasks zijn optionele stappen na gc --auto. Een taak die niets |
| 25 | // te doen heeft (geen packfiles) is geen fout. |
| 26 | func extraMaintTasks() [][]string { |
| 27 | return [][]string{ |
| 28 | {"maintenance", "run", "--task=loose-objects"}, |
| 29 | {"maintenance", "run", "--task=incremental-repack"}, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // MaintResult is de uitkomst van onderhoud op één bare repository. |
| 34 | type MaintResult struct { |
| 35 | Dir string |
| 36 | OK bool |
| 37 | Output string |
| 38 | Err string |
| 39 | Duration time.Duration |
| 40 | } |
| 41 | |
| 42 | // Maintain voert veilig git-onderhoud uit op één bare repository. |
| 43 | // Eerst `git gc --auto` (locking, geen --prune=now). Daarna optioneel |
| 44 | // loose-objects en incremental-repack; die mogen stil falen als er niets |
| 45 | // te doen is of als de git-binary maintenance niet kent. Een lock van |
| 46 | // een push of clone laat git falen; de aanroeper slaat de repo over. |
| 47 | func Maintain(ctx context.Context, gitBin, dir string) MaintResult { |
| 48 | start := time.Now() |
| 49 | res := MaintResult{Dir: dir} |
| 50 | if gitBin == "" { |
| 51 | gitBin = "git" |
| 52 | } |
| 53 | if dir == "" { |
| 54 | res.Err = "leeg pad" |
| 55 | res.Duration = time.Since(start) |
| 56 | return res |
| 57 | } |
| 58 | |
| 59 | var logs []string |
| 60 | out, err := runNice(ctx, gitBin, append([]string{"-C", dir}, GCAutoArgs()...)...) |
| 61 | if strings.TrimSpace(out) != "" { |
| 62 | logs = append(logs, strings.TrimSpace(out)) |
| 63 | } |
| 64 | if err != nil { |
| 65 | res.Err = err.Error() |
| 66 | res.Output = strings.Join(logs, "\n") |
| 67 | res.Duration = time.Since(start) |
| 68 | return res |
| 69 | } |
| 70 | for _, args := range extraMaintTasks() { |
| 71 | if ctx.Err() != nil { |
| 72 | break |
| 73 | } |
| 74 | out, err = runNice(ctx, gitBin, append([]string{"-C", dir}, args...)...) |
| 75 | if strings.TrimSpace(out) != "" { |
| 76 | logs = append(logs, strings.TrimSpace(out)) |
| 77 | } |
| 78 | if err == nil || isSkippableMaint(err) { |
| 79 | continue |
| 80 | } |
| 81 | // Een echte fout op een extra taak is geen reden de ronde te |
| 82 | // laten mislukken: gc --auto is al gelukt. We loggen het wel. |
| 83 | logs = append(logs, err.Error()) |
| 84 | } |
| 85 | res.Output = strings.Join(logs, "\n") |
| 86 | res.Duration = time.Since(start) |
| 87 | res.OK = true |
| 88 | return res |
| 89 | } |
| 90 | |
| 91 | func isSkippableMaint(err error) bool { |
| 92 | if err == nil { |
| 93 | return true |
| 94 | } |
| 95 | msg := strings.ToLower(err.Error()) |
| 96 | return strings.Contains(msg, "is not a git command") || |
| 97 | strings.Contains(msg, "unknown subcommand") || |
| 98 | strings.Contains(msg, "unknown command") || |
| 99 | strings.Contains(msg, "no pack files") || |
| 100 | strings.Contains(msg, "nothing to repack") || |
| 101 | strings.Contains(msg, "nothing to do") |
| 102 | } |
| 103 | |
| 104 | func runNice(ctx context.Context, bin string, args ...string) (string, error) { |
| 105 | var cmd *exec.Cmd |
| 106 | if nice, err := exec.LookPath("nice"); err == nil { |
| 107 | cmd = exec.CommandContext(ctx, nice, append([]string{"-n", "19", bin}, args...)...) |
| 108 | } else { |
| 109 | cmd = exec.CommandContext(ctx, bin, args...) |
| 110 | } |
| 111 | var stdout, stderr strings.Builder |
| 112 | cmd.Stdout = &stdout |
| 113 | cmd.Stderr = &stderr |
| 114 | if err := cmd.Run(); err != nil { |
| 115 | msg := strings.TrimSpace(stderr.String()) |
| 116 | if msg == "" { |
| 117 | msg = err.Error() |
| 118 | } |
| 119 | return stdout.String(), fmt.Errorf("%s", msg) |
| 120 | } |
| 121 | return stdout.String(), nil |
| 122 | } |