Skip to content

Commit 7c4a608

Browse files
authored
Merge pull request #50 from JackBekket/feature/deep_directory
add extended git url support with branch name and cd after clone; sup…
2 parents 7a5a1e6 + 33ee3ce commit 7c4a608

2 files changed

Lines changed: 66 additions & 7 deletions

File tree

internal/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package util
22

33
import (
4+
"errors"
45
"io/fs"
56
"log"
7+
"os"
68
"path/filepath"
79

810
ignore "github.com/crackcomm/go-gitignore"
@@ -15,7 +17,7 @@ func WalkDirIgnored(workdir, gitignorePath string, f WalkDirIgnoredFunction) err
1517
var err error
1618
if gitignorePath != "" {
1719
ignoreFile, err = ignore.CompileIgnoreFile(gitignorePath)
18-
if err != nil {
20+
if err != nil && !errors.Is(err, os.ErrNotExist) {
1921
log.Printf("failed to load .gitignore file %v", err)
2022
}
2123
}

main.go

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)