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 {
|
2024-08-04 19:44:50 +00:00
|
|
|
if c.GitClientConfig.OnClone == "init" {
|
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
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger.Info("Initializing git repository", "directory", dst, "repository", url)
|
2024-05-08 03:58:14 +00:00
|
|
|
args := []string{
|
|
|
|
"init",
|
|
|
|
}
|
|
|
|
if c.majorVersion > 2 || c.majorVersion == 2 && c.minorVersion >= 28 {
|
|
|
|
args = append(args, "--initial-branch", defaultBranch)
|
|
|
|
} else {
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger.Warn("Version of git is too old to support --initial-branch. Consider upgrading git to version >= 2.28.0")
|
2024-05-08 03:58:14 +00:00
|
|
|
}
|
|
|
|
args = append(args,
|
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
|
|
|
)
|
2024-06-06 05:51:34 +00:00
|
|
|
_, err := utils.ExecProcess(c.logger, "git", args...)
|
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(
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger,
|
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
|
|
|
"--",
|
2024-08-04 19:44:50 +00:00
|
|
|
c.GitClientConfig.Remote, // name
|
|
|
|
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(
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger,
|
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
|
2024-08-04 19:44:50 +00:00
|
|
|
c.GitClientConfig.Remote, // 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 remote in git repo %v: %v", dst, err)
|
2021-08-13 17:40:22 +00:00
|
|
|
}
|
|
|
|
_, err = utils.ExecProcessInDir(
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger,
|
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
|
2024-06-06 05:51:34 +00:00
|
|
|
c.logger.Info("Cloning git repository", "directory", dst, "repository", url)
|
2024-05-08 03:31:52 +00:00
|
|
|
args := []string{
|
|
|
|
"clone",
|
2024-08-04 19:44:50 +00:00
|
|
|
"--origin", c.GitClientConfig.Remote,
|
2024-05-08 03:31:52 +00:00
|
|
|
}
|
2024-08-04 19:44:50 +00:00
|
|
|
if c.GitClientConfig.Depth != 0 {
|
|
|
|
args = append(args, "--depth", strconv.Itoa(c.GitClientConfig.Depth))
|
2024-05-08 03:31:52 +00:00
|
|
|
}
|
|
|
|
args = append(args,
|
2021-03-03 05:24:27 +00:00
|
|
|
"--",
|
2022-03-03 22:47:36 +00:00
|
|
|
url, // repository
|
|
|
|
dst, // directory
|
2021-03-03 05:24:27 +00:00
|
|
|
)
|
2024-05-08 03:31:52 +00:00
|
|
|
|
2024-06-06 05:51:34 +00:00
|
|
|
_, err := utils.ExecProcess(c.logger, "git", args...)
|
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
|
|
|
|
}
|