push.go (1452B)
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 := ListJCIRunRefs() 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/jci-runs/* refs that exist on the remote 58 func getRemoteJCIRefs(remote string) map[string]bool { 59 remoteCI := make(map[string]bool) 60 out, err := git("ls-remote", "--refs", remote, "refs/jci-runs/*") 61 if err == nil && out != "" { 62 for _, line := range strings.Split(out, "\n") { 63 parts := strings.Fields(line) 64 if len(parts) >= 2 { 65 remoteCI[parts[1]] = true 66 } 67 } 68 } 69 return remoteCI 70 }