-
Notifications
You must be signed in to change notification settings - Fork 2
/
branch.go
110 lines (97 loc) · 4.13 KB
/
branch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"time"
"github.com/JunNishimura/Goit/internal/log"
"github.com/spf13/cobra"
)
var (
renameOption string = ""
deleteOption string = ""
)
// branchCmd represents the branch command
var branchCmd = &cobra.Command{
Use: "branch",
Short: "handle with branch operation",
Long: "handle with branch operation",
PreRunE: func(cmd *cobra.Command, args []string) error {
if client.RootGoitPath == "" {
return ErrGoitNotInitialized
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// get list flag
isList, err := cmd.Flags().GetBool("list")
if err != nil {
return fmt.Errorf("fail to get list flag: %w", err)
}
// parameter validation
if !((len(args) == 1 && !isList && renameOption == "" && deleteOption == "") ||
(len(args) == 0 && isList && renameOption == "" && deleteOption == "") ||
(len(args) == 0 && !isList && renameOption != "" && deleteOption == "") ||
(len(args) == 0 && !isList && renameOption == "" && deleteOption != "")) {
return fmt.Errorf("parameters are not valid")
}
// add branch
if len(args) == 1 {
addBranchName := args[0]
addBranchHash := client.Head.Commit.Hash
if err := client.Refs.AddBranch(client.RootGoitPath, addBranchName, addBranchHash); err != nil {
return fmt.Errorf("fail to add branch '%s': %w", addBranchName, err)
}
if err := gLogger.WriteBranch(log.NewRecord(log.BranchRecord, nil, addBranchHash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("Created from %s", client.Head.Reference)), addBranchName); err != nil {
return fmt.Errorf("log error: %w", err)
}
}
// list branches
if isList {
client.Refs.ListBranches(client.Head.Reference)
}
// rename current branch
if renameOption != "" {
prevBranch := client.Head.Reference
if err := client.Refs.RenameBranch(client.RootGoitPath, client.Head.Reference, renameOption); err != nil {
return fmt.Errorf("fail to rename branch: %w", err)
}
// update HEAD
if err := client.Head.Update(client.Refs, client.RootGoitPath, renameOption); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}
// log
if err := gLogger.WriteHEAD(log.NewRecord(log.BranchRecord, client.Head.Commit.Hash, nil, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("renamed refs/heads/%s to refs/heads/%s", prevBranch, client.Head.Reference))); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteHEAD(log.NewRecord(log.BranchRecord, nil, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("renamed refs/heads/%s to refs/heads/%s", prevBranch, client.Head.Reference))); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.DeleteBranch(prevBranch); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteBranch(log.NewRecord(log.BranchRecord, nil, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("Created from %s", prevBranch)), client.Head.Reference); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteBranch(log.NewRecord(log.BranchRecord, client.Head.Commit.Hash, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("renamed refs/heads/%s refs/heads/%s", prevBranch, client.Head.Reference)), client.Head.Reference); err != nil {
return fmt.Errorf("log error: %w", err)
}
}
if deleteOption != "" {
if err := client.Refs.DeleteBranch(client.RootGoitPath, client.Head.Reference, deleteOption); err != nil {
return fmt.Errorf("fail to delete branch: %w", err)
}
if err := gLogger.DeleteBranch(deleteOption); err != nil {
return fmt.Errorf("log error: %w", err)
}
}
return nil
},
}
func init() {
rootCmd.AddCommand(branchCmd)
branchCmd.Flags().BoolP("list", "l", false, "show list of branches")
branchCmd.Flags().StringVarP(&renameOption, "rename", "r", "", "rename branch")
branchCmd.Flags().StringVarP(&deleteOption, "delete", "d", "", "delete branch")
}