2020-12-31 02:18:18 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-03 04:43:38 +00:00
|
|
|
"strconv"
|
2020-12-31 02:18:18 +00:00
|
|
|
|
2021-03-03 04:43:38 +00:00
|
|
|
"github.com/badjware/gitlabfs/utils"
|
2020-12-31 02:18:18 +00:00
|
|
|
)
|
|
|
|
|
2022-03-03 22:47:36 +00:00
|
|
|
func (c *gitClient) clone(url string, defaultBranch string, dst string) error {
|
2021-03-03 05:24:27 +00:00
|
|
|
if c.CloneMethod == CloneInit {
|
2020-12-31 02:18:18 +00:00
|
|
|
// "Fake" cloning the repo by never actually talking to the git server
|
|
|
|
// This skip a fetch operation that we would do if we where to do a proper clone
|
|
|
|
// We can save a lot of time and network i/o doing it this way, at the cost of
|
|
|
|
// resulting in a very barebone local copy
|
2021-03-03 05:24:27 +00:00
|
|
|
|
|
|
|
// Init the local repo
|
2022-03-03 22:47:36 +00:00
|
|
|
fmt.Printf("Initializing %v into %v\n", url, dst)
|
2021-03-03 04:43:38 +00:00
|
|
|
_, err := utils.ExecProcess(
|
|
|
|
"git", "init",
|
2022-03-03 22:47:36 +00:00
|
|
|
"--initial-branch", defaultBranch,
|
2021-03-03 04:43:38 +00:00
|
|
|
"--",
|
2022-03-03 22:47:36 +00:00
|
|
|
dst, // directory
|
2021-03-03 04:43:38 +00:00
|
|
|
)
|
2020-12-31 02:18:18 +00:00
|
|
|
if err != nil {
|
2022-03-03 22:47:36 +00:00
|
|
|
return fmt.Errorf("failed to init git repo %v to %v: %v", url, dst, err)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the remote
|
2021-03-03 04:43:38 +00:00
|
|
|
_, err = utils.ExecProcessInDir(
|
2022-03-03 22:47:36 +00:00
|
|
|
dst, // workdir
|
2021-03-03 04:43:38 +00:00
|
|
|
"git", "remote", "add",
|
2022-03-03 22:47:36 +00:00
|
|
|
"-m", defaultBranch,
|
2021-03-03 04:43:38 +00:00
|
|
|
"--",
|
|
|
|
c.RemoteName, // name
|
2022-03-03 22:47:36 +00:00
|
|
|
url, // url
|
2021-03-03 04:43:38 +00:00
|
|
|
)
|
2020-12-31 02:18:18 +00:00
|
|
|
if err != nil {
|
2022-03-03 22:47:36 +00:00
|
|
|
return fmt.Errorf("failed to setup remote %v in git repo %v: %v", url, dst, err)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
2021-08-13 17:40:22 +00:00
|
|
|
|
|
|
|
// Configure the default branch
|
|
|
|
_, err = utils.ExecProcessInDir(
|
2022-03-03 22:47:36 +00:00
|
|
|
dst, // workdir
|
2021-08-13 17:40:22 +00:00
|
|
|
"git", "config", "--local",
|
|
|
|
"--",
|
2022-03-03 22:47:36 +00:00
|
|
|
fmt.Sprintf("branch.%s.remote", defaultBranch), // key
|
2021-08-13 17:40:22 +00:00
|
|
|
c.RemoteName, // value
|
|
|
|
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-03-03 22:47:36 +00:00
|
|
|
return fmt.Errorf("failed to setup default branch remote in git repo %v: %v", dst, err)
|
2021-08-13 17:40:22 +00:00
|
|
|
}
|
|
|
|
_, err = utils.ExecProcessInDir(
|
2022-03-03 22:47:36 +00:00
|
|
|
dst, // workdir
|
2021-08-13 17:40:22 +00:00
|
|
|
"git", "config", "--local",
|
|
|
|
"--",
|
2022-03-03 22:47:36 +00:00
|
|
|
fmt.Sprintf("branch.%s.merge", defaultBranch), // key
|
|
|
|
fmt.Sprintf("refs/heads/%s", defaultBranch), // value
|
2021-08-13 17:40:22 +00:00
|
|
|
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-03-03 22:47:36 +00:00
|
|
|
return fmt.Errorf("failed to setup default branch merge in git repo %v: %v", dst, err)
|
2021-08-13 17:40:22 +00:00
|
|
|
}
|
2021-03-03 05:24:27 +00:00
|
|
|
} else {
|
|
|
|
// Clone the repo
|
|
|
|
_, err := utils.ExecProcess(
|
|
|
|
"git", "clone",
|
|
|
|
"--origin", c.RemoteName,
|
|
|
|
"--depth", strconv.Itoa(c.PullDepth),
|
|
|
|
"--",
|
2022-03-03 22:47:36 +00:00
|
|
|
url, // repository
|
|
|
|
dst, // directory
|
2021-03-03 05:24:27 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-03-03 22:47:36 +00:00
|
|
|
return fmt.Errorf("failed to clone git repo %v to %v: %v", url, dst, err)
|
2020-12-31 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|