diff --git a/backend/cmd/config.go b/backend/cmd/config.go index 3cb268afe..e80b70d52 100644 --- a/backend/cmd/config.go +++ b/backend/cmd/config.go @@ -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 } diff --git a/backend/cmd/config.toml b/backend/cmd/config.toml index 297584b3f..8a9f54fa1 100644 --- a/backend/cmd/config.toml +++ b/backend/cmd/config.toml @@ -11,4 +11,7 @@ connections = "/backend" files = "/" [vehicle] -boardsList = ["VCU"] \ No newline at end of file +boardsList = ["VCU"] + +[adj] +branch = "main" # Leave blank when using ADJ as a submodule diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 2f18bc11d..1621f43ce 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -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") } diff --git a/backend/internal/adj/adj.go b/backend/internal/adj/adj.go index 52c02d2e1..7a6ba9ea3 100644 --- a/backend/internal/adj/adj.go +++ b/backend/internal/adj/adj.go @@ -7,6 +7,7 @@ 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 ( @@ -14,8 +15,8 @@ const ( 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 } @@ -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") @@ -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) {