Make function definition match its declaration
[TortoiseGit.git] / ext / gitdll / gitdll.c
blob6283eae73601a158d1953f7b7c5fe900a567cad1
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2017 - 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 #pragma warning(push)
25 #pragma warning(disable: 4100 4018 4127 4244 4267)
26 #include "git-compat-util.h"
27 #include "gitdll.h"
28 #include "cache.h"
29 #include "commit.h"
30 #include "diff.h"
31 #include "revision.h"
32 #include "diffcore.h"
33 #include "dir.h"
34 #include "builtin.h"
35 #include "exec_cmd.h"
36 #include "cache.h"
37 #include "quote.h"
38 #include "run-command.h"
39 #include "mailmap.h"
40 #pragma warning(pop)
42 extern char g_last_error[];
43 const char * g_prefix;
45 extern void die_dll(const char *err, va_list params);
46 extern void handle_error(const char* err, va_list params);
47 extern void handle_warning(const char* warn, va_list params);
48 extern int die_is_recursing_dll(void);
50 extern void free_all_pack(void);
51 extern void reset_git_env(void);
52 extern void drop_all_attr_stacks(void);
53 extern void git_atexit_dispatch(void);
54 extern void git_atexit_clear(void);
55 extern void invalidate_ref_cache(void);
56 extern void cmd_log_init(int argc, const char** argv, const char* prefix, struct rev_info* rev, struct setup_revision_opt* opt);
57 extern int estimate_commit_count(struct rev_info* rev, struct commit_list* list);
58 extern int log_tree_commit(struct rev_info*, struct commit*);
59 extern int write_entry(struct cache_entry* ce, char* path, const struct checkout* state, int to_tempfile);
60 extern struct object* deref_tag(struct object* o, const char* warn, int warnlen);
61 extern void diff_flush_stat(struct diff_filepair* p, struct diff_options* o, struct diffstat_t* diffstat);
62 extern void free_diffstat_info(struct diffstat_t* diffstat);
63 extern int for_each_reflog_ent(const char* refname, each_reflog_ent_fn fn, void* cb_data);
64 extern int for_each_ref_in(const char* prefix, each_ref_fn fn, void* cb_data);
66 void dll_entry(void)
68 set_die_routine(die_dll);
69 set_error_routine(handle_error);
70 set_warn_routine(handle_warning);
71 set_die_is_recursing_routine(die_is_recursing_dll);
74 int git_get_sha1(const char *name, GIT_HASH sha1)
76 return get_sha1(name,sha1);
79 static int convert_slash(char * path)
81 while(*path)
83 if(*path == '\\' )
84 *path = '/';
85 ++path;
87 return 0;
90 int git_init(void)
92 char path[MAX_PATH+1];
94 _fmode = _O_BINARY;
95 _setmode(_fileno(stdin), _O_BINARY);
96 _setmode(_fileno(stdout), _O_BINARY);
97 _setmode(_fileno(stderr), _O_BINARY);
99 GetModuleFileName(NULL, path, MAX_PATH);
100 convert_slash(path);
102 git_extract_argv0_path(path);
103 reset_git_env();
104 // set HOME if not set already
105 gitsetenv("HOME", get_windows_home_directory(), 0);
106 drop_all_attr_stacks();
107 git_config_clear();
108 g_prefix = setup_git_directory();
109 git_config(git_default_config, NULL);
110 invalidate_ref_cache();
112 return 0;
115 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR* author, const char* pbuff)
117 const char* end;
119 author->Name=pbuff;
120 end=strchr(pbuff,'<');
121 if( end == 0)
123 return -1;
125 author->NameSize = (int)(end - pbuff - 1);
127 pbuff = end +1;
128 end = strchr(pbuff, '>');
129 if( end == 0)
130 return -1;
132 author->Email = pbuff ;
133 author->EmailSize = (int)(end - pbuff);
135 pbuff = end + 2;
137 author->Date = atol(pbuff);
138 end = strchr(pbuff, ' ');
139 if( end == 0 )
140 return -1;
142 pbuff=end;
143 author->TimeZone = atol(pbuff);
145 return 0;
148 int git_parse_commit(GIT_COMMIT *commit)
150 int ret = 0;
151 const char* pbuf;
152 const char* end;
153 struct commit *p;
155 p= (struct commit *)commit->m_pGitCommit;
157 memcpy(commit->m_hash, p->object.oid.hash, GIT_HASH_SIZE);
159 commit->m_Encode = NULL;
160 commit->m_EncodeSize = 0;
162 commit->buffer = detach_commit_buffer(commit->m_pGitCommit, NULL);
164 pbuf = commit->buffer;
165 while(pbuf)
167 if (strncmp(pbuf, "author", 6) == 0)
169 ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);
170 if(ret)
171 return -4;
173 else if (strncmp(pbuf, "committer", 9) == 0)
175 ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10);
176 if(ret)
177 return -5;
179 pbuf = strchr(pbuf,'\n');
180 if(pbuf == NULL)
181 return -6;
183 else if (strncmp(pbuf, "encoding", 8) == 0)
185 pbuf += 9;
186 commit->m_Encode=pbuf;
187 end = strchr(pbuf,'\n');
188 commit->m_EncodeSize= (int)(end -pbuf);
191 // the headers end after the first empty line
192 else if (*pbuf == '\n')
194 ++pbuf;
196 commit->m_Subject=pbuf;
197 end = strchr(pbuf,'\n');
198 if( end == 0)
199 commit->m_SubjectSize = (int)strlen(pbuf);
200 else
202 commit->m_SubjectSize = (int)(end - pbuf);
203 pbuf = end +1;
204 commit->m_Body = pbuf;
205 commit->m_BodySize = (int)strlen(pbuf);
206 return 0;
210 pbuf = strchr(pbuf,'\n');
211 if(pbuf)
212 ++pbuf;
214 return 0;
217 int git_get_commit_from_hash(GIT_COMMIT* commit, const GIT_HASH hash)
219 int ret = 0;
221 struct commit *p;
223 if (commit == NULL)
224 return -1;
226 memset(commit,0,sizeof(GIT_COMMIT));
228 commit->m_pGitCommit = p = lookup_commit(hash);
230 if(p == NULL)
231 return -1;
233 ret = parse_commit(p);
234 if( ret )
235 return ret;
237 return git_parse_commit(commit);
240 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
242 struct commit *p = commit->m_pGitCommit;
244 if(list == NULL)
245 return -1;
247 *list = (GIT_COMMIT_LIST*)p->parents;
248 return 0;
251 int git_commit_is_root(const GIT_COMMIT* commit)
253 struct commit* p = commit->m_pGitCommit;
254 return (struct commit_list**)p->parents ? 1 : 0;
257 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
259 struct commit_list *l;
260 if (list == NULL)
261 return -1;
263 l = *(struct commit_list **)list;
264 if (l == NULL)
265 return -1;
267 if(hash)
268 memcpy(hash, l->item->object.oid.hash, GIT_HASH_SIZE);
270 *list = (GIT_COMMIT_LIST *)l->next;
271 return 0;
276 int git_free_commit(GIT_COMMIT *commit)
278 struct commit *p = commit->m_pGitCommit;
280 if( p->parents)
281 free_commit_list(p->parents);
283 if (p->tree)
284 free_tree_buffer(p->tree);
286 #pragma warning(push)
287 #pragma warning(disable: 4090)
288 if (commit->buffer)
289 free(commit->buffer);
290 #pragma warning(pop)
292 p->object.parsed = 0;
293 p->parents = 0;
294 p->tree = 0;
296 memset(commit,0,sizeof(GIT_COMMIT));
297 return 0;
300 char **strtoargv(char *arg, int *size)
302 int count=0;
303 char *p=arg;
304 char **argv;
306 int i=0;
307 while(*p)
309 if(*p == '\\')
310 *p='/';
311 ++p;
313 p=arg;
315 while(*p)
317 if(*p == ' ')
318 ++count;
319 ++p;
322 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
323 p=(char*)(argv+count+2);
325 while(*arg)
327 if(*arg != ' ')
329 char space=' ';
330 argv[i]=p;
332 while(*arg)
334 if(*arg == '"')
336 ++arg;
337 if(space == ' ')
338 space = '"';
339 else
340 space = ' ';
342 if((*arg == space) || (*arg == 0))
343 break;
345 *p++ = *arg++;
347 ++i;
348 *p++=0;
350 if(*arg == 0)
351 break;
352 ++arg;
354 argv[i]=NULL;
355 *size = i;
356 return argv;
358 int git_open_log(GIT_LOG * handle, char * arg)
360 struct rev_info *p_Rev;
361 char ** argv=0;
362 int argc=0;
363 struct setup_revision_opt opt;
365 /* clear flags */
366 unsigned int obj_size = get_max_object_index();
367 for (unsigned int i = 0; i < obj_size; ++i)
369 struct object *ob= get_indexed_object(i);
370 if(ob)
372 ob->flags=0;
373 if (ob->parsed && ob->type == OBJ_COMMIT)
375 struct commit* commit = (struct commit*)ob;
376 free_commit_list(commit->parents);
377 commit->parents = NULL;
378 if (commit->tree)
379 free_tree_buffer(commit->tree);
380 commit->tree = NULL;
381 ob->parsed = 0;
386 if(arg != NULL)
387 argv = strtoargv(arg,&argc);
389 if (!argv)
390 return -1;
392 p_Rev = malloc(sizeof(struct rev_info));
393 if (p_Rev == NULL)
395 free(argv);
396 return -1;
399 memset(p_Rev,0,sizeof(struct rev_info));
401 invalidate_ref_cache();
403 init_revisions(p_Rev, g_prefix);
404 p_Rev->diff = 1;
406 memset(&opt, 0, sizeof(opt));
407 opt.def = "HEAD";
409 cmd_log_init(argc, argv, g_prefix,p_Rev,&opt);
411 p_Rev->pPrivate = argv;
412 *handle = p_Rev;
413 return 0;
416 int git_get_log_firstcommit(GIT_LOG handle)
418 return prepare_revision_walk(handle);
421 int git_get_log_estimate_commit_count(GIT_LOG handle)
423 struct rev_info *p_Rev;
424 p_Rev=(struct rev_info *)handle;
426 return estimate_commit_count(p_Rev, p_Rev->commits);
429 int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit, int follow)
431 int ret =0;
433 if(commit == NULL)
434 return -1;
436 memset(commit, 0, sizeof(GIT_COMMIT));
438 commit->m_pGitCommit = get_revision(handle);
439 if( commit->m_pGitCommit == NULL)
440 return -2;
442 if (follow && !log_tree_commit(handle, commit->m_pGitCommit))
444 commit->m_ignore = 1;
445 return 0;
447 commit->m_ignore = 0;
449 ret=git_parse_commit(commit);
450 if(ret)
451 return ret;
453 return 0;
456 struct notes_tree **display_notes_trees;
457 int git_close_log(GIT_LOG handle)
459 if(handle)
461 struct rev_info *p_Rev;
462 p_Rev=(struct rev_info *)handle;
463 if(p_Rev->pPrivate)
464 free(p_Rev->pPrivate);
465 free(handle);
467 free_all_pack();
469 if (display_notes_trees)
470 free_notes(*display_notes_trees);
471 display_notes_trees = 0;
472 return 0;
475 int git_open_diff(GIT_DIFF *diff, char * arg)
477 struct rev_info *p_Rev;
478 char ** argv=0;
479 int argc=0;
481 if(arg != NULL)
482 argv = strtoargv(arg,&argc);
484 p_Rev = malloc(sizeof(struct rev_info));
485 memset(p_Rev,0,sizeof(struct rev_info));
487 p_Rev->pPrivate = argv;
488 *diff = (GIT_DIFF)p_Rev;
490 init_revisions(p_Rev, g_prefix);
491 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
492 p_Rev->abbrev = 0;
493 p_Rev->diff = 1;
494 argc = setup_revisions(argc, argv, p_Rev, NULL);
496 return 0;
498 int git_close_diff(GIT_DIFF handle)
500 git_diff_flush(handle);
501 if(handle)
503 struct rev_info *p_Rev;
504 p_Rev=(struct rev_info *)handle;
505 if(p_Rev->pPrivate)
506 free(p_Rev->pPrivate);
507 free(handle);
509 return 0;
511 int git_diff_flush(GIT_DIFF diff)
513 struct diff_queue_struct *q = &diff_queued_diff;
514 struct rev_info *p_Rev;
515 p_Rev = (struct rev_info *)diff;
517 if(q->nr == 0)
518 return 0;
520 for (int i = 0; i < q->nr; ++i)
521 diff_free_filepair(q->queue[i]);
523 if(q->queue)
525 free(q->queue);
526 q->queue = NULL;
527 q->nr = q->alloc = 0;
530 if (p_Rev->diffopt.close_file)
531 fclose(p_Rev->diffopt.file);
533 free_diffstat_info(&p_Rev->diffstat);
534 return 0;
537 int git_root_diff(GIT_DIFF diff, GIT_HASH hash,GIT_FILE *file, int *count, int isstat)
539 int ret;
540 struct rev_info *p_Rev;
541 struct diff_queue_struct *q = &diff_queued_diff;
543 p_Rev = (struct rev_info *)diff;
545 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
547 if(ret)
548 return ret;
550 if(isstat)
552 diffcore_std(&p_Rev->diffopt);
554 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
555 for (int i = 0; i < q->nr; ++i) {
556 struct diff_filepair *p = q->queue[i];
557 //if (check_pair_status(p))
558 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
562 if (file)
563 *file = q;
564 if (count)
565 *count = q->nr;
567 return 0;
570 int git_do_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count,int isstat)
572 struct rev_info *p_Rev;
573 int ret;
574 struct diff_queue_struct *q = &diff_queued_diff;
576 p_Rev = (struct rev_info *)diff;
578 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
579 if( ret )
581 free_all_pack();
582 return ret;
585 if(isstat)
587 diffcore_std(&p_Rev->diffopt);
588 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
589 for (int i = 0; i < q->nr; ++i) {
590 struct diff_filepair *p = q->queue[i];
591 //if (check_pair_status(p))
592 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
595 free_all_pack();
596 if(file)
597 *file = q;
598 if(count)
599 *count = q->nr;
600 return 0;
603 int git_get_diff_file(GIT_DIFF diff, GIT_FILE file, int i, char** newname, char** oldname, int* IsDir, int* status, int* IsBin, int* inc, int* dec)
605 struct diff_queue_struct *q = &diff_queued_diff;
606 struct rev_info *p_Rev;
607 p_Rev = (struct rev_info *)diff;
609 q = (struct diff_queue_struct *)file;
610 if(file == 0)
611 return -1;
612 if(i>=q->nr)
613 return -1;
615 assert(newname && oldname && status && IsDir);
617 *newname = q->queue[i]->two->path;
618 *oldname = q->queue[i]->one->path;
619 *status = q->queue[i]->status;
620 if (*status == 'D')
621 *IsDir = (q->queue[i]->one->mode & S_IFDIR) == S_IFDIR;
622 else
623 *IsDir = (q->queue[i]->two->mode & S_IFDIR) == S_IFDIR;
625 if (q->queue[i]->one->mode && q->queue[i]->two->mode && DIFF_PAIR_TYPE_CHANGED(q->queue[i]))
626 *IsDir = 0;
628 if(p_Rev->diffstat.files)
630 int j;
631 for (j = 0; j < p_Rev->diffstat.nr; ++j)
633 if(strcmp(*newname,p_Rev->diffstat.files[j]->name)==0)
634 break;
636 if( j== p_Rev->diffstat.nr)
638 *IsBin=1;
639 *inc=0;
640 *dec=0;
641 return 0;
643 if(IsBin)
644 *IsBin = p_Rev->diffstat.files[j]->is_binary;
645 if(inc)
646 *inc = (int)p_Rev->diffstat.files[j]->added;
647 if(dec)
648 *dec = (int)p_Rev->diffstat.files[j]->deleted;
649 }else
651 *IsBin=1;
652 *inc=0;
653 *dec=0;
656 return 0;
659 int git_add_exclude(const char *string, const char *base,
660 int baselen, EXCLUDE_LIST which, int lineno)
662 add_exclude(string, base, baselen, which, lineno);
663 return 0;
666 int git_create_exclude_list(EXCLUDE_LIST *which)
668 *which = malloc(sizeof(struct exclude_list));
669 memset(*which,0,sizeof(struct exclude_list));
670 return 0;
673 int git_free_exclude_list(EXCLUDE_LIST which)
675 struct exclude_list *p = (struct exclude_list *) which;
677 for (int i = 0; i < p->nr; ++i)
679 free(p->excludes[i]);
681 free(p->excludes);
682 free(p);
683 return 0;
686 int git_check_excluded_1(const char *pathname,
687 int pathlen, const char *basename, int *dtype,
688 EXCLUDE_LIST el, int ignorecase)
690 ignore_case = ignorecase;
691 return is_excluded_from_list(pathname, pathlen, basename, dtype, el);
694 int git_get_notes(const GIT_HASH hash, char** p_note)
696 struct strbuf sb;
697 size_t size;
698 strbuf_init(&sb,0);
699 format_display_notes(hash, &sb, "utf-8", 1);
700 *p_note = strbuf_detach(&sb,&size);
702 return 0;
705 struct cmd_struct {
706 const char *cmd;
707 int (*fn)(int, const char **, const char *);
708 int option;
711 #define RUN_SETUP (1<<0)
713 static struct cmd_struct commands[] = {
714 { "notes", cmd_notes, RUN_SETUP },
715 { "update-index", cmd_update_index, RUN_SETUP },
718 int git_run_cmd(char *cmd, char *arg)
721 char ** argv=0;
722 int argc=0;
724 git_init();
726 for (int i = 0; i < sizeof(commands) / sizeof(struct cmd_struct); ++i)
728 if(strcmp(cmd,commands[i].cmd)==0)
730 int ret;
731 if(arg != NULL)
732 argv = strtoargv(arg,&argc);
734 ret = commands[i].fn(argc, argv, NULL);
736 if(argv)
737 free(argv);
739 discard_cache();
740 free_all_pack();
742 return ret;
747 return -1;
750 void git_exit_cleanup(void)
752 git_atexit_dispatch();
753 git_atexit_clear();
756 int git_for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
758 return for_each_reflog_ent(ref,fn,cb_data);
761 static int update_some(const unsigned char* sha1, struct strbuf* base,
762 const char *pathname, unsigned mode, int stage, void *context)
764 struct cache_entry *ce;
765 UNREFERENCED_PARAMETER(stage);
767 ce = (struct cache_entry *)context;
769 if (S_ISDIR(mode))
770 return READ_TREE_RECURSIVE;
772 hashcpy(ce->oid.hash, sha1);
773 memcpy(ce->name, base->buf, base->len);
774 memcpy(ce->name + base->len, pathname, strlen(pathname));
775 ce->ce_flags = create_ce_flags((unsigned int)(strlen(pathname) + base->len));
776 ce->ce_mode = create_ce_mode(mode);
778 return 0;
781 int git_checkout_file(const char* ref, const char* path, char* outputpath)
783 struct cache_entry *ce;
784 int ret;
785 GIT_HASH sha1;
786 struct tree * root;
787 struct checkout state;
788 struct pathspec pathspec;
789 const char *matchbuf[1];
790 ret = get_sha1(ref, sha1);
791 if(ret)
792 return ret;
794 reprepare_packed_git();
795 root = parse_tree_indirect(sha1);
797 if(!root)
799 free_all_pack();
800 return -1;
803 ce = xcalloc(1, cache_entry_size(strlen(path)));
805 matchbuf[0] = NULL;
806 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC, PATHSPEC_PREFER_CWD, path, matchbuf);
807 pathspec.items[0].nowildcard_len = pathspec.items[0].len;
808 ret = read_tree_recursive(root, "", 0, 0, &pathspec, update_some, ce);
809 clear_pathspec(&pathspec);
811 if(ret)
813 free_all_pack();
814 free(ce);
815 return ret;
817 memset(&state, 0, sizeof(state));
818 state.force = 1;
819 state.refresh_cache = 0;
821 ret = write_entry(ce, outputpath, &state, 0);
822 free_all_pack();
823 free(ce);
824 return ret;
826 struct config_buf
828 char *buf;
829 const char *key;
830 size_t size;
831 int seen;
834 static int get_config(const char *key_, const char *value_, void *cb)
836 struct config_buf *buf;
837 buf=(struct config_buf*)cb;
838 if(strcmp(key_, buf->key))
839 return 0;
841 if (value_)
842 strncpy(buf->buf,value_,buf->size);
843 else
845 buf->buf[0] = 't';
846 buf->buf[1] = 'r';
847 buf->buf[2] = 'u';
848 buf->buf[3] = 'e';
849 buf->buf[4] = 0;
851 buf->seen = 1;
852 return 0;
856 // wchar_t wrapper for program_data_config()
857 const wchar_t* wget_program_data_config(void)
859 static const wchar_t *programdata_git_config = NULL;
860 wchar_t wpointer[MAX_PATH];
862 if (programdata_git_config)
863 return programdata_git_config;
865 if (xutftowcs_path(wpointer, program_data_config()) < 0)
866 return NULL;
868 programdata_git_config = _wcsdup(wpointer);
870 return programdata_git_config;
873 // wchar_t wrapper for git_etc_gitconfig()
874 const wchar_t *wget_msysgit_etc(void)
876 static const wchar_t *etc_gitconfig = NULL;
877 wchar_t wpointer[MAX_PATH];
879 if (etc_gitconfig)
880 return etc_gitconfig;
882 if (xutftowcs_path(wpointer, git_etc_gitconfig()) < 0)
883 return NULL;
885 etc_gitconfig = _wcsdup(wpointer);
887 return etc_gitconfig;
890 int git_get_config(const char *key, char *buffer, int size)
892 const char *home, *system, *programdata;
893 struct config_buf buf;
894 struct git_config_source config_source = { 0 };
896 struct config_options opts = { 0 };
897 opts.respect_includes = 1;
899 buf.buf=buffer;
900 buf.size=size;
901 buf.seen = 0;
902 buf.key = key;
904 if (have_git_dir())
906 char* local = git_pathdup("config");
907 config_source.file = local;
908 git_config_with_options(get_config, &buf, &config_source, &opts);
909 free(local);
910 if (buf.seen)
911 return !buf.seen;
914 home = get_windows_home_directory();
915 if (home)
917 char* global = xstrdup(mkpath("%s/.gitconfig", home));
918 if (global)
920 config_source.file = global;
921 git_config_with_options(get_config, &buf, &config_source, &opts);
922 free(global);
923 if (buf.seen)
924 return !buf.seen;
926 char* globalxdg = xstrdup(mkpath("%s/.config/git/config", home));
927 if (globalxdg)
929 config_source.file = globalxdg;
930 git_config_with_options(get_config, &buf, &config_source, &opts);
931 free(globalxdg);
932 if (buf.seen)
933 return !buf.seen;
937 system = git_etc_gitconfig();
938 if (system)
940 config_source.file = system;
941 git_config_with_options(get_config, &buf, &config_source, &opts);
942 if (buf.seen)
943 return !buf.seen;
946 programdata = git_program_data_config();
947 if (programdata)
949 config_source.file = programdata;
950 git_config_with_options(get_config, &buf, &config_source, &opts);
953 return !buf.seen;
956 // taken from msysgit: compat/mingw.c
957 const char *get_windows_home_directory(void)
959 static const char *home_directory = NULL;
960 const char* tmp;
962 if (home_directory)
963 return home_directory;
965 if ((tmp = getenv("HOME")) != NULL && *tmp)
967 home_directory = _strdup(tmp);
968 return home_directory;
971 if ((tmp = getenv("HOMEDRIVE")) != NULL)
973 struct strbuf buf = STRBUF_INIT;
974 strbuf_addstr(&buf, tmp);
975 if ((tmp = getenv("HOMEPATH")) != NULL)
977 strbuf_addstr(&buf, tmp);
978 if (is_directory(buf.buf))
980 home_directory = strbuf_detach(&buf, NULL);
981 return home_directory;
984 strbuf_release(&buf);
987 if ((tmp = getenv("USERPROFILE")) != NULL && *tmp)
988 home_directory = _strdup(tmp);
990 return home_directory;
993 // wchar_t wrapper for get_windows_home_directory()
994 const wchar_t *wget_windows_home_directory(void)
996 static const wchar_t *home_directory = NULL;
997 wchar_t wpointer[MAX_PATH];
999 if (home_directory)
1000 return home_directory;
1002 if (xutftowcs_path(wpointer, get_windows_home_directory()) < 0)
1003 return NULL;
1005 home_directory = _wcsdup(wpointer);
1007 return home_directory;
1010 int get_set_config(const char *key, const char *value, CONFIG_TYPE type)
1012 char * config_exclusive_filename = NULL;
1013 int ret;
1015 switch(type)
1017 case CONFIG_LOCAL:
1018 config_exclusive_filename = git_pathdup("config");
1019 break;
1020 case CONFIG_GLOBAL:
1021 case CONFIG_XDGGLOBAL:
1023 const char *home = get_windows_home_directory();
1024 if (home)
1026 if (type == CONFIG_GLOBAL)
1027 config_exclusive_filename = xstrdup(mkpath("%s/.gitconfig", home));
1028 else
1029 config_exclusive_filename = xstrdup(mkpath("%s/.config/git/config", home));
1032 break;
1035 if(!config_exclusive_filename)
1036 return -1;
1038 ret = git_config_set_multivar_in_file_gently(config_exclusive_filename, key, value, NULL, 0);
1039 free(config_exclusive_filename);
1040 return ret;
1043 struct mailmap_info {
1044 char *name;
1045 char *email;
1048 struct mailmap_entry {
1049 /* name and email for the simple mail-only case */
1050 char *name;
1051 char *email;
1053 /* name and email for the complex mail and name matching case */
1054 struct string_list namemap;
1057 int git_read_mailmap(GIT_MAILMAP *mailmap)
1059 struct string_list *map;
1060 int result;
1062 if (!mailmap)
1063 return -1;
1065 *mailmap = NULL;
1066 if ((map = (struct string_list *)calloc(1, sizeof(struct string_list))) == NULL)
1067 return -1;
1069 if ((result = read_mailmap(map, NULL)) != 0)
1071 clear_mailmap(map);
1072 free(map);
1073 return result;
1076 if (!map->items)
1078 clear_mailmap(map);
1079 free(map);
1081 return -1;
1084 *mailmap = map;
1085 return 0;
1088 int git_lookup_mailmap(GIT_MAILMAP mailmap, const char** email1, const char** name1, const char* email2, void* payload, const char *(*author2_cb)(void*))
1090 struct string_list *map;
1091 int imax, imin = 0;
1093 if (!mailmap)
1094 return -1;
1096 map = (struct string_list *)mailmap;
1097 imax = map->nr - 1;
1098 while (imax >= imin)
1100 int i = imin + ((imax - imin) / 2);
1101 struct string_list_item *si = (struct string_list_item *)&map->items[i];
1102 struct mailmap_entry *me = (struct mailmap_entry *)si->util;
1103 int comp = map->cmp(si->string, email2);
1105 if (!comp)
1107 if (me->namemap.nr)
1109 const char *author2 = author2_cb(payload);
1110 for (unsigned int j = 0; j < me->namemap.nr; ++j)
1112 struct string_list_item *sj = (struct string_list_item *)&me->namemap.items[j];
1113 struct mailmap_info *mi = (struct mailmap_info *)sj->util;
1115 if (!map->cmp(sj->string, author2))
1117 if (email1)
1118 *email1 = mi->email;
1119 if (name1)
1120 *name1 = mi->name;
1121 return 0;
1126 if (email1)
1127 *email1 = me->email;
1128 if (name1)
1129 *name1 = me->name;
1130 return 0;
1132 else if (comp < 0)
1133 imin = i + 1;
1134 else
1135 imax = i - 1;
1138 return -1;
1141 void git_free_mailmap(GIT_MAILMAP mailmap)
1143 if (!mailmap)
1144 return;
1146 clear_mailmap((struct string_list *)mailmap);
1147 free(mailmap);