Refreshing commit dialog might reset index to an old state
[TortoiseGit.git] / ext / gitdll / gitdll.c
blob7e3f22ad06d529e02b2dd5e0f5c19a4a0993e2b8
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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"
38 const char git_version_string[] = GIT_VERSION;
41 #if 0
43 // This is an example of an exported variable
44 GITDLL_API int ngitdll=0;
46 // This is an example of an exported function.
47 GITDLL_API int fngitdll(void)
49 return 42;
52 // This is the constructor of a class that has been exported.
53 // see gitdll.h for the class definition
54 Cgitdll::Cgitdll()
56 return;
58 #endif
60 extern char g_last_error[];
61 void * g_prefix;
63 char * get_git_last_error()
65 return g_last_error;
68 extern void die_dll(const char *err, va_list params);
70 void dll_entry()
72 set_die_routine(die_dll);
75 int git_get_sha1(const char *name, GIT_HASH sha1)
77 return get_sha1(name,sha1);
80 static int convert_slash(char * path)
82 while(*path)
84 if(*path == '\\' )
85 *path = '/';
86 path++;
88 return 0;
91 int git_init()
93 char path[MAX_PATH+1];
94 char *prefix;
95 int ret;
96 size_t homesize;
98 _fmode = _O_BINARY;
99 _setmode(_fileno(stdin), _O_BINARY);
100 _setmode(_fileno(stdout), _O_BINARY);
101 _setmode(_fileno(stderr), _O_BINARY);
103 // set HOME if not set already
104 getenv_s(&homesize, NULL, 0, "HOME");
105 if (!homesize)
107 _wputenv_s(L"HOME", wget_windows_home_directory());
109 GetModuleFileName(NULL, path, MAX_PATH);
110 convert_slash(path);
112 git_extract_argv0_path(path);
113 g_prefix = prefix = setup_git_directory();
114 ret = git_config(git_default_config, NULL);
116 if (!homesize)
118 _putenv_s("HOME","");/* clear home evironment to avoid affact third part software*/
121 return ret;
124 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR *author, char *pbuff)
126 char *end;
128 author->Name=pbuff;
129 end=strchr(pbuff,'<');
130 if( end == 0)
132 return -1;
134 author->NameSize = end - pbuff - 1;
136 pbuff = end +1;
137 end = strchr(pbuff, '>');
138 if( end == 0)
139 return -1;
141 author->Email = pbuff ;
142 author->EmailSize = end - pbuff;
144 pbuff = end + 2;
146 author->Date = atol(pbuff);
147 end = strchr(pbuff, ' ');
148 if( end == 0 )
149 return -1;
151 pbuff=end;
152 author->TimeZone = atol(pbuff);
154 return 0;
157 int git_parse_commit(GIT_COMMIT *commit)
159 int ret = 0;
160 char *pbuf;
161 char *end;
162 struct commit *p;
164 p= (struct commit *)commit->m_pGitCommit;
166 memcpy(commit->m_hash,p->object.sha1,GIT_HASH_SIZE);
168 commit->m_Encode = NULL;
169 commit->m_EncodeSize = 0;
171 if(p->buffer == NULL)
172 return -1;
174 pbuf = p->buffer;
175 while(pbuf)
177 if( strncmp(pbuf,"author",6) == 0)
179 ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);
180 if(ret)
181 return ret;
183 if( strncmp(pbuf, "committer",9) == 0)
185 ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10);
186 if(ret)
187 return ret;
189 pbuf = strchr(pbuf,'\n');
190 if(pbuf == NULL)
191 return -1;
193 pbuf ++;
195 if( strncmp(pbuf, "encoding",8) == 0 )
197 pbuf += 9;
198 commit->m_Encode=pbuf;
199 end = strchr(pbuf,'\n');
200 commit->m_EncodeSize=end -pbuf;
202 pbuf = end +1;
205 while((*pbuf) && (*pbuf == '\n'))
206 pbuf ++;
208 commit->m_Subject=pbuf;
209 end = strchr(pbuf,'\n');
210 if( end == 0)
211 commit->m_SubjectSize = strlen(pbuf);
212 else
214 commit->m_SubjectSize = end - pbuf;
215 pbuf = end +1;
216 commit->m_Body = pbuf;
217 commit->m_BodySize = strlen(pbuf);
218 return 0;
223 pbuf = strchr(pbuf,'\n');
224 if(pbuf)
225 pbuf ++;
227 return 0;
230 int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash)
232 int ret = 0;
234 struct commit *p;
236 memset(commit,0,sizeof(GIT_COMMIT));
238 commit->m_pGitCommit = p = lookup_commit(hash);
240 if(commit == NULL)
241 return -1;
243 if(p == NULL)
244 return -1;
246 ret = parse_commit(p);
247 if( ret )
248 return ret;
250 return git_parse_commit(commit);
253 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
255 struct commit *p = commit->m_pGitCommit;
257 if(list == NULL)
258 return -1;
260 *list = (GIT_COMMIT_LIST*)p->parents;
261 return 0;
263 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
265 struct commit_list *l = *(struct commit_list **)list;
266 if(list == NULL || l==NULL)
267 return -1;
269 if(hash)
270 memcpy(hash, l->item->object.sha1, GIT_HASH_SIZE);
272 *list = (GIT_COMMIT_LIST *)l->next;
273 return 0;
278 int git_free_commit(GIT_COMMIT *commit)
280 struct commit *p = commit->m_pGitCommit;
282 if( p->parents)
283 free_commit_list(p->parents);
285 if( p->buffer )
287 free(p->buffer);
288 p->buffer=NULL;
289 p->object.parsed=0;
290 p->parents=0;
291 p->tree=0;
293 memset(commit,0,sizeof(GIT_COMMIT));
294 return 0;
297 char **strtoargv(char *arg, int *size)
299 int count=0;
300 char *p=arg;
301 char **argv;
303 int i=0;
304 while(*p)
306 if(*p == '\\')
307 *p='/';
308 p++;
310 p=arg;
312 while(*p)
314 if(*p == ' ')
315 count ++;
316 p++;
319 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
320 p=(char*)(argv+count+2);
322 while(*arg)
324 if(*arg != ' ')
326 char space=' ';
327 argv[i]=p;
329 while(*arg)
331 if(*arg == '"')
333 arg++;
334 if(space == ' ')
335 space = '"';
336 else
337 space = ' ';
339 if((*arg == space) || (*arg == 0))
340 break;
342 *p++ = *arg++;
344 i++;
345 *p++=0;
347 if(*arg == 0)
348 break;
349 arg++;
351 argv[i]=NULL;
352 *size = i;
353 return argv;
355 int git_open_log(GIT_LOG * handle, char * arg)
357 struct rev_info *p_Rev;
358 char ** argv=0;
359 int argc=0;
360 unsigned int i=0;
361 struct setup_revision_opt opt;
363 /* clear flags */
364 unsigned int obj_size = get_max_object_index();
365 for(i =0; i<obj_size; i++)
367 struct object *ob= get_indexed_object(i);
368 if(ob)
369 ob->flags=0;
372 if(arg != NULL)
373 argv = strtoargv(arg,&argc);
375 p_Rev = malloc(sizeof(struct rev_info));
376 memset(p_Rev,0,sizeof(struct rev_info));
378 if(p_Rev == NULL)
379 return -1;
381 init_revisions(p_Rev, g_prefix);
382 p_Rev->diff = 1;
384 memset(&opt, 0, sizeof(opt));
385 opt.def = "HEAD";
387 cmd_log_init(argc, argv, g_prefix,p_Rev,&opt);
389 p_Rev->pPrivate = argv;
390 *handle = p_Rev;
391 return 0;
394 int git_get_log_firstcommit(GIT_LOG handle)
396 return prepare_revision_walk(handle);
399 int git_get_log_estimate_commit_count(GIT_LOG handle)
401 struct rev_info *p_Rev;
402 p_Rev=(struct rev_info *)handle;
404 return estimate_commit_count(p_Rev, p_Rev->commits);
407 int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit, int follow)
409 int ret =0;
411 if(commit == NULL)
412 return -1;
414 memset(commit, 0, sizeof(GIT_COMMIT));
416 commit->m_pGitCommit = get_revision(handle);
417 if( commit->m_pGitCommit == NULL)
418 return -2;
420 if (follow && !log_tree_commit(handle, commit->m_pGitCommit))
422 commit->m_ignore = 1;
423 return 0;
425 commit->m_ignore = 0;
427 ret=git_parse_commit(commit);
428 if(ret)
429 return ret;
431 return 0;
434 struct notes_tree **display_notes_trees;
435 int git_close_log(GIT_LOG handle)
437 if(handle)
439 struct rev_info *p_Rev;
440 p_Rev=(struct rev_info *)handle;
441 if(p_Rev->pPrivate)
442 free(p_Rev->pPrivate);
443 free(handle);
445 free_all_pack();
447 free_notes(*display_notes_trees);
448 display_notes_trees = 0;
449 return 0;
452 int git_open_diff(GIT_DIFF *diff, char * arg)
454 struct rev_info *p_Rev;
455 char ** argv=0;
456 int argc=0;
458 if(arg != NULL)
459 argv = strtoargv(arg,&argc);
461 p_Rev = malloc(sizeof(struct rev_info));
462 memset(p_Rev,0,sizeof(struct rev_info));
464 p_Rev->pPrivate = argv;
465 *diff = (GIT_DIFF)p_Rev;
467 init_revisions(p_Rev, g_prefix);
468 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
469 p_Rev->abbrev = 0;
470 p_Rev->diff = 1;
471 argc = setup_revisions(argc, argv, p_Rev, NULL);
473 return 0;
475 int git_close_diff(GIT_DIFF handle)
477 git_diff_flush(handle);
478 if(handle)
480 struct rev_info *p_Rev;
481 p_Rev=(struct rev_info *)handle;
482 if(p_Rev->pPrivate)
483 free(p_Rev->pPrivate);
484 free(handle);
486 return 0;
488 int git_diff_flush(GIT_DIFF diff)
490 struct diff_queue_struct *q = &diff_queued_diff;
491 struct rev_info *p_Rev;
492 int i;
493 p_Rev = (struct rev_info *)diff;
495 if(q->nr == 0)
496 return 0;
498 for (i = 0; i < q->nr; i++)
499 diff_free_filepair(q->queue[i]);
501 if(q->queue)
503 free(q->queue);
504 q->queue = NULL;
505 q->nr = q->alloc = 0;
508 if (p_Rev->diffopt.close_file)
509 fclose(p_Rev->diffopt.file);
511 free_diffstat_info(&p_Rev->diffstat);
512 return 0;
515 int git_root_diff(GIT_DIFF diff, GIT_HASH hash,GIT_FILE *file, int *count, int isstat)
517 int ret;
518 struct rev_info *p_Rev;
519 int i;
520 struct diff_queue_struct *q = &diff_queued_diff;
522 p_Rev = (struct rev_info *)diff;
524 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
526 if(ret)
527 return ret;
529 if(isstat)
531 diffcore_std(&p_Rev->diffopt);
533 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
534 for (i = 0; i < q->nr; i++) {
535 struct diff_filepair *p = q->queue[i];
536 //if (check_pair_status(p))
537 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
540 if(file)
541 *file = q;
542 if(count)
543 *count = q->nr;
545 return 0;
548 int git_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count,int isstat)
550 struct rev_info *p_Rev;
551 int ret;
552 int i;
553 struct diff_queue_struct *q = &diff_queued_diff;
555 p_Rev = (struct rev_info *)diff;
557 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
558 if( ret )
560 free_all_pack();
561 return ret;
564 if(isstat)
566 diffcore_std(&p_Rev->diffopt);
567 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
568 for (i = 0; i < q->nr; i++) {
569 struct diff_filepair *p = q->queue[i];
570 //if (check_pair_status(p))
571 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
574 free_all_pack();
575 if(file)
576 *file = q;
577 if(count)
578 *count = q->nr;
579 return 0;
582 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)
584 struct diff_queue_struct *q = &diff_queued_diff;
585 struct rev_info *p_Rev;
586 p_Rev = (struct rev_info *)diff;
588 q = (struct diff_queue_struct *)file;
589 if(file == 0)
590 return -1;
591 if(i>=q->nr)
592 return -1;
594 if(newname)
595 *newname = q->queue[i]->two->path;
597 if(oldname)
598 *oldname = q->queue[i]->one->path;
600 if(status)
601 *status = q->queue[i]->status;
603 if(p_Rev->diffstat.files)
605 int j;
606 for(j=0;j<p_Rev->diffstat.nr;j++)
608 if(strcmp(*newname,p_Rev->diffstat.files[j]->name)==0)
609 break;
611 if( j== p_Rev->diffstat.nr)
613 *IsBin=1;
614 *inc=0;
615 *dec=0;
616 return 0;
618 if(IsBin)
619 *IsBin = p_Rev->diffstat.files[j]->is_binary;
620 if(inc)
621 *inc = p_Rev->diffstat.files[j]->added;
622 if(dec)
623 *dec = p_Rev->diffstat.files[j]->deleted;
624 }else
626 *IsBin=1;
627 *inc=0;
628 *dec=0;
631 return 0;
634 int git_read_tree(GIT_HASH hash,read_tree_fn_t fn, void *context)
636 struct tree * root;
637 int ret;
638 reprepare_packed_git();
639 root = parse_tree_indirect(hash);
641 if (!root)
643 free_all_pack();
644 return -1;
646 ret = read_tree_recursive(root,NULL,NULL,0,NULL,fn,context);
647 free_all_pack();
648 return ret;
651 int git_add_exclude(const char *string, const char *base,
652 int baselen, struct exclude_list *which)
654 add_exclude(string, base, baselen, which);
655 return 0;
658 int git_create_exclude_list(EXCLUDE_LIST *which)
660 *which = malloc(sizeof(struct exclude_list));
661 memset(*which,0,sizeof(struct exclude_list));
662 return 0;
665 int git_free_exclude_list(EXCLUDE_LIST which)
667 int i=0;
668 struct exclude_list *p = (struct exclude_list *) which;
670 for(i=0; i<p->nr;i++)
672 free(p->excludes[i]);
674 free(p->excludes);
675 free(p);
676 return 0;
679 int git_check_excluded_1(const char *pathname,
680 int pathlen, const char *basename, int *dtype,
681 EXCLUDE_LIST el)
683 return excluded_from_list(pathname, pathlen, basename,dtype,el);
686 int git_get_notes(GIT_HASH hash, char **p_note)
688 struct strbuf sb;
689 size_t size;
690 strbuf_init(&sb,0);
691 format_display_notes(hash, &sb, "utf-8", 0);
692 *p_note = strbuf_detach(&sb,&size);
694 return 0;
697 struct cmd_struct {
698 const char *cmd;
699 int (*fn)(int, const char **, const char *);
700 int option;
703 #define RUN_SETUP (1<<0)
704 #define USE_PAGER (1<<1)
706 * require working tree to be present -- anything uses this needs
707 * RUN_SETUP for reading from the configuration file.
709 #define NEED_WORK_TREE (1<<2)
711 const char git_usage_string[] =
712 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
713 " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
714 " [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
715 " [-c name=value] [--help]\n"
716 " COMMAND [ARGS]";
718 const char git_more_info_string[] =
719 "See 'git help COMMAND' for more information on a specific command.";
721 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
722 int check_pager_config(const char *cmd)
724 return 0;
728 int git_run_cmd(char *cmd, char *arg)
731 int i=0;
732 char ** argv=0;
733 int argc=0;
735 static struct cmd_struct commands[] = {
736 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
737 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
738 { "annotate", cmd_annotate, RUN_SETUP },
739 { "apply", cmd_apply },
740 { "archive", cmd_archive },
741 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
742 { "blame", cmd_blame, RUN_SETUP },
743 { "branch", cmd_branch, RUN_SETUP },
744 { "bundle", cmd_bundle },
745 { "cat-file", cmd_cat_file, RUN_SETUP },
746 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
747 { "checkout-index", cmd_checkout_index,
748 RUN_SETUP | NEED_WORK_TREE},
749 { "check-ref-format", cmd_check_ref_format },
750 { "check-attr", cmd_check_attr, RUN_SETUP },
751 { "cherry", cmd_cherry, RUN_SETUP },
752 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
753 { "clone", cmd_clone },
754 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
755 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
756 { "commit-tree", cmd_commit_tree, RUN_SETUP },
757 { "config", cmd_config },
758 { "count-objects", cmd_count_objects, RUN_SETUP },
759 { "describe", cmd_describe, RUN_SETUP },
760 { "diff", cmd_diff },
761 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
762 { "diff-index", cmd_diff_index, RUN_SETUP },
763 { "diff-tree", cmd_diff_tree, RUN_SETUP },
764 { "fast-export", cmd_fast_export, RUN_SETUP },
765 { "fetch", cmd_fetch, RUN_SETUP },
766 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
767 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
768 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
769 { "format-patch", cmd_format_patch, RUN_SETUP },
770 { "fsck", cmd_fsck, RUN_SETUP },
771 { "fsck-objects", cmd_fsck, RUN_SETUP },
772 { "gc", cmd_gc, RUN_SETUP },
773 { "get-tar-commit-id", cmd_get_tar_commit_id },
774 { "grep", cmd_grep },
775 { "hash-object", cmd_hash_object },
776 { "help", cmd_help },
777 { "index-pack", cmd_index_pack },
778 { "init", cmd_init_db },
779 { "init-db", cmd_init_db },
780 { "log", cmd_log, RUN_SETUP },
781 { "ls-files", cmd_ls_files, RUN_SETUP },
782 { "ls-tree", cmd_ls_tree, RUN_SETUP },
783 { "ls-remote", cmd_ls_remote },
784 { "mailinfo", cmd_mailinfo },
785 { "mailsplit", cmd_mailsplit },
786 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
787 { "merge-base", cmd_merge_base, RUN_SETUP },
788 { "merge-file", cmd_merge_file },
789 { "merge-index", cmd_merge_index, RUN_SETUP },
790 { "merge-ours", cmd_merge_ours, RUN_SETUP },
791 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
792 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
793 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
794 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
795 { "merge-tree", cmd_merge_tree, RUN_SETUP },
796 { "mktag", cmd_mktag, RUN_SETUP },
797 { "mktree", cmd_mktree, RUN_SETUP },
798 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
799 { "name-rev", cmd_name_rev, RUN_SETUP },
800 { "notes", cmd_notes, RUN_SETUP },
801 { "pack-objects", cmd_pack_objects, RUN_SETUP },
802 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
803 { "patch-id", cmd_patch_id },
804 { "peek-remote", cmd_ls_remote },
805 { "pickaxe", cmd_blame, RUN_SETUP },
806 { "prune", cmd_prune, RUN_SETUP },
807 { "prune-packed", cmd_prune_packed, RUN_SETUP },
808 { "push", cmd_push, RUN_SETUP },
809 { "read-tree", cmd_read_tree, RUN_SETUP },
810 { "receive-pack", cmd_receive_pack },
811 { "reflog", cmd_reflog, RUN_SETUP },
812 { "remote", cmd_remote, RUN_SETUP },
813 { "replace", cmd_replace, RUN_SETUP },
814 { "repo-config", cmd_config },
815 { "rerere", cmd_rerere, RUN_SETUP },
816 { "reset", cmd_reset, RUN_SETUP },
817 { "rev-list", cmd_rev_list, RUN_SETUP },
818 { "rev-parse", cmd_rev_parse },
819 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
820 { "rm", cmd_rm, RUN_SETUP },
821 { "send-pack", cmd_send_pack, RUN_SETUP },
822 { "shortlog", cmd_shortlog, USE_PAGER },
823 { "show-branch", cmd_show_branch, RUN_SETUP },
824 { "show", cmd_show, RUN_SETUP },
825 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
826 { "stripspace", cmd_stripspace },
827 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
828 { "tag", cmd_tag, RUN_SETUP },
829 { "tar-tree", cmd_tar_tree },
830 { "unpack-file", cmd_unpack_file, RUN_SETUP },
831 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
832 { "update-index", cmd_update_index, RUN_SETUP },
833 { "update-ref", cmd_update_ref, RUN_SETUP },
834 { "update-server-info", cmd_update_server_info, RUN_SETUP },
835 { "upload-archive", cmd_upload_archive },
836 { "var", cmd_var },
837 { "verify-tag", cmd_verify_tag, RUN_SETUP },
838 { "version", cmd_version },
839 { "whatchanged", cmd_whatchanged, RUN_SETUP },
840 { "write-tree", cmd_write_tree, RUN_SETUP },
841 { "verify-pack", cmd_verify_pack },
842 { "show-ref", cmd_show_ref, RUN_SETUP },
843 { "pack-refs", cmd_pack_refs, RUN_SETUP },
846 git_init();
848 for(i=0;i< sizeof(commands) / sizeof(struct cmd_struct);i++)
850 if(strcmp(cmd,commands[i].cmd)==0)
852 int ret;
853 if(arg != NULL)
854 argv = strtoargv(arg,&argc);
856 ret = commands[i].fn(argc, argv, NULL);
858 if(argv)
859 free(argv);
861 discard_cache();
862 free_all_pack();
864 return ret;
869 return -1;
872 int git_for_each_ref_in(const char * refname, each_ref_fn fn, void * data)
874 int ret;
875 invalidate_ref_cache(NULL);
876 ret = for_each_ref_in(refname, fn, data);
877 free_all_pack();
878 return ret;
881 const char *git_resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
883 invalidate_ref_cache(NULL);
884 return resolve_ref_unsafe(ref,sha1,reading, flag);
886 int git_for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
888 return for_each_reflog_ent(ref,fn,cb_data);
891 int git_deref_tag(const unsigned char *tagsha1, GIT_HASH refhash)
893 struct object *obj = NULL;
894 obj = parse_object(tagsha1);
895 if (!obj)
896 return -1;
898 if (obj->type == OBJ_TAG)
900 obj = deref_tag(obj, "", 0);
901 if (!obj)
902 return -1;
904 memcpy(refhash, obj->sha1, sizeof(GIT_HASH));
905 return 0;
908 return -1;
911 static int update_some(const unsigned char *sha1, const char *base, int baselen,
912 const char *pathname, unsigned mode, int stage, void *context)
914 struct cache_entry *ce;
916 ce = (struct cache_entry *)context;
918 if (S_ISDIR(mode))
919 return READ_TREE_RECURSIVE;
921 hashcpy(ce->sha1, sha1);
922 memcpy(ce->name, base, baselen);
923 memcpy(ce->name + baselen, pathname, strlen(pathname));
924 ce->ce_flags = create_ce_flags(strlen(pathname)+baselen, 0);
925 ce->ce_mode = create_ce_mode(mode);
927 return 0;
930 int git_checkout_file(const char *ref, const char *path, const char *outputpath)
932 struct cache_entry *ce;
933 int ret;
934 GIT_HASH sha1;
935 struct tree * root;
936 struct checkout state;
937 struct pathspec pathspec;
938 const char *match[2];
939 ret = get_sha1(ref, sha1);
940 if(ret)
941 return ret;
943 reprepare_packed_git();
944 root = parse_tree_indirect(sha1);
946 if(!root)
948 free_all_pack();
949 return -1;
952 ce = xcalloc(1, cache_entry_size(strlen(path)));
954 match[0] = path;
955 match[1] = NULL;
957 init_pathspec(&pathspec, match);
958 pathspec.items[0].use_wildcard = 0;
959 ret = read_tree_recursive(root, "", 0, 0, &pathspec, update_some, ce);
960 free_pathspec(&pathspec);
962 if(ret)
964 free_all_pack();
965 free(ce);
966 return ret;
968 memset(&state, 0, sizeof(state));
969 state.force = 1;
970 state.refresh_cache = 0;
972 ret = write_entry(ce, outputpath, &state, 0);
973 free_all_pack();
974 free(ce);
975 return ret;
977 struct config_buf
979 char *buf;
980 char *key;
981 char *size;
982 int seen;
985 static int get_config(const char *key_, const char *value_, void *cb)
987 struct config_buf *buf;
988 buf=(struct config_buf*)cb;
989 if(strcmp(key_, buf->key))
990 return 0;
992 if (value_)
993 strncpy(buf->buf,value_,buf->size);
994 else
996 buf->buf[0] = 't';
997 buf->buf[1] = 'r';
998 buf->buf[2] = 'u';
999 buf->buf[3] = 'e';
1000 buf->buf[4] = 0;
1002 buf->seen = 1;
1003 return 0;
1007 // wchar_t wrapper for git_etc_gitconfig()
1008 const wchar_t *wget_msysgit_etc(void)
1010 static const wchar_t *etc_gitconfig = NULL;
1011 wchar_t wpointer[MAX_PATH];
1013 if (etc_gitconfig)
1014 return etc_gitconfig;
1016 if (xutftowcs_path(wpointer, git_etc_gitconfig()) < 0)
1017 return NULL;
1019 etc_gitconfig = _wcsdup(wpointer);
1021 return etc_gitconfig;
1024 int git_get_config(const char *key, char *buffer, int size, char *git_path)
1026 char *local, *global;
1027 const char *home, *system;
1028 struct config_buf buf;
1029 buf.buf=buffer;
1030 buf.size=size;
1031 buf.seen = 0;
1032 buf.key = key;
1034 local=global=system=NULL;
1036 home = get_windows_home_directory();
1037 if (home)
1038 global = xstrdup(mkpath("%s/.gitconfig", home));
1040 system = git_etc_gitconfig();
1042 local = git_pathdup("config");
1044 if ( !buf.seen)
1045 git_config_from_file(get_config, local, &buf);
1046 if (!buf.seen && global)
1047 git_config_from_file(get_config, global, &buf);
1048 if (!buf.seen && system)
1049 git_config_from_file(get_config, system, &buf);
1051 if(local)
1052 free(local);
1053 if(global)
1054 free(global);
1056 return !buf.seen;
1059 // taken from msysgit: compat/mingw.c
1060 const char *get_windows_home_directory(void)
1062 static const char *home_directory = NULL;
1063 struct strbuf buf = STRBUF_INIT;
1065 if (home_directory)
1066 return home_directory;
1068 home_directory = getenv("HOME");
1069 if (home_directory && *home_directory)
1070 return home_directory;
1072 strbuf_addf(&buf, "%s/%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
1073 home_directory = strbuf_detach(&buf, NULL);
1075 return home_directory;
1078 // wchar_t wrapper for get_windows_home_directory()
1079 const wchar_t *wget_windows_home_directory(void)
1081 static const wchar_t *home_directory = NULL;
1082 wchar_t wpointer[MAX_PATH];
1084 if (home_directory)
1085 return home_directory;
1087 if (xutftowcs_path(wpointer, get_windows_home_directory()) < 0)
1088 return NULL;
1090 home_directory = _wcsdup(wpointer);
1092 return home_directory;
1095 int get_set_config(const char *key, char *value, CONFIG_TYPE type,char *git_path)
1097 char * config_exclusive_filename;
1098 switch(type)
1100 case CONFIG_LOCAL:
1101 config_exclusive_filename = git_pathdup("config");
1102 break;
1103 case CONFIG_GLOBAL:
1105 const char *home = get_windows_home_directory();
1106 if (home)
1108 config_exclusive_filename = xstrdup(mkpath("%s/.gitconfig", home));
1111 break;
1112 default:
1113 config_exclusive_filename = NULL;
1114 break;
1117 if(!config_exclusive_filename)
1118 return -1;
1120 return git_config_set_multivar_in_file(config_exclusive_filename, key, value, NULL, 0);