Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic git show command #48

Merged
merged 14 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cmd/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"fmt"
)

// A string value compatible with a flag var
// that allows you to assign multiple flags
// to the same string value. If the value is
// set twice either by duplicating the flag
// or using the original and alias then an error
// is raised.
type aliasedStringValue string

func newAliasedStringValue(p *string, val string) *aliasedStringValue {
*p = val
return (*aliasedStringValue)(p)
}

func (s *aliasedStringValue) Set(val string) error {
if *s != "" {
return fmt.Errorf("Value already set to %v", val)
}
*s = aliasedStringValue(val)
return nil
}

func (s *aliasedStringValue) Get() interface{} { return string(*s) }

func (s *aliasedStringValue) String() string { return string(*s) }
26 changes: 26 additions & 0 deletions cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"flag"
"fmt"

"github.com/driusan/dgit/git"
)

func Show(c *git.Client, args []string) error {
flags := flag.NewFlagSet("show", flag.ExitOnError)
flags.SetOutput(flag.CommandLine.Output())
flags.Usage = func() {
flag.Usage()
fmt.Fprintf(flag.CommandLine.Output(), "\n\nOptions:\n")
flags.PrintDefaults()
}

opts := git.ShowOptions{}
flags.Var(newAliasedStringValue((*string)(&opts.Format), ""), "format", "Print the contents of commit logs in a specified format")
flags.Var(newAliasedStringValue((*string)(&opts.Format), ""), "pretty", "Alias for --format")
flags.Parse(args)

objects := flags.Args()
return git.Show(c, opts, objects)
}
90 changes: 90 additions & 0 deletions git/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package git

import (
"fmt"
"strings"
)

type FormatString string

func (f FormatString) FormatCommit(c *Client, cmt CommitID) (string, error) {
if f == "" || f == "medium" {
output, err := formatCommitMedium(cmt, c)
if err != nil {
return "", err
}
return fmt.Sprintf("%v\n", output), nil
}
if f == "raw" {
output := fmt.Sprintf("commit %v\n", cmt)
cmtObject, err := c.GetCommitObject(cmt)
if err != nil {
return "", err
}
return fmt.Sprintf("%v%v\n", output, cmtObject), nil
}

return "", fmt.Errorf("Format %s is not supported.\n", f)
}

type ShowOptions struct {
DiffOptions
Format FormatString
}

// Show implementes the "git show" command.
func Show(c *Client, opts ShowOptions, objects []string) error {
if len(objects) < 1 {
return fmt.Errorf("Provide at least one commit.")
}

commitIds := []CommitID{}

for _, object := range objects {
// Commits only for now
commit, err := RevParseCommit(c, &RevParseOptions{}, object)
if err != nil {
return err
}

commitIds = append(commitIds, commit)
}

for _, commit := range commitIds {
output, err := opts.Format.FormatCommit(c, commit)
if err != nil {
return err
}
fmt.Printf("%v", output)
}

return nil
}

func formatCommitMedium(cmt CommitID, c *Client) (string, error) {
author, err := cmt.GetAuthor(c)
if err != nil {
return "", err
}

date, err := cmt.GetDate(c)
if err != nil {
return "", err
}

msg, err := cmt.GetCommitMessage(c)
if err != nil {
return "", err
}

// Headers
output := fmt.Sprintf("commit %v\nAuthor: %s\nDate: %v\n\n", cmt, author, date.Format("Mon Jan 2 15:04:05 2006 -0700"))

// Commit message body
lines := strings.Split(strings.TrimSpace(msg.String()), "\n")
for _, l := range lines {
output = fmt.Sprintf("%v %v\n", output, l)
}

return output, nil
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(4)
}
case "show":
subcommandUsage = "<commit>..."
if err := cmd.Show(c, args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(4)
}
case "help":
flag.CommandLine.SetOutput(os.Stdout)
flag.Usage()
Expand Down Expand Up @@ -353,6 +359,7 @@ func main() {
apply
revert
help
show Show various types of objects
`)

os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion status.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ reset Almost git 2.9.2 -N not parsed, -p, --merge,
revert HappyPath git 2.14.2 (6) Sequencer options (--continue/quit/abort) are missing, can only do 1 revert at a time. GPG not implemented. MergeStrategy not implemented. --signoff passed to commit, but commit doesn't implement.
rm None Can use rm; git add instead.
shortlog None
show None
show HappyPath git 2.18.0 only commits (no special merge commit format), only --pretty=raw and standard
stash None
status HappyPath git 2.14.2 (6.5) missing --show-stash, --porcelain=2, -v, -v -v, --ignore-submodules, --ignored, --column/--no-column
submodule None
Expand Down