From: Frank Li Date: Tue, 29 Dec 2009 05:41:12 +0000 (+0800) Subject: Git DLL get commit information basic work. X-Git-Url: http://git.sourceforge.jp/view?p=tortoisegit%2FTortoiseGitJp.git;a=commitdiff_plain;h=d128d76fe5e119e60f2307a56fba37211cc36c62 Git DLL get commit information basic work. Signed-off-by: Frank Li --- diff --git a/ext/gitdll/gitdll.c b/ext/gitdll/gitdll.c index 47cdc52..4ef95d4 100644 --- a/ext/gitdll/gitdll.c +++ b/ext/gitdll/gitdll.c @@ -4,6 +4,7 @@ #include "stdafx.h" #include "gitdll.h" #include "cache.h" +#include "commit.h" #if 0 // This is an example of an exported variable @@ -42,7 +43,7 @@ void dll_entry() set_die_routine(die_dll); } -int git_get_sha1(const char *name, unsigned char *sha1) +int git_get_sha1(const char *name, GIT_HASH sha1) { return get_sha1(name,sha1); } @@ -56,6 +57,7 @@ static int convert_slash(char * path) path++; } } + int git_init() { char *home; @@ -76,5 +78,117 @@ int git_init() git_extract_argv0_path(path); prefix = setup_git_directory(); - git_config(git_default_config, NULL); -} \ No newline at end of file + return git_config(git_default_config, NULL); +} + +static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR *author, char *pbuff) +{ + char *end; + + author->Name=pbuff; + end=strchr(pbuff,'<'); + if( end == 0) + { + return -1; + } + author->NameSize = end - pbuff - 1; + + pbuff = end +1; + end = strchr(pbuff, '>'); + if( end == 0) + return -1; + + author->Email = pbuff ; + author->EmailSize = end - pbuff; + + pbuff = end + 2; + + author->Date = atol(pbuff); + end = strchr(pbuff, ' '); + if( end == 0 ) + return -1; + + pbuff=end; + author->TimeZone = atol(pbuff); + + return 0; +} + +static int git_parse_commit(GIT_COMMIT *commit) +{ + int ret = 0; + char *pbuf; + char *end; + struct commit *p; + + p= (struct commit *)commit->m_pGitCommit; + + memcpy(commit->m_hash,p->object.sha1,GIT_HASH_SIZE); + + if(p->buffer == NULL) + return -1; + + pbuf = p->buffer; + while(pbuf) + { + if( strncmp(pbuf,"author",6) == 0) + { + ret = git_parse_commit_author(&commit->m_Author,pbuf + 7); + if(ret) + return ret; + } + if( strncmp(pbuf, "committer",9) == 0) + { + ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10); + if(ret) + return ret; + + pbuf = strchr(pbuf,'\n'); + if(pbuf == NULL) + return -1; + + while((*pbuf) && (*pbuf == '\n')) + pbuf ++; + + commit->m_Subject=pbuf; + end = strchr(pbuf,'\n'); + if( end == 0) + commit->m_SubjectSize = strlen(pbuf); + else + { + commit->m_SubjectSize = end - pbuf; + pbuf = end +1; + commit->m_Body = pbuf; + commit->m_BodySize = strlen(pbuf); + return 0; + } + + } + + pbuf = strchr(pbuf,'\n'); + if(pbuf) + pbuf ++; + } + +} + +int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash) +{ + int ret = 0; + + struct commit *p = (struct commit*)commit; + commit->m_pGitCommit = p = lookup_commit(hash); + + if(commit == NULL) + return -1; + + if(p == NULL) + return -1; + + ret = parse_commit(p); + if( ret ) + return ret; + + return git_parse_commit(commit); +} + diff --git a/ext/gitdll/gitdll.h b/ext/gitdll/gitdll.h index f887712..3cec3d7 100644 --- a/ext/gitdll/gitdll.h +++ b/ext/gitdll/gitdll.h @@ -25,10 +25,71 @@ public: }; #endif +#define GIT_HASH_SIZE 20 + +typedef unsigned char GIT_HASH[GIT_HASH_SIZE]; +typedef unsigned int GIT_HANDLE; +typedef unsigned int GIT_LOG; + +typedef unsigned int GIT_DIFF; +typedef unsigned int GIT_FILE; + +struct GIT_COMMIT_AUTHOR +{ + char *Name; + int NameSize; + char *Email; + int EmailSize; + int Date; + int TimeZone; + +}; +typedef struct GIT_COMMIT_DATA +{ + GIT_HASH m_hash; + struct GIT_COMMIT_AUTHOR m_Author; + struct GIT_COMMIT_AUTHOR m_Committer; + char * m_Subject; + int m_SubjectSize; + char * m_Body; + int m_BodySize; + void * m_pGitCommit; /** internal used */ + +} GIT_COMMIT; + + GITDLL_API int ngitdll; GITDLL_API int fngitdll(void); - +/** + * Get Git Last Error string. + */ GITDLL_API char * get_git_last_error(); -GITDLL_API int git_get_sha1(const char *name, unsigned char *sha1); -GITDLL_API int git_init(); \ No newline at end of file +/** + * Get hash value. + * @param name [IN] Reference name, such as HEAD, master, ... + * @param sha1 [OUT] char[20] hash value. Caller prepare char[20] buffer. + * @return 0 success. + */ +GITDLL_API int git_get_sha1(const char *name, GIT_HASH sha1); +/** + * Init git dll + * @remark, this function must be call before other function. + * @return 0 success + */ +GITDLL_API int git_init(); + +GITDLL_API int git_open_log(GIT_LOG * handle, char * arg); +GITDLL_API int git_get_log_count(GIT_LOG handle); +GITDLL_API int git_get_log_firstcommit(GIT_LOG handle, GIT_COMMIT *commit); +GITDLL_API int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit, int skip); +GITDLL_API int git_close_log(GIT_LOG handle); + +GITDLL_API int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash); + +GITDLL_API int git_get_diff(GIT_COMMIT commit, GIT_DIFF *diff); +GITDLL_API int git_get_diff_firstfile(GIT_DIFF diff, GIT_FILE * file); +GITDLL_API int git_get_diff_nextfile(GIT_DIFF diff, GIT_FILE *file); +GITDLL_API int git_get_diff_status(GIT_DIFF diff, int * status); +GITDLL_API int git_get_diff_stat(GIT_FILE file, int *inc, int *dec, int *mode); +GITDLL_API int git_get_diff_file(GIT_FILE file, char *newname, int newsize, char *oldname, int oldsize, int *mode); \ No newline at end of file diff --git a/ext/gitdlltest/gitdlltest.cpp b/ext/gitdlltest/gitdlltest.cpp index b50f130..7298439 100644 --- a/ext/gitdlltest/gitdlltest.cpp +++ b/ext/gitdlltest/gitdlltest.cpp @@ -4,12 +4,31 @@ #include "stdafx.h" #include "gitdll.h" +int output(int ret, char * name) +{ + if(ret) + printf("Fail \t%s\r\n",name); + else + printf("Success\t%s\r\n",name); + return 0; +} int _tmain(int argc, _TCHAR* argv[]) { - unsigned char hash[20]; + GIT_HASH hash; + GIT_COMMIT commit; + char *buf; + int size; + memset(&hash,0,sizeof(GIT_HASH)); int ret; ret=git_init(); + output(ret,"git_init"); ret=git_get_sha1("master",hash); + output(ret,"git_get_sha1"); + ret=git_get_sha1("head",hash); + output(ret,"git_get_sha1"); + ret=git_get_commit_from_hash(&commit, hash); + output(ret,"git_get_commit_from_hash"); + return ret; }