b60a7f234c99bd0c53b8ba08c9f63e68039599c4

Author
Andy Lu <luandy64@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

Add commit command (#331)

* Add commit command

* fix bad refactor

* fix: actually support the color flag

* Squashed commit of the following:

commit 814ee9f8fa7014dd4fafab12974e2edd4b8f7d73
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 23:07:49 2023 -0400

    Run gofmt

commit 3db029502c74461b57e776a0ebc9d27c944acc5d
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 23:06:31 2023 -0400

    Pull out statsLine and diffLine, add a commit body line

commit eb1da4296e0181bae842622e0dc98ca8ca845049
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 23:05:36 2023 -0400

    Add flag for printing the patch only

commit e76702e92b085f02427aae1c7052f235454242fd
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 22:50:55 2023 -0400

    Add commit, author, date, and stats

commit 0359b1dc49417d11f8f8b8529f8b942417d79128
Author: Andy Lu <luandy64@gmail.com>
Date:   Fri Jun 30 21:43:35 2023 -0400

    Add commit, author, date, and stats

    Missing color on stats

* Whitespace cleanup

Diff

This diff is truncated to protect this page.

  1diff --git a/server/cmd/commit.go b/server/cmd/commit.go
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..9b96f9103e6790ab68c133383ba24da416a61c42
  4--- /dev/null
  5+++ b/server/cmd/commit.go
  6@@ -0,0 +1,166 @@
  7+package cmd
  8+
  9+import (
 10+	"fmt"
 11+	"strings"
 12+	"time"
 13+
 14+	gansi "github.com/charmbracelet/glamour/ansi"
 15+	"github.com/charmbracelet/soft-serve/git"
 16+	"github.com/charmbracelet/soft-serve/server/ui/common"
 17+	"github.com/charmbracelet/soft-serve/server/ui/styles"
 18+	"github.com/muesli/termenv"
 19+	"github.com/spf13/cobra"
 20+)
 21+
 22+// commitCommand returns a command that prints the contents of a commit.
 23+func commitCommand() *cobra.Command {
 24+	var color bool
 25+	var patchOnly bool
 26+
 27+	cmd := &cobra.Command{
 28+		Use:               "commit SHA",
 29+		Short:             "Print out the contents of a diff",
 30+		Args:              cobra.ExactArgs(2),
 31+		PersistentPreRunE: checkIfReadable,
 32+		RunE: func(cmd *cobra.Command, args []string) error {
 33+			cfg, _ := fromContext(cmd)
 34+			repoName := args[0]
 35+			commitSHA := args[1]
 36+
 37+			rr, err := cfg.Backend.Repository(repoName)
 38+			if err != nil {
 39+				return err
 40+			}
 41+
 42+			r, err := rr.Open()
 43+			if err != nil {
 44+				return err
 45+			}
 46+
 47+			raw_commit, err := r.CommitByRevision(commitSHA)
 48+
 49+			commit := &git.Commit{
 50+				Commit: raw_commit,
 51+				Hash:   git.Hash(commitSHA),
 52+			}
 53+
 54+			patch, err := r.Patch(commit)
 55+			if err != nil {
 56+				return err
 57+			}
 58+
 59+			diff, err := r.Diff(commit)
 60+			if err != nil {
 61+				return err
 62+			}
 63+
 64+			commonStyle := styles.DefaultStyles()
 65+			style := commonStyle.Log
 66+
 67+			s := strings.Builder{}
 68+			commitLine := "commit " + commitSHA
 69+			authorLine := "Author: " + commit.Author.Name
 70+			dateLine := "Date:   " + commit.Committer.When.Format(time.UnixDate)
 71+			msgLine := strings.ReplaceAll(commit.Message, "\r\n", "\n")
 72+			statsLine := renderStats(diff, commonStyle, color)
 73+			diffLine := renderDiff(patch, color)
 74+
 75+			if patchOnly {
 76+				cmd.Println(
 77+					diffLine,
 78+				)
 79+				return nil
 80+			}
 81+
 82+			if color {
 83+				s.WriteString(fmt.Sprintf("%s\n%s\n%s\n%s\n",
 84+					style.CommitHash.Render(commitLine),
 85+					style.CommitAuthor.Render(authorLine),
 86+					style.CommitDate.Render(dateLine),
 87+					style.CommitBody.Render(msgLine),
 88+				))
 89+			} else {
 90+				s.WriteString(fmt.Sprintf("%s\n%s\n%s\n%s\n",
 91+					commitLine,
 92+					authorLine,
 93+					dateLine,
 94+					msgLine,
 95+				))
 96+			}
 97+
 98+			s.WriteString(fmt.Sprintf("\n%s\n%s",
 99+				statsLine,
100+				diffLine,
101+			))
102+
103+			cmd.Println(
104+				s.String(),
105+			)
106diff --git a/server/cmd/repo.go b/server/cmd/repo.go
107index ae87361b585f1cc87123e32f3e410f9bce14cd4c..6371f167da813f2fe988193a16a631874d9e7bfa 100644
108--- a/server/cmd/repo.go
109+++ b/server/cmd/repo.go
110@@ -18,6 +18,7 @@ func repoCommand() *cobra.Command {
111 		blobCommand(),
112 		branchCommand(),
113 		collabCommand(),
114+		commitCommand(),
115 		createCommand(),
116 		deleteCommand(),
117 		descriptionCommand(),