1 // gitdll.cpp : Defines the exported functions for the DLL application.
5 #include "git-compat-util.h"
15 const char git_version_string
[] = GIT_VERSION
;
20 // This is an example of an exported variable
21 GITDLL_API
int ngitdll
=0;
23 // This is an example of an exported function.
24 GITDLL_API
int fngitdll(void)
29 // This is the constructor of a class that has been exported.
30 // see gitdll.h for the class definition
37 extern char g_last_error
[];
40 char * get_git_last_error()
45 extern void die_dll(const char *err
, va_list params
);
49 set_die_routine(die_dll
);
52 int git_get_sha1(const char *name
, GIT_HASH sha1
)
54 return get_sha1(name
,sha1
);
57 static int convert_slash(char * path
)
70 char path
[MAX_PATH
+1];
73 size_t homesize
,size
,httpsize
;
76 _setmode(_fileno(stdin
), _O_BINARY
);
77 _setmode(_fileno(stdout
), _O_BINARY
);
78 _setmode(_fileno(stderr
), _O_BINARY
);
80 // set HOME if not set already
81 getenv_s(&homesize
, NULL
, 0, "HOME");
84 _dupenv_s(&home
,&size
,"USERPROFILE");
85 _putenv_s("HOME",home
);
88 GetModuleFileName(NULL
, path
, MAX_PATH
);
91 git_extract_argv0_path(path
);
92 g_prefix
= prefix
= setup_git_directory();
93 ret
= git_config(git_default_config
, NULL
);
97 _putenv_s("HOME","");/* clear home evironment to avoid affact third part software*/
103 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR
*author
, char *pbuff
)
108 end
=strchr(pbuff
,'<');
113 author
->NameSize
= end
- pbuff
- 1;
116 end
= strchr(pbuff
, '>');
120 author
->Email
= pbuff
;
121 author
->EmailSize
= end
- pbuff
;
125 author
->Date
= atol(pbuff
);
126 end
= strchr(pbuff
, ' ');
131 author
->TimeZone
= atol(pbuff
);
136 int git_parse_commit(GIT_COMMIT
*commit
)
143 p
= (struct commit
*)commit
->m_pGitCommit
;
145 memcpy(commit
->m_hash
,p
->object
.sha1
,GIT_HASH_SIZE
);
147 commit
->m_Encode
= NULL
;
148 commit
->m_EncodeSize
= 0;
150 if(p
->buffer
== NULL
)
156 if( strncmp(pbuf
,"author",6) == 0)
158 ret
= git_parse_commit_author(&commit
->m_Author
,pbuf
+ 7);
162 if( strncmp(pbuf
, "committer",9) == 0)
164 ret
= git_parse_commit_author(&commit
->m_Committer
,pbuf
+ 10);
168 pbuf
= strchr(pbuf
,'\n');
172 while((*pbuf
) && (*pbuf
== '\n'))
175 if( strncmp(pbuf
, "encoding",8) == 0 )
178 commit
->m_Encode
=pbuf
;
179 end
= strchr(pbuf
,'\n');
180 commit
->m_EncodeSize
=end
-pbuf
;
183 while((*pbuf
) && (*pbuf
== '\n'))
186 commit
->m_Subject
=pbuf
;
187 end
= strchr(pbuf
,'\n');
189 commit
->m_SubjectSize
= strlen(pbuf
);
192 commit
->m_SubjectSize
= end
- pbuf
;
194 commit
->m_Body
= pbuf
;
195 commit
->m_BodySize
= strlen(pbuf
);
201 pbuf
= strchr(pbuf
,'\n');
208 int git_get_commit_from_hash(GIT_COMMIT
*commit
, GIT_HASH hash
)
214 memset(commit
,0,sizeof(GIT_COMMIT
));
216 commit
->m_pGitCommit
= p
= lookup_commit(hash
);
224 ret
= parse_commit(p
);
228 return git_parse_commit(commit
);
231 int git_get_commit_first_parent(GIT_COMMIT
*commit
,GIT_COMMIT_LIST
*list
)
233 struct commit
*p
= commit
->m_pGitCommit
;
238 *list
= (GIT_COMMIT_LIST
*)p
->parents
;
241 int git_get_commit_next_parent(GIT_COMMIT_LIST
*list
, GIT_HASH hash
)
243 struct commit_list
*l
= *(struct commit_list
**)list
;
244 if(list
== NULL
|| l
==NULL
)
248 memcpy(hash
, l
->item
->object
.sha1
, GIT_HASH_SIZE
);
250 *list
= (GIT_COMMIT_LIST
*)l
->next
;
256 int git_free_commit(GIT_COMMIT
*commit
)
258 struct commit
*p
= commit
->m_pGitCommit
;
261 free_commit_list(p
->parents
);
271 memset(commit
,0,sizeof(GIT_COMMIT
));
275 char **strtoargv(char *arg
, int *size
)
297 argv
=malloc(strlen(arg
)+1 + (count
+2)*sizeof(void*));
298 p
=(char*)(argv
+count
+2);
307 while(*arg
&& *arg
!= '"')
318 while(*arg
&& *arg
!=' ')
329 int git_open_log(GIT_LOG
* handle
, char * arg
)
331 struct rev_info
*p_Rev
;
338 unsigned int obj_size
= get_max_object_index();
339 for(i
=0; i
<obj_size
; i
++)
341 struct object
*ob
= get_indexed_object(i
);
347 argv
= strtoargv(arg
,&argc
);
349 p_Rev
= malloc(sizeof(struct rev_info
));
350 memset(p_Rev
,0,sizeof(struct rev_info
));
355 init_revisions(p_Rev
, g_prefix
);
358 cmd_log_init(argc
, argv
, g_prefix
,p_Rev
);
360 p_Rev
->pPrivate
= argv
;
365 int git_get_log_firstcommit(GIT_LOG handle
)
367 return prepare_revision_walk(handle
);
370 int git_get_log_estimate_commit_count(GIT_LOG handle
)
372 struct rev_info
*p_Rev
;
373 p_Rev
=(struct rev_info
*)handle
;
375 return estimate_commit_count(p_Rev
, p_Rev
->commits
);
378 int git_get_log_nextcommit(GIT_LOG handle
, GIT_COMMIT
*commit
)
385 memset(commit
, 0, sizeof(GIT_COMMIT
));
387 commit
->m_pGitCommit
= get_revision(handle
);
388 if( commit
->m_pGitCommit
== NULL
)
391 ret
=git_parse_commit(commit
);
398 int git_close_log(GIT_LOG handle
)
402 struct rev_info
*p_Rev
;
403 p_Rev
=(struct rev_info
*)handle
;
405 free(p_Rev
->pPrivate
);
412 int git_open_diff(GIT_DIFF
*diff
, char * arg
)
414 struct rev_info
*p_Rev
;
420 argv
= strtoargv(arg
,&argc
);
422 p_Rev
= malloc(sizeof(struct rev_info
));
423 memset(p_Rev
,0,sizeof(struct rev_info
));
425 p_Rev
->pPrivate
= argv
;
426 *diff
= (GIT_DIFF
)p_Rev
;
428 init_revisions(p_Rev
, g_prefix
);
429 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
432 argc
= setup_revisions(argc
, argv
, p_Rev
, NULL
);
436 int git_close_diff(GIT_DIFF handle
)
438 git_diff_flush(handle
);
441 struct rev_info
*p_Rev
;
442 p_Rev
=(struct rev_info
*)handle
;
444 free(p_Rev
->pPrivate
);
449 int git_diff_flush(GIT_DIFF diff
)
451 struct diff_queue_struct
*q
= &diff_queued_diff
;
452 struct rev_info
*p_Rev
;
454 p_Rev
= (struct rev_info
*)diff
;
459 for (i
= 0; i
< q
->nr
; i
++)
460 diff_free_filepair(q
->queue
[i
]);
466 q
->nr
= q
->alloc
= 0;
469 if (p_Rev
->diffopt
.close_file
)
470 fclose(p_Rev
->diffopt
.close_file
);
472 free_diffstat_info(&p_Rev
->diffstat
);
475 int git_root_diff(GIT_DIFF diff
, GIT_HASH hash
,GIT_FILE
*file
, int *count
)
478 struct rev_info
*p_Rev
;
480 struct diff_queue_struct
*q
= &diff_queued_diff
;
482 p_Rev
= (struct rev_info
*)diff
;
484 ret
=diff_root_tree_sha1(hash
, "", &p_Rev
->diffopt
);
489 diffcore_std(&p_Rev
->diffopt
);
491 memset(&p_Rev
->diffstat
, 0, sizeof(struct diffstat_t
));
492 for (i
= 0; i
< q
->nr
; i
++) {
493 struct diff_filepair
*p
= q
->queue
[i
];
494 //if (check_pair_status(p))
495 diff_flush_stat(p
, &p_Rev
->diffopt
, &p_Rev
->diffstat
);
506 int git_diff(GIT_DIFF diff
, GIT_HASH hash1
, GIT_HASH hash2
, GIT_FILE
* file
, int *count
)
508 struct rev_info
*p_Rev
;
511 struct diff_queue_struct
*q
= &diff_queued_diff
;
513 p_Rev
= (struct rev_info
*)diff
;
515 ret
= diff_tree_sha1(hash1
,hash2
,"",&p_Rev
->diffopt
);
519 diffcore_std(&p_Rev
->diffopt
);
521 memset(&p_Rev
->diffstat
, 0, sizeof(struct diffstat_t
));
522 for (i
= 0; i
< q
->nr
; i
++) {
523 struct diff_filepair
*p
= q
->queue
[i
];
524 //if (check_pair_status(p))
525 diff_flush_stat(p
, &p_Rev
->diffopt
, &p_Rev
->diffstat
);
535 int git_get_diff_file(GIT_DIFF diff
,GIT_FILE file
,int i
, char **newname
, char ** oldname
, int *status
, int *IsBin
, int *inc
, int *dec
)
537 struct diff_queue_struct
*q
= &diff_queued_diff
;
538 struct rev_info
*p_Rev
;
539 p_Rev
= (struct rev_info
*)diff
;
541 q
= (struct diff_queue_struct
*)file
;
548 *newname
= q
->queue
[i
]->two
->path
;
551 *oldname
= q
->queue
[i
]->one
->path
;
554 *status
= q
->queue
[i
]->status
;
556 if(p_Rev
->diffstat
.files
)
559 for(j
=0;j
<p_Rev
->diffstat
.nr
;j
++)
561 if(strcmp(*newname
,p_Rev
->diffstat
.files
[j
]->name
)==0)
564 if( j
== p_Rev
->diffstat
.nr
)
572 *IsBin
= p_Rev
->diffstat
.files
[j
]->is_binary
;
574 *inc
= p_Rev
->diffstat
.files
[j
]->added
;
576 *dec
= p_Rev
->diffstat
.files
[j
]->deleted
;
587 int git_read_tree(GIT_HASH hash
,read_tree_fn_t fn
, void *context
)
591 reprepare_packed_git();
592 root
= parse_tree_indirect(hash
);
599 ret
= read_tree_recursive(root
,NULL
,NULL
,0,NULL
,fn
,context
);
604 int git_add_exclude(const char *string
, const char *base
,
605 int baselen
, struct exclude_list
*which
)
607 add_exclude(string
, base
, baselen
, which
);
611 int git_create_exclude_list(EXCLUDE_LIST
*which
)
613 *which
= malloc(sizeof(struct exclude_list
));
614 memset(*which
,0,sizeof(struct exclude_list
));
618 int git_free_exclude_list(EXCLUDE_LIST which
)
621 struct exclude_list
*p
= (struct exclude_list
*) which
;
623 for(i
=0; i
<p
->nr
;i
++)
625 free(p
->excludes
[i
]);
631 int git_check_excluded_1(const char *pathname
,
632 int pathlen
, const char *basename
, int *dtype
,
635 return excluded_1(pathname
, pathlen
, basename
,dtype
,el
);