简而言之,就是使用C++调用CMD控制器,实现只要输入git地址和分支名,则自动克隆项目到本地的功能

代码如下:

头文件:

#include "stdafx.h"

using namespace std;

class AutoGitPerform
//@description: basic use cmd to auto git something to local
{
public:
AutoGitPerform();
~AutoGitPerform();

void setClonePath(std::string clone_path);
void setBranchesName(std::string branch_name);
void runGitCmd();
std::vector<string> getAllBranches(); //获取所有分支信息 (未完成)
//bool downloadAllBranchesToLocal(); //下载所有分支到本地 (未完成)

private:
string m_clone_path; //git 项目路径
string m_branch_name;//分支名
string m_git_cmd_path;//总的命令
std::vector<string> m_all_branches_name; //所有分支名
};

实现cpp文件:

// cmdPerform.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "cmdPerform.h"
using namespace std;

string g_cmd_init_str = "cmd.exe /c "; //CMD初始字段拼接
string g_cmd_split_str = " && " ;//CMD命令分隔符
string g_cmd_exec_str = "cmd.exe /c ";

AutoGitPerform::AutoGitPerform()
{
m_git_cmd_path = "git clone ";
m_clone_path = "";
m_branch_name = "";
}

AutoGitPerform::~AutoGitPerform()
{

}

//配置下载http链接
void AutoGitPerform::setClonePath(std::string clone_path)
{
m_clone_path =clone_path;
}

//配置分支名
void AutoGitPerform::setBranchesName(std::string branch_name)
{
m_branch_name = branch_name;
}

//运行Git Clone功能
void AutoGitPerform::runGitCmd()
{
//
if (m_branch_name.size() > 1)
{
m_git_cmd_path.append(" -b ");
m_git_cmd_path.append(m_branch_name);
m_git_cmd_path.append(" ");
}
m_git_cmd_path.append(m_clone_path);

//if 分支 in
if (m_branch_name.size() > 1)
{
string str_mkdir = "";
str_mkdir.append("mkdir ");
str_mkdir.append(m_branch_name);
str_mkdir.append(g_cmd_split_str);
str_mkdir.append("cd ");
str_mkdir.append(m_branch_name);
str_mkdir.append(g_cmd_split_str);
str_mkdir.append(m_git_cmd_path);
g_cmd_exec_str.append(str_mkdir);
WinExec(g_cmd_exec_str.c_str(),SW_HIDE);
return;
}
g_cmd_exec_str.append(m_git_cmd_path);
WinExec(g_cmd_exec_str.c_str(),SW_HIDE);
g_cmd_exec_str = "cmd.exe /c ";
}

//还未解决
std::vector<string> AutoGitPerform::getAllBranches()
{
std::vector<string > ss;
return ss;
}

int _tmain(int argc, _TCHAR* argv[])
{
AutoGitPerform* curGit = new AutoGitPerform();
curGit->setClonePath("https://github.com/huifeng-kooboo/WebChat.git");
curGit->setBranchesName("ziqi");
curGit->runGitCmd(); //执行
delete curGit; //释放内存
return 0;
}

使用规则 :初始化类,设置克隆地址和分支即可

    AutoGitPerform* curGit = new AutoGitPerform();
    curGit->setClonePath("https://github.com/huifeng-kooboo/WebChat.git");
    curGit->setBranchesName("ziqi");
    curGit->runGitCmd(); //执行

 

知识点:WINEXEC()实质为异步操作~

目前 还存在一个问题,一是自动获取所有分支功能暂未实现 二是下载所有分支功能 也还没实现,待完善~