From 896723a0cab34c1b1e4213d59a5de88dd19902e8 Mon Sep 17 00:00:00 2001 From: lewis <747342561@qq.com> Date: Fri, 20 Aug 2021 14:26:58 +0800 Subject: [PATCH] mod the remote origin url --- routers/repo/cloudbrain.go | 39 +++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index 5fce6b3a2..db6a6fe4a 100755 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -1,8 +1,10 @@ package repo import ( + "bufio" "encoding/json" "errors" + "io" "net/http" "os" "os/exec" @@ -494,11 +496,46 @@ func GetRate(ctx *context.Context) { } func downloadCode(repo *models.Repository, codePath string) error { - if err := git.Clone(repo.CloneLink().HTTPS, codePath, git.CloneRepoOptions{}); err != nil { + if err := git.Clone(repo.RepoPath(), codePath, git.CloneRepoOptions{}); err != nil { log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) return err } + configFile, err := os.OpenFile(codePath + "/.git/config", os.O_RDWR, 0666) + if err != nil { + log.Error("open file(%s) failed:%v", codePath + "/,git/config", err) + return err + } + + defer configFile.Close() + + pos := int64(0) + reader := bufio.NewReader(configFile) + for { + line, err := reader.ReadString('\n') + if err != nil { + if err == io.EOF { + log.Error("not find the remote-url") + return nil + } else { + log.Error("read error: %v", err) + return err + } + } + + if strings.Contains(line, "url") && strings.Contains(line, ".git"){ + bytes := []byte("\turl = " + repo.CloneLink().HTTPS + "\n") + _, err := configFile.WriteAt(bytes, pos) + if err != nil { + log.Error("WriteAt failed:%v", err) + return err + } + break + } + + pos += int64(len(line)) + } + return nil }