Skip to content

Commit 961ca00

Browse files
committed
*: Localize code that uses git2go in one small place
As will be explained in the next patch git2go exposes a lot of unsafety to the users that can really lead to crashes and data corruption. We are going to fix that via "always safe" wrapper over git2go. -> Factor all code, that uses git2go, into internal/git package as a preparatory step for that. No semantic change for now. /reviewed-on https://lab.nexedi.com/kirr/git-backup/-/merge_requests/12
1 parent a066d5e commit 961ca00

File tree

5 files changed

+174
-7
lines changed

5 files changed

+174
-7
lines changed

git-backup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import (
9090
"lab.nexedi.com/kirr/go123/xstrings"
9191
"lab.nexedi.com/kirr/go123/xsync"
9292

93-
git "github.com/libgit2/git2go/v31"
93+
"lab.nexedi.com/kirr/git-backup/internal/git"
9494
)
9595

9696
// verbose output

git-backup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
"lab.nexedi.com/kirr/go123/xruntime"
3737
"lab.nexedi.com/kirr/go123/xstrings"
3838

39-
git "github.com/libgit2/git2go/v31"
39+
"lab.nexedi.com/kirr/git-backup/internal/git"
4040
)
4141

4242
func xgetcwd(t *testing.T) string {

gitobjects.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2015-2021 Nexedi SA and Contributors.
1+
// Copyright (C) 2015-2025 Nexedi SA and Contributors.
22
// Kirill Smelkov <kirr@nexedi.com>
33
//
44
// This program is free software: you can Use, Study, Modify and Redistribute
@@ -33,7 +33,7 @@ import (
3333
"lab.nexedi.com/kirr/go123/mem"
3434
"lab.nexedi.com/kirr/go123/xstrings"
3535

36-
git "github.com/libgit2/git2go/v31"
36+
"lab.nexedi.com/kirr/git-backup/internal/git"
3737
)
3838

3939
// read/write raw objects

internal/git/git.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright (C) 2025 Nexedi SA and Contributors.
2+
// Kirill Smelkov <kirr@nexedi.com>
3+
//
4+
// This program is free software: you can Use, Study, Modify and Redistribute
5+
// it under the terms of the GNU General Public License version 3, or (at your
6+
// option) any later version, as published by the Free Software Foundation.
7+
//
8+
// You can also Link and Combine this program with other software covered by
9+
// the terms of any of the Free Software licenses or any of the Open Source
10+
// Initiative approved licenses and Convey the resulting work. Corresponding
11+
// source of such a combination shall include the source code for all other
12+
// software used.
13+
//
14+
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
15+
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16+
//
17+
// See COPYING file for full licensing terms.
18+
// See https://www.nexedi.com/licensing for rationale and options.
19+
20+
// Package internal/git wraps package git2go.
21+
package git
22+
23+
import (
24+
git2go "github.com/libgit2/git2go/v31"
25+
)
26+
27+
const (
28+
ObjectAny = git2go.ObjectAny
29+
ObjectInvalid = git2go.ObjectInvalid
30+
ObjectCommit = git2go.ObjectCommit
31+
ObjectTree = git2go.ObjectTree
32+
ObjectBlob = git2go.ObjectBlob
33+
ObjectTag = git2go.ObjectTag
34+
)
35+
36+
37+
type (
38+
ObjectType = git2go.ObjectType
39+
Oid = git2go.Oid
40+
Signature = git2go.Signature
41+
TreeEntry = git2go.TreeEntry
42+
)
43+
44+
45+
type Repository struct {
46+
repo *git2go.Repository
47+
References *ReferenceCollection
48+
}
49+
50+
type ReferenceCollection struct {
51+
r *Repository
52+
}
53+
54+
type Reference struct {
55+
ref *git2go.Reference
56+
}
57+
58+
type Commit struct {
59+
commit *git2go.Commit
60+
}
61+
62+
type Tree struct {
63+
tree *git2go.Tree
64+
}
65+
66+
type Odb struct {
67+
odb *git2go.Odb
68+
}
69+
70+
type OdbObject struct {
71+
obj *git2go.OdbObject
72+
}
73+
74+
75+
// function and methods to navigate object hierarchy from Repository to e.g. OdbObject or Commit.
76+
77+
func OpenRepository(path string) (*Repository, error) {
78+
repo, err := git2go.OpenRepository(path)
79+
if err != nil {
80+
return nil, err
81+
}
82+
r := &Repository{repo: repo}
83+
r.References = &ReferenceCollection{r}
84+
return r, nil
85+
}
86+
87+
func (rdb *ReferenceCollection) Create(name string, id *Oid, force bool, msg string) (*Reference, error) {
88+
ref, err := rdb.r.repo.References.Create(name, id, force, msg)
89+
if err != nil {
90+
return nil, err
91+
}
92+
return &Reference{ref}, nil
93+
}
94+
95+
func (r *Repository) LookupCommit(id *Oid) (*Commit, error) {
96+
commit, err := r.repo.LookupCommit(id)
97+
if err != nil {
98+
return nil, err
99+
}
100+
return &Commit{commit}, nil
101+
}
102+
103+
func (c *Commit) Tree() (*Tree, error) {
104+
tree, err := c.commit.Tree()
105+
if err != nil {
106+
return nil, err
107+
}
108+
return &Tree{tree}, nil
109+
}
110+
111+
func (r *Repository) Odb() (*Odb, error) {
112+
odb, err := r.repo.Odb()
113+
if err != nil {
114+
return nil, err
115+
}
116+
return &Odb{odb}, nil
117+
}
118+
119+
func (o *Odb) Read(oid *Oid) (*OdbObject, error) {
120+
obj, err := o.odb.Read(oid)
121+
if err != nil {
122+
return nil, err
123+
}
124+
return &OdbObject{obj}, nil
125+
}
126+
127+
128+
// wrappers over methods
129+
130+
func (c *Commit) ParentCount() uint { return c.commit.ParentCount() }
131+
func (o *OdbObject) Type() ObjectType { return o.obj.Type() }
132+
133+
134+
func (r *Repository) Path() string {
135+
return r.repo.Path()
136+
}
137+
138+
func (r *Repository) DefaultSignature() (*Signature, error) {
139+
return r.repo.DefaultSignature()
140+
}
141+
142+
143+
func (c *Commit) Message() string {
144+
return c.commit.Message()
145+
}
146+
147+
func (c *Commit) ParentId(n uint) *Oid {
148+
return c.commit.ParentId(n)
149+
}
150+
151+
func (t *Tree) EntryByName(filename string) *TreeEntry {
152+
return t.tree.EntryByName(filename)
153+
}
154+
155+
156+
func (o *Odb) Write(data []byte, otype ObjectType) (*Oid, error) {
157+
return o.odb.Write(data, otype)
158+
}
159+
160+
161+
func (o *OdbObject) Id() *Oid {
162+
return o.obj.Id()
163+
}
164+
165+
func (o *OdbObject) Data() []byte {
166+
return o.obj.Data()
167+
}

sha1.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2015-2021 Nexedi SA and Contributors.
1+
// Copyright (C) 2015-2025 Nexedi SA and Contributors.
22
// Kirill Smelkov <kirr@nexedi.com>
33
//
44
// This program is free software: you can Use, Study, Modify and Redistribute
@@ -27,7 +27,7 @@ import (
2727

2828
"lab.nexedi.com/kirr/go123/mem"
2929

30-
git "github.com/libgit2/git2go/v31"
30+
"lab.nexedi.com/kirr/git-backup/internal/git"
3131
)
3232

3333
const SHA1_RAWSIZE = 20
@@ -93,7 +93,7 @@ func (p BySha1) Len() int { return len(p) }
9393
func (p BySha1) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
9494
func (p BySha1) Less(i, j int) bool { return bytes.Compare(p[i].sha1[:], p[j].sha1[:]) < 0 }
9595

96-
// interoperability with git2go
96+
// interoperability with git package
9797
func (sha1 *Sha1) AsOid() *git.Oid {
9898
return (*git.Oid)(&sha1.sha1)
9999
}

0 commit comments

Comments
 (0)