git clone
(gitCloneUrl string, workSpaceDir string, no string, cmd *cobra.Command)
| 420 | |
| 421 | // git clone |
| 422 | func (instance *SSHRemote) GitClone(gitCloneUrl string, workSpaceDir string, no string, cmd *cobra.Command) error { |
| 423 | |
| 424 | fflags := cmd.Flags() |
| 425 | userName, _ := fflags.GetString(Flags_ServerUserName) |
| 426 | if instance.IsCloned(workSpaceDir) { |
| 427 | SmartIDELog.Info(i18n.GetInstance().Common.Info_gitrepo_cloned) |
| 428 | return nil |
| 429 | } |
| 430 | |
| 431 | if strings.TrimSpace(gitCloneUrl) == "" { |
| 432 | SmartIDELog.Error(i18n.GetInstance().Common.Err_sshremote_param_repourl_none) |
| 433 | } |
| 434 | if workSpaceDir == "" { |
| 435 | workSpaceDir = GetRepoName(gitCloneUrl) |
| 436 | } |
| 437 | |
| 438 | // git repo check |
| 439 | repoUrl := GIT.GetRepositoryUrl(gitCloneUrl) |
| 440 | if repoUrl != "" { |
| 441 | command := GIT.GetCommand4RepositoryUrl(repoUrl) |
| 442 | result, err := instance.ExeSSHCommand(command) |
| 443 | if err != nil { |
| 444 | return err |
| 445 | } |
| 446 | httpCode, _ := strconv.Atoi(result) |
| 447 | customErr := GIT.CheckError4RepositoryUrl(gitCloneUrl, httpCode) |
| 448 | if customErr != nil { |
| 449 | if customErr.GitRepoAccessStatus == GitRepoStatusEnum_NotExists { |
| 450 | return customErr |
| 451 | } else { |
| 452 | SmartIDELog.Warning(customErr.Error()) |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // 执行clone |
| 458 | //gitDirPath := strings.Replace(FilePahtJoin4Linux(workSpaceDir, ".git"), "~/", "", -1) // 把路径变成 “a/b/c” 的形式,不支持 “./a/b/c”、“~/a/b/c”、“./a/b/c” |
| 459 | GIT_SSH_COMMAND := fmt.Sprintf(`GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_%s_%s -o IdentitiesOnly=yes'`, userName, no) |
| 460 | |
| 461 | cloneCommand := fmt.Sprintf(`%s git clone %v %v`, |
| 462 | GIT_SSH_COMMAND, gitCloneUrl, workSpaceDir) // .git 文件如果不存在,在需要git clone |
| 463 | err := instance.ExecSSHCommandRealTimeFunc(cloneCommand, func(output string) error { |
| 464 | if strings.Contains(output, "error") || strings.Contains(output, "fatal") { |
| 465 | if strings.Contains(output, "git credential-store") && strings.Contains(output, "Syntax error: Unterminated quoted string") { |
| 466 | |
| 467 | } else { |
| 468 | return errors.New(output) |
| 469 | } |
| 470 | |
| 471 | } |
| 472 | |
| 473 | return nil |
| 474 | }) |
| 475 | |
| 476 | // log |
| 477 | if err == nil { |
| 478 | SmartIDELog.Info(i18n.GetInstance().Common.Info_gitrepo_clone_done) |
| 479 | } |
no test coverage detected