|
| 1 | +package lazycommit |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/go-git/go-git/v5" |
| 9 | + "github.com/go-git/go-git/v5/plumbing" |
| 10 | + |
| 11 | + "github.com/spenserblack/git-lazy-commit/pkg/fileutils" |
| 12 | +) |
| 13 | + |
| 14 | +// Commit commits all changes in the repository. |
| 15 | +// |
| 16 | +// It returns the commit hash and the commit message. |
| 17 | +func (r *LazyRepo) Commit() (hash plumbing.Hash, msg string, err error) { |
| 18 | + msg, err = r.CommitMsg() |
| 19 | + if err != nil { |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + hash, err = r.wt.Commit(msg, &git.CommitOptions{}) |
| 24 | + return |
| 25 | +} |
| 26 | + |
| 27 | +// CommitMsg builds a commit message using the tracked files in the repository. |
| 28 | +func (r *LazyRepo) CommitMsg() (string, error) { |
| 29 | + status, err := r.status() |
| 30 | + if err != nil { |
| 31 | + return "", err |
| 32 | + } |
| 33 | + for filename, fileStatus := range status { |
| 34 | + if fileStatus.Staging == git.Unmodified || fileStatus.Staging == git.Untracked { |
| 35 | + delete(status, filename) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if len(status) == 0 { |
| 40 | + return "", errors.New("no tracked files") |
| 41 | + } |
| 42 | + if len(status) == 1 { |
| 43 | + for filename, fileStatus := range status { |
| 44 | + return singleFileMsg(filename, fileStatus), nil |
| 45 | + } |
| 46 | + } |
| 47 | + return multiFileMsg(status), nil |
| 48 | +} |
| 49 | + |
| 50 | +func singleFileMsg(filename string, fileStatus *git.FileStatus) string { |
| 51 | + statusString := "" |
| 52 | + switch fileStatus.Staging { |
| 53 | + case git.Added: |
| 54 | + statusString = "Create" |
| 55 | + case git.Deleted: |
| 56 | + statusString = "Delete" |
| 57 | + case git.Modified: |
| 58 | + statusString = "Update" |
| 59 | + case git.Renamed: |
| 60 | + statusString = "Rename to" |
| 61 | + case git.Copied: |
| 62 | + statusString = "Copy to" |
| 63 | + default: |
| 64 | + statusString = "Do something to" |
| 65 | + } |
| 66 | + |
| 67 | + return fmt.Sprintf("%s %s", statusString, filename) |
| 68 | +} |
| 69 | + |
| 70 | +func multiFileMsg(status git.Status) string { |
| 71 | + var builder strings.Builder |
| 72 | + |
| 73 | + filenames := make([]string, 0, len(status)) |
| 74 | + for name := range status { |
| 75 | + filenames = append(filenames, name) |
| 76 | + } |
| 77 | + |
| 78 | + sharedDir := fileutils.SharedDirectory(filenames) |
| 79 | + |
| 80 | + if sharedDir == "/" { |
| 81 | + builder.WriteString("Update files\n") |
| 82 | + } else { |
| 83 | + builder.WriteString(fmt.Sprintf("Update %s/\n", sharedDir)) |
| 84 | + } |
| 85 | + builder.WriteRune('\n') |
| 86 | + |
| 87 | + for filename, fileStatus := range status { |
| 88 | + msgItem := singleFileMsg(filename, fileStatus) |
| 89 | + builder.WriteString(fmt.Sprintf("- %s\n", msgItem)) |
| 90 | + } |
| 91 | + |
| 92 | + return builder.String() |
| 93 | +} |
0 commit comments