Jaypore CI

> Jaypore CI: Minimal, Offline, Local CI system.
Log | Files | Refs | README | LICENSE

push.go (1722B)


      1 package jci
      2 
      3 import (
      4 	"fmt"
      5 	"strings"
      6 )
      7 
      8 // Push pushes CI results to remote
      9 func Push(args []string) error {
     10 	remote := "origin"
     11 	if len(args) > 0 {
     12 		remote = args[0]
     13 	}
     14 
     15 	// Get all local JCI refs
     16 	localRefs, err := ListAllJCIRefs()
     17 	if err != nil {
     18 		return err
     19 	}
     20 
     21 	if len(localRefs) == 0 {
     22 		fmt.Println("No CI results to push")
     23 		return nil
     24 	}
     25 
     26 	// Get remote refs to find what's already pushed
     27 	remoteRefs := getRemoteJCIRefs(remote)
     28 
     29 	// Find refs that need to be pushed
     30 	var toPush []string
     31 	for _, ref := range localRefs {
     32 		if !remoteRefs[ref] {
     33 			toPush = append(toPush, ref)
     34 		}
     35 	}
     36 
     37 	if len(toPush) == 0 {
     38 		fmt.Println("All CI results already pushed")
     39 		return nil
     40 	}
     41 
     42 	fmt.Printf("Pushing %d new CI result(s) to %s...\n", len(toPush), remote)
     43 
     44 	// Push each ref individually
     45 	for _, ref := range toPush {
     46 		_, err = git("push", remote, ref+":"+ref)
     47 		if err != nil {
     48 			return fmt.Errorf("failed to push %s: %w", ref, err)
     49 		}
     50 		fmt.Printf("  %s\n", ref)
     51 	}
     52 
     53 	fmt.Println("Done")
     54 	return nil
     55 }
     56 
     57 // getRemoteJCIRefs returns a set of refs that exist on the remote
     58 func getRemoteJCIRefs(remote string) map[string]bool {
     59 	remoteCI := make(map[string]bool)
     60 
     61 	// Get refs/jci/*
     62 	out, err := git("ls-remote", "--refs", remote, "refs/jci/*")
     63 	if err == nil && out != "" {
     64 		for _, line := range strings.Split(out, "\n") {
     65 			parts := strings.Fields(line)
     66 			if len(parts) >= 2 {
     67 				remoteCI[parts[1]] = true
     68 			}
     69 		}
     70 	}
     71 
     72 	// Get refs/jci-runs/*
     73 	out, err = git("ls-remote", "--refs", remote, "refs/jci-runs/*")
     74 	if err == nil && out != "" {
     75 		for _, line := range strings.Split(out, "\n") {
     76 			parts := strings.Fields(line)
     77 			if len(parts) >= 2 {
     78 				remoteCI[parts[1]] = true
     79 			}
     80 		}
     81 	}
     82 
     83 	return remoteCI
     84 }