Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"github.com/HyperloopUPV-H8/h9-backend/internal/vehicle"
)

type Adj struct {
Branch string
}
type Config struct {
Vehicle vehicle.Config
Server server.Config
Adj Adj
}
5 changes: 4 additions & 1 deletion backend/cmd/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ connections = "/backend"
files = "/"

[vehicle]
boardsList = ["VCU"]
boardsList = ["VCU"]

[adj]
branch = "main" # Leave blank when using ADJ as a submodule
3 changes: 2 additions & 1 deletion backend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ func main() {
defer pprof.StopCPUProfile()
}
runtime.SetBlockProfileRate(*blockprofile)

config := getConfig("./config.toml")

// <--- ADJ --->

adj, err := adj_module.NewADJ()
adj, err := adj_module.NewADJ(config.Adj.Branch)
if err != nil {
trace.Fatal().Err(err).Msg("setting up ADJ")
}
Expand Down
110 changes: 95 additions & 15 deletions backend/internal/adj/adj.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (

"github.com/HyperloopUPV-H8/h9-backend/internal/utils"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)

const (
RepoUrl = "https://github.com/HyperloopUPV-H8/JSON_ADE.git" // URL of the ADJ repository
RepoPath = "./JSON_ADE/" // Path where the ADJ repository is cloned
)

func NewADJ() (ADJ, error) {
infoRaw, boardsRaw, err := downloadADJ()
func NewADJ(AdjBranch string) (ADJ, error) {
infoRaw, boardsRaw, err := downloadADJ(AdjBranch)
if err != nil {
return ADJ{}, err
}
Expand Down Expand Up @@ -73,15 +74,8 @@ func NewADJ() (ADJ, error) {
return adj, nil
}

func downloadADJ() (json.RawMessage, json.RawMessage, error) {
if !checkRepo() {
_, err := git.PlainClone(RepoPath, false, &git.CloneOptions{
URL: RepoUrl,
})
if err != nil {
return nil, nil, err
}
}
func downloadADJ(AdjBranch string) (json.RawMessage, json.RawMessage, error) {
updateRepo(AdjBranch)

// The BoardIds are applied in the NewADJ function by the getBoardIds function
info, err := os.ReadFile(RepoPath + "general_info.json")
Expand All @@ -97,12 +91,98 @@ func downloadADJ() (json.RawMessage, json.RawMessage, error) {
return info, boardsList, nil
}

func checkRepo() bool {
if _, err := os.Stat(RepoPath); os.IsNotExist(err) {
return false
// WARNING: Doing tricks on it
func updateRepo(AdjBranch string) error {
var repo *git.Repository
var err error

if AdjBranch == "" {
// Makes use of submodule
return nil
} else {
if _, err = os.Stat(RepoPath); os.IsNotExist(err) {
repo, err = git.PlainClone(RepoPath, false, &git.CloneOptions{
URL: RepoUrl,
ReferenceName: plumbing.NewBranchReferenceName(AdjBranch),
SingleBranch: true,
Depth: 1,
})
if err != nil {
return err
}
} else {
repo, err = git.PlainOpen(RepoPath)
if err != nil {
return err
}
}

err = repo.Fetch(&git.FetchOptions{
RemoteName: "origin",
Force: true,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
return err
}

head, err := repo.Head()
if err != nil {
return err
}

branch := head.Name().Short()

println("actual branch is: ", branch)

worktree, err := repo.Worktree()
if err != nil {
return err
}

println(head.Name().Short(), AdjBranch)

if branch != AdjBranch {
localBranchRef := plumbing.NewBranchReferenceName(AdjBranch)
_, err = repo.Reference(localBranchRef, false)
if err != nil {
remoteBranchRef := plumbing.NewRemoteReferenceName("origin", AdjBranch)
remoteRef, err2 := repo.Reference(remoteBranchRef, true)
if err2 != nil {
return err2
}

err = worktree.Checkout(&git.CheckoutOptions{
Branch: localBranchRef,
Create: true,
Force: true,
Hash: remoteRef.Hash(),
})
if err != nil {
println(err.Error())
return err
}
} else {
err = worktree.Checkout(&git.CheckoutOptions{
Branch: localBranchRef,
Force: true,
})
}
}

err = worktree.Pull(&git.PullOptions{
RemoteName: "origin",
SingleBranch: true,
})
if err != nil {
if err == git.NoErrAlreadyUpToDate {
return nil
} else {
return err
}
}
}

return true
return nil
}

func getBoards(boardsList map[string]string) (map[string]Board, error) {
Expand Down
Loading