Fix dereferencing null pointer in git_close_log
[TortoiseGit.git] / ext / gitdll / gitdll.c
blob588ef2307d6ffea05386a0c9c2e69a1cec9b324c
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // gitdll.cpp : Defines the exported functions for the DLL application.
22 #include "stdafx.h"
23 #include "git-compat-util.h"
24 #include "msvc.h"
25 #include "gitdll.h"
26 #include "cache.h"
27 #include "commit.h"
28 #include "diff.h"
29 #include "revision.h"
30 #include "diffcore.h"
31 #include "dir.h"
32 #include "builtin.h"
33 #include "exec_cmd.h"
34 #include "cache.h"
35 #include "quote.h"
36 #include "run-command.h"
37 #include "mailmap.h"
39 #if 0
41 // This is an example of an exported variable
42 GITDLL_API int ngitdll=0;
44 // This is an example of an exported function.
45 GITDLL_API int fngitdll(void)
47 return 42;
50 // This is the constructor of a class that has been exported.
51 // see gitdll.h for the class definition
52 Cgitdll::Cgitdll()
54 return;
56 #endif
58 extern char g_last_error[];
59 void * g_prefix;
61 char * get_git_last_error()
63 return g_last_error;
66 extern void die_dll(const char *err, va_list params);
68 void dll_entry()
70 set_die_routine(die_dll);
73 int git_get_sha1(const char *name, GIT_HASH sha1)
75 return get_sha1(name,sha1);
78 static int convert_slash(char * path)
80 while(*path)
82 if(*path == '\\' )
83 *path = '/';
84 path++;
86 return 0;
89 int git_init()
91 char path[MAX_PATH+1];
92 char *prefix;
93 int ret;
94 size_t homesize;
96 _fmode = _O_BINARY;
97 _setmode(_fileno(stdin), _O_BINARY);
98 _setmode(_fileno(stdout), _O_BINARY);
99 _setmode(_fileno(stderr), _O_BINARY);
101 // set HOME if not set already
102 getenv_s(&homesize, NULL, 0, "HOME");
103 if (!homesize)
105 _wputenv_s(L"HOME", wget_windows_home_directory());
107 GetModuleFileName(NULL, path, MAX_PATH);
108 convert_slash(path);
110 git_extract_argv0_path(path);
111 g_prefix = prefix = setup_git_directory();
112 ret = git_config(git_default_config, NULL);
114 if (!homesize)
116 _putenv_s("HOME","");/* clear home evironment to avoid affact third part software*/
119 return ret;
122 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR *author, char *pbuff)
124 char *end;
126 author->Name=pbuff;
127 end=strchr(pbuff,'<');
128 if( end == 0)
130 return -1;
132 author->NameSize = end - pbuff - 1;
134 pbuff = end +1;
135 end = strchr(pbuff, '>');
136 if( end == 0)
137 return -1;
139 author->Email = pbuff ;
140 author->EmailSize = end - pbuff;
142 pbuff = end + 2;
144 author->Date = atol(pbuff);
145 end = strchr(pbuff, ' ');
146 if( end == 0 )
147 return -1;
149 pbuff=end;
150 author->TimeZone = atol(pbuff);
152 return 0;
155 int git_parse_commit(GIT_COMMIT *commit)
157 int ret = 0;
158 char *pbuf;
159 char *end;
160 struct commit *p;
162 p= (struct commit *)commit->m_pGitCommit;
164 memcpy(commit->m_hash,p->object.sha1,GIT_HASH_SIZE);
166 commit->m_Encode = NULL;
167 commit->m_EncodeSize = 0;
169 if(p->buffer == NULL)
170 return -1;
172 pbuf = p->buffer;
173 while(pbuf)
175 if (strncmp(pbuf, "author", 6) == 0)
177 ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);
178 if(ret)
179 return ret;
181 else if (strncmp(pbuf, "committer", 9) == 0)
183 ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10);
184 if(ret)
185 return ret;
187 pbuf = strchr(pbuf,'\n');
188 if(pbuf == NULL)
189 return -1;
191 else if (strncmp(pbuf, "encoding", 8) == 0)
193 pbuf += 9;
194 commit->m_Encode=pbuf;
195 end = strchr(pbuf,'\n');
196 commit->m_EncodeSize=end -pbuf;
199 // the headers end after the first empty line
200 else if (*pbuf == '\n')
202 pbuf++;
204 commit->m_Subject=pbuf;
205 end = strchr(pbuf,'\n');
206 if( end == 0)
207 commit->m_SubjectSize = strlen(pbuf);
208 else
210 commit->m_SubjectSize = end - pbuf;
211 pbuf = end +1;
212 commit->m_Body = pbuf;
213 commit->m_BodySize = strlen(pbuf);
214 return 0;
218 pbuf = strchr(pbuf,'\n');
219 if(pbuf)
220 pbuf ++;
222 return 0;
225 int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash)
227 int ret = 0;
229 struct commit *p;
231 memset(commit,0,sizeof(GIT_COMMIT));
233 commit->m_pGitCommit = p = lookup_commit(hash);
235 if(commit == NULL)
236 return -1;
238 if(p == NULL)
239 return -1;
241 ret = parse_commit(p);
242 if( ret )
243 return ret;
245 return git_parse_commit(commit);
248 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
250 struct commit *p = commit->m_pGitCommit;
252 if(list == NULL)
253 return -1;
255 *list = (GIT_COMMIT_LIST*)p->parents;
256 return 0;
258 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
260 struct commit_list *l = *(struct commit_list **)list;
261 if(list == NULL || l==NULL)
262 return -1;
264 if(hash)
265 memcpy(hash, l->item->object.sha1, GIT_HASH_SIZE);
267 *list = (GIT_COMMIT_LIST *)l->next;
268 return 0;
273 int git_free_commit(GIT_COMMIT *commit)
275 struct commit *p = commit->m_pGitCommit;
277 if( p->parents)
278 free_commit_list(p->parents);
280 if( p->buffer )
282 free(p->buffer);
283 p->buffer=NULL;
284 p->object.parsed=0;
285 p->parents=0;
286 p->tree=0;
288 memset(commit,0,sizeof(GIT_COMMIT));
289 return 0;
292 char **strtoargv(char *arg, int *size)
294 int count=0;
295 char *p=arg;
296 char **argv;
298 int i=0;
299 while(*p)
301 if(*p == '\\')
302 *p='/';
303 p++;
305 p=arg;
307 while(*p)
309 if(*p == ' ')
310 count ++;
311 p++;
314 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
315 p=(char*)(argv+count+2);
317 while(*arg)
319 if(*arg != ' ')
321 char space=' ';
322 argv[i]=p;
324 while(*arg)
326 if(*arg == '"')
328 arg++;
329 if(space == ' ')
330 space = '"';
331 else
332 space = ' ';
334 if((*arg == space) || (*arg == 0))
335 break;
337 *p++ = *arg++;
339 i++;
340 *p++=0;
342 if(*arg == 0)
343 break;
344 arg++;
346 argv[i]=NULL;
347 *size = i;
348 return argv;
350 int git_open_log(GIT_LOG * handle, char * arg)
352 struct rev_info *p_Rev;
353 char ** argv=0;
354 int argc=0;
355 unsigned int i=0;
356 struct setup_revision_opt opt;
358 /* clear flags */
359 unsigned int obj_size = get_max_object_index();
360 for(i =0; i<obj_size; i++)
362 struct object *ob= get_indexed_object(i);
363 if(ob)
364 ob->flags=0;
367 if(arg != NULL)
368 argv = strtoargv(arg,&argc);
370 p_Rev = malloc(sizeof(struct rev_info));
371 memset(p_Rev,0,sizeof(struct rev_info));
373 if(p_Rev == NULL)
374 return -1;
376 invalidate_ref_cache(NULL);
378 init_revisions(p_Rev, g_prefix);
379 p_Rev->diff = 1;
381 memset(&opt, 0, sizeof(opt));
382 opt.def = "HEAD";
384 cmd_log_init(argc, argv, g_prefix,p_Rev,&opt);
386 p_Rev->pPrivate = argv;
387 *handle = p_Rev;
388 return 0;
391 int git_get_log_firstcommit(GIT_LOG handle)
393 return prepare_revision_walk(handle);
396 int git_get_log_estimate_commit_count(GIT_LOG handle)
398 struct rev_info *p_Rev;
399 p_Rev=(struct rev_info *)handle;
401 return estimate_commit_count(p_Rev, p_Rev->commits);
404 int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit, int follow)
406 int ret =0;
408 if(commit == NULL)
409 return -1;
411 memset(commit, 0, sizeof(GIT_COMMIT));
413 commit->m_pGitCommit = get_revision(handle);
414 if( commit->m_pGitCommit == NULL)
415 return -2;
417 if (follow && !log_tree_commit(handle, commit->m_pGitCommit))
419 commit->m_ignore = 1;
420 return 0;
422 commit->m_ignore = 0;
424 ret=git_parse_commit(commit);
425 if(ret)
426 return ret;
428 return 0;
431 struct notes_tree **display_notes_trees;
432 int git_close_log(GIT_LOG handle)
434 if(handle)
436 struct rev_info *p_Rev;
437 p_Rev=(struct rev_info *)handle;
438 if(p_Rev->pPrivate)
439 free(p_Rev->pPrivate);
440 free(handle);
442 free_all_pack();
444 if (display_notes_trees)
445 free_notes(*display_notes_trees);
446 display_notes_trees = 0;
447 return 0;
450 int git_open_diff(GIT_DIFF *diff, char * arg)
452 struct rev_info *p_Rev;
453 char ** argv=0;
454 int argc=0;
456 if(arg != NULL)
457 argv = strtoargv(arg,&argc);
459 p_Rev = malloc(sizeof(struct rev_info));
460 memset(p_Rev,0,sizeof(struct rev_info));
462 p_Rev->pPrivate = argv;
463 *diff = (GIT_DIFF)p_Rev;
465 init_revisions(p_Rev, g_prefix);
466 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
467 p_Rev->abbrev = 0;
468 p_Rev->diff = 1;
469 argc = setup_revisions(argc, argv, p_Rev, NULL);
471 return 0;
473 int git_close_diff(GIT_DIFF handle)
475 git_diff_flush(handle);
476 if(handle)
478 struct rev_info *p_Rev;
479 p_Rev=(struct rev_info *)handle;
480 if(p_Rev->pPrivate)
481 free(p_Rev->pPrivate);
482 free(handle);
484 return 0;
486 int git_diff_flush(GIT_DIFF diff)
488 struct diff_queue_struct *q = &diff_queued_diff;
489 struct rev_info *p_Rev;
490 int i;
491 p_Rev = (struct rev_info *)diff;
493 if(q->nr == 0)
494 return 0;
496 for (i = 0; i < q->nr; i++)
497 diff_free_filepair(q->queue[i]);
499 if(q->queue)
501 free(q->queue);
502 q->queue = NULL;
503 q->nr = q->alloc = 0;
506 if (p_Rev->diffopt.close_file)
507 fclose(p_Rev->diffopt.file);
509 free_diffstat_info(&p_Rev->diffstat);
510 return 0;
513 int git_root_diff(GIT_DIFF diff, GIT_HASH hash,GIT_FILE *file, int *count, int isstat)
515 int ret;
516 struct rev_info *p_Rev;
517 int i;
518 struct diff_queue_struct *q = &diff_queued_diff;
520 p_Rev = (struct rev_info *)diff;
522 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
524 if(ret)
525 return ret;
527 if(isstat)
529 diffcore_std(&p_Rev->diffopt);
531 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
532 for (i = 0; i < q->nr; i++) {
533 struct diff_filepair *p = q->queue[i];
534 //if (check_pair_status(p))
535 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
538 if(file)
539 *file = q;
540 if(count)
541 *count = q->nr;
543 return 0;
546 int git_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count,int isstat)
548 struct rev_info *p_Rev;
549 int ret;
550 int i;
551 struct diff_queue_struct *q = &diff_queued_diff;
553 p_Rev = (struct rev_info *)diff;
555 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
556 if( ret )
558 free_all_pack();
559 return ret;
562 if(isstat)
564 diffcore_std(&p_Rev->diffopt);
565 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
566 for (i = 0; i < q->nr; i++) {
567 struct diff_filepair *p = q->queue[i];
568 //if (check_pair_status(p))
569 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
572 free_all_pack();
573 if(file)
574 *file = q;
575 if(count)
576 *count = q->nr;
577 return 0;
580 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)
582 struct diff_queue_struct *q = &diff_queued_diff;
583 struct rev_info *p_Rev;
584 p_Rev = (struct rev_info *)diff;
586 q = (struct diff_queue_struct *)file;
587 if(file == 0)
588 return -1;
589 if(i>=q->nr)
590 return -1;
592 if(newname)
593 *newname = q->queue[i]->two->path;
595 if(oldname)
596 *oldname = q->queue[i]->one->path;
598 if(status)
599 *status = q->queue[i]->status;
601 if(p_Rev->diffstat.files)
603 int j;
604 for(j=0;j<p_Rev->diffstat.nr;j++)
606 if(strcmp(*newname,p_Rev->diffstat.files[j]->name)==0)
607 break;
609 if( j== p_Rev->diffstat.nr)
611 *IsBin=1;
612 *inc=0;
613 *dec=0;
614 return 0;
616 if(IsBin)
617 *IsBin = p_Rev->diffstat.files[j]->is_binary;
618 if(inc)
619 *inc = p_Rev->diffstat.files[j]->added;
620 if(dec)
621 *dec = p_Rev->diffstat.files[j]->deleted;
622 }else
624 *IsBin=1;
625 *inc=0;
626 *dec=0;
629 return 0;
632 int git_read_tree(GIT_HASH hash,read_tree_fn_t fn, void *context)
634 struct tree * root;
635 int ret;
636 reprepare_packed_git();
637 root = parse_tree_indirect(hash);
639 if (!root)
641 free_all_pack();
642 return -1;
644 ret = read_tree_recursive(root,NULL,NULL,0,NULL,fn,context);
645 free_all_pack();
646 return ret;
649 int git_add_exclude(const char *string, const char *base,
650 int baselen, struct exclude_list *which)
652 add_exclude(string, base, baselen, which);
653 return 0;
656 int git_create_exclude_list(EXCLUDE_LIST *which)
658 *which = malloc(sizeof(struct exclude_list));
659 memset(*which,0,sizeof(struct exclude_list));
660 return 0;
663 int git_free_exclude_list(EXCLUDE_LIST which)
665 int i=0;
666 struct exclude_list *p = (struct exclude_list *) which;
668 for(i=0; i<p->nr;i++)
670 free(p->excludes[i]);
672 free(p->excludes);
673 free(p);
674 return 0;
677 int git_check_excluded_1(const char *pathname,
678 int pathlen, const char *basename, int *dtype,
679 EXCLUDE_LIST el)
681 return excluded_from_list(pathname, pathlen, basename,dtype,el);
684 int git_get_notes(GIT_HASH hash, char **p_note)
686 struct strbuf sb;
687 size_t size;
688 strbuf_init(&sb,0);
689 format_display_notes(hash, &sb, "utf-8", 0);
690 *p_note = strbuf_detach(&sb,&size);
692 return 0;
695 struct cmd_struct {
696 const char *cmd;
697 int (*fn)(int, const char **, const char *);
698 int option;
701 #define RUN_SETUP (1<<0)
702 #define USE_PAGER (1<<1)
704 * require working tree to be present -- anything uses this needs
705 * RUN_SETUP for reading from the configuration file.
707 #define NEED_WORK_TREE (1<<2)
709 const char git_usage_string[] =
710 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
711 " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
712 " [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
713 " [-c name=value] [--help]\n"
714 " COMMAND [ARGS]";
716 const char git_more_info_string[] =
717 "See 'git help COMMAND' for more information on a specific command.";
719 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
720 int check_pager_config(const char *cmd)
722 return 0;
726 int git_run_cmd(char *cmd, char *arg)
729 int i=0;
730 char ** argv=0;
731 int argc=0;
733 static struct cmd_struct commands[] = {
734 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
735 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
736 { "annotate", cmd_annotate, RUN_SETUP },
737 { "apply", cmd_apply },
738 { "archive", cmd_archive },
739 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
740 { "blame", cmd_blame, RUN_SETUP },
741 { "branch", cmd_branch, RUN_SETUP },
742 { "bundle", cmd_bundle },
743 { "cat-file", cmd_cat_file, RUN_SETUP },
744 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
745 { "checkout-index", cmd_checkout_index,
746 RUN_SETUP | NEED_WORK_TREE},
747 { "check-ref-format", cmd_check_ref_format },
748 { "check-attr", cmd_check_attr, RUN_SETUP },
749 { "cherry", cmd_cherry, RUN_SETUP },
750 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
751 { "clone", cmd_clone },
752 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
753 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
754 { "commit-tree", cmd_commit_tree, RUN_SETUP },
755 { "config", cmd_config },
756 { "count-objects", cmd_count_objects, RUN_SETUP },
757 { "describe", cmd_describe, RUN_SETUP },
758 { "diff", cmd_diff },
759 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
760 { "diff-index", cmd_diff_index, RUN_SETUP },
761 { "diff-tree", cmd_diff_tree, RUN_SETUP },
762 { "fast-export", cmd_fast_export, RUN_SETUP },
763 { "fetch", cmd_fetch, RUN_SETUP },
764 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
765 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
766 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
767 { "format-patch", cmd_format_patch, RUN_SETUP },
768 { "fsck", cmd_fsck, RUN_SETUP },
769 { "fsck-objects", cmd_fsck, RUN_SETUP },
770 { "gc", cmd_gc, RUN_SETUP },
771 { "get-tar-commit-id", cmd_get_tar_commit_id },
772 { "grep", cmd_grep },
773 { "hash-object", cmd_hash_object },
774 { "help", cmd_help },
775 { "index-pack", cmd_index_pack },
776 { "init", cmd_init_db },
777 { "init-db", cmd_init_db },
778 { "log", cmd_log, RUN_SETUP },
779 { "ls-files", cmd_ls_files, RUN_SETUP },
780 { "ls-tree", cmd_ls_tree, RUN_SETUP },
781 { "ls-remote", cmd_ls_remote },
782 { "mailinfo", cmd_mailinfo },
783 { "mailsplit", cmd_mailsplit },
784 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
785 { "merge-base", cmd_merge_base, RUN_SETUP },
786 { "merge-file", cmd_merge_file },
787 { "merge-index", cmd_merge_index, RUN_SETUP },
788 { "merge-ours", cmd_merge_ours, RUN_SETUP },
789 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
790 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
791 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
792 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
793 { "merge-tree", cmd_merge_tree, RUN_SETUP },
794 { "mktag", cmd_mktag, RUN_SETUP },
795 { "mktree", cmd_mktree, RUN_SETUP },
796 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
797 { "name-rev", cmd_name_rev, RUN_SETUP },
798 { "notes", cmd_notes, RUN_SETUP },
799 { "pack-objects", cmd_pack_objects, RUN_SETUP },
800 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
801 { "patch-id", cmd_patch_id },
802 { "peek-remote", cmd_ls_remote },
803 { "pickaxe", cmd_blame, RUN_SETUP },
804 { "prune", cmd_prune, RUN_SETUP },
805 { "prune-packed", cmd_prune_packed, RUN_SETUP },
806 { "push", cmd_push, RUN_SETUP },
807 { "read-tree", cmd_read_tree, RUN_SETUP },
808 { "receive-pack", cmd_receive_pack },
809 { "reflog", cmd_reflog, RUN_SETUP },
810 { "remote", cmd_remote, RUN_SETUP },
811 { "replace", cmd_replace, RUN_SETUP },
812 { "repo-config", cmd_config },
813 { "rerere", cmd_rerere, RUN_SETUP },
814 { "reset", cmd_reset, RUN_SETUP },
815 { "rev-list", cmd_rev_list, RUN_SETUP },
816 { "rev-parse", cmd_rev_parse },
817 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
818 { "rm", cmd_rm, RUN_SETUP },
819 { "send-pack", cmd_send_pack, RUN_SETUP },
820 { "shortlog", cmd_shortlog, USE_PAGER },
821 { "show-branch", cmd_show_branch, RUN_SETUP },
822 { "show", cmd_show, RUN_SETUP },
823 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
824 { "stripspace", cmd_stripspace },
825 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
826 { "tag", cmd_tag, RUN_SETUP },
827 { "tar-tree", cmd_tar_tree },
828 { "unpack-file", cmd_unpack_file, RUN_SETUP },
829 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
830 { "update-index", cmd_update_index, RUN_SETUP },
831 { "update-ref", cmd_update_ref, RUN_SETUP },
832 { "update-server-info", cmd_update_server_info, RUN_SETUP },
833 { "upload-archive", cmd_upload_archive },
834 { "var", cmd_var },
835 { "verify-tag", cmd_verify_tag, RUN_SETUP },
836 { "version", cmd_version },
837 { "whatchanged", cmd_whatchanged, RUN_SETUP },
838 { "write-tree", cmd_write_tree, RUN_SETUP },
839 { "verify-pack", cmd_verify_pack },
840 { "show-ref", cmd_show_ref, RUN_SETUP },
841 { "pack-refs", cmd_pack_refs, RUN_SETUP },
844 git_init();
846 for(i=0;i< sizeof(commands) / sizeof(struct cmd_struct);i++)
848 if(strcmp(cmd,commands[i].cmd)==0)
850 int ret;
851 if(arg != NULL)
852 argv = strtoargv(arg,&argc);
854 ret = commands[i].fn(argc, argv, NULL);
856 if(argv)
857 free(argv);
859 discard_cache();
860 free_all_pack();
862 return ret;
867 return -1;
870 int git_for_each_ref_in(const char * refname, each_ref_fn fn, void * data)
872 int ret;
873 invalidate_ref_cache(NULL);
874 ret = for_each_ref_in(refname, fn, data);
875 free_all_pack();
876 return ret;
879 const char *git_resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
881 invalidate_ref_cache(NULL);
882 return resolve_ref_unsafe(ref,sha1,reading, flag);
884 int git_for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
886 return for_each_reflog_ent(ref,fn,cb_data);
889 int git_deref_tag(const unsigned char *tagsha1, GIT_HASH refhash)
891 struct object *obj = NULL;
892 obj = parse_object(tagsha1);
893 if (!obj)
894 return -1;
896 if (obj->type == OBJ_TAG)
898 obj = deref_tag(obj, "", 0);
899 if (!obj)
900 return -1;
902 memcpy(refhash, obj->sha1, sizeof(GIT_HASH));
903 return 0;
906 return -1;
909 static int update_some(const unsigned char *sha1, const char *base, int baselen,
910 const char *pathname, unsigned mode, int stage, void *context)
912 struct cache_entry *ce;
914 ce = (struct cache_entry *)context;
916 if (S_ISDIR(mode))
917 return READ_TREE_RECURSIVE;
919 hashcpy(ce->sha1, sha1);
920 memcpy(ce->name, base, baselen);
921 memcpy(ce->name + baselen, pathname, strlen(pathname));
922 ce->ce_flags = create_ce_flags(strlen(pathname)+baselen, 0);
923 ce->ce_mode = create_ce_mode(mode);
925 return 0;
928 int git_checkout_file(const char *ref, const char *path, const char *outputpath)
930 struct cache_entry *ce;
931 int ret;
932 GIT_HASH sha1;
933 struct tree * root;
934 struct checkout state;
935 struct pathspec pathspec;
936 const char *match[2];
937 ret = get_sha1(ref, sha1);
938 if(ret)
939 return ret;
941 reprepare_packed_git();
942 root = parse_tree_indirect(sha1);
944 if(!root)
946 free_all_pack();
947 return -1;
950 ce = xcalloc(1, cache_entry_size(strlen(path)));
952 match[0] = path;
953 match[1] = NULL;
955 init_pathspec(&pathspec, match);
956 pathspec.items[0].use_wildcard = 0;
957 ret = read_tree_recursive(root, "", 0, 0, &pathspec, update_some, ce);
958 free_pathspec(&pathspec);
960 if(ret)
962 free_all_pack();
963 free(ce);
964 return ret;
966 memset(&state, 0, sizeof(state));
967 state.force = 1;
968 state.refresh_cache = 0;
970 ret = write_entry(ce, outputpath, &state, 0);
971 free_all_pack();
972 free(ce);
973 return ret;
975 struct config_buf
977 char *buf;
978 char *key;
979 char *size;
980 int seen;
983 static int get_config(const char *key_, const char *value_, void *cb)
985 struct config_buf *buf;
986 buf=(struct config_buf*)cb;
987 if(strcmp(key_, buf->key))
988 return 0;
990 if (value_)
991 strncpy(buf->buf,value_,buf->size);
992 else
994 buf->buf[0] = 't';
995 buf->buf[1] = 'r';
996 buf->buf[2] = 'u';
997 buf->buf[3] = 'e';
998 buf->buf[4] = 0;
1000 buf->seen = 1;
1001 return 0;
1005 // wchar_t wrapper for git_etc_gitconfig()
1006 const wchar_t *wget_msysgit_etc(void)
1008 static const wchar_t *etc_gitconfig = NULL;
1009 wchar_t wpointer[MAX_PATH];
1011 if (etc_gitconfig)
1012 return etc_gitconfig;
1014 if (xutftowcs_path(wpointer, git_etc_gitconfig()) < 0)
1015 return NULL;
1017 etc_gitconfig = _wcsdup(wpointer);
1019 return etc_gitconfig;
1022 int git_get_config(const char *key, char *buffer, int size, char *git_path)
1024 char *local, *global, *globalxdg;
1025 const char *home, *system;
1026 struct config_buf buf;
1027 buf.buf=buffer;
1028 buf.size=size;
1029 buf.seen = 0;
1030 buf.key = key;
1032 home = get_windows_home_directory();
1033 if (home)
1035 global = xstrdup(mkpath("%s/.gitconfig", home));
1036 globalxdg = xstrdup(mkpath("%s/.config/git/config", home));
1038 else
1040 global = NULL;
1041 globalxdg = NULL;
1044 system = git_etc_gitconfig();
1046 local = git_pathdup("config");
1048 if ( !buf.seen)
1049 git_config_from_file(get_config, local, &buf);
1050 if (!buf.seen && global)
1051 git_config_from_file(get_config, global, &buf);
1052 if (!buf.seen && globalxdg)
1053 git_config_from_file(get_config, globalxdg, &buf);
1054 if (!buf.seen && system)
1055 git_config_from_file(get_config, system, &buf);
1057 if(local)
1058 free(local);
1059 if(global)
1060 free(global);
1061 if (globalxdg)
1062 free(globalxdg);
1064 return !buf.seen;
1067 // taken from msysgit: compat/mingw.c
1068 const char *get_windows_home_directory(void)
1070 static const char *home_directory = NULL;
1071 struct strbuf buf = STRBUF_INIT;
1073 if (home_directory)
1074 return home_directory;
1076 home_directory = getenv("HOME");
1077 if (home_directory && *home_directory)
1078 return home_directory;
1080 strbuf_addf(&buf, "%s/%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
1081 home_directory = strbuf_detach(&buf, NULL);
1083 return home_directory;
1086 // wchar_t wrapper for get_windows_home_directory()
1087 const wchar_t *wget_windows_home_directory(void)
1089 static const wchar_t *home_directory = NULL;
1090 wchar_t wpointer[MAX_PATH];
1092 if (home_directory)
1093 return home_directory;
1095 if (xutftowcs_path(wpointer, get_windows_home_directory()) < 0)
1096 return NULL;
1098 home_directory = _wcsdup(wpointer);
1100 return home_directory;
1103 int get_set_config(const char *key, char *value, CONFIG_TYPE type,char *git_path)
1105 char * config_exclusive_filename = NULL;
1106 switch(type)
1108 case CONFIG_LOCAL:
1109 config_exclusive_filename = git_pathdup("config");
1110 break;
1111 case CONFIG_GLOBAL:
1112 case CONFIG_XDGGLOBAL:
1114 const char *home = get_windows_home_directory();
1115 if (home)
1117 if (type == CONFIG_GLOBAL)
1118 config_exclusive_filename = xstrdup(mkpath("%s/.gitconfig", home));
1119 else
1120 config_exclusive_filename = xstrdup(mkpath("%s/.config/git/config", home));
1123 break;
1126 if(!config_exclusive_filename)
1127 return -1;
1129 return git_config_set_multivar_in_file(config_exclusive_filename, key, value, NULL, 0);
1132 struct mailmap_info {
1133 char *name;
1134 char *email;
1137 struct mailmap_entry {
1138 /* name and email for the simple mail-only case */
1139 char *name;
1140 char *email;
1142 /* name and email for the complex mail and name matching case */
1143 struct string_list namemap;
1146 int git_read_mailmap(GIT_MAILMAP *mailmap)
1148 struct string_list *map;
1149 int result;
1151 if (!mailmap)
1152 return -1;
1154 *mailmap = NULL;
1155 if (!(map = (struct string_list *)calloc(1, sizeof(struct string_list))))
1156 return -1;
1158 if ((result = read_mailmap(map, NULL)))
1159 return result;
1161 *mailmap = map;
1162 return 0;
1165 const char * git_get_mailmap_author(GIT_MAILMAP mailmap, const char *email2, void *payload, const char *(*author2_cb)(void *))
1167 struct string_list *map;
1168 int imax, imin = 0;
1170 if (!mailmap)
1171 return NULL;
1173 map = (struct string_list *)mailmap;
1174 imax = map->nr - 1;
1175 while (imax >= imin)
1177 int i = imin + ((imax - imin) / 2);
1178 struct string_list_item *si = (struct string_list_item *)&map->items[i];
1179 struct mailmap_entry *me = (struct mailmap_entry *)si->util;
1180 int comp = strcmp(si->string, email2);
1182 if (!comp)
1184 if (me->namemap.nr)
1186 const char *author2 = author2_cb(payload);
1187 int j;
1188 for (j = 0; j < me->namemap.nr; ++j)
1190 struct string_list_item *sj = (struct string_list_item *)&me->namemap.items[j];
1191 struct mailmap_info *mi = (struct mailmap_info *)sj->util;
1193 if (!strcmp(sj->string, author2))
1194 return mi->name;
1198 return me->name;
1200 else if (comp < 0)
1201 imin = i + 1;
1202 else
1203 imax = i - 1;
1206 return NULL;
1209 void git_clear_mailmap(GIT_MAILMAP mailmap)
1211 if (!mailmap)
1212 return;
1214 clear_mailmap((struct string_list *)mailmap);