Skip to content

Commit ea6e658

Browse files
committed
log_remote and log_local commands
1 parent 6d73f13 commit ea6e658

File tree

4 files changed

+92
-24
lines changed

4 files changed

+92
-24
lines changed

src/cmd_clone.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ func cmdClone() *cli.Command {
1818
Aliases: []string{"i"},
1919
Usage: "alternative ssh-key from `FILE`",
2020
},
21-
&cli.StringFlag{
22-
Name: "branch",
23-
Aliases: []string{"b"},
24-
Usage: "other then default branch",
25-
},
21+
// &cli.StringFlag{
22+
// Name: "branch",
23+
// Aliases: []string{"b"},
24+
// Usage: "other then default branch",
25+
// },
2626
&cli.BoolFlag{
2727
Name: "insecure",
2828
Aliases: []string{"s"},

src/cmd_log_local.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
import (
3+
"fmt"
4+
"encoding/json"
5+
"time"
6+
"github.com/urfave/cli/v2"
7+
"github.com/go-git/go-git/v5"
8+
"github.com/go-git/go-git/v5/plumbing/object"
9+
)
10+
11+
func cmdLogLocal() *cli.Command {
12+
13+
return &cli.Command{
14+
Name: "log_local",
15+
Usage: "show logs from local repo",
16+
Action: func(c *cli.Context) error {
17+
18+
directory := c.Args().Get(0)
19+
20+
r, err := git.PlainOpen(directory)
21+
CheckIfError(err)
22+
23+
ref, err := r.Head()
24+
CheckIfError(err)
25+
26+
since := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
27+
until := time.Date(2099, 1, 30, 0, 0, 0, 0, time.UTC)
28+
cIter, err := r.Log(&git.LogOptions{From: ref.Hash(), Since: &since, Until: &until})
29+
CheckIfError(err)
30+
31+
var jsonCommits []*jsonCommitEntry
32+
33+
err = cIter.ForEach(func(c *object.Commit) error {
34+
35+
commitEntry := &jsonCommitEntry{
36+
Author: c.Author.String(),
37+
Message: c.Message,
38+
Ref: c.Hash.String(),
39+
Date: c.Author.When.Format(DateFormat),
40+
}
41+
42+
jsonCommits = append(jsonCommits, commitEntry)
43+
44+
return nil
45+
})
46+
CheckIfError(err)
47+
48+
data, _ := json.Marshal(jsonCommits)
49+
fmt.Println(string(data))
50+
51+
return nil
52+
},
53+
54+
}
55+
}
56+
57+

src/cmd_log.go renamed to src/cmd_log_remote.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package main
22
import (
33
"fmt"
4-
"os"
4+
"encoding/json"
55
"time"
66
"github.com/urfave/cli/v2"
77
"github.com/go-git/go-git/v5"
8-
98
"github.com/go-git/go-git/v5/plumbing/object"
109
"github.com/go-git/go-git/v5/storage/memory"
1110
)
1211

13-
func cmdLog() *cli.Command {
12+
func cmdLogRemote() *cli.Command {
1413

1514
return &cli.Command{
16-
Name: "log",
15+
Name: "log_remote",
1716
Usage: "show logs from remote",
1817
Flags: []cli.Flag{
1918
&cli.StringFlag{
@@ -30,41 +29,45 @@ func cmdLog() *cli.Command {
3029
Action: func(c *cli.Context) error {
3130

3231
url := c.Args().Get(0)
33-
directory := c.Args().Get(1)
32+
//directory := c.Args().Get(1)
3433
auth := setAuth(c.String("ssh-key"), c.Bool("insecure"))
3534
//branch := c.String("branch")
3635

37-
Info("git log %s %s", url, directory)
38-
3936
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
4037
URL: url,
41-
Progress: os.Stdout,
4238
Auth: auth,
4339
})
4440

4541
CheckIfError(err)
4642

47-
// Gets the HEAD history from HEAD, just like this command:
48-
Info("git log")
49-
50-
// ... retrieves the branch pointed by HEAD
5143
ref, err := r.Head()
5244
CheckIfError(err)
5345

54-
// ... retrieves the commit history
55-
since := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
56-
until := time.Date(2019, 7, 30, 0, 0, 0, 0, time.UTC)
46+
since := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
47+
until := time.Date(2099, 1, 30, 0, 0, 0, 0, time.UTC)
5748
cIter, err := r.Log(&git.LogOptions{From: ref.Hash(), Since: &since, Until: &until})
5849
CheckIfError(err)
5950

60-
// ... just iterates over the commits, printing it
51+
var jsonCommits []*jsonCommitEntry
52+
6153
err = cIter.ForEach(func(c *object.Commit) error {
62-
fmt.Println(c)
54+
55+
commitEntry := &jsonCommitEntry{
56+
Author: c.Author.String(),
57+
Message: c.Message,
58+
Ref: c.Hash.String(),
59+
Date: c.Author.When.Format(DateFormat),
60+
}
61+
62+
jsonCommits = append(jsonCommits, commitEntry)
6363

6464
return nil
6565
})
6666
CheckIfError(err)
6767

68+
data, _ := json.Marshal(jsonCommits)
69+
fmt.Println(string(data))
70+
6871
return nil
6972
},
7073

src/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import (
1616
)
1717

1818
const version = "v0.5.2"
19+
const DateFormat = "Mon Jan 02 15:04:05 2006 -0700"
20+
21+
type jsonCommitEntry struct {
22+
Message string `json:"message"`
23+
Author string `json:"author"`
24+
Date string `json:"date"`
25+
Ref string `json:"ref"`
26+
}
1927

2028
func setAuth(keyfilepath string, ignoreHostkey bool) transport.AuthMethod {
2129

@@ -59,8 +67,8 @@ func main() {
5967
cmdAddAll(),
6068
cmdClone(),
6169
cmdCommit(),
62-
cmdLog(),
63-
cmdGit2test(),
70+
cmdLogLocal(),
71+
cmdLogRemote(),
6472
cmdPull(),
6573
cmdPush(),
6674
cmdResetHard(),

0 commit comments

Comments
 (0)