Validate the drawing area after the custom painting of the control
[TortoiseGit.git] / ext / gitdll / gitdll.c
blobb5bf03c926c3d79baba8c4a8b8b6edf5cb932157
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"
38 #if 0
40 // This is an example of an exported variable
41 GITDLL_API int ngitdll=0;
43 // This is an example of an exported function.
44 GITDLL_API int fngitdll(void)
46 return 42;
49 // This is the constructor of a class that has been exported.
50 // see gitdll.h for the class definition
51 Cgitdll::Cgitdll()
53 return;
55 #endif
57 extern char g_last_error[];
58 void * g_prefix;
60 char * get_git_last_error()
62 return g_last_error;
65 extern void die_dll(const char *err, va_list params);
67 void dll_entry()
69 set_die_routine(die_dll);
72 int git_get_sha1(const char *name, GIT_HASH sha1)
74 return get_sha1(name,sha1);
77 static int convert_slash(char * path)
79 while(*path)
81 if(*path == '\\' )
82 *path = '/';
83 path++;
85 return 0;
88 int git_init()
90 char path[MAX_PATH+1];
91 char *prefix;
92 int ret;
93 size_t homesize;
95 _fmode = _O_BINARY;
96 _setmode(_fileno(stdin), _O_BINARY);
97 _setmode(_fileno(stdout), _O_BINARY);
98 _setmode(_fileno(stderr), _O_BINARY);
100 // set HOME if not set already
101 getenv_s(&homesize, NULL, 0, "HOME");
102 if (!homesize)
104 _wputenv_s(L"HOME", wget_windows_home_directory());
106 GetModuleFileName(NULL, path, MAX_PATH);
107 convert_slash(path);
109 git_extract_argv0_path(path);
110 g_prefix = prefix = setup_git_directory();
111 ret = git_config(git_default_config, NULL);
113 if (!homesize)
115 _putenv_s("HOME","");/* clear home evironment to avoid affact third part software*/
118 return ret;
121 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR *author, char *pbuff)
123 char *end;
125 author->Name=pbuff;
126 end=strchr(pbuff,'<');
127 if( end == 0)
129 return -1;
131 author->NameSize = end - pbuff - 1;
133 pbuff = end +1;
134 end = strchr(pbuff, '>');
135 if( end == 0)
136 return -1;
138 author->Email = pbuff ;
139 author->EmailSize = end - pbuff;
141 pbuff = end + 2;
143 author->Date = atol(pbuff);
144 end = strchr(pbuff, ' ');
145 if( end == 0 )
146 return -1;
148 pbuff=end;
149 author->TimeZone = atol(pbuff);
151 return 0;
154 int git_parse_commit(GIT_COMMIT *commit)
156 int ret = 0;
157 char *pbuf;
158 char *end;
159 struct commit *p;
161 p= (struct commit *)commit->m_pGitCommit;
163 memcpy(commit->m_hash,p->object.sha1,GIT_HASH_SIZE);
165 commit->m_Encode = NULL;
166 commit->m_EncodeSize = 0;
168 if(p->buffer == NULL)
169 return -1;
171 pbuf = p->buffer;
172 while(pbuf)
174 if (strncmp(pbuf, "author", 6) == 0)
176 ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);
177 if(ret)
178 return ret;
180 else if (strncmp(pbuf, "committer", 9) == 0)
182 ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10);
183 if(ret)
184 return ret;
186 pbuf = strchr(pbuf,'\n');
187 if(pbuf == NULL)
188 return -1;
190 else if (strncmp(pbuf, "encoding", 8) == 0)
192 pbuf += 9;
193 commit->m_Encode=pbuf;
194 end = strchr(pbuf,'\n');
195 commit->m_EncodeSize=end -pbuf;
198 // the headers end after the first empty line
199 else if (*pbuf == '\n')
201 pbuf++;
203 commit->m_Subject=pbuf;
204 end = strchr(pbuf,'\n');
205 if( end == 0)
206 commit->m_SubjectSize = strlen(pbuf);
207 else
209 commit->m_SubjectSize = end - pbuf;
210 pbuf = end +1;
211 commit->m_Body = pbuf;
212 commit->m_BodySize = strlen(pbuf);
213 return 0;
217 pbuf = strchr(pbuf,'\n');
218 if(pbuf)
219 pbuf ++;
221 return 0;
224 int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash)
226 int ret = 0;
228 struct commit *p;
230 memset(commit,0,sizeof(GIT_COMMIT));
232 commit->m_pGitCommit = p = lookup_commit(hash);
234 if(commit == NULL)
235 return -1;
237 if(p == NULL)
238 return -1;
240 ret = parse_commit(p);
241 if( ret )
242 return ret;
244 return git_parse_commit(commit);
247 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
249 struct commit *p = commit->m_pGitCommit;
251 if(list == NULL)
252 return -1;
254 *list = (GIT_COMMIT_LIST*)p->parents;
255 return 0;
257 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
259 struct commit_list *l = *(struct commit_list **)list;
260 if(list == NULL || l==NULL)
261 return -1;
263 if(hash)
264 memcpy(hash, l->item->object.sha1, GIT_HASH_SIZE);
266 *list = (GIT_COMMIT_LIST *)l->next;
267 return 0;
272 int git_free_commit(GIT_COMMIT *commit)
274 struct commit *p = commit->m_pGitCommit;
276 if( p->parents)
277 free_commit_list(p->parents);
279 if( p->buffer )
281 free(p->buffer);
282 p->buffer=NULL;
283 p->object.parsed=0;
284 p->parents=0;
285 p->tree=0;
287 memset(commit,0,sizeof(GIT_COMMIT));
288 return 0;
291 char **strtoargv(char *arg, int *size)
293 int count=0;
294 char *p=arg;
295 char **argv;
297 int i=0;
298 while(*p)
300 if(*p == '\\')
301 *p='/';
302 p++;
304 p=arg;
306 while(*p)
308 if(*p == ' ')
309 count ++;
310 p++;
313 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
314 p=(char*)(argv+count+2);
316 while(*arg)
318 if(*arg != ' ')
320 char space=' ';
321 argv[i]=p;
323 while(*arg)
325 if(*arg == '"')
327 arg++;
328 if(space == ' ')
329 space = '"';
330 else
331 space = ' ';
333 if((*arg == space) || (*arg == 0))
334 break;
336 *p++ = *arg++;
338 i++;
339 *p++=0;
341 if(*arg == 0)
342 break;
343 arg++;
345 argv[i]=NULL;
346 *size = i;
347 return argv;
349 int git_open_log(GIT_LOG * handle, char * arg)
351 struct rev_info *p_Rev;
352 char ** argv=0;
353 int argc=0;
354 unsigned int i=0;
355 struct setup_revision_opt opt;
357 /* clear flags */
358 unsigned int obj_size = get_max_object_index();
359 for(i =0; i<obj_size; i++)
361 struct object *ob= get_indexed_object(i);
362 if(ob)
363 ob->flags=0;
366 if(arg != NULL)
367 argv = strtoargv(arg,&argc);
369 p_Rev = malloc(sizeof(struct rev_info));
370 memset(p_Rev,0,sizeof(struct rev_info));
372 if(p_Rev == NULL)
373 return -1;
375 invalidate_ref_cache(NULL);
377 init_revisions(p_Rev, g_prefix);
378 p_Rev->diff = 1;
380 memset(&opt, 0, sizeof(opt));
381 opt.def = "HEAD";
383 cmd_log_init(argc, argv, g_prefix,p_Rev,&opt);
385 p_Rev->pPrivate = argv;
386 *handle = p_Rev;
387 return 0;
390 int git_get_log_firstcommit(GIT_LOG handle)
392 return prepare_revision_walk(handle);
395 int git_get_log_estimate_commit_count(GIT_LOG handle)
397 struct rev_info *p_Rev;
398 p_Rev=(struct rev_info *)handle;
400 return estimate_commit_count(p_Rev, p_Rev->commits);
403 int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit, int follow)
405 int ret =0;
407 if(commit == NULL)
408 return -1;
410 memset(commit, 0, sizeof(GIT_COMMIT));
412 commit->m_pGitCommit = get_revision(handle);
413 if( commit->m_pGitCommit == NULL)
414 return -2;
416 if (follow && !log_tree_commit(handle, commit->m_pGitCommit))
418 commit->m_ignore = 1;
419 return 0;
421 commit->m_ignore = 0;
423 ret=git_parse_commit(commit);
424 if(ret)
425 return ret;
427 return 0;
430 struct notes_tree **display_notes_trees;
431 int git_close_log(GIT_LOG handle)
433 if(handle)
435 struct rev_info *p_Rev;
436 p_Rev=(struct rev_info *)handle;
437 if(p_Rev->pPrivate)
438 free(p_Rev->pPrivate);
439 free(handle);
441 free_all_pack();
443 free_notes(*display_notes_trees);
444 display_notes_trees = 0;
445 return 0;
448 int git_open_diff(GIT_DIFF *diff, char * arg)
450 struct rev_info *p_Rev;
451 char ** argv=0;
452 int argc=0;
454 if(arg != NULL)
455 argv = strtoargv(arg,&argc);
457 p_Rev = malloc(sizeof(struct rev_info));
458 memset(p_Rev,0,sizeof(struct rev_info));
460 p_Rev->pPrivate = argv;
461 *diff = (GIT_DIFF)p_Rev;
463 init_revisions(p_Rev, g_prefix);
464 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
465 p_Rev->abbrev = 0;
466 p_Rev->diff = 1;
467 argc = setup_revisions(argc, argv, p_Rev, NULL);
469 return 0;
471 int git_close_diff(GIT_DIFF handle)
473 git_diff_flush(handle);
474 if(handle)
476 struct rev_info *p_Rev;
477 p_Rev=(struct rev_info *)handle;
478 if(p_Rev->pPrivate)
479 free(p_Rev->pPrivate);
480 free(handle);
482 return 0;
484 int git_diff_flush(GIT_DIFF diff)
486 struct diff_queue_struct *q = &diff_queued_diff;
487 struct rev_info *p_Rev;
488 int i;
489 p_Rev = (struct rev_info *)diff;
491 if(q->nr == 0)
492 return 0;
494 for (i = 0; i < q->nr; i++)
495 diff_free_filepair(q->queue[i]);
497 if(q->queue)
499 free(q->queue);
500 q->queue = NULL;
501 q->nr = q->alloc = 0;
504 if (p_Rev->diffopt.close_file)
505 fclose(p_Rev->diffopt.file);
507 free_diffstat_info(&p_Rev->diffstat);
508 return 0;
511 int git_root_diff(GIT_DIFF diff, GIT_HASH hash,GIT_FILE *file, int *count, int isstat)
513 int ret;
514 struct rev_info *p_Rev;
515 int i;
516 struct diff_queue_struct *q = &diff_queued_diff;
518 p_Rev = (struct rev_info *)diff;
520 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
522 if(ret)
523 return ret;
525 if(isstat)
527 diffcore_std(&p_Rev->diffopt);
529 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
530 for (i = 0; i < q->nr; i++) {
531 struct diff_filepair *p = q->queue[i];
532 //if (check_pair_status(p))
533 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
536 if(file)
537 *file = q;
538 if(count)
539 *count = q->nr;
541 return 0;
544 int git_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count,int isstat)
546 struct rev_info *p_Rev;
547 int ret;
548 int i;
549 struct diff_queue_struct *q = &diff_queued_diff;
551 p_Rev = (struct rev_info *)diff;
553 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
554 if( ret )
556 free_all_pack();
557 return ret;
560 if(isstat)
562 diffcore_std(&p_Rev->diffopt);
563 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
564 for (i = 0; i < q->nr; i++) {
565 struct diff_filepair *p = q->queue[i];
566 //if (check_pair_status(p))
567 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
570 free_all_pack();
571 if(file)
572 *file = q;
573 if(count)
574 *count = q->nr;
575 return 0;
578 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)
580 struct diff_queue_struct *q = &diff_queued_diff;
581 struct rev_info *p_Rev;
582 p_Rev = (struct rev_info *)diff;
584 q = (struct diff_queue_struct *)file;
585 if(file == 0)
586 return -1;
587 if(i>=q->nr)
588 return -1;
590 if(newname)
591 *newname = q->queue[i]->two->path;
593 if(oldname)
594 *oldname = q->queue[i]->one->path;
596 if(status)
597 *status = q->queue[i]->status;
599 if(p_Rev->diffstat.files)
601 int j;
602 for(j=0;j<p_Rev->diffstat.nr;j++)
604 if(strcmp(*newname,p_Rev->diffstat.files[j]->name)==0)
605 break;
607 if( j== p_Rev->diffstat.nr)
609 *IsBin=1;
610 *inc=0;
611 *dec=0;
612 return 0;
614 if(IsBin)
615 *IsBin = p_Rev->diffstat.files[j]->is_binary;
616 if(inc)
617 *inc = p_Rev->diffstat.files[j]->added;
618 if(dec)
619 *dec = p_Rev->diffstat.files[j]->deleted;
620 }else
622 *IsBin=1;
623 *inc=0;
624 *dec=0;
627 return 0;
630 int git_read_tree(GIT_HASH hash,read_tree_fn_t fn, void *context)
632 struct tree * root;
633 int ret;
634 reprepare_packed_git();
635 root = parse_tree_indirect(hash);
637 if (!root)
639 free_all_pack();
640 return -1;
642 ret = read_tree_recursive(root,NULL,NULL,0,NULL,fn,context);
643 free_all_pack();
644 return ret;
647 int git_add_exclude(const char *string, const char *base,
648 int baselen, struct exclude_list *which)
650 add_exclude(string, base, baselen, which);
651 return 0;
654 int git_create_exclude_list(EXCLUDE_LIST *which)
656 *which = malloc(sizeof(struct exclude_list));
657 memset(*which,0,sizeof(struct exclude_list));
658 return 0;
661 int git_free_exclude_list(EXCLUDE_LIST which)
663 int i=0;
664 struct exclude_list *p = (struct exclude_list *) which;
666 for(i=0; i<p->nr;i++)
668 free(p->excludes[i]);
670 free(p->excludes);
671 free(p);
672 return 0;
675 int git_check_excluded_1(const char *pathname,
676 int pathlen, const char *basename, int *dtype,
677 EXCLUDE_LIST el)
679 return excluded_from_list(pathname, pathlen, basename,dtype,el);
682 int git_get_notes(GIT_HASH hash, char **p_note)
684 struct strbuf sb;
685 size_t size;
686 strbuf_init(&sb,0);
687 format_display_notes(hash, &sb, "utf-8", 0);
688 *p_note = strbuf_detach(&sb,&size);
690 return 0;
693 struct cmd_struct {
694 const char *cmd;
695 int (*fn)(int, const char **, const char *);
696 int option;
699 #define RUN_SETUP (1<<0)
700 #define USE_PAGER (1<<1)
702 * require working tree to be present -- anything uses this needs
703 * RUN_SETUP for reading from the configuration file.
705 #define NEED_WORK_TREE (1<<2)
707 const char git_usage_string[] =
708 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
709 " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
710 " [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
711 " [-c name=value] [--help]\n"
712 " COMMAND [ARGS]";
714 const char git_more_info_string[] =
715 "See 'git help COMMAND' for more information on a specific command.";
717 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
718 int check_pager_config(const char *cmd)
720 return 0;
724 int git_run_cmd(char *cmd, char *arg)
727 int i=0;
728 char ** argv=0;
729 int argc=0;
731 static struct cmd_struct commands[] = {
732 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
733 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
734 { "annotate", cmd_annotate, RUN_SETUP },
735 { "apply", cmd_apply },
736 { "archive", cmd_archive },
737 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
738 { "blame", cmd_blame, RUN_SETUP },
739 { "branch", cmd_branch, RUN_SETUP },
740 { "bundle", cmd_bundle },
741 { "cat-file", cmd_cat_file, RUN_SETUP },
742 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
743 { "checkout-index", cmd_checkout_index,
744 RUN_SETUP | NEED_WORK_TREE},
745 { "check-ref-format", cmd_check_ref_format },
746 { "check-attr", cmd_check_attr, RUN_SETUP },
747 { "cherry", cmd_cherry, RUN_SETUP },
748 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
749 { "clone", cmd_clone },
750 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
751 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
752 { "commit-tree", cmd_commit_tree, RUN_SETUP },
753 { "config", cmd_config },
754 { "count-objects", cmd_count_objects, RUN_SETUP },
755 { "describe", cmd_describe, RUN_SETUP },
756 { "diff", cmd_diff },
757 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
758 { "diff-index", cmd_diff_index, RUN_SETUP },
759 { "diff-tree", cmd_diff_tree, RUN_SETUP },
760 { "fast-export", cmd_fast_export, RUN_SETUP },
761 { "fetch", cmd_fetch, RUN_SETUP },
762 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
763 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
764 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
765 { "format-patch", cmd_format_patch, RUN_SETUP },
766 { "fsck", cmd_fsck, RUN_SETUP },
767 { "fsck-objects", cmd_fsck, RUN_SETUP },
768 { "gc", cmd_gc, RUN_SETUP },
769 { "get-tar-commit-id", cmd_get_tar_commit_id },
770 { "grep", cmd_grep },
771 { "hash-object", cmd_hash_object },
772 { "help", cmd_help },
773 { "index-pack", cmd_index_pack },
774 { "init", cmd_init_db },
775 { "init-db", cmd_init_db },
776 { "log", cmd_log, RUN_SETUP },
777 { "ls-files", cmd_ls_files, RUN_SETUP },
778 { "ls-tree", cmd_ls_tree, RUN_SETUP },
779 { "ls-remote", cmd_ls_remote },
780 { "mailinfo", cmd_mailinfo },
781 { "mailsplit", cmd_mailsplit },
782 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
783 { "merge-base", cmd_merge_base, RUN_SETUP },
784 { "merge-file", cmd_merge_file },
785 { "merge-index", cmd_merge_index, RUN_SETUP },
786 { "merge-ours", cmd_merge_ours, RUN_SETUP },
787 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
788 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
789 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
790 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
791 { "merge-tree", cmd_merge_tree, RUN_SETUP },
792 { "mktag", cmd_mktag, RUN_SETUP },
793 { "mktree", cmd_mktree, RUN_SETUP },
794 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
795 { "name-rev", cmd_name_rev, RUN_SETUP },
796 { "notes", cmd_notes, RUN_SETUP },
797 { "pack-objects", cmd_pack_objects, RUN_SETUP },
798 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
799 { "patch-id", cmd_patch_id },
800 { "peek-remote", cmd_ls_remote },
801 { "pickaxe", cmd_blame, RUN_SETUP },
802 { "prune", cmd_prune, RUN_SETUP },
803 { "prune-packed", cmd_prune_packed, RUN_SETUP },
804 { "push", cmd_push, RUN_SETUP },
805 { "read-tree", cmd_read_tree, RUN_SETUP },
806 { "receive-pack", cmd_receive_pack },
807 { "reflog", cmd_reflog, RUN_SETUP },
808 { "remote", cmd_remote, RUN_SETUP },
809 { "replace", cmd_replace, RUN_SETUP },
810 { "repo-config", cmd_config },
811 { "rerere", cmd_rerere, RUN_SETUP },
812 { "reset", cmd_reset, RUN_SETUP },
813 { "rev-list", cmd_rev_list, RUN_SETUP },
814 { "rev-parse", cmd_rev_parse },
815 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
816 { "rm", cmd_rm, RUN_SETUP },
817 { "send-pack", cmd_send_pack, RUN_SETUP },
818 { "shortlog", cmd_shortlog, USE_PAGER },
819 { "show-branch", cmd_show_branch, RUN_SETUP },
820 { "show", cmd_show, RUN_SETUP },
821 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
822 { "stripspace", cmd_stripspace },
823 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
824 { "tag", cmd_tag, RUN_SETUP },
825 { "tar-tree", cmd_tar_tree },
826 { "unpack-file", cmd_unpack_file, RUN_SETUP },
827 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
828 { "update-index", cmd_update_index, RUN_SETUP },
829 { "update-ref", cmd_update_ref, RUN_SETUP },
830 { "update-server-info", cmd_update_server_info, RUN_SETUP },
831 { "upload-archive", cmd_upload_archive },
832 { "var", cmd_var },
833 { "verify-tag", cmd_verify_tag, RUN_SETUP },
834 { "version", cmd_version },
835 { "whatchanged", cmd_whatchanged, RUN_SETUP },
836 { "write-tree", cmd_write_tree, RUN_SETUP },
837 { "verify-pack", cmd_verify_pack },
838 { "show-ref", cmd_show_ref, RUN_SETUP },
839 { "pack-refs", cmd_pack_refs, RUN_SETUP },
842 git_init();
844 for(i=0;i< sizeof(commands) / sizeof(struct cmd_struct);i++)
846 if(strcmp(cmd,commands[i].cmd)==0)
848 int ret;
849 if(arg != NULL)
850 argv = strtoargv(arg,&argc);
852 ret = commands[i].fn(argc, argv, NULL);
854 if(argv)
855 free(argv);
857 discard_cache();
858 free_all_pack();
860 return ret;
865 return -1;
868 int git_for_each_ref_in(const char * refname, each_ref_fn fn, void * data)
870 int ret;
871 invalidate_ref_cache(NULL);
872 ret = for_each_ref_in(refname, fn, data);
873 free_all_pack();
874 return ret;
877 const char *git_resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
879 invalidate_ref_cache(NULL);
880 return resolve_ref_unsafe(ref,sha1,reading, flag);
882 int git_for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
884 return for_each_reflog_ent(ref,fn,cb_data);
887 int git_deref_tag(const unsigned char *tagsha1, GIT_HASH refhash)
889 struct object *obj = NULL;
890 obj = parse_object(tagsha1);
891 if (!obj)
892 return -1;
894 if (obj->type == OBJ_TAG)
896 obj = deref_tag(obj, "", 0);
897 if (!obj)
898 return -1;
900 memcpy(refhash, obj->sha1, sizeof(GIT_HASH));
901 return 0;
904 return -1;
907 static int update_some(const unsigned char *sha1, const char *base, int baselen,
908 const char *pathname, unsigned mode, int stage, void *context)
910 struct cache_entry *ce;
912 ce = (struct cache_entry *)context;
914 if (S_ISDIR(mode))
915 return READ_TREE_RECURSIVE;
917 hashcpy(ce->sha1, sha1);
918 memcpy(ce->name, base, baselen);
919 memcpy(ce->name + baselen, pathname, strlen(pathname));
920 ce->ce_flags = create_ce_flags(strlen(pathname)+baselen, 0);
921 ce->ce_mode = create_ce_mode(mode);
923 return 0;
926 int git_checkout_file(const char *ref, const char *path, const char *outputpath)
928 struct cache_entry *ce;
929 int ret;
930 GIT_HASH sha1;
931 struct tree * root;
932 struct checkout state;
933 struct pathspec pathspec;
934 const char *match[2];
935 ret = get_sha1(ref, sha1);
936 if(ret)
937 return ret;
939 reprepare_packed_git();
940 root = parse_tree_indirect(sha1);
942 if(!root)
944 free_all_pack();
945 return -1;
948 ce = xcalloc(1, cache_entry_size(strlen(path)));
950 match[0] = path;
951 match[1] = NULL;
953 init_pathspec(&pathspec, match);
954 pathspec.items[0].use_wildcard = 0;
955 ret = read_tree_recursive(root, "", 0, 0, &pathspec, update_some, ce);
956 free_pathspec(&pathspec);
958 if(ret)
960 free_all_pack();
961 free(ce);
962 return ret;
964 memset(&state, 0, sizeof(state));
965 state.force = 1;
966 state.refresh_cache = 0;
968 ret = write_entry(ce, outputpath, &state, 0);
969 free_all_pack();
970 free(ce);
971 return ret;
973 struct config_buf
975 char *buf;
976 char *key;
977 char *size;
978 int seen;
981 static int get_config(const char *key_, const char *value_, void *cb)
983 struct config_buf *buf;
984 buf=(struct config_buf*)cb;
985 if(strcmp(key_, buf->key))
986 return 0;
988 if (value_)
989 strncpy(buf->buf,value_,buf->size);
990 else
992 buf->buf[0] = 't';
993 buf->buf[1] = 'r';
994 buf->buf[2] = 'u';
995 buf->buf[3] = 'e';
996 buf->buf[4] = 0;
998 buf->seen = 1;
999 return 0;
1003 // wchar_t wrapper for git_etc_gitconfig()
1004 const wchar_t *wget_msysgit_etc(void)
1006 static const wchar_t *etc_gitconfig = NULL;
1007 wchar_t wpointer[MAX_PATH];
1009 if (etc_gitconfig)
1010 return etc_gitconfig;
1012 if (xutftowcs_path(wpointer, git_etc_gitconfig()) < 0)
1013 return NULL;
1015 etc_gitconfig = _wcsdup(wpointer);
1017 return etc_gitconfig;
1020 int git_get_config(const char *key, char *buffer, int size, char *git_path)
1022 char *local, *global, *globalxdg;
1023 const char *home, *system;
1024 struct config_buf buf;
1025 buf.buf=buffer;
1026 buf.size=size;
1027 buf.seen = 0;
1028 buf.key = key;
1030 home = get_windows_home_directory();
1031 if (home)
1033 global = xstrdup(mkpath("%s/.gitconfig", home));
1034 globalxdg = xstrdup(mkpath("%s/.config/git/config", home));
1036 else
1038 global = NULL;
1039 globalxdg = NULL;
1042 system = git_etc_gitconfig();
1044 local = git_pathdup("config");
1046 if ( !buf.seen)
1047 git_config_from_file(get_config, local, &buf);
1048 if (!buf.seen && global)
1049 git_config_from_file(get_config, global, &buf);
1050 if (!buf.seen && globalxdg)
1051 git_config_from_file(get_config, globalxdg, &buf);
1052 if (!buf.seen && system)
1053 git_config_from_file(get_config, system, &buf);
1055 if(local)
1056 free(local);
1057 if(global)
1058 free(global);
1059 if (globalxdg)
1060 free(globalxdg);
1062 return !buf.seen;
1065 // taken from msysgit: compat/mingw.c
1066 const char *get_windows_home_directory(void)
1068 static const char *home_directory = NULL;
1069 struct strbuf buf = STRBUF_INIT;
1071 if (home_directory)
1072 return home_directory;
1074 home_directory = getenv("HOME");
1075 if (home_directory && *home_directory)
1076 return home_directory;
1078 strbuf_addf(&buf, "%s/%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
1079 home_directory = strbuf_detach(&buf, NULL);
1081 return home_directory;
1084 // wchar_t wrapper for get_windows_home_directory()
1085 const wchar_t *wget_windows_home_directory(void)
1087 static const wchar_t *home_directory = NULL;
1088 wchar_t wpointer[MAX_PATH];
1090 if (home_directory)
1091 return home_directory;
1093 if (xutftowcs_path(wpointer, get_windows_home_directory()) < 0)
1094 return NULL;
1096 home_directory = _wcsdup(wpointer);
1098 return home_directory;
1101 int get_set_config(const char *key, char *value, CONFIG_TYPE type,char *git_path)
1103 char * config_exclusive_filename = NULL;
1104 switch(type)
1106 case CONFIG_LOCAL:
1107 config_exclusive_filename = git_pathdup("config");
1108 break;
1109 case CONFIG_GLOBAL:
1110 case CONFIG_XDGGLOBAL:
1112 const char *home = get_windows_home_directory();
1113 if (home)
1115 if (type == CONFIG_GLOBAL)
1116 config_exclusive_filename = xstrdup(mkpath("%s/.gitconfig", home));
1117 else
1118 config_exclusive_filename = xstrdup(mkpath("%s/.config/git/config", home));
1121 break;
1124 if(!config_exclusive_filename)
1125 return -1;
1127 return git_config_set_multivar_in_file(config_exclusive_filename, key, value, NULL, 0);