Correctly parse commit headers
[TortoiseGit.git] / ext / gitdll / gitdll.c
blobb919ba81b89b31975d6990539030fe6b032edba3
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 else 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 else if (strncmp(pbuf, "encoding", 8) == 0)
195 pbuf += 9;
196 commit->m_Encode=pbuf;
197 end = strchr(pbuf,'\n');
198 commit->m_EncodeSize=end -pbuf;
201 // the headers end after the first empty line
202 else if (*pbuf == '\n')
204 pbuf++;
206 commit->m_Subject=pbuf;
207 end = strchr(pbuf,'\n');
208 if( end == 0)
209 commit->m_SubjectSize = strlen(pbuf);
210 else
212 commit->m_SubjectSize = end - pbuf;
213 pbuf = end +1;
214 commit->m_Body = pbuf;
215 commit->m_BodySize = strlen(pbuf);
216 return 0;
220 pbuf = strchr(pbuf,'\n');
221 if(pbuf)
222 pbuf ++;
224 return 0;
227 int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash)
229 int ret = 0;
231 struct commit *p;
233 memset(commit,0,sizeof(GIT_COMMIT));
235 commit->m_pGitCommit = p = lookup_commit(hash);
237 if(commit == NULL)
238 return -1;
240 if(p == NULL)
241 return -1;
243 ret = parse_commit(p);
244 if( ret )
245 return ret;
247 return git_parse_commit(commit);
250 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
252 struct commit *p = commit->m_pGitCommit;
254 if(list == NULL)
255 return -1;
257 *list = (GIT_COMMIT_LIST*)p->parents;
258 return 0;
260 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
262 struct commit_list *l = *(struct commit_list **)list;
263 if(list == NULL || l==NULL)
264 return -1;
266 if(hash)
267 memcpy(hash, l->item->object.sha1, GIT_HASH_SIZE);
269 *list = (GIT_COMMIT_LIST *)l->next;
270 return 0;
275 int git_free_commit(GIT_COMMIT *commit)
277 struct commit *p = commit->m_pGitCommit;
279 if( p->parents)
280 free_commit_list(p->parents);
282 if( p->buffer )
284 free(p->buffer);
285 p->buffer=NULL;
286 p->object.parsed=0;
287 p->parents=0;
288 p->tree=0;
290 memset(commit,0,sizeof(GIT_COMMIT));
291 return 0;
294 char **strtoargv(char *arg, int *size)
296 int count=0;
297 char *p=arg;
298 char **argv;
300 int i=0;
301 while(*p)
303 if(*p == '\\')
304 *p='/';
305 p++;
307 p=arg;
309 while(*p)
311 if(*p == ' ')
312 count ++;
313 p++;
316 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
317 p=(char*)(argv+count+2);
319 while(*arg)
321 if(*arg != ' ')
323 char space=' ';
324 argv[i]=p;
326 while(*arg)
328 if(*arg == '"')
330 arg++;
331 if(space == ' ')
332 space = '"';
333 else
334 space = ' ';
336 if((*arg == space) || (*arg == 0))
337 break;
339 *p++ = *arg++;
341 i++;
342 *p++=0;
344 if(*arg == 0)
345 break;
346 arg++;
348 argv[i]=NULL;
349 *size = i;
350 return argv;
352 int git_open_log(GIT_LOG * handle, char * arg)
354 struct rev_info *p_Rev;
355 char ** argv=0;
356 int argc=0;
357 unsigned int i=0;
358 struct setup_revision_opt opt;
360 /* clear flags */
361 unsigned int obj_size = get_max_object_index();
362 for(i =0; i<obj_size; i++)
364 struct object *ob= get_indexed_object(i);
365 if(ob)
366 ob->flags=0;
369 if(arg != NULL)
370 argv = strtoargv(arg,&argc);
372 p_Rev = malloc(sizeof(struct rev_info));
373 memset(p_Rev,0,sizeof(struct rev_info));
375 if(p_Rev == NULL)
376 return -1;
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 free_notes(*display_notes_trees);
445 display_notes_trees = 0;
446 return 0;
449 int git_open_diff(GIT_DIFF *diff, char * arg)
451 struct rev_info *p_Rev;
452 char ** argv=0;
453 int argc=0;
455 if(arg != NULL)
456 argv = strtoargv(arg,&argc);
458 p_Rev = malloc(sizeof(struct rev_info));
459 memset(p_Rev,0,sizeof(struct rev_info));
461 p_Rev->pPrivate = argv;
462 *diff = (GIT_DIFF)p_Rev;
464 init_revisions(p_Rev, g_prefix);
465 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
466 p_Rev->abbrev = 0;
467 p_Rev->diff = 1;
468 argc = setup_revisions(argc, argv, p_Rev, NULL);
470 return 0;
472 int git_close_diff(GIT_DIFF handle)
474 git_diff_flush(handle);
475 if(handle)
477 struct rev_info *p_Rev;
478 p_Rev=(struct rev_info *)handle;
479 if(p_Rev->pPrivate)
480 free(p_Rev->pPrivate);
481 free(handle);
483 return 0;
485 int git_diff_flush(GIT_DIFF diff)
487 struct diff_queue_struct *q = &diff_queued_diff;
488 struct rev_info *p_Rev;
489 int i;
490 p_Rev = (struct rev_info *)diff;
492 if(q->nr == 0)
493 return 0;
495 for (i = 0; i < q->nr; i++)
496 diff_free_filepair(q->queue[i]);
498 if(q->queue)
500 free(q->queue);
501 q->queue = NULL;
502 q->nr = q->alloc = 0;
505 if (p_Rev->diffopt.close_file)
506 fclose(p_Rev->diffopt.file);
508 free_diffstat_info(&p_Rev->diffstat);
509 return 0;
512 int git_root_diff(GIT_DIFF diff, GIT_HASH hash,GIT_FILE *file, int *count, int isstat)
514 int ret;
515 struct rev_info *p_Rev;
516 int i;
517 struct diff_queue_struct *q = &diff_queued_diff;
519 p_Rev = (struct rev_info *)diff;
521 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
523 if(ret)
524 return ret;
526 if(isstat)
528 diffcore_std(&p_Rev->diffopt);
530 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
531 for (i = 0; i < q->nr; i++) {
532 struct diff_filepair *p = q->queue[i];
533 //if (check_pair_status(p))
534 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
537 if(file)
538 *file = q;
539 if(count)
540 *count = q->nr;
542 return 0;
545 int git_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count,int isstat)
547 struct rev_info *p_Rev;
548 int ret;
549 int i;
550 struct diff_queue_struct *q = &diff_queued_diff;
552 p_Rev = (struct rev_info *)diff;
554 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
555 if( ret )
557 free_all_pack();
558 return ret;
561 if(isstat)
563 diffcore_std(&p_Rev->diffopt);
564 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
565 for (i = 0; i < q->nr; i++) {
566 struct diff_filepair *p = q->queue[i];
567 //if (check_pair_status(p))
568 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
571 free_all_pack();
572 if(file)
573 *file = q;
574 if(count)
575 *count = q->nr;
576 return 0;
579 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)
581 struct diff_queue_struct *q = &diff_queued_diff;
582 struct rev_info *p_Rev;
583 p_Rev = (struct rev_info *)diff;
585 q = (struct diff_queue_struct *)file;
586 if(file == 0)
587 return -1;
588 if(i>=q->nr)
589 return -1;
591 if(newname)
592 *newname = q->queue[i]->two->path;
594 if(oldname)
595 *oldname = q->queue[i]->one->path;
597 if(status)
598 *status = q->queue[i]->status;
600 if(p_Rev->diffstat.files)
602 int j;
603 for(j=0;j<p_Rev->diffstat.nr;j++)
605 if(strcmp(*newname,p_Rev->diffstat.files[j]->name)==0)
606 break;
608 if( j== p_Rev->diffstat.nr)
610 *IsBin=1;
611 *inc=0;
612 *dec=0;
613 return 0;
615 if(IsBin)
616 *IsBin = p_Rev->diffstat.files[j]->is_binary;
617 if(inc)
618 *inc = p_Rev->diffstat.files[j]->added;
619 if(dec)
620 *dec = p_Rev->diffstat.files[j]->deleted;
621 }else
623 *IsBin=1;
624 *inc=0;
625 *dec=0;
628 return 0;
631 int git_read_tree(GIT_HASH hash,read_tree_fn_t fn, void *context)
633 struct tree * root;
634 int ret;
635 reprepare_packed_git();
636 root = parse_tree_indirect(hash);
638 if (!root)
640 free_all_pack();
641 return -1;
643 ret = read_tree_recursive(root,NULL,NULL,0,NULL,fn,context);
644 free_all_pack();
645 return ret;
648 int git_add_exclude(const char *string, const char *base,
649 int baselen, struct exclude_list *which)
651 add_exclude(string, base, baselen, which);
652 return 0;
655 int git_create_exclude_list(EXCLUDE_LIST *which)
657 *which = malloc(sizeof(struct exclude_list));
658 memset(*which,0,sizeof(struct exclude_list));
659 return 0;
662 int git_free_exclude_list(EXCLUDE_LIST which)
664 int i=0;
665 struct exclude_list *p = (struct exclude_list *) which;
667 for(i=0; i<p->nr;i++)
669 free(p->excludes[i]);
671 free(p->excludes);
672 free(p);
673 return 0;
676 int git_check_excluded_1(const char *pathname,
677 int pathlen, const char *basename, int *dtype,
678 EXCLUDE_LIST el)
680 return excluded_from_list(pathname, pathlen, basename,dtype,el);
683 int git_get_notes(GIT_HASH hash, char **p_note)
685 struct strbuf sb;
686 size_t size;
687 strbuf_init(&sb,0);
688 format_display_notes(hash, &sb, "utf-8", 0);
689 *p_note = strbuf_detach(&sb,&size);
691 return 0;
694 struct cmd_struct {
695 const char *cmd;
696 int (*fn)(int, const char **, const char *);
697 int option;
700 #define RUN_SETUP (1<<0)
701 #define USE_PAGER (1<<1)
703 * require working tree to be present -- anything uses this needs
704 * RUN_SETUP for reading from the configuration file.
706 #define NEED_WORK_TREE (1<<2)
708 const char git_usage_string[] =
709 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
710 " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
711 " [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
712 " [-c name=value] [--help]\n"
713 " COMMAND [ARGS]";
715 const char git_more_info_string[] =
716 "See 'git help COMMAND' for more information on a specific command.";
718 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
719 int check_pager_config(const char *cmd)
721 return 0;
725 int git_run_cmd(char *cmd, char *arg)
728 int i=0;
729 char ** argv=0;
730 int argc=0;
732 static struct cmd_struct commands[] = {
733 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
734 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
735 { "annotate", cmd_annotate, RUN_SETUP },
736 { "apply", cmd_apply },
737 { "archive", cmd_archive },
738 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
739 { "blame", cmd_blame, RUN_SETUP },
740 { "branch", cmd_branch, RUN_SETUP },
741 { "bundle", cmd_bundle },
742 { "cat-file", cmd_cat_file, RUN_SETUP },
743 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
744 { "checkout-index", cmd_checkout_index,
745 RUN_SETUP | NEED_WORK_TREE},
746 { "check-ref-format", cmd_check_ref_format },
747 { "check-attr", cmd_check_attr, RUN_SETUP },
748 { "cherry", cmd_cherry, RUN_SETUP },
749 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
750 { "clone", cmd_clone },
751 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
752 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
753 { "commit-tree", cmd_commit_tree, RUN_SETUP },
754 { "config", cmd_config },
755 { "count-objects", cmd_count_objects, RUN_SETUP },
756 { "describe", cmd_describe, RUN_SETUP },
757 { "diff", cmd_diff },
758 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
759 { "diff-index", cmd_diff_index, RUN_SETUP },
760 { "diff-tree", cmd_diff_tree, RUN_SETUP },
761 { "fast-export", cmd_fast_export, RUN_SETUP },
762 { "fetch", cmd_fetch, RUN_SETUP },
763 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
764 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
765 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
766 { "format-patch", cmd_format_patch, RUN_SETUP },
767 { "fsck", cmd_fsck, RUN_SETUP },
768 { "fsck-objects", cmd_fsck, RUN_SETUP },
769 { "gc", cmd_gc, RUN_SETUP },
770 { "get-tar-commit-id", cmd_get_tar_commit_id },
771 { "grep", cmd_grep },
772 { "hash-object", cmd_hash_object },
773 { "help", cmd_help },
774 { "index-pack", cmd_index_pack },
775 { "init", cmd_init_db },
776 { "init-db", cmd_init_db },
777 { "log", cmd_log, RUN_SETUP },
778 { "ls-files", cmd_ls_files, RUN_SETUP },
779 { "ls-tree", cmd_ls_tree, RUN_SETUP },
780 { "ls-remote", cmd_ls_remote },
781 { "mailinfo", cmd_mailinfo },
782 { "mailsplit", cmd_mailsplit },
783 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
784 { "merge-base", cmd_merge_base, RUN_SETUP },
785 { "merge-file", cmd_merge_file },
786 { "merge-index", cmd_merge_index, RUN_SETUP },
787 { "merge-ours", cmd_merge_ours, RUN_SETUP },
788 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
789 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
790 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
791 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
792 { "merge-tree", cmd_merge_tree, RUN_SETUP },
793 { "mktag", cmd_mktag, RUN_SETUP },
794 { "mktree", cmd_mktree, RUN_SETUP },
795 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
796 { "name-rev", cmd_name_rev, RUN_SETUP },
797 { "notes", cmd_notes, RUN_SETUP },
798 { "pack-objects", cmd_pack_objects, RUN_SETUP },
799 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
800 { "patch-id", cmd_patch_id },
801 { "peek-remote", cmd_ls_remote },
802 { "pickaxe", cmd_blame, RUN_SETUP },
803 { "prune", cmd_prune, RUN_SETUP },
804 { "prune-packed", cmd_prune_packed, RUN_SETUP },
805 { "push", cmd_push, RUN_SETUP },
806 { "read-tree", cmd_read_tree, RUN_SETUP },
807 { "receive-pack", cmd_receive_pack },
808 { "reflog", cmd_reflog, RUN_SETUP },
809 { "remote", cmd_remote, RUN_SETUP },
810 { "replace", cmd_replace, RUN_SETUP },
811 { "repo-config", cmd_config },
812 { "rerere", cmd_rerere, RUN_SETUP },
813 { "reset", cmd_reset, RUN_SETUP },
814 { "rev-list", cmd_rev_list, RUN_SETUP },
815 { "rev-parse", cmd_rev_parse },
816 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
817 { "rm", cmd_rm, RUN_SETUP },
818 { "send-pack", cmd_send_pack, RUN_SETUP },
819 { "shortlog", cmd_shortlog, USE_PAGER },
820 { "show-branch", cmd_show_branch, RUN_SETUP },
821 { "show", cmd_show, RUN_SETUP },
822 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
823 { "stripspace", cmd_stripspace },
824 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
825 { "tag", cmd_tag, RUN_SETUP },
826 { "tar-tree", cmd_tar_tree },
827 { "unpack-file", cmd_unpack_file, RUN_SETUP },
828 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
829 { "update-index", cmd_update_index, RUN_SETUP },
830 { "update-ref", cmd_update_ref, RUN_SETUP },
831 { "update-server-info", cmd_update_server_info, RUN_SETUP },
832 { "upload-archive", cmd_upload_archive },
833 { "var", cmd_var },
834 { "verify-tag", cmd_verify_tag, RUN_SETUP },
835 { "version", cmd_version },
836 { "whatchanged", cmd_whatchanged, RUN_SETUP },
837 { "write-tree", cmd_write_tree, RUN_SETUP },
838 { "verify-pack", cmd_verify_pack },
839 { "show-ref", cmd_show_ref, RUN_SETUP },
840 { "pack-refs", cmd_pack_refs, RUN_SETUP },
843 git_init();
845 for(i=0;i< sizeof(commands) / sizeof(struct cmd_struct);i++)
847 if(strcmp(cmd,commands[i].cmd)==0)
849 int ret;
850 if(arg != NULL)
851 argv = strtoargv(arg,&argc);
853 ret = commands[i].fn(argc, argv, NULL);
855 if(argv)
856 free(argv);
858 discard_cache();
859 free_all_pack();
861 return ret;
866 return -1;
869 int git_for_each_ref_in(const char * refname, each_ref_fn fn, void * data)
871 int ret;
872 invalidate_ref_cache(NULL);
873 ret = for_each_ref_in(refname, fn, data);
874 free_all_pack();
875 return ret;
878 const char *git_resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
880 invalidate_ref_cache(NULL);
881 return resolve_ref_unsafe(ref,sha1,reading, flag);
883 int git_for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
885 return for_each_reflog_ent(ref,fn,cb_data);
888 int git_deref_tag(const unsigned char *tagsha1, GIT_HASH refhash)
890 struct object *obj = NULL;
891 obj = parse_object(tagsha1);
892 if (!obj)
893 return -1;
895 if (obj->type == OBJ_TAG)
897 obj = deref_tag(obj, "", 0);
898 if (!obj)
899 return -1;
901 memcpy(refhash, obj->sha1, sizeof(GIT_HASH));
902 return 0;
905 return -1;
908 static int update_some(const unsigned char *sha1, const char *base, int baselen,
909 const char *pathname, unsigned mode, int stage, void *context)
911 struct cache_entry *ce;
913 ce = (struct cache_entry *)context;
915 if (S_ISDIR(mode))
916 return READ_TREE_RECURSIVE;
918 hashcpy(ce->sha1, sha1);
919 memcpy(ce->name, base, baselen);
920 memcpy(ce->name + baselen, pathname, strlen(pathname));
921 ce->ce_flags = create_ce_flags(strlen(pathname)+baselen, 0);
922 ce->ce_mode = create_ce_mode(mode);
924 return 0;
927 int git_checkout_file(const char *ref, const char *path, const char *outputpath)
929 struct cache_entry *ce;
930 int ret;
931 GIT_HASH sha1;
932 struct tree * root;
933 struct checkout state;
934 struct pathspec pathspec;
935 const char *match[2];
936 ret = get_sha1(ref, sha1);
937 if(ret)
938 return ret;
940 reprepare_packed_git();
941 root = parse_tree_indirect(sha1);
943 if(!root)
945 free_all_pack();
946 return -1;
949 ce = xcalloc(1, cache_entry_size(strlen(path)));
951 match[0] = path;
952 match[1] = NULL;
954 init_pathspec(&pathspec, match);
955 pathspec.items[0].use_wildcard = 0;
956 ret = read_tree_recursive(root, "", 0, 0, &pathspec, update_some, ce);
957 free_pathspec(&pathspec);
959 if(ret)
961 free_all_pack();
962 free(ce);
963 return ret;
965 memset(&state, 0, sizeof(state));
966 state.force = 1;
967 state.refresh_cache = 0;
969 ret = write_entry(ce, outputpath, &state, 0);
970 free_all_pack();
971 free(ce);
972 return ret;
974 struct config_buf
976 char *buf;
977 char *key;
978 char *size;
979 int seen;
982 static int get_config(const char *key_, const char *value_, void *cb)
984 struct config_buf *buf;
985 buf=(struct config_buf*)cb;
986 if(strcmp(key_, buf->key))
987 return 0;
989 if (value_)
990 strncpy(buf->buf,value_,buf->size);
991 else
993 buf->buf[0] = 't';
994 buf->buf[1] = 'r';
995 buf->buf[2] = 'u';
996 buf->buf[3] = 'e';
997 buf->buf[4] = 0;
999 buf->seen = 1;
1000 return 0;
1004 // wchar_t wrapper for git_etc_gitconfig()
1005 const wchar_t *wget_msysgit_etc(void)
1007 static const wchar_t *etc_gitconfig = NULL;
1008 wchar_t wpointer[MAX_PATH];
1010 if (etc_gitconfig)
1011 return etc_gitconfig;
1013 if (xutftowcs_path(wpointer, git_etc_gitconfig()) < 0)
1014 return NULL;
1016 etc_gitconfig = _wcsdup(wpointer);
1018 return etc_gitconfig;
1021 int git_get_config(const char *key, char *buffer, int size, char *git_path)
1023 char *local, *global;
1024 const char *home, *system;
1025 struct config_buf buf;
1026 buf.buf=buffer;
1027 buf.size=size;
1028 buf.seen = 0;
1029 buf.key = key;
1031 local=global=system=NULL;
1033 home = get_windows_home_directory();
1034 if (home)
1035 global = xstrdup(mkpath("%s/.gitconfig", home));
1037 system = git_etc_gitconfig();
1039 local = git_pathdup("config");
1041 if ( !buf.seen)
1042 git_config_from_file(get_config, local, &buf);
1043 if (!buf.seen && global)
1044 git_config_from_file(get_config, global, &buf);
1045 if (!buf.seen && system)
1046 git_config_from_file(get_config, system, &buf);
1048 if(local)
1049 free(local);
1050 if(global)
1051 free(global);
1053 return !buf.seen;
1056 // taken from msysgit: compat/mingw.c
1057 const char *get_windows_home_directory(void)
1059 static const char *home_directory = NULL;
1060 struct strbuf buf = STRBUF_INIT;
1062 if (home_directory)
1063 return home_directory;
1065 home_directory = getenv("HOME");
1066 if (home_directory && *home_directory)
1067 return home_directory;
1069 strbuf_addf(&buf, "%s/%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
1070 home_directory = strbuf_detach(&buf, NULL);
1072 return home_directory;
1075 // wchar_t wrapper for get_windows_home_directory()
1076 const wchar_t *wget_windows_home_directory(void)
1078 static const wchar_t *home_directory = NULL;
1079 wchar_t wpointer[MAX_PATH];
1081 if (home_directory)
1082 return home_directory;
1084 if (xutftowcs_path(wpointer, get_windows_home_directory()) < 0)
1085 return NULL;
1087 home_directory = _wcsdup(wpointer);
1089 return home_directory;
1092 int get_set_config(const char *key, char *value, CONFIG_TYPE type,char *git_path)
1094 char * config_exclusive_filename;
1095 switch(type)
1097 case CONFIG_LOCAL:
1098 config_exclusive_filename = git_pathdup("config");
1099 break;
1100 case CONFIG_GLOBAL:
1102 const char *home = get_windows_home_directory();
1103 if (home)
1105 config_exclusive_filename = xstrdup(mkpath("%s/.gitconfig", home));
1108 break;
1109 default:
1110 config_exclusive_filename = NULL;
1111 break;
1114 if(!config_exclusive_filename)
1115 return -1;
1117 return git_config_set_multivar_in_file(config_exclusive_filename, key, value, NULL, 0);