Commit 961ca00d authored by Kirill Smelkov's avatar Kirill Smelkov

*: 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 !12
parent a066d5e8
......@@ -90,7 +90,7 @@ import (
"lab.nexedi.com/kirr/go123/xstrings"
"lab.nexedi.com/kirr/go123/xsync"
git "github.com/libgit2/git2go/v31"
"lab.nexedi.com/kirr/git-backup/internal/git"
)
// verbose output
......
......@@ -36,7 +36,7 @@ import (
"lab.nexedi.com/kirr/go123/xruntime"
"lab.nexedi.com/kirr/go123/xstrings"
git "github.com/libgit2/git2go/v31"
"lab.nexedi.com/kirr/git-backup/internal/git"
)
func xgetcwd(t *testing.T) string {
......
// Copyright (C) 2015-2021 Nexedi SA and Contributors.
// Copyright (C) 2015-2025 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -33,7 +33,7 @@ import (
"lab.nexedi.com/kirr/go123/mem"
"lab.nexedi.com/kirr/go123/xstrings"
git "github.com/libgit2/git2go/v31"
"lab.nexedi.com/kirr/git-backup/internal/git"
)
// read/write raw objects
......
// Copyright (C) 2025 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Package internal/git wraps package git2go.
package git
import (
git2go "github.com/libgit2/git2go/v31"
)
const (
ObjectAny = git2go.ObjectAny
ObjectInvalid = git2go.ObjectInvalid
ObjectCommit = git2go.ObjectCommit
ObjectTree = git2go.ObjectTree
ObjectBlob = git2go.ObjectBlob
ObjectTag = git2go.ObjectTag
)
type (
ObjectType = git2go.ObjectType
Oid = git2go.Oid
Signature = git2go.Signature
TreeEntry = git2go.TreeEntry
)
type Repository struct {
repo *git2go.Repository
References *ReferenceCollection
}
type ReferenceCollection struct {
r *Repository
}
type Reference struct {
ref *git2go.Reference
}
type Commit struct {
commit *git2go.Commit
}
type Tree struct {
tree *git2go.Tree
}
type Odb struct {
odb *git2go.Odb
}
type OdbObject struct {
obj *git2go.OdbObject
}
// function and methods to navigate object hierarchy from Repository to e.g. OdbObject or Commit.
func OpenRepository(path string) (*Repository, error) {
repo, err := git2go.OpenRepository(path)
if err != nil {
return nil, err
}
r := &Repository{repo: repo}
r.References = &ReferenceCollection{r}
return r, nil
}
func (rdb *ReferenceCollection) Create(name string, id *Oid, force bool, msg string) (*Reference, error) {
ref, err := rdb.r.repo.References.Create(name, id, force, msg)
if err != nil {
return nil, err
}
return &Reference{ref}, nil
}
func (r *Repository) LookupCommit(id *Oid) (*Commit, error) {
commit, err := r.repo.LookupCommit(id)
if err != nil {
return nil, err
}
return &Commit{commit}, nil
}
func (c *Commit) Tree() (*Tree, error) {
tree, err := c.commit.Tree()
if err != nil {
return nil, err
}
return &Tree{tree}, nil
}
func (r *Repository) Odb() (*Odb, error) {
odb, err := r.repo.Odb()
if err != nil {
return nil, err
}
return &Odb{odb}, nil
}
func (o *Odb) Read(oid *Oid) (*OdbObject, error) {
obj, err := o.odb.Read(oid)
if err != nil {
return nil, err
}
return &OdbObject{obj}, nil
}
// wrappers over methods
func (c *Commit) ParentCount() uint { return c.commit.ParentCount() }
func (o *OdbObject) Type() ObjectType { return o.obj.Type() }
func (r *Repository) Path() string {
return r.repo.Path()
}
func (r *Repository) DefaultSignature() (*Signature, error) {
return r.repo.DefaultSignature()
}
func (c *Commit) Message() string {
return c.commit.Message()
}
func (c *Commit) ParentId(n uint) *Oid {
return c.commit.ParentId(n)
}
func (t *Tree) EntryByName(filename string) *TreeEntry {
return t.tree.EntryByName(filename)
}
func (o *Odb) Write(data []byte, otype ObjectType) (*Oid, error) {
return o.odb.Write(data, otype)
}
func (o *OdbObject) Id() *Oid {
return o.obj.Id()
}
func (o *OdbObject) Data() []byte {
return o.obj.Data()
}
// Copyright (C) 2015-2021 Nexedi SA and Contributors.
// Copyright (C) 2015-2025 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
......@@ -27,7 +27,7 @@ import (
"lab.nexedi.com/kirr/go123/mem"
git "github.com/libgit2/git2go/v31"
"lab.nexedi.com/kirr/git-backup/internal/git"
)
const SHA1_RAWSIZE = 20
......@@ -93,7 +93,7 @@ func (p BySha1) Len() int { return len(p) }
func (p BySha1) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p BySha1) Less(i, j int) bool { return bytes.Compare(p[i].sha1[:], p[j].sha1[:]) < 0 }
// interoperability with git2go
// interoperability with git package
func (sha1 *Sha1) AsOid() *git.Oid {
return (*git.Oid)(&sha1.sha1)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment