Debian Codegraph @

internal/command/reset.go

raw symbols SCIP
1 package command
2
3 import (
4 "fmt"
5
6 "github.com/isacikgoz/gitbatch/internal/git"
7 gogit "github.com/go-git/go-git/v5"
8 "github.com/go-git/go-git/v5/plumbing"
9 )
10
11 // ResetOptions defines the rules of git reset command
12 type ResetOptions struct {
13 // Hash is the reference to be resetted
14 Hash string
15 // Type is the mode of a reset operation
16 ResetType ResetType
17 // Mode is the command mode
18 CommandMode Mode
19 }
20
21 // ResetType defines a string type for reset git command.
22 type ResetType string
23
24 const (
25 // ResetHard Resets the index and working tree. Any changes to tracked
26 // files in the working tree since <commit> are discarded.
27 ResetHard ResetType = "hard"
28
29 // ResetMixed Resets the index but not the working tree (i.e., the changed
30 // files are preserved but not marked for commit) and reports what has not
31 // been updated. This is the default action.
32 ResetMixed ResetType = "mixed"
33
34 // ResetMerge Resets the index and updates the files in the working tree
35 // that are different between <commit> and HEAD, but keeps those which are
36 // different between the index and working tree
37 ResetMerge ResetType = "merge"
38
39 // ResetSoft Does not touch the index file or the working tree at all
40 // (but resets the head to <commit>
41 ResetSoft ResetType = "soft"
42
43 // ResetKeep Resets index entries and updates files in the working tree
44 // that are different between <commit> and HEAD
45 ResetKeep ResetType = "keep"
46 )
47
48 // Reset is the wrapper of "git reset" command
49 func Reset(r *git.Repository, file *git.File, o *ResetOptions) error {
50 mode := o.CommandMode
51
52 switch mode {
53 case ModeLegacy:
54 err := resetWithGit(r, file, o)
55 return err
56 case ModeNative:
57
58 }
59 return fmt.Errorf("unhandled reset operation")
60 }
61
62 func resetWithGit(r *git.Repository, file *git.File, option *ResetOptions) error {
63 args := make([]string, 0)
64 args = append(args, "reset")
65
66 args = append(args, "--")
67 args = append(args, file.Name)
68 if len(option.ResetType) > 0 {
69 args = append(args, "--"+string(option.ResetType))
70 }
71 _, err := Run(r.AbsPath, "git", args)
72 if err != nil {
73 return fmt.Errorf("could not reset file %s: %v", file.AbsPath, err)
74 }
75 return nil
76 }
77
78 // ResetAll resets the changes in a repository, should be used wise
79 func ResetAll(r *git.Repository, o *ResetOptions) error {
80
81 switch o.CommandMode {
82 case ModeLegacy:
83 err := resetAllWithGit(r, o)
84 return err
85 case ModeNative:
86 err := resetAllWithGoGit(r, o)
87 return err
88 }
89 return fmt.Errorf("unhandled reset operation")
90 }
91
92 func resetAllWithGit(r *git.Repository, option *ResetOptions) error {
93 args := make([]string, 0)
94 args = append(args, "reset")
95 if len(option.ResetType) > 0 {
96 args = append(args, "--"+string(option.ResetType))
97 }
98 _, err := Run(r.AbsPath, "git", args)
99 if err != nil {
100 return fmt.Errorf("could not reset all: %v", err)
101 }
102 return nil
103 }
104
105 func resetAllWithGoGit(r *git.Repository, option *ResetOptions) error {
106 w, err := r.Repo.Worktree()
107 if err != nil {
108 return err
109 }
110 var mode gogit.ResetMode
111 switch option.ResetType {
112 case ResetHard:
113 mode = gogit.HardReset
114 case ResetMixed:
115 mode = gogit.MixedReset
116 case ResetMerge:
117 mode = gogit.MergeReset
118 case ResetSoft:
119 mode = gogit.SoftReset
120 }
121 err = w.Reset(&gogit.ResetOptions{
122 Commit: plumbing.NewHash(option.Hash),
123 Mode: mode,
124 })
125 return err
126 }