forge/doCommit.go

122 lines
2.7 KiB
Go

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"os"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
func doCommit() {
if argv.All {
doCheckDirtyAndConfigSave()
found := findDirty()
var newpatches bool
for repo := range found.IterAll() {
log.Info("do a commit on repo", repo.GetGoPath())
if err := doCommitRepo(repo); err != nil {
badExit(err)
}
newpatches = true
}
if !argv.Commit.Submit {
okExit("")
}
if newpatches {
patchSubmitAndExit()
}
okExit("")
}
pwd, _ := os.Getwd()
repo := me.forge.Repos.FindByFullPath(pwd)
if repo == nil {
log.Info("todo: forge doesn't know how to work here yet")
okExit("")
}
if !repo.CheckDirty() {
okExit(log.Sprintf("this repo %s is not dirty.\n\n--all # commit all changes in all repos", repo.GetFullPath()))
} else {
log.Info("repo is dirty", repo.GetFullPath())
}
if repo.GetCurrentBranchName() != repo.GetUserBranchName() {
found := new(gitpb.Repos)
found.Append(repo)
me.forge.PrintHumanTable(found)
log.Info("")
log.Info("wrong branch. Can not commit on", repo.GetCurrentBranchName())
log.Info("")
okExit("")
}
os.Setenv("LESS", "-XR")
if err := shell.Exec([]string{"git", "diff"}); err != nil {
badExit(err)
}
if argv.All {
if err := shell.ExecCheck([]string{"git", "add", "--all"}); err != nil {
badExit(err)
}
}
if err := shell.ExecCheck([]string{"git", "commit", "--all"}); err != nil {
badExit(err)
}
patchSubmitAndExit()
}
func patchSubmitAndExit() {
_, err := me.forge.SubmitDevelPatchSet("forge auto commit")
if err != nil {
// TRY different URL
me.forge.SetForgeURL("https://forge.grid.wit.com/")
log.Info("GOING TO TRY AGAIN", me.forge.GetForgeURL())
}
_, err = me.forge.SubmitDevelPatchSet("forge auto commit")
if err != nil {
badExit(err)
}
okExit("git commit ok. forge done")
}
func doCommitRepo(repo *gitpb.Repo) error {
if repo.GetCurrentBranchName() != repo.GetUserBranchName() {
found := new(gitpb.Repos)
found.Append(repo)
me.forge.PrintHumanTable(found)
log.Info("")
log.Info("wrong branch. Can not commit on", repo.GetCurrentBranchName())
log.Info("")
return nil
}
os.Chdir(repo.GetFullPath())
os.Setenv("LESS", "-XR")
if err := shell.Exec([]string{"git", "diff"}); err != nil {
return err
}
if argv.All {
if err := shell.ExecCheck([]string{"git", "add", "--all"}); err != nil {
return err
}
}
if err := shell.ExecCheck([]string{"git", "commit", "--all"}); err != nil {
shell.ExecCheck([]string{"git", "reset"})
return err
}
log.Info("git commit ok. forge done")
return nil
}