Something went wrong. Try again.
Lightweight and easily self-hostable Git forge.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101package db
import ( "farga/db/types" "farga/lib" "farga/lib/errors" lib_git "farga/lib/git" "fmt" "strings"
"github.com/go-git/go-git/v6" "github.com/google/uuid" "gorm.io/gorm/clause")
// creates a new repo, both in-DB and on-disk.// it may fail with `errors.ErrRepoAlreadyExists` if the repo exists.// do not forget to close the git repo afterwards.func (db *DB) NewRepo(user *types.User, repo *types.Repo) (*git.Repository, error) { branch := user.Config.GitDefaultBranch if branch == "" { branch = lib.GitDefaultBranch }
if repoMeta, err := db.Repo(*repo); err == nil && repoMeta.ID != uuid.Nil { return nil, errors.ErrRepoAlreadyExists // check if it already exists on a DB level. }
// create the record inside the db err := db.Create(repo).Error if err != nil { if strings.Contains(err.Error(), "UNIQUE constraint") { return nil, errors.ErrRepoAlreadyExists // shouldn't happen but handle gracefully, just in case. } return nil, err }
// create the actual git repo gitRepo, err := lib_git.InitRepo(user, &repo.ID) if err != nil { // cleanup suberr := db.Where(&repo).Delete(&repo).Error if suberr != nil { return nil, fmt.Errorf("init failed: %w; cleanup also failed: %w", err, suberr) } return nil, err }
return gitRepo, nil}
func (db *DB) Repo(repo types.Repo) (types.Repo, error) { err := db.Preload(clause.Associations).Where(&repo).Find(&repo).Error return repo, err}
// gets a repo based off a user's handle and their repo's name.func (db *DB) RepoHandle(userHandle, repoName string) (types.Repo, error) { var repo types.Repo
err := db. Joins("JOIN users ON users.id = repos.user_id"). Where("users.handle = ? AND repos.name = ?", userHandle, repoName). Preload(clause.Associations). First(&repo).Error
return repo, err}
// deletes a repofunc (db *DB) DeleteRepo(repo *types.Repo) error { return db.Delete(repo).Error}
// gets all repos from a given user.func (db *DB) UserRepos(userId *uuid.UUID) ([]types.Repo, error) { var repos []types.Repo err := db.Preload("User").Where(&types.Repo{UserID: userId}).Find(&repos).Error return repos, err}
// updates a given repo's default branchfunc (db *DB) SetRepoDefaultBranch(repo types.Repo, branch string) error { return db.Model(&repo).Update("base_branch", branch).Error // be careful with other ways to update this since they get rid of EVERYTHING.}
// gets a repo by its name and owner's ID.func (db *DB) RepoByName(ownerID *uuid.UUID, name string) (types.Repo, error) { var repo types.Repo err := db.Where(&types.Repo{UserID: ownerID, Name: name}).First(&repo).Error return repo, err}
// assigns a hackatime project to a repo. use the hackatime project name. if fed "", it'll be set to NULL.func (db *DB) SetRepoHackatimeName(repoID uuid.UUID, name string) error { return db.Model(&types.Repo{}). Where("id = ?", repoID). Update("hackatime_name", name). Error}