Exemple #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
}
Exemple #2
0
//获取项目指定branch的commit信息,包括id和message
func GitFileCommitids(projectName, branchName, filepath string) (commitInfos []CommitInfo, err error) {
	deployDir := config.GitDeployDir + projectName

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

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

	realFilePath := path.Join(deployDir, filepath)

	if ok := util.IsExist(realFilePath); !ok {
		err = util.NewError("File[%s] Not Exist, Please Check", filepath)
		return
	}

	args := [][]string{
		strings.Split(fmt.Sprintf("--git-dir=%s --work-tree=%s log --pretty=format:\"%%H:%%s\" %s %s", gitDir, deployDir, branchName, realFilePath), " "),
	}

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

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

	infos := strings.Split(msg, "\n")

	for _, info := range infos {
		ss := strings.Split(strings.Trim(info, "\""), ":")
		if len(ss) != 2 {
			err = util.NewError("Commit Info [%s] Split Error", info)
			return
		}
		commitInfos = append(commitInfos, CommitInfo{Id: ss[0], Msg: ss[1]})
	}

	return
}
Exemple #3
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
}
Exemple #4
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
}