fix crash when filter input space at log dialog
[TortoiseGit.git] / ext / gitdll / gitdll.c
blob5cd35e5266e205cc5baf0eea595cd9fd80b3b394
1 // gitdll.cpp : Defines the exported functions for the DLL application.
2 //
4 #include "stdafx.h"
5 #include "git-compat-util.h"
6 #include "msvc.h"
7 #include "gitdll.h"
8 #include "cache.h"
9 #include "commit.h"
10 #include "diff.h"
11 #include "revision.h"
12 #include "diffcore.h"
13 #include "dir.h"
14 #include "builtin.h"
15 #include "exec_cmd.h"
16 #include "cache.h"
17 #include "quote.h"
18 #include "run-command.h"
20 const char git_version_string[] = GIT_VERSION;
23 #if 0
25 // This is an example of an exported variable
26 GITDLL_API int ngitdll=0;
28 // This is an example of an exported function.
29 GITDLL_API int fngitdll(void)
31 return 42;
34 // This is the constructor of a class that has been exported.
35 // see gitdll.h for the class definition
36 Cgitdll::Cgitdll()
38 return;
40 #endif
42 extern char g_last_error[];
43 void * g_prefix;
45 char * get_git_last_error()
47 return g_last_error;
50 extern void die_dll(const char *err, va_list params);
52 void dll_entry()
54 set_die_routine(die_dll);
57 int git_get_sha1(const char *name, GIT_HASH sha1)
59 return get_sha1(name,sha1);
62 static int convert_slash(char * path)
64 while(*path)
66 if(*path == '\\' )
67 *path = '/';
68 path++;
70 return 0;
73 int git_init()
75 char *home;
76 char path[MAX_PATH+1];
77 char *prefix;
78 int ret;
79 size_t homesize,size;
81 _fmode = _O_BINARY;
82 _setmode(_fileno(stdin), _O_BINARY);
83 _setmode(_fileno(stdout), _O_BINARY);
84 _setmode(_fileno(stderr), _O_BINARY);
86 // set HOME if not set already
87 getenv_s(&homesize, NULL, 0, "HOME");
88 if (!homesize)
90 _dupenv_s(&home,&size,"USERPROFILE");
91 _putenv_s("HOME",home);
92 free(home);
94 GetModuleFileName(NULL, path, MAX_PATH);
95 convert_slash(path);
97 git_extract_argv0_path(path);
98 g_prefix = prefix = setup_git_directory();
99 ret = git_config(git_default_config, NULL);
101 if (!homesize)
103 _putenv_s("HOME","");/* clear home evironment to avoid affact third part software*/
106 return ret;
109 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR *author, char *pbuff)
111 char *end;
113 author->Name=pbuff;
114 end=strchr(pbuff,'<');
115 if( end == 0)
117 return -1;
119 author->NameSize = end - pbuff - 1;
121 pbuff = end +1;
122 end = strchr(pbuff, '>');
123 if( end == 0)
124 return -1;
126 author->Email = pbuff ;
127 author->EmailSize = end - pbuff;
129 pbuff = end + 2;
131 author->Date = atol(pbuff);
132 end = strchr(pbuff, ' ');
133 if( end == 0 )
134 return -1;
136 pbuff=end;
137 author->TimeZone = atol(pbuff);
139 return 0;
142 int git_parse_commit(GIT_COMMIT *commit)
144 int ret = 0;
145 char *pbuf;
146 char *end;
147 struct commit *p;
149 p= (struct commit *)commit->m_pGitCommit;
151 memcpy(commit->m_hash,p->object.sha1,GIT_HASH_SIZE);
153 commit->m_Encode = NULL;
154 commit->m_EncodeSize = 0;
156 if(p->buffer == NULL)
157 return -1;
159 pbuf = p->buffer;
160 while(pbuf)
162 if( strncmp(pbuf,"author",6) == 0)
164 ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);
165 if(ret)
166 return ret;
168 if( strncmp(pbuf, "committer",9) == 0)
170 ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10);
171 if(ret)
172 return ret;
174 pbuf = strchr(pbuf,'\n');
175 if(pbuf == NULL)
176 return -1;
178 while((*pbuf) && (*pbuf == '\n'))
179 pbuf ++;
181 if( strncmp(pbuf, "encoding",8) == 0 )
183 pbuf += 9;
184 commit->m_Encode=pbuf;
185 end = strchr(pbuf,'\n');
186 commit->m_EncodeSize=end -pbuf;
188 pbuf = end +1;
189 while((*pbuf) && (*pbuf == '\n'))
190 pbuf ++;
192 commit->m_Subject=pbuf;
193 end = strchr(pbuf,'\n');
194 if( end == 0)
195 commit->m_SubjectSize = strlen(pbuf);
196 else
198 commit->m_SubjectSize = end - pbuf;
199 pbuf = end +1;
200 commit->m_Body = pbuf;
201 commit->m_BodySize = strlen(pbuf);
202 return 0;
207 pbuf = strchr(pbuf,'\n');
208 if(pbuf)
209 pbuf ++;
211 return 0;
214 int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash)
216 int ret = 0;
218 struct commit *p;
220 memset(commit,0,sizeof(GIT_COMMIT));
222 commit->m_pGitCommit = p = lookup_commit(hash);
224 if(commit == NULL)
225 return -1;
227 if(p == NULL)
228 return -1;
230 ret = parse_commit(p);
231 if( ret )
232 return ret;
234 return git_parse_commit(commit);
237 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
239 struct commit *p = commit->m_pGitCommit;
241 if(list == NULL)
242 return -1;
244 *list = (GIT_COMMIT_LIST*)p->parents;
245 return 0;
247 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
249 struct commit_list *l = *(struct commit_list **)list;
250 if(list == NULL || l==NULL)
251 return -1;
253 if(hash)
254 memcpy(hash, l->item->object.sha1, GIT_HASH_SIZE);
256 *list = (GIT_COMMIT_LIST *)l->next;
257 return 0;
262 int git_free_commit(GIT_COMMIT *commit)
264 struct commit *p = commit->m_pGitCommit;
266 if( p->parents)
267 free_commit_list(p->parents);
269 if( p->buffer )
271 free(p->buffer);
272 p->buffer=NULL;
273 p->object.parsed=0;
274 p->parents=0;
275 p->tree=0;
277 memset(commit,0,sizeof(GIT_COMMIT));
278 return 0;
281 char **strtoargv(char *arg, int *size)
283 int count=0;
284 char *p=arg;
285 char **argv;
287 int i=0;
288 while(*p)
290 if(*p == '\\')
291 *p='/';
292 p++;
294 p=arg;
296 while(*p)
298 if(*p == ' ')
299 count ++;
300 p++;
303 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
304 p=(char*)(argv+count+2);
306 while(*arg)
308 if(*arg != ' ')
310 char space=' ';
311 argv[i]=p;
313 while(*arg)
315 if(*arg == '"')
317 arg++;
318 if(space == ' ')
319 space = '"';
320 else
321 space = ' ';
323 if((*arg == space) || (*arg == 0))
324 break;
326 *p++ = *arg++;
328 i++;
329 *p++=0;
331 if(*arg == 0)
332 break;
333 arg++;
335 argv[i]=NULL;
336 *size = i;
337 return argv;
339 int git_open_log(GIT_LOG * handle, char * arg)
341 struct rev_info *p_Rev;
342 char ** argv=0;
343 int argc=0;
344 unsigned int i=0;
345 struct setup_revision_opt opt;
347 /* clear flags */
348 unsigned int obj_size = get_max_object_index();
349 for(i =0; i<obj_size; i++)
351 struct object *ob= get_indexed_object(i);
352 if(ob)
353 ob->flags=0;
356 if(arg != NULL)
357 argv = strtoargv(arg,&argc);
359 p_Rev = malloc(sizeof(struct rev_info));
360 memset(p_Rev,0,sizeof(struct rev_info));
362 if(p_Rev == NULL)
363 return -1;
365 init_revisions(p_Rev, g_prefix);
366 p_Rev->diff = 1;
368 memset(&opt, 0, sizeof(opt));
369 opt.def = "HEAD";
371 cmd_log_init(argc, argv, g_prefix,p_Rev,&opt);
373 p_Rev->pPrivate = argv;
374 *handle = p_Rev;
375 return 0;
378 int git_get_log_firstcommit(GIT_LOG handle)
380 return prepare_revision_walk(handle);
383 int git_get_log_estimate_commit_count(GIT_LOG handle)
385 struct rev_info *p_Rev;
386 p_Rev=(struct rev_info *)handle;
388 return estimate_commit_count(p_Rev, p_Rev->commits);
391 int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit)
393 int ret =0;
395 if(commit == NULL)
396 return -1;
398 memset(commit, 0, sizeof(GIT_COMMIT));
400 commit->m_pGitCommit = get_revision(handle);
401 if( commit->m_pGitCommit == NULL)
402 return -2;
404 ret=git_parse_commit(commit);
405 if(ret)
406 return ret;
408 return 0;
411 struct notes_tree **display_notes_trees;
412 int git_close_log(GIT_LOG handle)
414 if(handle)
416 struct rev_info *p_Rev;
417 p_Rev=(struct rev_info *)handle;
418 if(p_Rev->pPrivate)
419 free(p_Rev->pPrivate);
420 free(handle);
423 free_notes(*display_notes_trees);
424 display_notes_trees = 0;
425 return 0;
428 int git_open_diff(GIT_DIFF *diff, char * arg)
430 struct rev_info *p_Rev;
431 char ** argv=0;
432 int argc=0;
434 if(arg != NULL)
435 argv = strtoargv(arg,&argc);
437 p_Rev = malloc(sizeof(struct rev_info));
438 memset(p_Rev,0,sizeof(struct rev_info));
440 p_Rev->pPrivate = argv;
441 *diff = (GIT_DIFF)p_Rev;
443 init_revisions(p_Rev, g_prefix);
444 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
445 p_Rev->abbrev = 0;
446 p_Rev->diff = 1;
447 argc = setup_revisions(argc, argv, p_Rev, NULL);
449 return 0;
451 int git_close_diff(GIT_DIFF handle)
453 git_diff_flush(handle);
454 if(handle)
456 struct rev_info *p_Rev;
457 p_Rev=(struct rev_info *)handle;
458 if(p_Rev->pPrivate)
459 free(p_Rev->pPrivate);
460 free(handle);
462 return 0;
464 int git_diff_flush(GIT_DIFF diff)
466 struct diff_queue_struct *q = &diff_queued_diff;
467 struct rev_info *p_Rev;
468 int i;
469 p_Rev = (struct rev_info *)diff;
471 if(q->nr == 0)
472 return 0;
474 for (i = 0; i < q->nr; i++)
475 diff_free_filepair(q->queue[i]);
477 if(q->queue)
479 free(q->queue);
480 q->queue = NULL;
481 q->nr = q->alloc = 0;
484 if (p_Rev->diffopt.close_file)
485 fclose(p_Rev->diffopt.close_file);
487 free_diffstat_info(&p_Rev->diffstat);
488 return 0;
491 int git_root_diff(GIT_DIFF diff, GIT_HASH hash,GIT_FILE *file, int *count)
493 int ret;
494 struct rev_info *p_Rev;
495 int i;
496 struct diff_queue_struct *q = &diff_queued_diff;
498 p_Rev = (struct rev_info *)diff;
500 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
502 if(ret)
503 return ret;
505 diffcore_std(&p_Rev->diffopt);
507 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
508 for (i = 0; i < q->nr; i++) {
509 struct diff_filepair *p = q->queue[i];
510 //if (check_pair_status(p))
511 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
514 if(file)
515 *file = q;
516 if(count)
517 *count = q->nr;
519 return 0;
522 int git_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count)
524 struct rev_info *p_Rev;
525 int ret;
526 int i;
527 struct diff_queue_struct *q = &diff_queued_diff;
529 p_Rev = (struct rev_info *)diff;
531 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
532 if( ret )
533 return ret;
535 diffcore_std(&p_Rev->diffopt);
537 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
538 for (i = 0; i < q->nr; i++) {
539 struct diff_filepair *p = q->queue[i];
540 //if (check_pair_status(p))
541 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
544 if(file)
545 *file = q;
546 if(count)
547 *count = q->nr;
548 return 0;
551 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)
553 struct diff_queue_struct *q = &diff_queued_diff;
554 struct rev_info *p_Rev;
555 p_Rev = (struct rev_info *)diff;
557 q = (struct diff_queue_struct *)file;
558 if(file == 0)
559 return -1;
560 if(i>=q->nr)
561 return -1;
563 if(newname)
564 *newname = q->queue[i]->two->path;
566 if(oldname)
567 *oldname = q->queue[i]->one->path;
569 if(status)
570 *status = q->queue[i]->status;
572 if(p_Rev->diffstat.files)
574 int j;
575 for(j=0;j<p_Rev->diffstat.nr;j++)
577 if(strcmp(*newname,p_Rev->diffstat.files[j]->name)==0)
578 break;
580 if( j== p_Rev->diffstat.nr)
582 *IsBin=1;
583 *inc=0;
584 *dec=0;
585 return 0;
587 if(IsBin)
588 *IsBin = p_Rev->diffstat.files[j]->is_binary;
589 if(inc)
590 *inc = p_Rev->diffstat.files[j]->added;
591 if(dec)
592 *dec = p_Rev->diffstat.files[j]->deleted;
593 }else
595 *IsBin=1;
596 *inc=0;
597 *dec=0;
600 return 0;
603 int git_read_tree(GIT_HASH hash,read_tree_fn_t fn, void *context)
605 struct tree * root;
606 int ret;
607 reprepare_packed_git();
608 root = parse_tree_indirect(hash);
610 if (!root)
612 free_all_pack();
613 return -1;
615 ret = read_tree_recursive(root,NULL,NULL,0,NULL,fn,context);
616 free_all_pack();
617 return ret;
620 int git_add_exclude(const char *string, const char *base,
621 int baselen, struct exclude_list *which)
623 add_exclude(string, base, baselen, which);
624 return 0;
627 int git_create_exclude_list(EXCLUDE_LIST *which)
629 *which = malloc(sizeof(struct exclude_list));
630 memset(*which,0,sizeof(struct exclude_list));
631 return 0;
634 int git_free_exclude_list(EXCLUDE_LIST which)
636 int i=0;
637 struct exclude_list *p = (struct exclude_list *) which;
639 for(i=0; i<p->nr;i++)
641 free(p->excludes[i]);
643 free(p->excludes);
644 free(p);
645 return 0;
648 int git_check_excluded_1(const char *pathname,
649 int pathlen, const char *basename, int *dtype,
650 EXCLUDE_LIST el)
652 return excluded_from_list(pathname, pathlen, basename,dtype,el);
655 int git_get_notes(GIT_HASH hash, char **p_note)
657 struct strbuf sb;
658 size_t size;
659 strbuf_init(&sb,0);
660 format_display_notes(hash, &sb, "utf-8", 0);
661 *p_note = strbuf_detach(&sb,&size);
663 return 0;
666 struct cmd_struct {
667 const char *cmd;
668 int (*fn)(int, const char **, const char *);
669 int option;
672 #define RUN_SETUP (1<<0)
673 #define USE_PAGER (1<<1)
675 * require working tree to be present -- anything uses this needs
676 * RUN_SETUP for reading from the configuration file.
678 #define NEED_WORK_TREE (1<<2)
680 const char git_usage_string[] =
681 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
682 " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
683 " [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
684 " [-c name=value] [--help]\n"
685 " COMMAND [ARGS]";
687 const char git_more_info_string[] =
688 "See 'git help COMMAND' for more information on a specific command.";
690 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
691 int check_pager_config(const char *cmd)
693 return 0;
697 int git_run_cmd(char *cmd, char *arg)
700 int i=0;
701 char ** argv=0;
702 int argc=0;
704 static struct cmd_struct commands[] = {
705 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
706 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
707 { "annotate", cmd_annotate, RUN_SETUP },
708 { "apply", cmd_apply },
709 { "archive", cmd_archive },
710 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
711 { "blame", cmd_blame, RUN_SETUP },
712 { "branch", cmd_branch, RUN_SETUP },
713 { "bundle", cmd_bundle },
714 { "cat-file", cmd_cat_file, RUN_SETUP },
715 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
716 { "checkout-index", cmd_checkout_index,
717 RUN_SETUP | NEED_WORK_TREE},
718 { "check-ref-format", cmd_check_ref_format },
719 { "check-attr", cmd_check_attr, RUN_SETUP },
720 { "cherry", cmd_cherry, RUN_SETUP },
721 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
722 { "clone", cmd_clone },
723 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
724 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
725 { "commit-tree", cmd_commit_tree, RUN_SETUP },
726 { "config", cmd_config },
727 { "count-objects", cmd_count_objects, RUN_SETUP },
728 { "describe", cmd_describe, RUN_SETUP },
729 { "diff", cmd_diff },
730 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
731 { "diff-index", cmd_diff_index, RUN_SETUP },
732 { "diff-tree", cmd_diff_tree, RUN_SETUP },
733 { "fast-export", cmd_fast_export, RUN_SETUP },
734 { "fetch", cmd_fetch, RUN_SETUP },
735 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
736 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
737 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
738 { "format-patch", cmd_format_patch, RUN_SETUP },
739 { "fsck", cmd_fsck, RUN_SETUP },
740 { "fsck-objects", cmd_fsck, RUN_SETUP },
741 { "gc", cmd_gc, RUN_SETUP },
742 { "get-tar-commit-id", cmd_get_tar_commit_id },
743 { "grep", cmd_grep },
744 { "hash-object", cmd_hash_object },
745 { "help", cmd_help },
746 { "index-pack", cmd_index_pack },
747 { "init", cmd_init_db },
748 { "init-db", cmd_init_db },
749 { "log", cmd_log, RUN_SETUP },
750 { "ls-files", cmd_ls_files, RUN_SETUP },
751 { "ls-tree", cmd_ls_tree, RUN_SETUP },
752 { "ls-remote", cmd_ls_remote },
753 { "mailinfo", cmd_mailinfo },
754 { "mailsplit", cmd_mailsplit },
755 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
756 { "merge-base", cmd_merge_base, RUN_SETUP },
757 { "merge-file", cmd_merge_file },
758 { "merge-index", cmd_merge_index, RUN_SETUP },
759 { "merge-ours", cmd_merge_ours, RUN_SETUP },
760 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
761 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
762 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
763 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
764 { "merge-tree", cmd_merge_tree, RUN_SETUP },
765 { "mktag", cmd_mktag, RUN_SETUP },
766 { "mktree", cmd_mktree, RUN_SETUP },
767 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
768 { "name-rev", cmd_name_rev, RUN_SETUP },
769 { "notes", cmd_notes, RUN_SETUP },
770 { "pack-objects", cmd_pack_objects, RUN_SETUP },
771 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
772 { "patch-id", cmd_patch_id },
773 { "peek-remote", cmd_ls_remote },
774 { "pickaxe", cmd_blame, RUN_SETUP },
775 { "prune", cmd_prune, RUN_SETUP },
776 { "prune-packed", cmd_prune_packed, RUN_SETUP },
777 { "push", cmd_push, RUN_SETUP },
778 { "read-tree", cmd_read_tree, RUN_SETUP },
779 { "receive-pack", cmd_receive_pack },
780 { "reflog", cmd_reflog, RUN_SETUP },
781 { "remote", cmd_remote, RUN_SETUP },
782 { "replace", cmd_replace, RUN_SETUP },
783 { "repo-config", cmd_config },
784 { "rerere", cmd_rerere, RUN_SETUP },
785 { "reset", cmd_reset, RUN_SETUP },
786 { "rev-list", cmd_rev_list, RUN_SETUP },
787 { "rev-parse", cmd_rev_parse },
788 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
789 { "rm", cmd_rm, RUN_SETUP },
790 { "send-pack", cmd_send_pack, RUN_SETUP },
791 { "shortlog", cmd_shortlog, USE_PAGER },
792 { "show-branch", cmd_show_branch, RUN_SETUP },
793 { "show", cmd_show, RUN_SETUP },
794 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
795 { "stripspace", cmd_stripspace },
796 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
797 { "tag", cmd_tag, RUN_SETUP },
798 { "tar-tree", cmd_tar_tree },
799 { "unpack-file", cmd_unpack_file, RUN_SETUP },
800 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
801 { "update-index", cmd_update_index, RUN_SETUP },
802 { "update-ref", cmd_update_ref, RUN_SETUP },
803 { "update-server-info", cmd_update_server_info, RUN_SETUP },
804 { "upload-archive", cmd_upload_archive },
805 { "var", cmd_var },
806 { "verify-tag", cmd_verify_tag, RUN_SETUP },
807 { "version", cmd_version },
808 { "whatchanged", cmd_whatchanged, RUN_SETUP },
809 { "write-tree", cmd_write_tree, RUN_SETUP },
810 { "verify-pack", cmd_verify_pack },
811 { "show-ref", cmd_show_ref, RUN_SETUP },
812 { "pack-refs", cmd_pack_refs, RUN_SETUP },
815 git_init();
817 for(i=0;i< sizeof(commands) / sizeof(struct cmd_struct);i++)
819 if(strcmp(cmd,commands[i].cmd)==0)
821 int ret;
822 if(arg != NULL)
823 argv = strtoargv(arg,&argc);
825 ret = commands[i].fn(argc, argv, NULL);
827 if(argv)
828 free(argv);
830 return ret;
835 return -1;
838 int git_for_each_ref_in(const char * refname, each_ref_fn fn, void * data)
840 invalidate_cached_refs();
841 return for_each_ref_in(refname, fn, data);
844 const char *git_resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
846 invalidate_cached_refs();
847 return resolve_ref(ref,sha1,reading, flag);
849 int git_for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
851 return for_each_reflog_ent(ref,fn,cb_data);
854 int git_deref_tag(unsigned char *tagsha1, GIT_HASH refhash)
856 struct object *obj = NULL;
857 obj = parse_object(tagsha1);
858 if (!obj)
859 return -1;
861 if (obj->type == OBJ_TAG)
863 obj = deref_tag(obj, "", 0);
864 if (!obj)
865 return -1;
867 memcpy(refhash, obj->sha1, sizeof(GIT_HASH));
868 return 0;
871 return -1;
874 static int update_some(const unsigned char *sha1, const char *base, int baselen,
875 const char *pathname, unsigned mode, int stage, void *context)
877 struct cache_entry *ce;
879 ce = (struct cache_entry *)context;
881 if (S_ISDIR(mode))
882 return READ_TREE_RECURSIVE;
884 hashcpy(ce->sha1, sha1);
885 memcpy(ce->name, base, baselen);
886 memcpy(ce->name + baselen, pathname, strlen(pathname));
887 ce->ce_flags = create_ce_flags(strlen(pathname)+baselen, 0);
888 ce->ce_mode = create_ce_mode(mode);
890 return 0;
893 int git_checkout_file(const char *ref, const char *path, const char *outputpath)
895 struct cache_entry *ce;
896 int ret;
897 GIT_HASH sha1;
898 struct tree * root;
899 struct checkout state;
900 char *match[2];
901 ret = get_sha1(ref, sha1);
902 if(ret)
903 return ret;
905 reprepare_packed_git();
906 root = parse_tree_indirect(sha1);
908 if(!root)
909 return -1;
911 ce = xcalloc(1, cache_entry_size(strlen(path)));
913 match[0] = path;
914 match[1] = NULL;
915 ret = read_tree_recursive(root,"",0,0,match,update_some,ce);
917 if(ret)
919 free(ce);
920 return ret;
922 memset(&state, 0, sizeof(state));
923 state.force = 1;
924 state.refresh_cache = 0;
926 ret = write_entry(ce, outputpath, &state, 0);
927 free(ce);
928 return ret;
930 struct config_buf
932 char *buf;
933 char *key;
934 char *size;
935 int seen;
938 static int get_config(const char *key_, const char *value_, void *cb)
940 struct config_buf *buf;
941 buf=(struct config_buf*)cb;
942 if(strcmp(key_, buf->key))
943 return 0;
945 strncpy(buf->buf,value_,buf->size);
946 buf->seen = 1;
947 return 0;
950 int git_get_config(const char *key, char *buffer, int size, char *git_path)
952 char *local,*global,*system_wide,*p;
953 struct config_buf buf;
954 buf.buf=buffer;
955 buf.size=size;
956 buf.seen = 0;
957 buf.key = key;
959 local=global=system_wide=NULL;
961 //local = config_exclusive_filename;
962 if (!local) {
963 const char *home = getenv("HOME");
964 if(!home)
965 home=getenv("USERPROFILE");
967 local=p= git_pathdup("config");
968 if(git_path&&strlen(git_path))
970 local=xstrdup(mkpath("%s/%s", git_path, p));
971 free(p);
973 if (git_config_global() && home)
974 global = xstrdup(mkpath("%s/.gitconfig", home));
975 if (git_config_system())
976 system_wide = git_etc_gitconfig();
979 if ( !buf.seen)
980 git_config_from_file(get_config, local, &buf);
981 if (!buf.seen && global)
982 git_config_from_file(get_config, global, &buf);
983 if (!buf.seen && system_wide)
984 git_config_from_file(get_config, system_wide, &buf);
986 if(local)
987 free(local);
988 if(global)
989 free(global);
990 //if(system_wide)
991 // free(system_wide);
993 return !buf.seen;
996 int get_set_config(const char *key, char *value, CONFIG_TYPE type,char *git_path)
998 char *local,*global,*system_wide,*p;
999 int ret;
1000 local=global=system_wide=NULL;
1002 //local = config_exclusive_filename;
1003 if (!local) {
1004 char *home = getenv("HOME");
1006 if(!home)
1007 home=getenv("USERPROFILE");
1009 local=p= git_pathdup("config");
1010 if(git_path&&strlen(git_path))
1012 local=xstrdup(mkpath("%s/%s", git_path, p));
1013 free(p);
1015 if (git_config_global() && home)
1016 global = xstrdup(mkpath("%s/.gitconfig", home));
1017 if (git_config_system())
1018 system_wide = git_etc_gitconfig();
1021 switch(type)
1023 case CONFIG_LOCAL:
1024 config_exclusive_filename = local;
1025 break;
1026 case CONFIG_GLOBAL:
1027 config_exclusive_filename = global;
1028 break;
1029 case CONFIG_SYSTEM:
1030 config_exclusive_filename = system_wide;
1031 break;
1032 default:
1033 config_exclusive_filename = NULL;
1034 break;
1037 if(!config_exclusive_filename)
1038 return -1;
1040 ret = git_config_set(key, value);
1042 if(local)
1043 free(local);
1044 if(global)
1045 free(global);
1046 //if(system_wide)
1047 // free(system_wide);
1049 return ret;