Beispiel #1
0
func GitCloneToDir(remoteUrl, projectName string) (msg string, err error) {
	if err = util.PingRemote(strings.Split(remoteUrl, ":")[0]); err != nil {
		err = util.NewError(fmt.Sprintf("Cannot SSH To Remote. Check ssh-key : %s", err.Error()))
		return
	}

	deployDir := config.GitDeployDir + projectName

	if ok, _ := util.IsDir(deployDir); ok {
		err = util.NewError("Project[%s] Directory Existed", projectName)
		return
	}

	err = os.MkdirAll(path.Dir(deployDir), 0700)
	if err != nil {
		err = util.NewError("Project[%s] Directory Mkdir Failed: %s", projectName, err.Error())
		return
	}

	so, se, err := util.RunCmd(config.GIT, "clone", remoteUrl, deployDir)

	if len(se) != 0 {
		err = util.NewError("Command Git Clone exec stderr: %s", se)
		return
	}

	msg = so

	return
}
Beispiel #2
0
/*
	git remote set-url origin remoteUrl
	git checkout branchname
	git add -A
	git commit -am commitmessage
	git push origin branchname
*/
func GitPushToRemote(remoteUrl, projectName, branchName, commitMsg string) (msg string, err error) {

	if ok := strings.Contains(commitMsg, "|||"); ok {
		err = util.NewError("Commit Message Can't Contains [|||] String")
		return
	}

	if err = util.PingRemote(strings.Split(remoteUrl, ":")[0]); err != nil {
		err = util.NewError(fmt.Sprintf("Cannot SSH To Remote. Check ssh-key : %s", err.Error()))
		return
	}

	deployDir := config.GitDeployDir + projectName

	if ok, _ := util.IsDir(deployDir); !ok {
		err = util.NewError("Project[%s] Directory Not Existed", projectName)
		return
	}

	gitDir := path.Join(deployDir, ".git")

	args := [][]string{
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s remote set-url origin %s", gitDir, deployDir, remoteUrl), " "),
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s checkout %s", gitDir, deployDir, branchName), " "),
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s add -A", gitDir, deployDir), " "),
		strings.Split(fmt.Sprintf("--git-dir=%s|||--work-tree=%s|||commit|||-am|||%s", gitDir, deployDir, commitMsg), "|||"),
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s push origin %s", gitDir, deployDir, branchName), " "),
	}

	for _, arg := range args {
		so, se, e := util.RunCmd(config.GIT, arg...)
		msg += (so + se)
		if e != nil {
			err = e
			return
		}
	}
	return
}
Beispiel #3
0
/*
git-pull command 不支持 --work-tree参数,因此必须使用
git fetch origin
git merge origin/master
代替,本身merge的过程中也可能出现merge出错

git remote set-url origin remoteUrl
git checkout branchname
git fetch origin branchname
git merge origin/branchname
*/
func GitPullToDir(remoteUrl, projectName, branchName string) (msg string, err error) {
	if err = util.PingRemote(strings.Split(remoteUrl, ":")[0]); err != nil {
		err = util.NewError(fmt.Sprintf("Cannot SSH To Remote. Check ssh-key : %s", err.Error()))
		return
	}

	deployDir := config.GitDeployDir + projectName

	if ok, _ := util.IsDir(deployDir); !ok {
		err = util.NewError("Project[%s] Git Repository Not Existed", projectName)
		return
	}

	gitDir := path.Join(deployDir, ".git")

	args := [][]string{
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s remote set-url origin %s", gitDir, deployDir, remoteUrl), " "),
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s checkout %s", gitDir, deployDir, branchName), " "),
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s fetch origin %s", gitDir, deployDir, branchName), " "),
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s merge origin/%s", gitDir, deployDir, branchName), " "),
	}

	for _, arg := range args {
		so, se, e := util.RunCmd(config.GIT, arg...)
		msg += (so + se)
		if e != nil {
			err = e
			return
		}

		//		if len(se) != 0 {
		//			err = util.NewError("Command Git Pull exec stderr: %s", se)
		//			return
		//		}
	}
	return
}