git 相关操作
(sshRemote common.SSHRemote, workspaceInfo workspace.WorkspaceInfo, cmd *cobra.Command)
| 478 | |
| 479 | // git 相关操作 |
| 480 | func GitCloneAndCheckoutBranch(sshRemote common.SSHRemote, workspaceInfo workspace.WorkspaceInfo, cmd *cobra.Command) error { |
| 481 | // 执行 git clone |
| 482 | actualGitRepoUrl := workspaceInfo.GitCloneRepoUrl |
| 483 | if workspaceInfo.GitRepoAuthType == workspace.GitRepoAuthType_Basic { |
| 484 | var err error |
| 485 | actualGitRepoUrl, err = |
| 486 | common.AddUsernamePassword4ActualGitRpoUrl(actualGitRepoUrl, workspaceInfo.GitUserName, workspaceInfo.GitPassword) |
| 487 | if err != nil { |
| 488 | return err |
| 489 | } |
| 490 | } |
| 491 | err := sshRemote.GitClone(actualGitRepoUrl, workspaceInfo.WorkingDirectoryPath, common.SmartIDELog.Ws_id, cmd) |
| 492 | if err != nil { |
| 493 | return err |
| 494 | } |
| 495 | checkoutCommand := "" |
| 496 | isSSHClone := strings.Index(workspaceInfo.GitCloneRepoUrl, "git@") == 0 |
| 497 | fflags := cmd.Flags() |
| 498 | userName, _ := fflags.GetString("serverusername") |
| 499 | GIT_SSH_COMMAND := fmt.Sprintf(`GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_%s_%s -o IdentitiesOnly=yes'`, |
| 500 | userName, common.SmartIDELog.Ws_id) |
| 501 | |
| 502 | // git checkout |
| 503 | if isSSHClone { |
| 504 | checkoutCommand = fmt.Sprintf("%s git fetch ", GIT_SSH_COMMAND) |
| 505 | if workspaceInfo.GitBranch != "" { |
| 506 | checkoutCommand += fmt.Sprintf("&& %s git checkout ", GIT_SSH_COMMAND) + workspaceInfo.GitBranch |
| 507 | } else { // 有可能当前目录所处的分支非主分支 |
| 508 | // 获取分支列表,确认主分支是master 还是 main |
| 509 | command := fmt.Sprintf("cd %v && %s git branch", workspaceInfo.WorkingDirectoryPath, GIT_SSH_COMMAND) |
| 510 | output, _ := sshRemote.ExeSSHCommand(command) |
| 511 | branches := strings.Split(output, "\n") |
| 512 | //isContainMaster := false |
| 513 | for _, branch := range branches { |
| 514 | if strings.Index(branch, "*") == 0 { |
| 515 | checkoutCommand += fmt.Sprintf("&& %s git checkout ", GIT_SSH_COMMAND) + branch[2:] |
| 516 | } |
| 517 | |
| 518 | } |
| 519 | |
| 520 | } |
| 521 | } else { |
| 522 | checkoutCommand = "git fetch " |
| 523 | if workspaceInfo.GitBranch != "" { |
| 524 | checkoutCommand += "&& git checkout " + workspaceInfo.GitBranch |
| 525 | } else { // 有可能当前目录所处的分支非主分支 |
| 526 | // 获取分支列表,确认主分支是master 还是 main |
| 527 | output, _ := sshRemote.ExeSSHCommand(fmt.Sprintf("cd %v && git branch", workspaceInfo.WorkingDirectoryPath)) |
| 528 | branches := strings.Split(output, "\n") |
| 529 | //isContainMaster := false |
| 530 | for _, branch := range branches { |
| 531 | if strings.Index(branch, "*") == 0 { |
| 532 | checkoutCommand += "&& git checkout " + branch[2:] |
| 533 | } |
| 534 | |
| 535 | } |
| 536 | |
| 537 | } |
no test coverage detected