Skip to content

Commit a9b197f

Browse files
committed
experimental libgit2 flake and code
1 parent 1cbd198 commit a9b197f

File tree

6 files changed

+168
-5
lines changed

6 files changed

+168
-5
lines changed

flake.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
inputs = {
3-
nixpkgs.url = "github:NixOS/nixpkgs";
3+
#nixpkgs.url = "github:NixOS/nixpkgs";
4+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
45
utils.url = "github:numtide/flake-utils";
56
};
67

@@ -31,7 +32,15 @@
3132
devShell = pkgs.mkShell {
3233
buildInputs = with pkgs; [
3334
gnumake
35+
libgit2
36+
#openssl zstd libgit2 libssh2
37+
pkg-config
3438
];
39+
40+
shellHook = ''
41+
export LIBSSH2_SYS_USE_PKG_CONFIG=1
42+
export PKG_CONFIG_PATH=${pkgs.libgit2}/lib/pkgconfig/
43+
'';
3544
};
3645
});
37-
}
46+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/imdario/mergo v0.3.12 // indirect
2525
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
2626
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
27+
github.com/libgit2/git2go/v34 v34.0.0 // indirect
2728
github.com/magiconair/properties v1.8.6 // indirect
2829
github.com/mitchellh/go-homedir v1.1.0 // indirect
2930
github.com/mitchellh/mapstructure v1.5.0 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe
140140
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
141141
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
142142
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
143+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
143144
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
144145
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
145146
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
@@ -169,6 +170,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
169170
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
170171
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
171172
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
173+
github.com/libgit2/git2go/v34 v34.0.0 h1:UKoUaKLmiCRbOCD3PtUi2hD6hESSXzME/9OUZrGcgu8=
174+
github.com/libgit2/git2go/v34 v34.0.0/go.mod h1:blVco2jDAw6YTXkErMMqzHLcAjKkwF0aWIRHBqiJkZ0=
172175
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
173176
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
174177
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
@@ -238,6 +241,7 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
238241
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
239242
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
240243
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
244+
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
241245
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
242246
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
243247
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -343,6 +347,7 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w
343347
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
344348
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
345349
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
350+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
346351
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
347352
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
348353
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -362,6 +367,7 @@ golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7w
362367
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
363368
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
364369
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
370+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
365371
golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
366372
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
367373
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -373,6 +379,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
373379
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
374380
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
375381
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
382+
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
376383
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
377384
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
378385
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

src/cmd_git2test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
import (
3+
"github.com/urfave/cli/v2"
4+
5+
"github.com/libgit2/git2go/v34"
6+
)
7+
8+
func cmdGit2test() *cli.Command {
9+
10+
return &cli.Command{
11+
Name: "git2test",
12+
Usage: "show logs from remote",
13+
Flags: []cli.Flag{
14+
&cli.StringFlag{
15+
Name: "ssh-key",
16+
Aliases: []string{"i"},
17+
Usage: "alternative ssh-key from `FILE`",
18+
},
19+
&cli.BoolFlag{
20+
Name: "insecure",
21+
Aliases: []string{"s"},
22+
Usage: "skip host key validation",
23+
},
24+
},
25+
Action: func(c *cli.Context) error {
26+
27+
28+
repo, err := git.OpenRepository(".")
29+
if err != nil {
30+
return err
31+
}
32+
defer repo.Free()
33+
34+
headRef, err := repo.Head()
35+
if err != nil {
36+
return err
37+
}
38+
defer headRef.Free()
39+
40+
revWalk, err := repo.Walk()
41+
if err != nil {
42+
return err
43+
}
44+
defer revWalk.Free()
45+
46+
if err := revWalk.Push(headRef.Target()); err != nil {
47+
return err
48+
}
49+
50+
revWalk.Sorting(git.SortTime)
51+
52+
count := 0
53+
if err := revWalk.Iterate(func(commit *git.Commit) bool {
54+
defer commit.Free()
55+
count++
56+
Git2GoCommitRevWalk = commit
57+
return true
58+
}); err != nil {
59+
return err
60+
}
61+
62+
return nil
63+
64+
},
65+
66+
}
67+
}
68+
69+

src/cmd_log.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
import (
3+
"fmt"
4+
"os"
5+
"time"
6+
"github.com/urfave/cli/v2"
7+
"github.com/go-git/go-git/v5"
8+
9+
"github.com/go-git/go-git/v5/plumbing/object"
10+
"github.com/go-git/go-git/v5/storage/memory"
11+
)
12+
13+
func cmdLog() *cli.Command {
14+
15+
return &cli.Command{
16+
Name: "log",
17+
Usage: "show logs from remote",
18+
Flags: []cli.Flag{
19+
&cli.StringFlag{
20+
Name: "ssh-key",
21+
Aliases: []string{"i"},
22+
Usage: "alternative ssh-key from `FILE`",
23+
},
24+
&cli.BoolFlag{
25+
Name: "insecure",
26+
Aliases: []string{"s"},
27+
Usage: "skip host key validation",
28+
},
29+
},
30+
Action: func(c *cli.Context) error {
31+
32+
33+
url := c.Args().Get(0)
34+
directory := c.Args().Get(1)
35+
auth := setAuth(c.String("ssh-key"), c.Bool("insecure"))
36+
//branch := c.String("branch")
37+
38+
Info("git log %s %s", url, directory)
39+
40+
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
41+
URL: url,
42+
Progress: os.Stdout,
43+
Auth: auth,
44+
})
45+
46+
CheckIfError(err)
47+
48+
49+
// Gets the HEAD history from HEAD, just like this command:
50+
Info("git log")
51+
52+
// ... retrieves the branch pointed by HEAD
53+
ref, err := r.Head()
54+
CheckIfError(err)
55+
56+
// ... retrieves the commit history
57+
since := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
58+
until := time.Date(2019, 7, 30, 0, 0, 0, 0, time.UTC)
59+
cIter, err := r.Log(&git.LogOptions{From: ref.Hash(), Since: &since, Until: &until})
60+
CheckIfError(err)
61+
62+
// ... just iterates over the commits, printing it
63+
err = cIter.ForEach(func(c *object.Commit) error {
64+
fmt.Println(c)
65+
66+
return nil
67+
})
68+
CheckIfError(err)
69+
70+
return nil
71+
},
72+
73+
}
74+
}
75+
76+

0 commit comments

Comments
 (0)