From 4bc6a907df95ce1101ba7e3d30106a1f6a69794b Mon Sep 17 00:00:00 2001 From: Johan t Hart Date: Sun, 1 Feb 2009 23:47:05 +0100 Subject: [PATCH] CGit::Run() overloaded with another function to make command output callback possible --- src/Git/Git.cpp | 32 +++++++++++++++++++++++++------- src/Git/Git.h | 16 ++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Git/Git.cpp b/src/Git/Git.cpp index 6b880c1..df2ce46 100644 --- a/src/Git/Git.cpp +++ b/src/Git/Git.cpp @@ -106,6 +106,8 @@ static BOOL FindGitPath() #define MAX_DIRBUFFER 1000 +#define CALL_OUTPUT_READ_CHUNK_SIZE 1024 + CString CGit::ms_LastMsysGitDir; CGit g_Git; BOOL g_IsWingitDllload = TRUE; @@ -279,20 +281,18 @@ BOOL CGit::IsInitRepos() return FALSE; } -int CGit::Run(CString cmd,BYTE_VECTOR *vector) +int CGit::Run(CGitCall* pcall) { PROCESS_INFORMATION pi; HANDLE hRead; - if(RunAsync(cmd,&pi,&hRead)) + if(RunAsync(pcall->GetCmd(),&pi,&hRead)) return GIT_ERROR_CREATE_PROCESS; DWORD readnumber; - BYTE data; - while(ReadFile(hRead,&data,1,&readnumber,NULL)) + BYTE data[CALL_OUTPUT_READ_CHUNK_SIZE]; + while(ReadFile(hRead,data,CALL_OUTPUT_READ_CHUNK_SIZE,&readnumber,NULL)) { - //g_Buffer[readnumber]=0; - vector->push_back(data); -// StringAppend(output,g_Buffer,codes); + pcall->OnOutputData(data,readnumber); } @@ -310,7 +310,25 @@ int CGit::Run(CString cmd,BYTE_VECTOR *vector) CloseHandle(hRead); return exitcode; +} +class CGitCall_ByteVector : public CGitCall +{ +public: + CGitCall_ByteVector(CString cmd,BYTE_VECTOR* pvector):CGitCall(cmd),m_pvector(pvector){} + virtual bool OnOutputData(const BYTE* data, size_t size) + { + size_t oldsize=m_pvector->size(); + m_pvector->resize(m_pvector->size()+size); + memcpy(&*(m_pvector->begin()+oldsize),data,size); + return false; + } + BYTE_VECTOR* m_pvector; +}; +int CGit::Run(CString cmd,BYTE_VECTOR *vector) +{ + CGitCall_ByteVector call(cmd,vector); + return Run(&call); } int CGit::Run(CString cmd, CString* output,int code) { diff --git a/src/Git/Git.h b/src/Git/Git.h index 97bf1b6..b527790 100644 --- a/src/Git/Git.h +++ b/src/Git/Git.h @@ -4,6 +4,21 @@ #include "GitStatus.h" #include "GitAdminDir.h" +class CGitCall +{ +public: + CGitCall(CString cmd):m_Cmd(cmd){} + + CString GetCmd()const{return m_Cmd;} + + //When this function returns 'true' the git command should be aborted. + //This behavior is not implemented yet. + virtual bool OnOutputData(const BYTE* data, size_t size)=0; + +private: + CString m_Cmd; + +}; class CGit { @@ -19,6 +34,7 @@ public: int Run(CString cmd, CString* output,int code); int Run(CString cmd, BYTE_VECTOR *byte_array); + int Run(CGitCall* pcall); int RunAsync(CString cmd,PROCESS_INFORMATION *pi, HANDLE* hRead, CString *StdioFile=NULL); int RunLogFile(CString cmd, CString &filename); -- 2.11.0