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
;
336 struct setup_revision_opt opt
;
339 unsigned int obj_size
= get_max_object_index();
340 for(i
=0; i
<obj_size
; i
++)
342 struct object
*ob
= get_indexed_object(i
);
348 argv
= strtoargv(arg
,&argc
);
350 p_Rev
= malloc(sizeof(struct rev_info
));
351 memset(p_Rev
,0,sizeof(struct rev_info
));
356 init_revisions(p_Rev
, g_prefix
);
359 memset(&opt
, 0, sizeof(opt
));
362 cmd_log_init(argc
, argv
, g_prefix
,p_Rev
,&opt
);
364 p_Rev
->pPrivate
= argv
;
369 int git_get_log_firstcommit(GIT_LOG handle
)
371 return prepare_revision_walk(handle
);
374 int git_get_log_estimate_commit_count(GIT_LOG handle
)
376 struct rev_info
*p_Rev
;
377 p_Rev
=(struct rev_info
*)handle
;
379 return estimate_commit_count(p_Rev
, p_Rev
->commits
);
382 int git_get_log_nextcommit(GIT_LOG handle
, GIT_COMMIT
*commit
)
389 memset(commit
, 0, sizeof(GIT_COMMIT
));
391 commit
->m_pGitCommit
= get_revision(handle
);
392 if( commit
->m_pGitCommit
== NULL
)
395 ret
=git_parse_commit(commit
);
402 int git_close_log(GIT_LOG handle
)
406 struct rev_info
*p_Rev
;
407 p_Rev
=(struct rev_info
*)handle
;
409 free(p_Rev
->pPrivate
);
416 int git_open_diff(GIT_DIFF
*diff
, char * arg
)
418 struct rev_info
*p_Rev
;
424 argv
= strtoargv(arg
,&argc
);
426 p_Rev
= malloc(sizeof(struct rev_info
));
427 memset(p_Rev
,0,sizeof(struct rev_info
));
429 p_Rev
->pPrivate
= argv
;
430 *diff
= (GIT_DIFF
)p_Rev
;
432 init_revisions(p_Rev
, g_prefix
);
433 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
436 argc
= setup_revisions(argc
, argv
, p_Rev
, NULL
);
440 int git_close_diff(GIT_DIFF handle
)
442 git_diff_flush(handle
);
445 struct rev_info
*p_Rev
;
446 p_Rev
=(struct rev_info
*)handle
;
448 free(p_Rev
->pPrivate
);
453 int git_diff_flush(GIT_DIFF diff
)
455 struct diff_queue_struct
*q
= &diff_queued_diff
;
456 struct rev_info
*p_Rev
;
458 p_Rev
= (struct rev_info
*)diff
;
463 for (i
= 0; i
< q
->nr
; i
++)
464 diff_free_filepair(q
->queue
[i
]);
470 q
->nr
= q
->alloc
= 0;
473 if (p_Rev
->diffopt
.close_file
)
474 fclose(p_Rev
->diffopt
.close_file
);
476 free_diffstat_info(&p_Rev
->diffstat
);
479 int git_root_diff(GIT_DIFF diff
, GIT_HASH hash
,GIT_FILE
*file
, int *count
)
482 struct rev_info
*p_Rev
;
484 struct diff_queue_struct
*q
= &diff_queued_diff
;
486 p_Rev
= (struct rev_info
*)diff
;
488 ret
=diff_root_tree_sha1(hash
, "", &p_Rev
->diffopt
);
493 diffcore_std(&p_Rev
->diffopt
);
495 memset(&p_Rev
->diffstat
, 0, sizeof(struct diffstat_t
));
496 for (i
= 0; i
< q
->nr
; i
++) {
497 struct diff_filepair
*p
= q
->queue
[i
];
498 //if (check_pair_status(p))
499 diff_flush_stat(p
, &p_Rev
->diffopt
, &p_Rev
->diffstat
);
510 int git_diff(GIT_DIFF diff
, GIT_HASH hash1
, GIT_HASH hash2
, GIT_FILE
* file
, int *count
)
512 struct rev_info
*p_Rev
;
515 struct diff_queue_struct
*q
= &diff_queued_diff
;
517 p_Rev
= (struct rev_info
*)diff
;
519 ret
= diff_tree_sha1(hash1
,hash2
,"",&p_Rev
->diffopt
);
523 diffcore_std(&p_Rev
->diffopt
);
525 memset(&p_Rev
->diffstat
, 0, sizeof(struct diffstat_t
));
526 for (i
= 0; i
< q
->nr
; i
++) {
527 struct diff_filepair
*p
= q
->queue
[i
];
528 //if (check_pair_status(p))
529 diff_flush_stat(p
, &p_Rev
->diffopt
, &p_Rev
->diffstat
);
539 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
)
541 struct diff_queue_struct
*q
= &diff_queued_diff
;
542 struct rev_info
*p_Rev
;
543 p_Rev
= (struct rev_info
*)diff
;
545 q
= (struct diff_queue_struct
*)file
;
552 *newname
= q
->queue
[i
]->two
->path
;
555 *oldname
= q
->queue
[i
]->one
->path
;
558 *status
= q
->queue
[i
]->status
;
560 if(p_Rev
->diffstat
.files
)
563 for(j
=0;j
<p_Rev
->diffstat
.nr
;j
++)
565 if(strcmp(*newname
,p_Rev
->diffstat
.files
[j
]->name
)==0)
568 if( j
== p_Rev
->diffstat
.nr
)
576 *IsBin
= p_Rev
->diffstat
.files
[j
]->is_binary
;
578 *inc
= p_Rev
->diffstat
.files
[j
]->added
;
580 *dec
= p_Rev
->diffstat
.files
[j
]->deleted
;
591 int git_read_tree(GIT_HASH hash
,read_tree_fn_t fn
, void *context
)
595 reprepare_packed_git();
596 root
= parse_tree_indirect(hash
);
603 ret
= read_tree_recursive(root
,NULL
,NULL
,0,NULL
,fn
,context
);
608 int git_add_exclude(const char *string
, const char *base
,
609 int baselen
, struct exclude_list
*which
)
611 add_exclude(string
, base
, baselen
, which
);
615 int git_create_exclude_list(EXCLUDE_LIST
*which
)
617 *which
= malloc(sizeof(struct exclude_list
));
618 memset(*which
,0,sizeof(struct exclude_list
));
622 int git_free_exclude_list(EXCLUDE_LIST which
)
625 struct exclude_list
*p
= (struct exclude_list
*) which
;
627 for(i
=0; i
<p
->nr
;i
++)
629 free(p
->excludes
[i
]);
635 int git_check_excluded_1(const char *pathname
,
636 int pathlen
, const char *basename
, int *dtype
,
639 return excluded_from_list(pathname
, pathlen
, basename
,dtype
,el
);