Use directory/file information from git status/diff output
[TortoiseGit.git] / ext / gitdll / gitdll.c
blob82eb746ad1add20a572bd013ecfd55e7276f00ff
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - 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_attr_stack(void);
53 extern void git_atexit_dispatch(void);
54 extern void git_atexit_clear(void);
55 extern void invalidate_ref_cache(const char* submodule);
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_attr_stack();
107 g_prefix = setup_git_directory();
108 git_config(git_default_config, NULL);
110 return 0;
113 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR* author, const char* pbuff)
115 const 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 const char* pbuf;
150 const char* end;
151 struct commit *p;
153 p= (struct commit *)commit->m_pGitCommit;
155 memcpy(commit->m_hash, p->object.oid.hash, GIT_HASH_SIZE);
157 commit->m_Encode = NULL;
158 commit->m_EncodeSize = 0;
160 commit->buffer = detach_commit_buffer(commit->m_pGitCommit, NULL);
162 pbuf = commit->buffer;
163 while(pbuf)
165 if (strncmp(pbuf, "author", 6) == 0)
167 ret = git_parse_commit_author(&commit->m_Author,pbuf + 7);
168 if(ret)
169 return -4;
171 else if (strncmp(pbuf, "committer", 9) == 0)
173 ret = git_parse_commit_author(&commit->m_Committer,pbuf + 10);
174 if(ret)
175 return -5;
177 pbuf = strchr(pbuf,'\n');
178 if(pbuf == NULL)
179 return -6;
181 else if (strncmp(pbuf, "encoding", 8) == 0)
183 pbuf += 9;
184 commit->m_Encode=pbuf;
185 end = strchr(pbuf,'\n');
186 commit->m_EncodeSize= (int)(end -pbuf);
189 // the headers end after the first empty line
190 else if (*pbuf == '\n')
192 pbuf++;
194 commit->m_Subject=pbuf;
195 end = strchr(pbuf,'\n');
196 if( end == 0)
197 commit->m_SubjectSize = (int)strlen(pbuf);
198 else
200 commit->m_SubjectSize = (int)(end - pbuf);
201 pbuf = end +1;
202 commit->m_Body = pbuf;
203 commit->m_BodySize = (int)strlen(pbuf);
204 return 0;
208 pbuf = strchr(pbuf,'\n');
209 if(pbuf)
210 pbuf ++;
212 return 0;
215 int git_get_commit_from_hash(GIT_COMMIT* commit, const GIT_HASH hash)
217 int ret = 0;
219 struct commit *p;
221 if (commit == NULL)
222 return -1;
224 memset(commit,0,sizeof(GIT_COMMIT));
226 commit->m_pGitCommit = p = lookup_commit(hash);
228 if(p == NULL)
229 return -1;
231 ret = parse_commit(p);
232 if( ret )
233 return ret;
235 return git_parse_commit(commit);
238 int git_get_commit_first_parent(GIT_COMMIT *commit,GIT_COMMIT_LIST *list)
240 struct commit *p = commit->m_pGitCommit;
242 if(list == NULL)
243 return -1;
245 *list = (GIT_COMMIT_LIST*)p->parents;
246 return 0;
249 int git_commit_is_root(const GIT_COMMIT* commit)
251 struct commit* p = commit->m_pGitCommit;
252 return (struct commit_list**)p->parents ? 1 : 0;
255 int git_get_commit_next_parent(GIT_COMMIT_LIST *list, GIT_HASH hash)
257 struct commit_list *l;
258 if (list == NULL)
259 return -1;
261 l = *(struct commit_list **)list;
262 if (l == NULL)
263 return -1;
265 if(hash)
266 memcpy(hash, l->item->object.oid.hash, GIT_HASH_SIZE);
268 *list = (GIT_COMMIT_LIST *)l->next;
269 return 0;
274 int git_free_commit(GIT_COMMIT *commit)
276 struct commit *p = commit->m_pGitCommit;
278 if( p->parents)
279 free_commit_list(p->parents);
281 if (p->tree)
282 free_tree_buffer(p->tree);
284 #pragma warning(push)
285 #pragma warning(disable: 4090)
286 if (commit->buffer)
287 free(commit->buffer);
288 #pragma warning(pop)
290 p->object.parsed = 0;
291 p->parents = 0;
292 p->tree = 0;
294 memset(commit,0,sizeof(GIT_COMMIT));
295 return 0;
298 char **strtoargv(char *arg, int *size)
300 int count=0;
301 char *p=arg;
302 char **argv;
304 int i=0;
305 while(*p)
307 if(*p == '\\')
308 *p='/';
309 p++;
311 p=arg;
313 while(*p)
315 if(*p == ' ')
316 count ++;
317 p++;
320 argv=malloc(strlen(arg)+1 + (count +2)*sizeof(void*));
321 p=(char*)(argv+count+2);
323 while(*arg)
325 if(*arg != ' ')
327 char space=' ';
328 argv[i]=p;
330 while(*arg)
332 if(*arg == '"')
334 arg++;
335 if(space == ' ')
336 space = '"';
337 else
338 space = ' ';
340 if((*arg == space) || (*arg == 0))
341 break;
343 *p++ = *arg++;
345 i++;
346 *p++=0;
348 if(*arg == 0)
349 break;
350 arg++;
352 argv[i]=NULL;
353 *size = i;
354 return argv;
356 int git_open_log(GIT_LOG * handle, char * arg)
358 struct rev_info *p_Rev;
359 char ** argv=0;
360 int argc=0;
361 unsigned int i=0;
362 struct setup_revision_opt opt;
364 /* clear flags */
365 unsigned int obj_size = get_max_object_index();
366 for(i =0; i<obj_size; i++)
368 struct object *ob= get_indexed_object(i);
369 if(ob)
371 ob->flags=0;
372 if (ob->parsed && ob->type == OBJ_COMMIT)
374 struct commit* commit = (struct commit*)ob;
375 free_commit_list(commit->parents);
376 commit->parents = NULL;
377 if (commit->tree)
378 free_tree_buffer(commit->tree);
379 commit->tree = NULL;
380 ob->parsed = 0;
385 if(arg != NULL)
386 argv = strtoargv(arg,&argc);
388 if (!argv)
389 return -1;
391 p_Rev = malloc(sizeof(struct rev_info));
392 if (p_Rev == NULL)
394 free(argv);
395 return -1;
398 memset(p_Rev,0,sizeof(struct rev_info));
400 invalidate_ref_cache(NULL);
402 init_revisions(p_Rev, g_prefix);
403 p_Rev->diff = 1;
405 memset(&opt, 0, sizeof(opt));
406 opt.def = "HEAD";
408 cmd_log_init(argc, argv, g_prefix,p_Rev,&opt);
410 p_Rev->pPrivate = argv;
411 *handle = p_Rev;
412 return 0;
415 int git_get_log_firstcommit(GIT_LOG handle)
417 return prepare_revision_walk(handle);
420 int git_get_log_estimate_commit_count(GIT_LOG handle)
422 struct rev_info *p_Rev;
423 p_Rev=(struct rev_info *)handle;
425 return estimate_commit_count(p_Rev, p_Rev->commits);
428 int git_get_log_nextcommit(GIT_LOG handle, GIT_COMMIT *commit, int follow)
430 int ret =0;
432 if(commit == NULL)
433 return -1;
435 memset(commit, 0, sizeof(GIT_COMMIT));
437 commit->m_pGitCommit = get_revision(handle);
438 if( commit->m_pGitCommit == NULL)
439 return -2;
441 if (follow && !log_tree_commit(handle, commit->m_pGitCommit))
443 commit->m_ignore = 1;
444 return 0;
446 commit->m_ignore = 0;
448 ret=git_parse_commit(commit);
449 if(ret)
450 return ret;
452 return 0;
455 struct notes_tree **display_notes_trees;
456 int git_close_log(GIT_LOG handle)
458 if(handle)
460 struct rev_info *p_Rev;
461 p_Rev=(struct rev_info *)handle;
462 if(p_Rev->pPrivate)
463 free(p_Rev->pPrivate);
464 free(handle);
466 free_all_pack();
468 if (display_notes_trees)
469 free_notes(*display_notes_trees);
470 display_notes_trees = 0;
471 return 0;
474 int git_open_diff(GIT_DIFF *diff, char * arg)
476 struct rev_info *p_Rev;
477 char ** argv=0;
478 int argc=0;
480 if(arg != NULL)
481 argv = strtoargv(arg,&argc);
483 p_Rev = malloc(sizeof(struct rev_info));
484 memset(p_Rev,0,sizeof(struct rev_info));
486 p_Rev->pPrivate = argv;
487 *diff = (GIT_DIFF)p_Rev;
489 init_revisions(p_Rev, g_prefix);
490 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
491 p_Rev->abbrev = 0;
492 p_Rev->diff = 1;
493 argc = setup_revisions(argc, argv, p_Rev, NULL);
495 return 0;
497 int git_close_diff(GIT_DIFF handle)
499 git_diff_flush(handle);
500 if(handle)
502 struct rev_info *p_Rev;
503 p_Rev=(struct rev_info *)handle;
504 if(p_Rev->pPrivate)
505 free(p_Rev->pPrivate);
506 free(handle);
508 return 0;
510 int git_diff_flush(GIT_DIFF diff)
512 struct diff_queue_struct *q = &diff_queued_diff;
513 struct rev_info *p_Rev;
514 int i;
515 p_Rev = (struct rev_info *)diff;
517 if(q->nr == 0)
518 return 0;
520 for (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 int i;
542 struct diff_queue_struct *q = &diff_queued_diff;
544 p_Rev = (struct rev_info *)diff;
546 ret=diff_root_tree_sha1(hash, "", &p_Rev->diffopt);
548 if(ret)
549 return ret;
551 if(isstat)
553 diffcore_std(&p_Rev->diffopt);
555 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
556 for (i = 0; i < q->nr; i++) {
557 struct diff_filepair *p = q->queue[i];
558 //if (check_pair_status(p))
559 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
563 if (file)
564 *file = q;
565 if (count)
566 *count = q->nr;
568 return 0;
571 int git_do_diff(GIT_DIFF diff, GIT_HASH hash1, GIT_HASH hash2, GIT_FILE * file, int *count,int isstat)
573 struct rev_info *p_Rev;
574 int ret;
575 int i;
576 struct diff_queue_struct *q = &diff_queued_diff;
578 p_Rev = (struct rev_info *)diff;
580 ret = diff_tree_sha1(hash1,hash2,"",&p_Rev->diffopt);
581 if( ret )
583 free_all_pack();
584 return ret;
587 if(isstat)
589 diffcore_std(&p_Rev->diffopt);
590 memset(&p_Rev->diffstat, 0, sizeof(struct diffstat_t));
591 for (i = 0; i < q->nr; i++) {
592 struct diff_filepair *p = q->queue[i];
593 //if (check_pair_status(p))
594 diff_flush_stat(p, &p_Rev->diffopt, &p_Rev->diffstat);
597 free_all_pack();
598 if(file)
599 *file = q;
600 if(count)
601 *count = q->nr;
602 return 0;
605 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)
607 struct diff_queue_struct *q = &diff_queued_diff;
608 struct rev_info *p_Rev;
609 p_Rev = (struct rev_info *)diff;
611 q = (struct diff_queue_struct *)file;
612 if(file == 0)
613 return -1;
614 if(i>=q->nr)
615 return -1;
617 assert(newname && oldname && status && IsDir);
619 *newname = q->queue[i]->two->path;
620 *oldname = q->queue[i]->one->path;
621 *status = q->queue[i]->status;
622 if (*status == 'D')
623 *IsDir = (q->queue[i]->one->mode & S_IFDIR) == S_IFDIR;
624 else
625 *IsDir = (q->queue[i]->two->mode & S_IFDIR) == S_IFDIR;
627 if (q->queue[i]->one->mode && q->queue[i]->two->mode && DIFF_PAIR_TYPE_CHANGED(q->queue[i]))
628 *IsDir = 0;
630 if(p_Rev->diffstat.files)
632 int j;
633 for(j=0;j<p_Rev->diffstat.nr;j++)
635 if(strcmp(*newname,p_Rev->diffstat.files[j]->name)==0)
636 break;
638 if( j== p_Rev->diffstat.nr)
640 *IsBin=1;
641 *inc=0;
642 *dec=0;
643 return 0;
645 if(IsBin)
646 *IsBin = p_Rev->diffstat.files[j]->is_binary;
647 if(inc)
648 *inc = (int)p_Rev->diffstat.files[j]->added;
649 if(dec)
650 *dec = (int)p_Rev->diffstat.files[j]->deleted;
651 }else
653 *IsBin=1;
654 *inc=0;
655 *dec=0;
658 return 0;
661 int git_add_exclude(const char *string, const char *base,
662 int baselen, struct exclude_list *which, int lineno)
664 add_exclude(string, base, baselen, which, lineno);
665 return 0;
668 int git_create_exclude_list(EXCLUDE_LIST *which)
670 *which = malloc(sizeof(struct exclude_list));
671 memset(*which,0,sizeof(struct exclude_list));
672 return 0;
675 int git_free_exclude_list(EXCLUDE_LIST which)
677 int i=0;
678 struct exclude_list *p = (struct exclude_list *) which;
680 for(i=0; i<p->nr;i++)
682 free(p->excludes[i]);
684 free(p->excludes);
685 free(p);
686 return 0;
689 int git_check_excluded_1(const char *pathname,
690 int pathlen, const char *basename, int *dtype,
691 EXCLUDE_LIST el)
693 return is_excluded_from_list(pathname, pathlen, basename, dtype, el);
696 int git_get_notes(const GIT_HASH hash, char** p_note)
698 struct strbuf sb;
699 size_t size;
700 strbuf_init(&sb,0);
701 format_display_notes(hash, &sb, "utf-8", 1);
702 *p_note = strbuf_detach(&sb,&size);
704 return 0;
707 struct cmd_struct {
708 const char *cmd;
709 int (*fn)(int, const char **, const char *);
710 int option;
713 #define RUN_SETUP (1<<0)
715 static struct cmd_struct commands[] = {
716 { "notes", cmd_notes, RUN_SETUP },
717 { "update-index", cmd_update_index, RUN_SETUP },
720 int git_run_cmd(char *cmd, char *arg)
723 int i=0;
724 char ** argv=0;
725 int argc=0;
727 git_init();
729 for(i=0;i< sizeof(commands) / sizeof(struct cmd_struct);i++)
731 if(strcmp(cmd,commands[i].cmd)==0)
733 int ret;
734 if(arg != NULL)
735 argv = strtoargv(arg,&argc);
737 ret = commands[i].fn(argc, argv, NULL);
739 if(argv)
740 free(argv);
742 discard_cache();
743 free_all_pack();
745 return ret;
750 return -1;
753 void git_exit_cleanup(void)
755 git_atexit_dispatch();
756 git_atexit_clear();
759 int git_for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
761 return for_each_reflog_ent(ref,fn,cb_data);
764 static int update_some(const unsigned char* sha1, struct strbuf* base,
765 const char *pathname, unsigned mode, int stage, void *context)
767 struct cache_entry *ce;
768 UNREFERENCED_PARAMETER(stage);
770 ce = (struct cache_entry *)context;
772 if (S_ISDIR(mode))
773 return READ_TREE_RECURSIVE;
775 hashcpy(ce->sha1, sha1);
776 memcpy(ce->name, base->buf, base->len);
777 memcpy(ce->name + base->len, pathname, strlen(pathname));
778 ce->ce_flags = create_ce_flags((unsigned int)(strlen(pathname) + base->len));
779 ce->ce_mode = create_ce_mode(mode);
781 return 0;
784 int git_checkout_file(const char* ref, const char* path, char* outputpath)
786 struct cache_entry *ce;
787 int ret;
788 GIT_HASH sha1;
789 struct tree * root;
790 struct checkout state;
791 struct pathspec pathspec;
792 const char *matchbuf[1];
793 ret = get_sha1(ref, sha1);
794 if(ret)
795 return ret;
797 reprepare_packed_git();
798 root = parse_tree_indirect(sha1);
800 if(!root)
802 free_all_pack();
803 return -1;
806 ce = xcalloc(1, cache_entry_size(strlen(path)));
808 matchbuf[0] = NULL;
809 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC, PATHSPEC_PREFER_CWD, path, matchbuf);
810 pathspec.items[0].nowildcard_len = pathspec.items[0].len;
811 ret = read_tree_recursive(root, "", 0, 0, &pathspec, update_some, ce);
812 clear_pathspec(&pathspec);
814 if(ret)
816 free_all_pack();
817 free(ce);
818 return ret;
820 memset(&state, 0, sizeof(state));
821 state.force = 1;
822 state.refresh_cache = 0;
824 ret = write_entry(ce, outputpath, &state, 0);
825 free_all_pack();
826 free(ce);
827 return ret;
829 struct config_buf
831 char *buf;
832 const char *key;
833 size_t size;
834 int seen;
837 static int get_config(const char *key_, const char *value_, void *cb)
839 struct config_buf *buf;
840 buf=(struct config_buf*)cb;
841 if(strcmp(key_, buf->key))
842 return 0;
844 if (value_)
845 strncpy(buf->buf,value_,buf->size);
846 else
848 buf->buf[0] = 't';
849 buf->buf[1] = 'r';
850 buf->buf[2] = 'u';
851 buf->buf[3] = 'e';
852 buf->buf[4] = 0;
854 buf->seen = 1;
855 return 0;
859 // wchar_t wrapper for program_data_config()
860 const wchar_t* wget_program_data_config(void)
862 static const wchar_t *programdata_git_config = NULL;
863 wchar_t wpointer[MAX_PATH];
865 if (programdata_git_config)
866 return programdata_git_config;
868 if (xutftowcs_path(wpointer, program_data_config()) < 0)
869 return NULL;
871 programdata_git_config = _wcsdup(wpointer);
873 return programdata_git_config;
876 // wchar_t wrapper for git_etc_gitconfig()
877 const wchar_t *wget_msysgit_etc(void)
879 static const wchar_t *etc_gitconfig = NULL;
880 wchar_t wpointer[MAX_PATH];
882 if (etc_gitconfig)
883 return etc_gitconfig;
885 if (xutftowcs_path(wpointer, git_etc_gitconfig()) < 0)
886 return NULL;
888 etc_gitconfig = _wcsdup(wpointer);
890 return etc_gitconfig;
893 int git_get_config(const char *key, char *buffer, int size)
895 char *local, *global, *globalxdg;
896 const char *home, *system, *programdata;
897 struct config_buf buf;
898 struct git_config_source config_source = { 0 };
900 buf.buf=buffer;
901 buf.size=size;
902 buf.seen = 0;
903 buf.key = key;
905 home = get_windows_home_directory();
906 if (home)
908 global = xstrdup(mkpath("%s/.gitconfig", home));
909 globalxdg = xstrdup(mkpath("%s/.config/git/config", home));
911 else
913 global = NULL;
914 globalxdg = NULL;
917 system = git_etc_gitconfig();
918 programdata = git_program_data_config();
920 local = git_pathdup("config");
922 if (!buf.seen)
924 config_source.file = local;
925 git_config_with_options(get_config, &buf, &config_source, 1);
927 if (!buf.seen && global)
929 config_source.file = global;
930 git_config_with_options(get_config, &buf, &config_source, 1);
932 if (!buf.seen && globalxdg)
934 config_source.file = globalxdg;
935 git_config_with_options(get_config, &buf, &config_source, 1);
937 if (!buf.seen && system)
939 config_source.file = system;
940 git_config_with_options(get_config, &buf, &config_source, 1);
942 if (!buf.seen && programdata)
944 config_source.file = programdata;
945 git_config_with_options(get_config, &buf, &config_source, 1);
948 if(local)
949 free(local);
950 if(global)
951 free(global);
952 if (globalxdg)
953 free(globalxdg);
955 return !buf.seen;
958 // taken from msysgit: compat/mingw.c
959 const char *get_windows_home_directory(void)
961 static const char *home_directory = NULL;
962 struct strbuf buf = STRBUF_INIT;
964 if (home_directory)
965 return home_directory;
967 home_directory = getenv("HOME");
968 if (home_directory && *home_directory)
970 home_directory = _strdup(home_directory);
971 return home_directory;
974 strbuf_addf(&buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
975 home_directory = strbuf_detach(&buf, NULL);
977 return home_directory;
980 // wchar_t wrapper for get_windows_home_directory()
981 const wchar_t *wget_windows_home_directory(void)
983 static const wchar_t *home_directory = NULL;
984 wchar_t wpointer[MAX_PATH];
986 if (home_directory)
987 return home_directory;
989 if (xutftowcs_path(wpointer, get_windows_home_directory()) < 0)
990 return NULL;
992 home_directory = _wcsdup(wpointer);
994 return home_directory;
997 int get_set_config(const char *key, const char *value, CONFIG_TYPE type)
999 char * config_exclusive_filename = NULL;
1000 int ret;
1002 switch(type)
1004 case CONFIG_LOCAL:
1005 config_exclusive_filename = git_pathdup("config");
1006 break;
1007 case CONFIG_GLOBAL:
1008 case CONFIG_XDGGLOBAL:
1010 const char *home = get_windows_home_directory();
1011 if (home)
1013 if (type == CONFIG_GLOBAL)
1014 config_exclusive_filename = xstrdup(mkpath("%s/.gitconfig", home));
1015 else
1016 config_exclusive_filename = xstrdup(mkpath("%s/.config/git/config", home));
1019 break;
1022 if(!config_exclusive_filename)
1023 return -1;
1025 ret = git_config_set_multivar_in_file_gently(config_exclusive_filename, key, value, NULL, 0);
1026 free(config_exclusive_filename);
1027 return ret;
1030 struct mailmap_info {
1031 char *name;
1032 char *email;
1035 struct mailmap_entry {
1036 /* name and email for the simple mail-only case */
1037 char *name;
1038 char *email;
1040 /* name and email for the complex mail and name matching case */
1041 struct string_list namemap;
1044 int git_read_mailmap(GIT_MAILMAP *mailmap)
1046 struct string_list *map;
1047 int result;
1049 if (!mailmap)
1050 return -1;
1052 *mailmap = NULL;
1053 if ((map = (struct string_list *)calloc(1, sizeof(struct string_list))) == NULL)
1054 return -1;
1056 if ((result = read_mailmap(map, NULL)) != 0)
1058 clear_mailmap(map);
1059 free(map);
1060 return result;
1063 if (!map->items)
1065 clear_mailmap(map);
1066 free(map);
1068 return -1;
1071 *mailmap = map;
1072 return 0;
1075 int git_lookup_mailmap(GIT_MAILMAP mailmap, const char** email1, const char** name1, const char* email2, void* payload, const char *(*author2_cb)(void*))
1077 struct string_list *map;
1078 int imax, imin = 0;
1080 if (!mailmap)
1081 return -1;
1083 map = (struct string_list *)mailmap;
1084 imax = map->nr - 1;
1085 while (imax >= imin)
1087 int i = imin + ((imax - imin) / 2);
1088 struct string_list_item *si = (struct string_list_item *)&map->items[i];
1089 struct mailmap_entry *me = (struct mailmap_entry *)si->util;
1090 int comp = map->cmp(si->string, email2);
1092 if (!comp)
1094 if (me->namemap.nr)
1096 const char *author2 = author2_cb(payload);
1097 unsigned int j;
1098 for (j = 0; j < me->namemap.nr; ++j)
1100 struct string_list_item *sj = (struct string_list_item *)&me->namemap.items[j];
1101 struct mailmap_info *mi = (struct mailmap_info *)sj->util;
1103 if (!map->cmp(sj->string, author2))
1105 if (email1)
1106 *email1 = mi->email;
1107 if (name1)
1108 *name1 = mi->name;
1109 return 0;
1114 if (email1)
1115 *email1 = me->email;
1116 if (name1)
1117 *name1 = me->name;
1118 return 0;
1120 else if (comp < 0)
1121 imin = i + 1;
1122 else
1123 imax = i - 1;
1126 return -1;
1129 void git_free_mailmap(GIT_MAILMAP mailmap)
1131 if (!mailmap)
1132 return;
1134 clear_mailmap((struct string_list *)mailmap);
1135 free(mailmap);