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 8 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
25 changes: 25 additions & 0 deletions cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.StringVar(&opts.Pretty, "pretty", "", "Pretty-print the contents of commit logs in a specified format")
flags.Parse(args)

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

import (
"fmt"
"strings"
)

type ShowOptions struct {
Pretty string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The man page implies this should have a DiffCommonOptions composed in. I also think "Format" is more descriptive than the Pretty alias.

(Maybe we should have a FormatString type to distinguish it from other types of strings too)

}

// 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.")
}

if opts.Pretty != "" && opts.Pretty != "raw" {
return fmt.Errorf("Only raw format is supported, not %v", opts.Pretty)
}

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 {
if opts.Pretty == "raw" {
if err := showCommitRaw(commit, c); err != nil {
return err
}
} else {
if err := showCommit(commit, c); err != nil {
return err
}
}
}

return nil
}

func showCommit(cmt CommitID, c *Client) 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
fmt.Printf("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 {
fmt.Printf(" %v\n", l)
}
fmt.Printf("\n")

return nil
}

func showCommitRaw(cmt CommitID, c *Client) 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
}

parents, err := cmt.Parents(c)
if err != nil {
return err
}

tree, err := cmt.TreeID(c)
if err != nil {
return err
}

// Headers
fmt.Printf("commit %v\ntree %v\nparent %v\nauthor %s %v +0000\ncommiter %s %v +0000\n\n", cmt, tree, parents[0], author, date.Unix(), author, date.Unix())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parent should use string.Join("\n", ...) rather than silently discarding the other parents. ( In fact, I think this whole bit is unnecessary. You can either just call cat-file -p or call c.GetCommitObject and just print the results using the stringer to get the raw commit.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll have a look at doing that for the raw case.


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

return nil
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(4)
}
case "show":
subcommandUsage = fmt.Sprintf("%v [global options] show [options] <commit>...\n", os.Args[0])
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 @@ -348,6 +354,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