OSDN Git Service

Git DLL get commit information basic work.
[tortoisegit/TortoiseGitJp.git] / ext / gitdll / gitdll.c
1 // gitdll.cpp : Defines the exported functions for the DLL application.\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "gitdll.h"\r
6 #include "cache.h"\r
7 #include "commit.h"\r
8 #if 0\r
9 \r
10 // This is an example of an exported variable\r
11 GITDLL_API int ngitdll=0;\r
12 \r
13 // This is an example of an exported function.\r
14 GITDLL_API int fngitdll(void)\r
15 {\r
16         return 42;\r
17 }\r
18 \r
19 // This is the constructor of a class that has been exported.\r
20 // see gitdll.h for the class definition\r
21 Cgitdll::Cgitdll()\r
22 {\r
23         return;\r
24 }\r
25 #endif\r
26 \r
27 #define MAX_ERROR_STR_SIZE 512\r
28 char g_last_error[MAX_ERROR_STR_SIZE]={0};\r
29 \r
30 char * get_git_last_error()\r
31 {\r
32         return g_last_error;\r
33 }\r
34 \r
35 static void die_dll(const char *err, va_list params)\r
36 {\r
37         memset(g_last_error,0,MAX_ERROR_STR_SIZE);\r
38         vsnprintf(g_last_error, MAX_ERROR_STR_SIZE-1, err, params);     \r
39 }\r
40 \r
41 void dll_entry()\r
42 {\r
43         set_die_routine(die_dll);\r
44 }\r
45 \r
46 int git_get_sha1(const char *name, GIT_HASH sha1)\r
47 {\r
48         return get_sha1(name,sha1);\r
49 }\r
50 \r
51 static int convert_slash(char * path)\r
52 {\r
53         while(*path)\r
54         {\r
55                 if(*path == '\\' )\r
56                         *path = '/';\r
57                 path++;\r
58         }\r
59 }\r
60 \r
61 int git_init()\r
62 {\r
63         char *home;\r
64         char path[MAX_PATH+1];\r
65         char *prefix;\r
66         size_t homesize,size,httpsize;\r
67 \r
68         // set HOME if not set already\r
69         getenv_s(&homesize, NULL, 0, "HOME");\r
70         if (!homesize)\r
71         {\r
72                 _dupenv_s(&home,&size,"USERPROFILE"); \r
73                 _putenv_s("HOME",home);\r
74                 free(home);\r
75         }\r
76         GetModuleFileName(NULL, path, MAX_PATH);\r
77         convert_slash(path);\r
78 \r
79         git_extract_argv0_path(path);\r
80         prefix = setup_git_directory();\r
81         return git_config(git_default_config, NULL);\r
82 }\r
83 \r
84 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR *author, char *pbuff)\r
85 {\r
86         char *end;\r
87 \r
88         author->Name=pbuff;\r
89         end=strchr(pbuff,'<');\r
90         if( end == 0)\r
91         {\r
92                 return -1;\r
93         }\r
94         author->NameSize = end - pbuff - 1;\r
95 \r
96         pbuff = end +1;\r
97         end = strchr(pbuff, '>');\r
98         if( end == 0)\r
99                 return -1;\r
100 \r
101         author->Email = pbuff ;\r
102         author->EmailSize = end - pbuff;\r
103 \r
104         pbuff = end + 2;\r
105 \r
106         author->Date = atol(pbuff);\r
107         end =  strchr(pbuff, ' ');\r
108         if( end == 0 )\r
109                 return -1;\r
110 \r
111         pbuff=end;\r
112         author->TimeZone = atol(pbuff);\r
113 \r
114         return 0;\r
115 }\r
116 \r
117 static int git_parse_commit(GIT_COMMIT *commit)\r
118 {\r
119         int ret = 0;\r
120         char *pbuf;\r
121         char *end;\r
122         struct commit *p;\r
123 \r
124         p= (struct commit *)commit->m_pGitCommit;\r
125 \r
126         memcpy(commit->m_hash,p->object.sha1,GIT_HASH_SIZE);\r
127 \r
128         if(p->buffer == NULL)\r
129                 return -1;\r
130 \r
131         pbuf = p->buffer;\r
132         while(pbuf)\r
133         {\r
134                 if( strncmp(pbuf,"author",6) == 0)\r
135                 {\r
136                         ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);\r
137                         if(ret)\r
138                                 return ret;\r
139                 }\r
140                 if( strncmp(pbuf, "committer",9) == 0)\r
141                 {\r
142                         ret =  git_parse_commit_author(&commit->m_Committer,pbuf + 10);\r
143                         if(ret)\r
144                                 return ret;\r
145 \r
146                         pbuf = strchr(pbuf,'\n');\r
147                         if(pbuf == NULL)\r
148                                 return -1;\r
149 \r
150                         while((*pbuf) && (*pbuf == '\n'))\r
151                                 pbuf ++;\r
152 \r
153                         commit->m_Subject=pbuf;\r
154                         end = strchr(pbuf,'\n');\r
155                         if( end == 0)\r
156                                 commit->m_SubjectSize = strlen(pbuf);\r
157                         else\r
158                         {\r
159                                 commit->m_SubjectSize = end - pbuf;\r
160                                 pbuf = end +1;\r
161                                 commit->m_Body = pbuf;\r
162                                 commit->m_BodySize = strlen(pbuf);\r
163                                 return 0;\r
164                         }\r
165 \r
166                 }\r
167 \r
168                 pbuf = strchr(pbuf,'\n');\r
169                 if(pbuf)\r
170                         pbuf ++;\r
171         }\r
172 \r
173 }\r
174 \r
175 int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash)\r
176 {\r
177         int ret = 0;\r
178         \r
179         struct commit *p = (struct commit*)commit;\r
180         commit->m_pGitCommit = p = lookup_commit(hash);\r
181 \r
182         if(commit == NULL)\r
183                 return -1;\r
184         \r
185         if(p == NULL)\r
186                 return -1;\r
187         \r
188         ret = parse_commit(p);\r
189         if( ret )\r
190                 return ret;\r
191 \r
192         return git_parse_commit(commit);\r
193 }\r
194 \r