Fix C4244 warnings: 'conversion' conversion from 'type1' to 'type2', possible loss...
[TortoiseGit.git] / ext / gitdll / gitdll.c
blob031d564d13d8f4ac3f19b430f7cc03b45dc9c9ce
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2015 - 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 "../build/libgit-defines.h"
24 #include "git-compat-util.h"
25 #include "gitdll.h"
26 #include "cache.h"
27 #include "commit.h"
28 #include "diff.h"
29 #include "revision.h"
30 #include "diffcore.h"
31 #include "dir.h"
32 #include "builtin.h"
33 #include "exec_cmd.h"
34 #include "cache.h"
35 #include "quote.h"
36 #include "run-command.h"
37 #include "mailmap.h"
39 extern char g_last_error[];
40 const char * g_prefix;
42 extern void die_dll(const char *err, va_list params);
43 extern int die_is_recursing_dll(void);
45 extern void free_all_pack();
46 extern void reset_git_env();
47 extern void invalidate_ref_cache(const char* submodule);
48 extern void cmd_log_init(int argc, const char** argv, const char* prefix, struct rev_info* rev, struct setup_revision_opt* opt);
49 extern int estimate_commit_count(struct rev_info* rev, struct commit_list* list);
50 extern int log_tree_commit(struct rev_info*, struct commit*);
51 extern int write_entry(struct cache_entry* ce, char* path, const struct checkout* state, int to_tempfile);
52 extern struct object* deref_tag(struct object* o, const char* warn, int warnlen);
53 extern void diff_flush_stat(struct diff_filepair* p, struct diff_options* o, struct diffstat_t* diffstat);
54 extern void free_diffstat_info(struct diffstat_t* diffstat);
55 extern int for_each_reflog_ent(const char* refname, each_reflog_ent_fn fn, void* cb_data);
56 extern int for_each_ref_in(const char* prefix, each_ref_fn fn, void* cb_data);
58 void dll_entry()
60 set_die_routine(die_dll);
61 set_die_is_recursing_routine(die_is_recursing_dll);
64 int git_get_sha1(const char *name, GIT_HASH sha1)
66 return get_sha1(name,sha1);
69 static int convert_slash(char * path)
71 while(*path)
73 if(*path == '\\' )
74 *path = '/';
75 path++;
77 return 0;
80 int git_init()
82 char path[MAX_PATH+1];
83 int ret;
84 size_t homesize;
86 _fmode = _O_BINARY;
87 _setmode(_fileno(stdin), _O_BINARY);
88 _setmode(_fileno(stdout), _O_BINARY);
89 _setmode(_fileno(stderr), _O_BINARY);
91 // set HOME if not set already
92 getenv_s(&homesize, NULL, 0, "HOME");
93 if (!homesize)
95 _wputenv_s(L"HOME", wget_windows_home_directory());
97 GetModuleFileName(NULL, path, MAX_PATH);
98 convert_slash(path);
100 git_extract_argv0_path(path);
101 reset_git_env();
102 g_prefix = setup_git_directory();
103 ret = git_config(git_default_config, NULL);
105 if (!homesize)
107 _putenv_s("HOME","");/* clear home evironment to avoid affact third part software*/
110 return ret;
113 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR *author, char *pbuff)
115 char *end;
117 author->Name=pbuff;
118 end=strchr(pbuff,'<');
119 if( end == 0)
121 return -1;
123 author->NameSize = (int)(end - pbuff - 1);
125 pbuff = end +1;
126 end = strchr(pbuff, '>');
127 if( end == 0)
128 return -1;
130 author->Email = pbuff ;
131 author->EmailSize = (int)(end - pbuff);
133 pbuff = end + 2;
135 author->Date = atol(pbuff);
136 end = strchr(pbuff, ' ');
137 if( end == 0 )
138 return -1;
140 pbuff=end;
141 author->TimeZone = atol(pbuff);
143 return 0;
146 int git_parse_commit(GIT_COMMIT *commit)
148 int ret = 0;
149 char *pbuf;
150 char *end;
151 struct commit *p;
153 p= (struct commit *)commit->m_pGitCommit;
155 memcpy(commit->m_hash,p->object.sha1,GIT_HASH_SIZE);
157 commit->m_Encode = NULL;
158 commit->m_EncodeSize = 0;
160 if(p->buffer == NULL)
161 return -3;
163 pbuf = p->buffer;
164 while(pbuf)
166 if (strncmp(pbuf, "author", 6) == 0)
168 ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);
169 if(ret)
170 return -4;
172 else if (strncmp(pbuf, "committer", 9) == 0)
174 ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10);
175 if(ret)
176 return -5;
178 pbuf = strchr(pbuf,'\n');
179 if(pbuf == NULL)
180 return -6;
182 else if (strncmp(pbuf, "encoding", 8) == 0)
184 pbuf += 9;
185 commit->m_Encode=pbuf;
186 end = strchr(pbuf,'\n');
187 commit->m_EncodeSize= (int)(end -pbuf);
190 // the headers end after the first empty line
191 else if (*pbuf == '\n')
193 pbuf++;
195 commit->m_Subject=pbuf;
196 end = strchr(pbuf,'\n');
197 if( end == 0)
198 commit->m_SubjectSize = strlen(pbuf);
199 else
201 commit->m_SubjectSize = (int)(end - pbuf);
202 pbuf = end +1;
203 commit->m_Body = pbuf;
204 commit->m_BodySize = strlen(pbuf);
205 return 0;
209 pbuf = strchr(pbuf,'\n');
210 if(pbuf)
211 pbuf ++;
213 return 0;
216 int git_get_commit_from_hash(GIT_COMMIT *commit, GIT_HASH hash)
218 int ret = 0;
220 struct commit *p;
222 if (commit == NULL)
223 return -1;
225 memset(commit,0,sizeof(GIT_COMMIT));
227 commit->m_pGitCommit = p = lookup_commit(hash);
229 if(p == NULL)
230 return -1;
232 ret = parse_commit(p);
233 if( ret )
234 return ret;
236 return git_parse_commit(commit);
239 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
241 struct commit *p = commit->m_pGitCommit;
243 if(list == NULL)
244 return -1;
246 *list = (GIT_COMMIT_LIST*)p->parents;
247 return 0;
249 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
251 struct commit_list *l;
252 if (list == NULL)
253 return -1;
255 l = *(struct commit_list **)list;
256 if (l == NULL)
257 return -1;
259 if(hash)
260 memcpy(hash, l->item->object.sha1, GIT_HASH_SIZE);
262 *list = (GIT_COMMIT_LIST *)l->next;
263 return 0;
268 int git_free_commit(GIT_COMMIT *commit)
270 struct commit *p = commit->m_pGitCommit;
272 if( p->parents)
273 free_commit_list(p->parents);
275 if( p->buffer )
277 free(p->buffer);
278 p->buffer=NULL;
279 p->object.parsed=0;
280 p->parents=0;
281 p->tree=0;
283 memset(commit,0,sizeof(GIT_COMMIT));
284 return 0;
287 char **strtoargv(char *arg, int *size)
289 int count=0;
290 char *p=arg;
291 char **argv;
293 int i=0;
294 while(*p)
296 if(*p == '\\')
297 *p='/';
298 p++;
300 p=arg;
302 while(*p)
304 if(*p == ' ')
305 count ++;
306 p++;
309 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
310 p=(char*)(argv+count+2);
312 while(*arg)
314 if(*arg != ' ')
316 char space=' ';
317 argv[i]=p;
319 while(*arg)
321 if(*arg == '"')
323 arg++;
324 if(space == ' ')
325 space = '"';
326 else
327 space = ' ';
329 if((*arg == space) || (*arg == 0))
330 break;
332 *p++ = *arg++;
334 i++;
335 *p++=0;
337 if(*arg == 0)
338 break;
339 arg++;
341 argv[i]=NULL;
342 *size = i;
343 return argv;
345 int git_open_log(GIT_LOG * handle, char * arg)
347 struct rev_info *p_Rev;
348 char ** argv=0;
349 int argc=0;
350 unsigned int i=0;
351 struct setup_revision_opt opt;
353 /* clear flags */
354 unsigned int obj_size = get_max_object_index();
355 for(i =0; i<obj_size; i++)
357 struct object *ob= get_indexed_object(i);
358 if(ob)
359 ob->flags=0;
362 if(arg != NULL)
363 argv = strtoargv(arg,&argc);
365 if (!argv)
366 return -1;
368 p_Rev = malloc(sizeof(struct rev_info));
369 if (p_Rev == NULL)
371 free(argv);
372 return -1;
375 memset(p_Rev,0,sizeof(struct rev_info));
377 invalidate_ref_cache(NULL);
379 init_revisions(p_Rev, g_prefix);
380 p_Rev->diff = 1;
382 memset(&opt, 0, sizeof(opt));
383 opt.def = "HEAD";
385 cmd_log_init(argc, argv, g_prefix,p_Rev,&opt);
387 p_Rev->pPrivate = argv;
388 *handle = p_Rev;
389 return 0;
392 int git_get_log_firstcommit(GIT_LOG handle)
394 return prepare_revision_walk(handle);
397 int git_get_log_estimate_commit_count(GIT_LOG handle)
399 struct rev_info *p_Rev;
400 p_Rev=(struct rev_info *)handle;
402 return estimate_commit_count(p_Rev, p_Rev->commits);
405 int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit, int follow)
407 int ret =0;
409 if(commit == NULL)
410 return -1;
412 memset(commit, 0, sizeof(GIT_COMMIT));
414 commit->m_pGitCommit = get_revision(handle);
415 if( commit->m_pGitCommit == NULL)
416 return -2;
418 if (follow && !log_tree_commit(handle, commit->m_pGitCommit))
420 commit->m_ignore = 1;
421 return 0;
423 commit->m_ignore = 0;
425 ret=git_parse_commit(commit);
426 if(ret)
427 return ret;
429 return 0;
432 struct notes_tree **display_notes_trees;
433 int git_close_log(GIT_LOG handle)
435 if(handle)
437 struct rev_info *p_Rev;
438 p_Rev=(struct rev_info *)handle;
439 if(p_Rev->pPrivate)
440 free(p_Rev->pPrivate);
441 free(handle);
443 free_all_pack();
445 if (display_notes_trees)
446 free_notes(*display_notes_trees);
447 display_notes_trees = 0;
448 return 0;
451 int git_open_diff(GIT_DIFF *diff, char * arg)
453 struct rev_info *p_Rev;
454 char ** argv=0;
455 int argc=0;
457 if(arg != NULL)
458 argv = strtoargv(arg,&argc);
460 p_Rev = malloc(sizeof(struct rev_info));
461 memset(p_Rev,0,sizeof(struct rev_info));
463 p_Rev->pPrivate = argv;
464 *diff = (GIT_DIFF)p_Rev;
466 init_revisions(p_Rev, g_prefix);
467 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
468 p_Rev->abbrev = 0;
469 p_Rev->diff = 1;
470 argc = setup_revisions(argc, argv, p_Rev, NULL);
472 return 0;
474 int git_close_diff(GIT_DIFF handle)
476 git_diff_flush(handle);
477 if(handle)
479 struct rev_info *p_Rev;
480 p_Rev=(struct rev_info *)handle;
481 if(p_Rev->pPrivate)
482 free(p_Rev->pPrivate);
483 free(handle);
485 return 0;
487 int git_diff_flush(GIT_DIFF diff)
489 struct diff_queue_struct *q = &diff_queued_diff;
490 struct rev_info *p_Rev;
491 int i;
492 p_Rev = (struct rev_info *)diff;
494 if(q->nr == 0)
495 return 0;
497 for (i = 0; i < q->nr; i++)
498 diff_free_filepair(q->queue[i]);
500 if(q->queue)
502 free(q->queue);
503 q->queue = NULL;
504 q->nr = q->alloc = 0;
507 if (p_Rev->diffopt.close_file)
508 fclose(p_Rev->diffopt.file);
510 free_diffstat_info(&p_Rev->diffstat);
511 return 0;
514 int git_root_diff(GIT_DIFF diff, GIT_HASH hash,GIT_FILE *file, int *count, int isstat)
516 int ret;
517 struct rev_info *p_Rev;
518 int i;
519 struct diff_queue_struct *q = &diff_queued_diff;
521 p_Rev = (struct rev_info *)diff;
523 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
525 if(ret)
526 return ret;
528 if(isstat)
530 diffcore_std(&p_Rev->diffopt);
532 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
533 for (i = 0; i < q->nr; i++) {
534 struct diff_filepair *p = q->queue[i];
535 //if (check_pair_status(p))
536 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
539 if(file)
540 *file = q;
541 if(count)
542 *count = q->nr;
544 return 0;
547 int git_do_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count,int isstat)
549 struct rev_info *p_Rev;
550 int ret;
551 int i;
552 struct diff_queue_struct *q = &diff_queued_diff;
554 p_Rev = (struct rev_info *)diff;
556 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
557 if( ret )
559 free_all_pack();
560 return ret;
563 if(isstat)
565 diffcore_std(&p_Rev->diffopt);
566 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
567 for (i = 0; i < q->nr; i++) {
568 struct diff_filepair *p = q->queue[i];
569 //if (check_pair_status(p))
570 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
573 free_all_pack();
574 if(file)
575 *file = q;
576 if(count)
577 *count = q->nr;
578 return 0;
581 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)
583 struct diff_queue_struct *q = &diff_queued_diff;
584 struct rev_info *p_Rev;
585 p_Rev = (struct rev_info *)diff;
587 q = (struct diff_queue_struct *)file;
588 if(file == 0)
589 return -1;
590 if(i>=q->nr)
591 return -1;
593 assert(newname && oldname && status);
595 *newname = q->queue[i]->two->path;
596 *oldname = q->queue[i]->one->path;
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 = (int)p_Rev->diffstat.files[j]->added;
618 if(dec)
619 *dec = (int)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, 0, 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, int lineno)
650 add_exclude(string, base, baselen, which, lineno);
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 is_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", 1);
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 static struct cmd_struct commands[] = {
718 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
719 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
720 { "annotate", cmd_annotate, RUN_SETUP },
721 { "apply", cmd_apply },
722 { "archive", cmd_archive },
723 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
724 { "blame", cmd_blame, RUN_SETUP },
725 { "branch", cmd_branch, RUN_SETUP },
726 { "bundle", cmd_bundle },
727 { "cat-file", cmd_cat_file, RUN_SETUP },
728 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
729 { "checkout-index", cmd_checkout_index,
730 RUN_SETUP | NEED_WORK_TREE},
731 { "check-ref-format", cmd_check_ref_format },
732 { "check-attr", cmd_check_attr, RUN_SETUP },
733 { "cherry", cmd_cherry, RUN_SETUP },
734 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
735 { "clone", cmd_clone },
736 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
737 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
738 { "commit-tree", cmd_commit_tree, RUN_SETUP },
739 { "config", cmd_config },
740 { "count-objects", cmd_count_objects, RUN_SETUP },
741 { "describe", cmd_describe, RUN_SETUP },
742 { "diff", cmd_diff },
743 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
744 { "diff-index", cmd_diff_index, RUN_SETUP },
745 { "diff-tree", cmd_diff_tree, RUN_SETUP },
746 { "fast-export", cmd_fast_export, RUN_SETUP },
747 { "fetch", cmd_fetch, RUN_SETUP },
748 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
749 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
750 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
751 { "format-patch", cmd_format_patch, RUN_SETUP },
752 { "fsck", cmd_fsck, RUN_SETUP },
753 { "fsck-objects", cmd_fsck, RUN_SETUP },
754 { "gc", cmd_gc, RUN_SETUP },
755 { "get-tar-commit-id", cmd_get_tar_commit_id },
756 { "grep", cmd_grep },
757 { "hash-object", cmd_hash_object },
758 { "help", cmd_help },
759 { "index-pack", cmd_index_pack },
760 { "init", cmd_init_db },
761 { "init-db", cmd_init_db },
762 { "log", cmd_log, RUN_SETUP },
763 { "ls-files", cmd_ls_files, RUN_SETUP },
764 { "ls-tree", cmd_ls_tree, RUN_SETUP },
765 { "ls-remote", cmd_ls_remote },
766 { "mailinfo", cmd_mailinfo },
767 { "mailsplit", cmd_mailsplit },
768 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
769 { "merge-base", cmd_merge_base, RUN_SETUP },
770 { "merge-file", cmd_merge_file },
771 { "merge-index", cmd_merge_index, RUN_SETUP },
772 { "merge-ours", cmd_merge_ours, RUN_SETUP },
773 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
774 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
775 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
776 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
777 { "merge-tree", cmd_merge_tree, RUN_SETUP },
778 { "mktag", cmd_mktag, RUN_SETUP },
779 { "mktree", cmd_mktree, RUN_SETUP },
780 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
781 { "name-rev", cmd_name_rev, RUN_SETUP },
782 { "notes", cmd_notes, RUN_SETUP },
783 { "pack-objects", cmd_pack_objects, RUN_SETUP },
784 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
785 { "patch-id", cmd_patch_id },
786 { "pickaxe", cmd_blame, RUN_SETUP },
787 { "prune", cmd_prune, RUN_SETUP },
788 { "prune-packed", cmd_prune_packed, RUN_SETUP },
789 { "push", cmd_push, RUN_SETUP },
790 { "read-tree", cmd_read_tree, RUN_SETUP },
791 { "receive-pack", cmd_receive_pack },
792 { "reflog", cmd_reflog, RUN_SETUP },
793 { "remote", cmd_remote, RUN_SETUP },
794 { "replace", cmd_replace, RUN_SETUP },
795 { "rerere", cmd_rerere, RUN_SETUP },
796 { "reset", cmd_reset, RUN_SETUP },
797 { "rev-list", cmd_rev_list, RUN_SETUP },
798 { "rev-parse", cmd_rev_parse },
799 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
800 { "rm", cmd_rm, RUN_SETUP },
801 { "send-pack", cmd_send_pack, RUN_SETUP },
802 { "shortlog", cmd_shortlog, USE_PAGER },
803 { "show-branch", cmd_show_branch, RUN_SETUP },
804 { "show", cmd_show, RUN_SETUP },
805 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
806 { "stripspace", cmd_stripspace },
807 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
808 { "tag", cmd_tag, RUN_SETUP },
809 { "unpack-file", cmd_unpack_file, RUN_SETUP },
810 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
811 { "update-index", cmd_update_index, RUN_SETUP },
812 { "update-ref", cmd_update_ref, RUN_SETUP },
813 { "update-server-info", cmd_update_server_info, RUN_SETUP },
814 { "upload-archive", cmd_upload_archive },
815 { "var", cmd_var },
816 { "verify-tag", cmd_verify_tag, RUN_SETUP },
817 { "version", cmd_version },
818 { "whatchanged", cmd_whatchanged, RUN_SETUP },
819 { "write-tree", cmd_write_tree, RUN_SETUP },
820 { "verify-pack", cmd_verify_pack },
821 { "show-ref", cmd_show_ref, RUN_SETUP },
822 { "pack-refs", cmd_pack_refs, RUN_SETUP },
825 int is_builtin(const char *s)
827 int i;
828 for (i = 0; i < ARRAY_SIZE(commands); i++) {
829 struct cmd_struct *p = commands+i;
830 if (!strcmp(s, p->cmd))
831 return 1;
833 return 0;
836 int git_run_cmd(char *cmd, char *arg)
839 int i=0;
840 char ** argv=0;
841 int argc=0;
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;
912 UNREFERENCED_PARAMETER(stage);
914 ce = (struct cache_entry *)context;
916 if (S_ISDIR(mode))
917 return READ_TREE_RECURSIVE;
919 hashcpy(ce->sha1, sha1);
920 memcpy(ce->name, base, baselen);
921 memcpy(ce->name + baselen, pathname, strlen(pathname));
922 ce->ce_flags = create_ce_flags(strlen(pathname) + baselen);
923 ce->ce_mode = create_ce_mode(mode);
925 return 0;
928 int git_checkout_file(const char* ref, const char* path, char* outputpath)
930 struct cache_entry *ce;
931 int ret;
932 GIT_HASH sha1;
933 struct tree * root;
934 struct checkout state;
935 struct pathspec pathspec;
936 const char *matchbuf[1];
937 ret = get_sha1(ref, sha1);
938 if(ret)
939 return ret;
941 reprepare_packed_git();
942 root = parse_tree_indirect(sha1);
944 if(!root)
946 free_all_pack();
947 return -1;
950 ce = xcalloc(1, cache_entry_size(strlen(path)));
952 matchbuf[0] = NULL;
953 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC, PATHSPEC_PREFER_CWD, path, matchbuf);
954 pathspec.items[0].nowildcard_len = pathspec.items[0].len;
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 const char *key;
977 size_t 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)
1022 char *local, *global, *globalxdg;
1023 const char *home, *system;
1024 struct config_buf buf;
1026 buf.buf=buffer;
1027 buf.size=size;
1028 buf.seen = 0;
1029 buf.key = key;
1031 home = get_windows_home_directory();
1032 if (home)
1034 global = xstrdup(mkpath("%s/.gitconfig", home));
1035 globalxdg = xstrdup(mkpath("%s/.config/git/config", home));
1037 else
1039 global = NULL;
1040 globalxdg = NULL;
1043 system = git_etc_gitconfig();
1045 local = git_pathdup("config");
1047 if ( !buf.seen)
1048 git_config_with_options(get_config, &buf, local, NULL, 1);
1049 if (!buf.seen && global)
1050 git_config_with_options(get_config, &buf, global, NULL, 1);
1051 if (!buf.seen && globalxdg)
1052 git_config_with_options(get_config, &buf, globalxdg, NULL, 1);
1053 if (!buf.seen && system)
1054 git_config_with_options(get_config, &buf, system, NULL, 1);
1056 if(local)
1057 free(local);
1058 if(global)
1059 free(global);
1060 if (globalxdg)
1061 free(globalxdg);
1063 return !buf.seen;
1066 // taken from msysgit: compat/mingw.c
1067 const char *get_windows_home_directory(void)
1069 static const char *home_directory = NULL;
1070 struct strbuf buf = STRBUF_INIT;
1072 if (home_directory)
1073 return home_directory;
1075 home_directory = getenv("HOME");
1076 if (home_directory && *home_directory)
1077 return home_directory;
1079 strbuf_addf(&buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
1080 home_directory = strbuf_detach(&buf, NULL);
1082 return home_directory;
1085 // wchar_t wrapper for get_windows_home_directory()
1086 const wchar_t *wget_windows_home_directory(void)
1088 static const wchar_t *home_directory = NULL;
1089 wchar_t wpointer[MAX_PATH];
1091 if (home_directory)
1092 return home_directory;
1094 if (xutftowcs_path(wpointer, get_windows_home_directory()) < 0)
1095 return NULL;
1097 home_directory = _wcsdup(wpointer);
1099 return home_directory;
1102 int get_set_config(const char *key, const char *value, CONFIG_TYPE type)
1104 char * config_exclusive_filename = NULL;
1106 switch(type)
1108 case CONFIG_LOCAL:
1109 config_exclusive_filename = git_pathdup("config");
1110 break;
1111 case CONFIG_GLOBAL:
1112 case CONFIG_XDGGLOBAL:
1114 const char *home = get_windows_home_directory();
1115 if (home)
1117 if (type == CONFIG_GLOBAL)
1118 config_exclusive_filename = xstrdup(mkpath("%s/.gitconfig", home));
1119 else
1120 config_exclusive_filename = xstrdup(mkpath("%s/.config/git/config", home));
1123 break;
1126 if(!config_exclusive_filename)
1127 return -1;
1129 return git_config_set_multivar_in_file(config_exclusive_filename, key, value, NULL, 0);
1132 struct mailmap_info {
1133 char *name;
1134 char *email;
1137 struct mailmap_entry {
1138 /* name and email for the simple mail-only case */
1139 char *name;
1140 char *email;
1142 /* name and email for the complex mail and name matching case */
1143 struct string_list namemap;
1146 int git_read_mailmap(GIT_MAILMAP *mailmap)
1148 struct string_list *map;
1149 int result;
1151 if (!mailmap)
1152 return -1;
1154 *mailmap = NULL;
1155 if ((map = (struct string_list *)calloc(1, sizeof(struct string_list))) == NULL)
1156 return -1;
1158 if ((result = read_mailmap(map, NULL)) != 0)
1159 return result;
1161 *mailmap = map;
1162 return 0;
1165 const char * git_get_mailmap_author(GIT_MAILMAP mailmap, const char *email2, void *payload, const char *(*author2_cb)(void *))
1167 struct string_list *map;
1168 int imax, imin = 0;
1170 if (!mailmap)
1171 return NULL;
1173 map = (struct string_list *)mailmap;
1174 imax = map->nr - 1;
1175 while (imax >= imin)
1177 int i = imin + ((imax - imin) / 2);
1178 struct string_list_item *si = (struct string_list_item *)&map->items[i];
1179 struct mailmap_entry *me = (struct mailmap_entry *)si->util;
1180 int comp = strcmp(si->string, email2);
1182 if (!comp)
1184 if (me->namemap.nr)
1186 const char *author2 = author2_cb(payload);
1187 unsigned int j;
1188 for (j = 0; j < me->namemap.nr; ++j)
1190 struct string_list_item *sj = (struct string_list_item *)&me->namemap.items[j];
1191 struct mailmap_info *mi = (struct mailmap_info *)sj->util;
1193 if (!strcmp(sj->string, author2))
1194 return mi->name;
1198 return me->name;
1200 else if (comp < 0)
1201 imin = i + 1;
1202 else
1203 imax = i - 1;
1206 return NULL;
1209 void git_free_mailmap(GIT_MAILMAP mailmap)
1211 if (!mailmap)
1212 return;
1214 clear_mailmap((struct string_list *)mailmap);
1215 free(mailmap);