Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): suppress git console windows on Windows (#2358)

Kagurafb026f2607f9
diff --git a/server/cmd/multica/cmd_daemon.go b/server/cmd/multica/cmd_daemon.go
index b2af1768ac..7e59a098a0 100644
--- a/server/cmd/multica/cmd_daemon.go
+++ b/server/cmd/multica/cmd_daemon.go
@@ -19,6 +19,7 @@ import (
"github.com/multica-ai/multica/server/internal/cli"
"github.com/multica-ai/multica/server/internal/daemon"
logger_pkg "github.com/multica-ai/multica/server/internal/logger"
+ "github.com/multica-ai/multica/server/internal/util"
)
var daemonCmd = &cobra.Command{
@@ -301,6 +302,8 @@ func buildDaemonStartArgs(cmd *cobra.Command) []string {
}
func runDaemonForeground(cmd *cobra.Command) error {
+ util.EnsureHiddenConsole()
+
profile := resolveProfile(cmd)
serverURL := cli.FlagOrEnv(cmd, "server-url", "MULTICA_SERVER_URL", "")
diff --git a/server/internal/daemon/execenv/git.go b/server/internal/daemon/execenv/git.go
index 7e87f59dc4..3364a4dcc6 100644
--- a/server/internal/daemon/execenv/git.go
+++ b/server/internal/daemon/execenv/git.go
@@ -9,6 +9,7 @@ import (
"regexp"
"strings"
"time"
+
)
// detectGitRepo checks if dir is inside a git repository (regular or bare).
@@ -16,12 +17,14 @@ import (
func detectGitRepo(dir string) (string, bool) {
// Try regular repo first.
cmd := exec.Command("git", "-C", dir, "rev-parse", "--show-toplevel")
+
if out, err := cmd.Output(); err == nil {
return strings.TrimSpace(string(out)), true
}
// Try bare repo: git-dir is "." for bare repos when -C points at the repo.
cmd = exec.Command("git", "-C", dir, "rev-parse", "--is-bare-repository")
+
if out, err := cmd.Output(); err == nil && strings.TrimSpace(string(out)) == "true" {
return dir, true
}
@@ -32,6 +35,7 @@ func detectGitRepo(dir string) (string, bool) {
// fetchOrigin runs `git fetch origin` to ensure the local repo has the latest remote refs.
func fetchOrigin(gitRoot string) error {
cmd := exec.Command("git", "-C", gitRoot, "fetch", "origin")
+
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("git fetch origin: %s: %w", strings.TrimSpace(string(out)), err)
}
@@ -43,6 +47,7 @@ func fetchOrigin(gitRoot string) error {
func getRemoteDefaultBranch(gitRoot string) string {
// Try symbolic-ref of origin/HEAD (set by `git clone` or `git remote set-head`).
cmd := exec.Command("git", "-C", gitRoot, "symbolic-ref", "refs/remotes/origin/HEAD")
+
if out, err := cmd.Output(); err == nil {
ref := strings.TrimSpace(string(out))
// ref looks like "refs/remotes/origin/main" — return "origin/main".
@@ -54,12 +59,14 @@ func getRemoteDefaultBranch(gitRoot string) string {
// Fallback: check if origin/main exists.
cmd = exec.Command("git", "-C", gitRoot, "rev-parse", "--verify", "origin/main")
+
if err := cmd.Run(); err == nil {
return "origin/main"
}
// Fallback: check if origin/master exists.
cmd = exec.Command("git", "-C", gitRoot, "rev-parse", "--verify", "origin/master")
+
if err := cmd.Run(); err == nil {
return "origin/master"
}
@@ -85,6 +92,7 @@ func setupGitWorktree(gitRoot, worktreePath, branchName, baseRef string) error {
func runGitWorktreeAdd(gitRoot, worktreePath, branchName, baseRef string) error {
cmd := exec.Command("git", "-C", gitRoot, "worktree", "add", "-b", branchName, worktreePath, baseRef)
+
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("git worktree add: %s: %w", strings.TrimSpace(string(out)), err)
}
@@ -95,6 +103,7 @@ func runGitWorktreeAdd(gitRoot, worktreePath, branchName, baseRef string) error
func removeGitWorktree(gitRoot, worktreePath, branchName string, logger *slog.Logger) {
// Remove the worktree.
cmd := exec.Command("git", "-C", gitRoot, "worktree", "remove", "--force", worktreePath)
+
if out, err := cmd.CombinedOutput(); err != nil {
logger.Warn("execenv: git worktree remove failed", "output", strings.TrimSpace(string(out)), "error", err)
}
@@ -102,6 +111,7 @@ func removeGitWorktree(gitRoot, worktreePath, branchName string, logger *slog.Lo
// Delete the branch (best-effort).
if branchName != "" {
cmd = exec.Command("git", "-C", gitRoot, "branch", "-D", branchName)
+
if out, err := cmd.CombinedOutput(); err != nil {
logger.Warn("execenv: git branch delete failed", "branch", branchName, "output", strings.TrimSpace(string(out)), "error", err)
}
@@ -112,6 +122,7 @@ func removeGitWorktree(gitRoot, worktreePath, branchName string, logger *slog.Lo
func excludeFromGit(worktreePath, pattern string) error {
// Resolve the actual git dir for this worktree.
cmd := exec.Command("git", "-C", worktreePath, "rev-parse", "--git-dir")
+
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("resolve git dir: %w", err)
diff --git a/server/internal/daemon/gc.go b/server/internal/daemon/gc.go
index 433d2ce10a..92c68dc36d 100644
--- a/server/internal/daemon/gc.go
+++ b/server/internal/daemon/gc.go
@@ -550,6 +550,7 @@ func (d *Daemon) pruneWorktree(barePath string) {
ctx, cancel := context.WithTimeout(context.Background(), gitCmdTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "git", "-C", barePath, "worktree", "prune")
+
if out, err := cmd.CombinedOutput(); err != nil {
d.logger.Warn("gc: worktree prune failed",
"repo", barePath,
diff --git a/server/internal/daemon/repocache/cache.go b/server/internal/daemon/repocache/cache.go
index ef5eab4bc8..0583c881b2 100644
--- a/server/internal/daemon/repocache/cache.go
+++ b/server/internal/daemon/repocache/cache.go
@@ -14,6 +14,7 @@ import (
"strings"
"sync"
"time"
+
)
// gitEnv returns an environment for git subprocesses that contact remotes.
@@ -243,6 +244,7 @@ const modernFetchRefspec = "+refs/heads/*:refs/remotes/origin/*"
func gitCloneBare(url, dest string) error {
cmd := exec.Command("git", "clone", "--bare", url, dest)
cmd.Env = gitEnv()
+
if out, err := cmd.CombinedOutput(); err != nil {
// Clean up partial clone.
os.RemoveAll(dest)
@@ -283,6 +285,7 @@ func gitFetch(barePath string) error {
// on this call.
cmd := exec.Command("git", "-C", barePath, "remote", "set-head", "origin", "--auto")
cmd.Env = gitEnv()
+
_ = cmd.Run()
return nil
}
@@ -292,6 +295,7 @@ func gitFetch(barePath string) error {
func runGitFetch(barePath string) error {
cmd := exec.Command("git", "-C", barePath, "fetch", "origin")
cmd.Env = gitEnv()
+
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("git fetch: %s: %w", strings.TrimSpace(string(out)), err)
}
@@ -326,6 +330,7 @@ func ensureRemoteTrackingLayout(barePath string) error {
// Non-fatal: if this fails we fall back to origin/main, origin/master.
cmd := exec.Command("git", "-C", barePath, "remote", "set-head", "origin", "--auto")
cmd.Env = gitEnv()
+
_ = cmd.Run()
return nil
}
@@ -334,7 +339,9 @@ func ensureRemoteTrackingLayout(barePath string) error {
// the empty string if it's not set. Distinguishes "missing" (exit 1) from
// real git errors.
func readFetchRefspec(barePath string) (string, error) {
- out, err := exec.Command("git", "-C", barePath, "config", "--get", "remote.origin.fetch").Output()
+ cmd := exec.Command("git", "-C", barePath, "config", "--get", "remote.origin.fetch")
+
+ out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok && ee.ExitCode() == 1 {
return "", nil // key missing, not an error
@@ -345,7 +352,9 @@ func readFetchRefspec(barePath string) (string, error) {
}
func setFetchRefspec(barePath, refspec string) error {
- out, err := exec.Command("git", "-C", barePath, "config", "remote.origin.fetch", refspec).CombinedOutput()
+ cmd := exec.Command("git", "-C", barePath, "config", "remote.origin.fetch", refspec)
+
+ out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("set remote.origin.fetch: %s: %w", strings.TrimSpace(string(out)), err)
}
@@ -530,6 +539,7 @@ func resolveBaseRef(barePath, requestedRef string) (string, error) {
func gitRefExists(repoPath, ref string) bool {
cmd := exec.Command("git", "-C", repoPath, "rev-parse", "--verify", "--quiet", ref)
+
return cmd.Run() == nil
}
@@ -560,6 +570,7 @@ func createWorktree(gitRoot, worktreePath, branchName, baseRef string) (string,
func runWorktreeAdd(gitRoot, worktreePath, branchName, baseRef string) error {
cmd := exec.Command("git", "-C", gitRoot, "worktree", "add", "-b", branchName, worktreePath, baseRef)
+
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("git worktree add: %s: %w", strings.TrimSpace(string(out)), err)
}
@@ -659,17 +670,21 @@ func getRemoteDefaultBranch(barePath string) string {
// repo can leave a symref pointing at a deleted ref, and returning
// it here would later fail in `git worktree add` with a confusing
// "invalid reference" error.
- if out, err := exec.Command("git", "-C", barePath, "symbolic-ref", "refs/remotes/origin/HEAD").Output(); err == nil {
+ symrefCmd := exec.Command("git", "-C", barePath, "symbolic-ref", "refs/remotes/origin/HEAD")
+ if out, err := symrefCmd.Output(); err == nil {
ref := strings.TrimSpace(string(out))
if ref != "" {
- if err := exec.Command("git", "-C", barePath, "rev-parse", "--verify", ref).Run(); err == nil {
+ verifyCmd := exec.Command("git", "-C", barePath, "rev-parse", "--verify", ref)
+ if err := verifyCmd.Run(); err == nil {
return ref
}
}
}
// 2) Common default branch names under the origin namespace.
for _, candidate := range []string{"refs/remotes/origin/main", "refs/remotes/origin/master"} {
- if err := exec.Command("git", "-C", barePath, "rev-parse", "--verify", candidate).Run(); err == nil {
+ cmd := exec.Command("git", "-C", barePath, "rev-parse", "--verify", candidate)
+
+ if err := cmd.Run(); err == nil {
return candidate
}
}
@@ -682,7 +697,9 @@ func getRemoteDefaultBranch(barePath string) string {
bareRef := bareHeadBranch(barePath)
if bareRef != "" {
originRef := "refs/remotes/origin/" + strings.TrimPrefix(bareRef, "refs/heads/")
- if err := exec.Command("git", "-C", barePath, "rev-parse", "--verify", originRef).Run(); err == nil {
+ cmd := exec.Command("git", "-C", barePath, "rev-parse", "--verify", originRef)
+
+ if err := cmd.Run(); err == nil {
return originRef
}
}
@@ -694,7 +711,8 @@ func getRemoteDefaultBranch(barePath string) string {
// "legacy empty" apart from "ambiguous".
originCount := 0
var singleton string
- if out, err := exec.Command("git", "-C", barePath, "for-each-ref", "--format=%(refname)", "refs/remotes/origin/").Output(); err == nil {
+ foreachCmd := exec.Command("git", "-C", barePath, "for-each-ref", "--format=%(refname)", "refs/remotes/origin/")
+ if out, err := foreachCmd.Output(); err == nil {
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
line = strings.TrimSpace(line)
if line == "" || line == "refs/remotes/origin/HEAD" {
@@ -730,7 +748,9 @@ func getRemoteDefaultBranch(barePath string) string {
// modern caches should never reach this path because origin/* resolution
// succeeds first.
func bareHeadBranch(barePath string) string {
- out, err := exec.Command("git", "-C", barePath, "symbolic-ref", "HEAD").Output()
+ cmd := exec.Command("git", "-C", barePath, "symbolic-ref", "HEAD")
+
+ out, err := cmd.Output()
if err != nil {
return ""
}
@@ -738,7 +758,8 @@ func bareHeadBranch(barePath string) string {
if ref == "" {
return ""
}
- if err := exec.Command("git", "-C", barePath, "rev-parse", "--verify", ref).Run(); err != nil {
+ verifyCmd := exec.Command("git", "-C", barePath, "rev-parse", "--verify", ref)
+ if err := verifyCmd.Run(); err != nil {
return ""
}
return ref
@@ -795,6 +816,7 @@ git interpret-trailers --in-place --trailer "$TRAILER" "$COMMIT_MSG_FILE"
// worktrees created from this cache.
func installCoAuthoredByHook(worktreePath string) error {
cmd := exec.Command("git", "-C", worktreePath, "rev-parse", "--git-common-dir")
+
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("resolve git common dir: %w", err)
@@ -838,6 +860,7 @@ func isDaemonInstalledHook(contents []byte) bool {
// the path.
func removeCoAuthoredByHook(worktreePath string) error {
cmd := exec.Command("git", "-C", worktreePath, "rev-parse", "--git-common-dir")
+
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("resolve git common dir: %w", err)
@@ -868,6 +891,7 @@ func removeCoAuthoredByHook(worktreePath string) error {
// excludeFromGit adds a pattern to the worktree's .git/info/exclude file.
func excludeFromGit(worktreePath, pattern string) error {
cmd := exec.Command("git", "-C", worktreePath, "rev-parse", "--git-dir")
+
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("resolve git dir: %w", err)
diff --git a/server/internal/util/proc_other.go b/server/internal/util/proc_other.go
new file mode 100644
index 0000000000..67e1fd1436
--- /dev/null
+++ b/server/internal/util/proc_other.go
@@ -0,0 +1,6 @@
+//go:build !windows
+
+package util
+
+// EnsureHiddenConsole is a no-op on non-Windows platforms.
+func EnsureHiddenConsole() {}
diff --git a/server/internal/util/proc_windows.go b/server/internal/util/proc_windows.go
new file mode 100644
index 0000000000..c3a2ccb8ed
--- /dev/null
+++ b/server/internal/util/proc_windows.go
@@ -0,0 +1,31 @@
+//go:build windows
+
+package util
+
+import "syscall"
+
+var (
+ kernel32 = syscall.NewLazyDLL("kernel32.dll")
+ user32 = syscall.NewLazyDLL("user32.dll")
+ procAllocConsole = kernel32.NewProc("AllocConsole")
+ procGetConsoleWnd = kernel32.NewProc("GetConsoleWindow")
+ procShowWindow = user32.NewProc("ShowWindow")
+)
+
+const swHide = 0
+
+// EnsureHiddenConsole guarantees the daemon owns a console that is not
+// visible to the user. Every child process (git, cmd, etc.) inherits this
+// hidden console automatically, so no per-call SysProcAttr gymnastics are
+// needed.
+func EnsureHiddenConsole() {
+ if hwnd, _, _ := procGetConsoleWnd.Call(); hwnd != 0 {
+ return // already have a console
+ }
+ if r, _, _ := procAllocConsole.Call(); r == 0 {
+ return // AllocConsole failed
+ }
+ if hwnd, _, _ := procGetConsoleWnd.Call(); hwnd != 0 {
+ procShowWindow.Call(hwnd, swHide)
+ }
+}