2025-02-01 11:57:52 -06:00
|
|
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the GPL 3.0
|
|
|
|
|
2024-12-27 22:27:19 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-12-28 03:00:42 -06:00
|
|
|
"path/filepath"
|
2025-01-30 10:28:04 -06:00
|
|
|
"strings"
|
2024-12-27 22:27:19 -06:00
|
|
|
|
2025-01-30 19:47:48 -06:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-12-27 22:27:19 -06:00
|
|
|
"go.wit.com/lib/protobuf/forgepb"
|
2025-01-05 04:54:05 -06:00
|
|
|
"go.wit.com/lib/protobuf/gitpb"
|
2024-12-27 22:27:19 -06:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2025-01-28 13:20:10 -06:00
|
|
|
// saves the patches in ~/.config/forge/currentpatches/
|
2025-01-28 21:50:07 -06:00
|
|
|
func savePatchset(pset *forgepb.Patchset) error {
|
|
|
|
log.Info("savePatches() NAME", pset.Name)
|
|
|
|
log.Info("savePatches() COMMENT", pset.Comment)
|
|
|
|
log.Info("savePatches() GIT_AUTHOR_NAME", pset.GetGitAuthorName())
|
|
|
|
log.Info("savePatches() GIT_AUTHOR_EMAIL", pset.GetGitAuthorEmail())
|
|
|
|
log.Info("savePatches() Branch Name", pset.GetStartBranchName())
|
|
|
|
log.Info("savePatches() Start Hash", pset.GetStartBranchHash())
|
2025-01-28 13:20:10 -06:00
|
|
|
|
|
|
|
var count int
|
|
|
|
var bad int
|
2025-01-28 21:50:07 -06:00
|
|
|
var lasterr error
|
2025-01-28 13:20:10 -06:00
|
|
|
all := pset.Patches.SortByFilename()
|
|
|
|
for all.Scan() {
|
|
|
|
p := all.Next()
|
|
|
|
basedir := filepath.Join(os.Getenv("FORGE_CONFIG"), "currentpatches")
|
|
|
|
if fullname, err := savePatchFile(p, basedir); err != nil {
|
|
|
|
log.Info(fullname, "save failed", err)
|
|
|
|
bad += 1
|
2025-01-28 21:50:07 -06:00
|
|
|
lasterr = err
|
2025-01-28 13:20:10 -06:00
|
|
|
}
|
|
|
|
count += 1
|
|
|
|
}
|
|
|
|
log.Info("pset has", count, "total patches, ", bad, "bad save patches")
|
|
|
|
if bad == 0 {
|
2025-01-28 21:50:07 -06:00
|
|
|
return lasterr
|
2025-01-28 13:20:10 -06:00
|
|
|
}
|
2025-01-28 21:50:07 -06:00
|
|
|
return nil
|
2025-01-28 13:20:10 -06:00
|
|
|
}
|
|
|
|
|
2025-01-05 04:54:05 -06:00
|
|
|
// re-run git CheckDirty() on everything
|
|
|
|
func IsAnythingDirty() bool {
|
|
|
|
me.found = new(gitpb.Repos)
|
|
|
|
findAll() // select all the repos
|
2025-01-05 05:48:02 -06:00
|
|
|
doCheckDirtyAndConfigSave()
|
2025-01-05 04:54:05 -06:00
|
|
|
me.found = new(gitpb.Repos)
|
|
|
|
findDirty()
|
|
|
|
if len(me.found.Repos) == 0 {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// From 18ee541f89be2e9f9a91c54873da87885e8ffdf5 Mon Sep 17 00:00:00 2001
|
|
|
|
// From: Jeff Carr <jcarr@wit.com>
|
|
|
|
// Date: Sun, 5 Jan 2025 01:18:47 -0600
|
|
|
|
// Subject: [PATCH] 'forge dirty' will find and list only dirty repos
|
|
|
|
|
|
|
|
// list patches in jcarr but not in devel
|
|
|
|
// git log --format="%H %Subject" jcarr --not devel
|
|
|
|
func countCurrentPatches(repo *gitpb.Repo) int {
|
|
|
|
cmd := []string{"git", "log", "--format=\"%H %s\"", "--no-merges", "jcarr", "--not", "devel"}
|
|
|
|
result := repo.Run(cmd)
|
|
|
|
return len(result.Stdout)
|
2025-01-05 02:01:15 -06:00
|
|
|
}
|
|
|
|
|
2025-01-11 05:54:19 -06:00
|
|
|
func applyPatchset(pset *forgepb.Patchset) error {
|
2024-12-30 02:03:56 -06:00
|
|
|
var everythingworked bool = true
|
|
|
|
tmpdir, err := os.MkdirTemp("", "forge")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-28 03:00:42 -06:00
|
|
|
// log.Info("got to applyPatches() pset", pset)
|
2024-12-30 00:50:51 -06:00
|
|
|
log.Info("applyPatches() NAME", pset.Name)
|
|
|
|
log.Info("applyPatches() COMMENT", pset.Comment)
|
|
|
|
log.Info("applyPatches() GIT_AUTHOR_NAME", pset.GetGitAuthorName())
|
|
|
|
log.Info("applyPatches() GIT_AUTHOR_EMAIL", pset.GetGitAuthorEmail())
|
2025-01-13 04:14:06 -06:00
|
|
|
all := pset.Patches.SortByFilename()
|
2024-12-27 22:27:19 -06:00
|
|
|
for all.Scan() {
|
|
|
|
p := all.Next()
|
2025-01-28 13:20:10 -06:00
|
|
|
|
|
|
|
basedir := me.forge.GetGoSrc()
|
|
|
|
if fullname, err := savePatchFile(p, basedir); err != nil {
|
|
|
|
log.Info(fullname, "save failed", err)
|
2024-12-30 02:03:56 -06:00
|
|
|
continue
|
2025-01-28 13:20:10 -06:00
|
|
|
} else {
|
2025-01-30 19:47:48 -06:00
|
|
|
basedir, filename := filepath.Split(fullname)
|
|
|
|
cmd := []string{"git", "am", filename}
|
|
|
|
log.Info("Should run: at", basedir, ":", cmd)
|
|
|
|
log.Info(basedir, filename)
|
|
|
|
result := shell.PathRun(basedir, cmd)
|
|
|
|
for _, line := range result.Stdout {
|
|
|
|
log.Warn("stdout:", line)
|
|
|
|
}
|
|
|
|
for _, line := range result.Stderr {
|
|
|
|
log.Warn("stderr:", line)
|
|
|
|
}
|
2024-12-30 02:03:56 -06:00
|
|
|
}
|
|
|
|
everythingworked = false
|
|
|
|
}
|
|
|
|
if everythingworked {
|
|
|
|
os.RemoveAll(tmpdir) // clean up
|
2024-12-27 22:27:19 -06:00
|
|
|
}
|
2024-12-28 03:00:42 -06:00
|
|
|
log.Info("THIS IS THE END MY FRIEND")
|
2024-12-27 22:27:19 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-28 13:20:10 -06:00
|
|
|
func savePatchFile(p *forgepb.Patch, basedir string) (string, error) {
|
|
|
|
basepath, filename := filepath.Split(p.Filename)
|
|
|
|
fulldir := filepath.Join(basedir, basepath)
|
|
|
|
err := os.MkdirAll(fulldir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("applyPathces() MkdirAll failed for", fulldir)
|
|
|
|
log.Info("applyPathces() MkdirAll failed err", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
tmpname := filepath.Join(fulldir, filename)
|
|
|
|
log.Info("pset filename FILENAME IS REAL?", tmpname)
|
|
|
|
raw, _ := os.OpenFile(tmpname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
raw.Write(p.Data)
|
|
|
|
raw.Close()
|
|
|
|
return tmpname, nil
|
|
|
|
}
|
|
|
|
|
2025-01-11 05:54:19 -06:00
|
|
|
func readPatchFile(pbfile string) (*forgepb.Patchset, error) {
|
2024-12-27 22:27:19 -06:00
|
|
|
bytes, err := os.ReadFile(pbfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("readfile error", pbfile, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-28 03:00:42 -06:00
|
|
|
return handleBytes(bytes)
|
|
|
|
}
|
|
|
|
|
2025-01-11 05:54:19 -06:00
|
|
|
func handleBytes(bytes []byte) (*forgepb.Patchset, error) {
|
|
|
|
var pset *forgepb.Patchset
|
|
|
|
pset = new(forgepb.Patchset)
|
2024-12-28 03:00:42 -06:00
|
|
|
err := pset.Unmarshal(bytes)
|
2024-12-27 22:27:19 -06:00
|
|
|
if err != nil {
|
2024-12-28 03:00:42 -06:00
|
|
|
log.Info("Unmarshal failed", err)
|
2024-12-27 22:27:19 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pset, nil
|
|
|
|
}
|
2025-01-30 10:28:04 -06:00
|
|
|
|
|
|
|
func doRegister(newurl string) error {
|
|
|
|
var url string
|
|
|
|
url = me.urlbase + "/register?url=" + newurl
|
|
|
|
body, err := me.forge.HttpPost(url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("httpPost() failed:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
test := strings.TrimSpace(string(body))
|
|
|
|
for _, line := range strings.Split(test, "\n") {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
log.Info("server returned:", line)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|