Merge branch 'main' into feat_config

This commit is contained in:
Theis
2025-06-03 20:03:17 +02:00
committed by GitHub
8 changed files with 313 additions and 45 deletions
+75
View File
@@ -0,0 +1,75 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"strings"
"github.com/Slug-Boi/cocommit/src/cmd/tui"
"github.com/Slug-Boi/cocommit/src/cmd/utils"
"github.com/spf13/cobra"
)
// amendCmd represents the amend command
var amendCmd = &cobra.Command{
Use: "amend",
Short: "Amend a commit message",
Long: `Ammend an existing commit message and add co-authors to it.
If ran without any arguments, it will open the TUI to select co-authors.
If ran with arguments, it will add the co-authors to the commit message.`,
Run: func(cmd *cobra.Command, args []string) {
// check if the print flag is set
pflag, _ := cmd.Flags().GetBool("print-output")
tflag, _ := cmd.Flags().GetBool("test_print")
git_flags, _ := cmd.Flags().GetString("git-flags")
edit, _ := cmd.Flags().GetBool("edit")
hash, _ := cmd.Flags().GetString("hash")
if hash != "" {
println("Hash based commit amendment is not yet implemented please use rebase option manually in git and then use this command to add co-authors.")
hash = ""
return
}
if edit {
}
var authors string
if len(args) == 0 {
// open the TUI to select co-authors
list_authors := tui.Entry()
if list_authors == nil {
println("No authors selected, exiting.")
os.Exit(1)
}
authors = utils.Commit("", list_authors)
} else {
authors = utils.Commit("", args)
}
git_flags_split := []string{}
if git_flags != "" {
git_flags_split = strings.Split(git_flags, " ")
}
err, _ := utils.GitCommitAppender(authors, hash, git_flags_split, tflag, pflag)
if err != nil {
println("Error amending commit:", err.Error())
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(amendCmd)
amendCmd.Flags().StringP("git-flags", "g", "", "Git flags to add to the commit command")
amendCmd.Flags().BoolP("print-output", "p", false, "Print the commit message to stdout")
amendCmd.Flags().BoolP("test_print", "t", false, "Print the commit message to stdout without amending")
amendCmd.Flags().BoolP("edit", "e", false, "Edit the commit message in the editor")
amendCmd.Flags().StringP("hash", "s", "", "Hash of the commit to amend")
}
+7
View File
@@ -488,6 +488,13 @@ func Entry() []string {
}
for i := range selected {
short := dupProtect[i]
if short == "" {
split := strings.Split(i, " - ")
name := split[0]
email := split[1]
utils.TempAddUser(name, email)
short = name
}
if negation {
short = "^" + short
}
+70 -20
View File
@@ -31,30 +31,30 @@ func Commit(message string, authors []string) string {
return sb.String()
}
// Loop that adds users
for _, committer := range authors {
if _, ok := Users[committer]; ok {
sb_author(committer, &sb)
} else if match := reg.MatchString(committer); match {
str := strings.Split(committer, ":")
// Loop that adds users
for _, committer := range authors {
if _, ok := Users[committer]; ok {
sb_author(committer, &sb)
} else if match := reg.MatchString(committer); match {
str := strings.Split(committer, ":")
sb.WriteString("\nCo-authored-by: ")
sb.WriteString(str[0])
sb.WriteString(" <")
sb.WriteString(str[1])
sb.WriteRune('>')
sb.WriteString("\nCo-authored-by: ")
sb.WriteString(str[0])
sb.WriteString(" <")
sb.WriteString(str[1])
sb.WriteRune('>')
} else if committer[0] == '^' { // Negations
excludeMode = append(excludeMode, Users[committer[1:]].Username)
} else {
println(committer, " was unknown. User either not defined or name typed wrong")
} else if committer[0] == '^' { // Negations
excludeMode = append(excludeMode, Users[committer[1:]].Username)
} else {
println(committer, " was unknown. User either not defined or name typed wrong")
}
}
}
// Add excluded users after processing all authors
if len(excludeMode) > 0 {
add_x_users(excludeMode, &sb)
}
// Add excluded users after processing all authors
if len(excludeMode) > 0 {
add_x_users(excludeMode, &sb)
}
return sb.String()
}
@@ -128,3 +128,53 @@ func group_selection(group []User, excludeMode []string) []string {
return excludeMode
}
func GitCommitAppender(authors string, hash string, flags []string, t,p bool) (error, string) {
// Get old commit message
var cmd *exec.Cmd
//TODO: Make the hash ammend work with rebase but its more complicated than orignally thought.
// git log --format=%B -n1
if hash == "" {
cmd = exec.Command("git", "log", "--format=%B", "-n1")
} else {
cmd = exec.Command("git", "log", "--format=%B", "-n1", hash)
}
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("error: %s", err), ""
}
// Convert the output to a string
old_commit := string(out)
// commit shell command
// specify git command1
input := []string{"commit"}
input = append(input, flags...)
old_commit = strings.TrimSpace(old_commit)
input = append(input, "--amend", "-m", old_commit+"\n"+authors)
if p {
println(old_commit + "\n" + authors)
if t {
return nil, old_commit + "\n" + authors
}
}
// append the message to the flags
// concat the git command and the flags + message
cmd = exec.Command("git", input...)
// https://stackoverflow.com/questions/18159704/how-to-debug-exit-status-1-error-when-running-exec-command-in-golang
cmd_output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error: %s : %s", err, string(cmd_output)), ""
} else {
println(string(cmd_output))
}
return nil, old_commit + "\n" + authors
}
+82
View File
@@ -636,6 +636,88 @@ func Test_GitPush(t *testing.T) {
}
}
func Test_CommitAppender(t *testing.T) {
setup()
defer teardown()
utils.Define_users("author_file_test")
// Test CommitAppender with a single author
authors := []string{"te"}
cmd := exec.Command("git", "log", "--format=%B", "-n1")
out, err := cmd.Output()
if err != nil {
t.Fatalf("Failed to get git log: %v", err)
}
message := strings.TrimSpace(string(out))
commit := utils.Commit("", authors)
err, appendedMessage := utils.GitCommitAppender(commit, "", nil, true, true)
if err != nil {
t.Errorf("GitCommitAppender() returned error: %v", err)
}
expectedMessage := message+"\n\n\nCo-authored-by: TestUser <test@test.test>"
if appendedMessage != expectedMessage {
t.Errorf("CommitAppender() = %v;\nwant:\n%v", appendedMessage, expectedMessage)
}
// check inverted commit
authors = []string{"^te"}
commit = utils.Commit("", authors)
err, appendedMessage = utils.GitCommitAppender(commit, "", nil, true, true)
if err != nil {
t.Errorf("GitCommitAppender() returned error: %v", err)
}
expectedMessage = message+"\n\n\nCo-authored-by: UserName2 <testing@user.io>"
if appendedMessage != expectedMessage {
t.Errorf("CommitAppender() = %v;\nwant:\n%v", appendedMessage, expectedMessage)
}
// Test CommitAppender with multiple authors
authors = []string{"te", "testtest"}
commit = utils.Commit("", authors)
err, appendedMessage = utils.GitCommitAppender(commit, "", nil, true, true)
if err != nil {
t.Errorf("GitCommitAppender() returned error: %v", err)
}
expectedMessage = message+"\n\n\nCo-authored-by: TestUser <test@test.test>\nCo-authored-by: UserName2 <testing@user.io>"
if appendedMessage != expectedMessage {
t.Errorf("CommitAppender() = %v;\nwant:\n%v", appendedMessage, expectedMessage)
}
// Test CommitAppender with all authors
authors = []string{"all"}
commit = utils.Commit("", authors)
err, appendedMessage = utils.GitCommitAppender(commit, "", nil, true, true)
if err != nil {
t.Errorf("GitCommitAppender() returned error: %v", err)
}
expectedMessage = message+"\n\n\nCo-authored-by: TestUser <test@test.test>\nCo-authored-by: UserName2 <testing@user.io>"
expectedMessage2 := message+"\n\n\nCo-authored-by: UserName2 <testing@user.io>\nCo-authored-by: TestUser <test@test.test>"
if appendedMessage != expectedMessage && appendedMessage != expectedMessage2 {
t.Errorf("CommitAppender() = %v;\nwant:\n%v", appendedMessage, expectedMessage)
}
// Test CommitAppender with group authors
authors = []string{"gr1"}
commit = utils.Commit("", authors)
err, appendedMessage = utils.GitCommitAppender(commit, "", nil, true, true)
if err != nil {
t.Errorf("GitCommitAppender() returned error: %v", err)
}
expectedMessage = message+"\n\n\nCo-authored-by: UserName2 <testing@user.io>"
if appendedMessage != expectedMessage {
t.Errorf("CommitAppender() = %v;\nwant:\n%v", appendedMessage, expectedMessage)
}
message = ""
}
// Commit tests END
// Github tests BEGIN