From ed3b3c794458372341cc5ac59a47d2d303760a2e Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Thu, 14 May 2026 02:21:51 +0900 Subject: [PATCH] knotmirror/git: sync HEAD seperately on resync Ideally this should be done by dedicated event like `gitHeadUpdate`, but as we have lots of self-hosted knots, doing all the work from knotmirror would be easier than requesting users to update their knot again. Signed-off-by: Seongmin Lee --- knotmirror/git.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/knotmirror/git.go b/knotmirror/git.go index e87ae956..79d3776e 100644 --- a/knotmirror/git.go +++ b/knotmirror/git.go @@ -91,6 +91,38 @@ func (c *CliGitMirrorManager) fetch(ctx context.Context, path, url string) error } return fmt.Errorf("running 'git fetch': %w\n%s", err, string(out)) } + + // TODO(boltless): make this dedicated event instead + lsRemoteCmd := exec.CommandContext(ctx, "git", "ls-remote", "--symref", url, "HEAD") + out, err := lsRemoteCmd.CombinedOutput() + if err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + return fmt.Errorf("running 'git ls-remote --symref': %w\n%s", err, string(out)) + } + + var headRef string + for line := range strings.SplitSeq(string(out), "\n") { + if !strings.HasPrefix(line, "ref: ") { + continue + } + fields := strings.Fields(line) + if len(fields) >= 2 { + headRef = fields[1] + break + } + } + if headRef != "" { + symrefCmd := exec.CommandContext(ctx, "git", "-C", path, "symbolic-ref", "HEAD", headRef) + if out, err := symrefCmd.CombinedOutput(); err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + return fmt.Errorf("running 'git symbolic-ref HEAD %s': %w\n%s", headRef, err, string(out)) + } + } + return nil } -- 2.51.2