@@ -13,8 +13,10 @@ import (
1313 "strings"
1414
1515 git "github.com/go-git/go-git/v5"
16+ "github.com/go-git/go-git/v5/config"
1617 "github.com/go-git/go-git/v5/plumbing"
1718 "github.com/go-git/go-git/v5/plumbing/transport/http"
19+ "github.com/go-git/go-git/v5/storage/memory"
1820 "github.com/joho/godotenv"
1921 "github.com/tmc/langchaingo/llms"
2022
@@ -219,19 +221,72 @@ func processWorkingDirectory(githubLink, githubBranch, githubUsername, githubTok
219221 }
220222
221223 sPath := strings .Split (strings .TrimPrefix (u .Path , "/" ), "/" )
222- if len (sPath ) != 2 {
223- return "" , errors .New ("github repository url does not have two path elements" )
224+ cdAfter := ""
225+ if len (sPath ) < 2 {
226+ return "" , errors .New ("github repository url does not have at least two path elements" )
227+ } else if len (sPath ) > 2 {
228+ // set githubLink to a repository only
229+ u .Path = strings .Join (sPath [:2 ], "/" )
230+ githubLink = u .String ()
231+
232+ if sPath [2 ] != "tree" {
233+ return "" , errors .New ("extended github repository url should have 'tree' route after repository name" )
234+ }
235+ if len (sPath ) < 4 {
236+ return "" , errors .New ("extended github repository url should have branch name after tree route" )
237+ }
238+ // do not override branch set from the flag
239+ if githubBranch == "" {
240+ githubBranch = sPath [3 ]
241+ }
242+ cdAfter = strings .Join (sPath [4 :], "/" )
224243 }
225244
226245 tempDirEl := []string {workdir , "temp" }
246+ tempDirEl = append (tempDirEl , sPath [:2 ]... )
247+
227248 if githubBranch != "" {
228- tempDirEl = append (tempDirEl , "with_branch" )
229- tempDirEl = append (tempDirEl , sPath ... )
230249 tempDirEl = append (tempDirEl , githubBranch )
231250 } else {
232- tempDirEl = append (tempDirEl , "root_branch" )
233- tempDirEl = append (tempDirEl , sPath ... )
251+ rem := git .NewRemote (memory .NewStorage (), & config.RemoteConfig {
252+ Name : "origin" ,
253+ URLs : []string {githubLink },
254+ })
255+ refs , err := rem .List (& git.ListOptions {})
256+ if err != nil {
257+ return "" , err
258+ }
259+
260+ var defaultBranchRef * plumbing.Reference
261+ for _ , ref := range refs {
262+ if ref .Name () == "HEAD" {
263+ defaultBranchRef = ref
264+ break
265+ }
266+ }
267+
268+ if defaultBranchRef == nil {
269+ return "" , fmt .Errorf ("HEAD reference not found in ls-remote output" )
270+ }
271+
272+ if defaultBranchRef .Type () == plumbing .SymbolicReference {
273+ targetRefName := defaultBranchRef .Target ()
274+ for _ , ref := range refs {
275+ if ref .Name () == targetRefName {
276+ defaultBranchRef = ref
277+ break
278+ }
279+ }
280+ }
281+
282+ tempDirEl = append (tempDirEl ,
283+ strings .Replace (
284+ defaultBranchRef .Name ().String (),
285+ "refs/heads/" ,
286+ "" , 1 ),
287+ )
234288 }
289+
235290 tempDir := filepath .Join (tempDirEl ... )
236291
237292 workdir = tempDir
@@ -270,6 +325,8 @@ func processWorkingDirectory(githubLink, githubBranch, githubUsername, githubTok
270325 return "" , err
271326 }
272327 }
328+ workdir = filepath .Join (workdir , cdAfter )
329+
273330 } else if len (flag .Args ()) > 0 {
274331 workdir = flag .Arg (0 )
275332 if _ , err := os .Stat (workdir ); err != nil {
0 commit comments