Skip to content

Commit 3054467

Browse files
authored
Merge pull request #203 from HyperloopUPV-H8/backend/submodule-adj
[backend] ADJ submodule manipulation
2 parents 703b098 + dbadf8d commit 3054467

4 files changed

Lines changed: 105 additions & 17 deletions

File tree

backend/cmd/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import (
55
"github.com/HyperloopUPV-H8/h9-backend/internal/vehicle"
66
)
77

8+
type Adj struct {
9+
Branch string
10+
}
811
type Config struct {
912
Vehicle vehicle.Config
1013
Server server.Config
14+
Adj Adj
1115
}

backend/cmd/config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ connections = "/backend"
1111
files = "/"
1212

1313
[vehicle]
14-
boardsList = ["VCU"]
14+
boardsList = ["VCU"]
15+
16+
[adj]
17+
branch = "main" # Leave blank when using ADJ as a submodule

backend/cmd/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ func main() {
9797
defer pprof.StopCPUProfile()
9898
}
9999
runtime.SetBlockProfileRate(*blockprofile)
100+
100101
config := getConfig("./config.toml")
101102

102103
// <--- ADJ --->
103104

104-
adj, err := adj_module.NewADJ()
105+
adj, err := adj_module.NewADJ(config.Adj.Branch)
105106
if err != nil {
106107
trace.Fatal().Err(err).Msg("setting up ADJ")
107108
}

backend/internal/adj/adj.go

Lines changed: 95 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77

88
"github.com/HyperloopUPV-H8/h9-backend/internal/utils"
99
"github.com/go-git/go-git/v5"
10+
"github.com/go-git/go-git/v5/plumbing"
1011
)
1112

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

17-
func NewADJ() (ADJ, error) {
18-
infoRaw, boardsRaw, err := downloadADJ()
18+
func NewADJ(AdjBranch string) (ADJ, error) {
19+
infoRaw, boardsRaw, err := downloadADJ(AdjBranch)
1920
if err != nil {
2021
return ADJ{}, err
2122
}
@@ -73,15 +74,8 @@ func NewADJ() (ADJ, error) {
7374
return adj, nil
7475
}
7576

76-
func downloadADJ() (json.RawMessage, json.RawMessage, error) {
77-
if !checkRepo() {
78-
_, err := git.PlainClone(RepoPath, false, &git.CloneOptions{
79-
URL: RepoUrl,
80-
})
81-
if err != nil {
82-
return nil, nil, err
83-
}
84-
}
77+
func downloadADJ(AdjBranch string) (json.RawMessage, json.RawMessage, error) {
78+
updateRepo(AdjBranch)
8579

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

100-
func checkRepo() bool {
101-
if _, err := os.Stat(RepoPath); os.IsNotExist(err) {
102-
return false
94+
// WARNING: Doing tricks on it
95+
func updateRepo(AdjBranch string) error {
96+
var repo *git.Repository
97+
var err error
98+
99+
if AdjBranch == "" {
100+
// Makes use of submodule
101+
return nil
102+
} else {
103+
if _, err = os.Stat(RepoPath); os.IsNotExist(err) {
104+
repo, err = git.PlainClone(RepoPath, false, &git.CloneOptions{
105+
URL: RepoUrl,
106+
ReferenceName: plumbing.NewBranchReferenceName(AdjBranch),
107+
SingleBranch: true,
108+
Depth: 1,
109+
})
110+
if err != nil {
111+
return err
112+
}
113+
} else {
114+
repo, err = git.PlainOpen(RepoPath)
115+
if err != nil {
116+
return err
117+
}
118+
}
119+
120+
err = repo.Fetch(&git.FetchOptions{
121+
RemoteName: "origin",
122+
Force: true,
123+
})
124+
if err != nil && err != git.NoErrAlreadyUpToDate {
125+
return err
126+
}
127+
128+
head, err := repo.Head()
129+
if err != nil {
130+
return err
131+
}
132+
133+
branch := head.Name().Short()
134+
135+
println("actual branch is: ", branch)
136+
137+
worktree, err := repo.Worktree()
138+
if err != nil {
139+
return err
140+
}
141+
142+
println(head.Name().Short(), AdjBranch)
143+
144+
if branch != AdjBranch {
145+
localBranchRef := plumbing.NewBranchReferenceName(AdjBranch)
146+
_, err = repo.Reference(localBranchRef, false)
147+
if err != nil {
148+
remoteBranchRef := plumbing.NewRemoteReferenceName("origin", AdjBranch)
149+
remoteRef, err2 := repo.Reference(remoteBranchRef, true)
150+
if err2 != nil {
151+
return err2
152+
}
153+
154+
err = worktree.Checkout(&git.CheckoutOptions{
155+
Branch: localBranchRef,
156+
Create: true,
157+
Force: true,
158+
Hash: remoteRef.Hash(),
159+
})
160+
if err != nil {
161+
println(err.Error())
162+
return err
163+
}
164+
} else {
165+
err = worktree.Checkout(&git.CheckoutOptions{
166+
Branch: localBranchRef,
167+
Force: true,
168+
})
169+
}
170+
}
171+
172+
err = worktree.Pull(&git.PullOptions{
173+
RemoteName: "origin",
174+
SingleBranch: true,
175+
})
176+
if err != nil {
177+
if err == git.NoErrAlreadyUpToDate {
178+
return nil
179+
} else {
180+
return err
181+
}
182+
}
103183
}
104184

105-
return true
185+
return nil
106186
}
107187

108188
func getBoards(boardsList map[string]string) (map[string]Board, error) {

0 commit comments

Comments
 (0)