@@ -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
1213const (
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
108188func getBoards (boardsList map [string ]string ) (map [string ]Board , error ) {
0 commit comments