-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from 8 commits
c0bcdec
a1f3c85
81b924f
b1008ff
c0b08d9
6c5f8db
ee1a978
4dd570d
6647264
7695d1d
16f93bf
13db2c3
b8f115e
54b86bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type ShowOptions struct { | ||
Pretty string | ||
} | ||
|
||
// 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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)