1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - 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.
23 #include "../build/libgit-defines.h"
24 #include "git-compat-util.h"
36 #include "run-command.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();
47 set_die_routine(die_dll
);
48 set_die_is_recursing_routine(die_is_recursing_dll
);
51 int git_get_sha1(const char *name
, GIT_HASH sha1
)
53 return get_sha1(name
,sha1
);
56 static int convert_slash(char * path
)
69 char path
[MAX_PATH
+1];
74 _setmode(_fileno(stdin
), _O_BINARY
);
75 _setmode(_fileno(stdout
), _O_BINARY
);
76 _setmode(_fileno(stderr
), _O_BINARY
);
78 // set HOME if not set already
79 getenv_s(&homesize
, NULL
, 0, "HOME");
82 _wputenv_s(L
"HOME", wget_windows_home_directory());
84 GetModuleFileName(NULL
, path
, MAX_PATH
);
87 git_extract_argv0_path(path
);
88 g_prefix
= setup_git_directory();
89 ret
= git_config(git_default_config
, NULL
);
93 _putenv_s("HOME","");/* clear home evironment to avoid affact third part software*/
99 static int git_parse_commit_author(struct GIT_COMMIT_AUTHOR
*author
, char *pbuff
)
104 end
=strchr(pbuff
,'<');
109 author
->NameSize
= end
- pbuff
- 1;
112 end
= strchr(pbuff
, '>');
116 author
->Email
= pbuff
;
117 author
->EmailSize
= end
- pbuff
;
121 author
->Date
= atol(pbuff
);
122 end
= strchr(pbuff
, ' ');
127 author
->TimeZone
= atol(pbuff
);
132 int git_parse_commit(GIT_COMMIT
*commit
)
139 p
= (struct commit
*)commit
->m_pGitCommit
;
141 memcpy(commit
->m_hash
,p
->object
.sha1
,GIT_HASH_SIZE
);
143 commit
->m_Encode
= NULL
;
144 commit
->m_EncodeSize
= 0;
146 if(p
->buffer
== NULL
)
152 if (strncmp(pbuf
, "author", 6) == 0)
154 ret
= git_parse_commit_author(&commit
->m_Author
,pbuf
+ 7);
158 else if (strncmp(pbuf
, "committer", 9) == 0)
160 ret
= git_parse_commit_author(&commit
->m_Committer
,pbuf
+ 10);
164 pbuf
= strchr(pbuf
,'\n');
168 else if (strncmp(pbuf
, "encoding", 8) == 0)
171 commit
->m_Encode
=pbuf
;
172 end
= strchr(pbuf
,'\n');
173 commit
->m_EncodeSize
=end
-pbuf
;
176 // the headers end after the first empty line
177 else if (*pbuf
== '\n')
181 commit
->m_Subject
=pbuf
;
182 end
= strchr(pbuf
,'\n');
184 commit
->m_SubjectSize
= strlen(pbuf
);
187 commit
->m_SubjectSize
= end
- pbuf
;
189 commit
->m_Body
= pbuf
;
190 commit
->m_BodySize
= strlen(pbuf
);
195 pbuf
= strchr(pbuf
,'\n');
202 int git_get_commit_from_hash(GIT_COMMIT
*commit
, GIT_HASH hash
)
211 memset(commit
,0,sizeof(GIT_COMMIT
));
213 commit
->m_pGitCommit
= p
= lookup_commit(hash
);
218 ret
= parse_commit(p
);
222 return git_parse_commit(commit
);
225 int git_get_commit_first_parent(GIT_COMMIT
*commit
,GIT_COMMIT_LIST
*list
)
227 struct commit
*p
= commit
->m_pGitCommit
;
232 *list
= (GIT_COMMIT_LIST
*)p
->parents
;
235 int git_get_commit_next_parent(GIT_COMMIT_LIST
*list
, GIT_HASH hash
)
237 struct commit_list
*l
;
241 l
= *(struct commit_list
**)list
;
246 memcpy(hash
, l
->item
->object
.sha1
, GIT_HASH_SIZE
);
248 *list
= (GIT_COMMIT_LIST
*)l
->next
;
254 int git_free_commit(GIT_COMMIT
*commit
)
256 struct commit
*p
= commit
->m_pGitCommit
;
259 free_commit_list(p
->parents
);
269 memset(commit
,0,sizeof(GIT_COMMIT
));
273 char **strtoargv(char *arg
, int *size
)
295 argv
=malloc(strlen(arg
)+1 + (count
+2)*sizeof(void*));
296 p
=(char*)(argv
+count
+2);
315 if((*arg
== space
) || (*arg
== 0))
331 int git_open_log(GIT_LOG
* handle
, char * arg
)
333 struct rev_info
*p_Rev
;
337 struct setup_revision_opt opt
;
340 unsigned int obj_size
= get_max_object_index();
341 for(i
=0; i
<obj_size
; i
++)
343 struct object
*ob
= get_indexed_object(i
);
349 argv
= strtoargv(arg
,&argc
);
354 p_Rev
= malloc(sizeof(struct rev_info
));
355 memset(p_Rev
,0,sizeof(struct rev_info
));
360 invalidate_ref_cache(NULL
);
362 init_revisions(p_Rev
, g_prefix
);
365 memset(&opt
, 0, sizeof(opt
));
368 cmd_log_init(argc
, argv
, g_prefix
,p_Rev
,&opt
);
370 p_Rev
->pPrivate
= argv
;
375 int git_get_log_firstcommit(GIT_LOG handle
)
377 return prepare_revision_walk(handle
);
380 int git_get_log_estimate_commit_count(GIT_LOG handle
)
382 struct rev_info
*p_Rev
;
383 p_Rev
=(struct rev_info
*)handle
;
385 return estimate_commit_count(p_Rev
, p_Rev
->commits
);
388 int git_get_log_nextcommit(GIT_LOG handle
, GIT_COMMIT
*commit
, int follow
)
395 memset(commit
, 0, sizeof(GIT_COMMIT
));
397 commit
->m_pGitCommit
= get_revision(handle
);
398 if( commit
->m_pGitCommit
== NULL
)
401 if (follow
&& !log_tree_commit(handle
, commit
->m_pGitCommit
))
403 commit
->m_ignore
= 1;
406 commit
->m_ignore
= 0;
408 ret
=git_parse_commit(commit
);
415 struct notes_tree
**display_notes_trees
;
416 int git_close_log(GIT_LOG handle
)
420 struct rev_info
*p_Rev
;
421 p_Rev
=(struct rev_info
*)handle
;
423 free(p_Rev
->pPrivate
);
428 if (display_notes_trees
)
429 free_notes(*display_notes_trees
);
430 display_notes_trees
= 0;
434 int git_open_diff(GIT_DIFF
*diff
, char * arg
)
436 struct rev_info
*p_Rev
;
441 argv
= strtoargv(arg
,&argc
);
443 p_Rev
= malloc(sizeof(struct rev_info
));
444 memset(p_Rev
,0,sizeof(struct rev_info
));
446 p_Rev
->pPrivate
= argv
;
447 *diff
= (GIT_DIFF
)p_Rev
;
449 init_revisions(p_Rev
, g_prefix
);
450 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
453 argc
= setup_revisions(argc
, argv
, p_Rev
, NULL
);
457 int git_close_diff(GIT_DIFF handle
)
459 git_diff_flush(handle
);
462 struct rev_info
*p_Rev
;
463 p_Rev
=(struct rev_info
*)handle
;
465 free(p_Rev
->pPrivate
);
470 int git_diff_flush(GIT_DIFF diff
)
472 struct diff_queue_struct
*q
= &diff_queued_diff
;
473 struct rev_info
*p_Rev
;
475 p_Rev
= (struct rev_info
*)diff
;
480 for (i
= 0; i
< q
->nr
; i
++)
481 diff_free_filepair(q
->queue
[i
]);
487 q
->nr
= q
->alloc
= 0;
490 if (p_Rev
->diffopt
.close_file
)
491 fclose(p_Rev
->diffopt
.file
);
493 free_diffstat_info(&p_Rev
->diffstat
);
497 int git_root_diff(GIT_DIFF diff
, GIT_HASH hash
,GIT_FILE
*file
, int *count
, int isstat
)
500 struct rev_info
*p_Rev
;
502 struct diff_queue_struct
*q
= &diff_queued_diff
;
504 p_Rev
= (struct rev_info
*)diff
;
506 ret
=diff_root_tree_sha1(hash
, "", &p_Rev
->diffopt
);
513 diffcore_std(&p_Rev
->diffopt
);
515 memset(&p_Rev
->diffstat
, 0, sizeof(struct diffstat_t
));
516 for (i
= 0; i
< q
->nr
; i
++) {
517 struct diff_filepair
*p
= q
->queue
[i
];
518 //if (check_pair_status(p))
519 diff_flush_stat(p
, &p_Rev
->diffopt
, &p_Rev
->diffstat
);
530 int git_do_diff(GIT_DIFF diff
, GIT_HASH hash1
, GIT_HASH hash2
, GIT_FILE
* file
, int *count
,int isstat
)
532 struct rev_info
*p_Rev
;
535 struct diff_queue_struct
*q
= &diff_queued_diff
;
537 p_Rev
= (struct rev_info
*)diff
;
539 ret
= diff_tree_sha1(hash1
,hash2
,"",&p_Rev
->diffopt
);
548 diffcore_std(&p_Rev
->diffopt
);
549 memset(&p_Rev
->diffstat
, 0, sizeof(struct diffstat_t
));
550 for (i
= 0; i
< q
->nr
; i
++) {
551 struct diff_filepair
*p
= q
->queue
[i
];
552 //if (check_pair_status(p))
553 diff_flush_stat(p
, &p_Rev
->diffopt
, &p_Rev
->diffstat
);
564 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
)
566 struct diff_queue_struct
*q
= &diff_queued_diff
;
567 struct rev_info
*p_Rev
;
568 p_Rev
= (struct rev_info
*)diff
;
570 q
= (struct diff_queue_struct
*)file
;
576 assert(newname
&& oldname
&& status
);
578 *newname
= q
->queue
[i
]->two
->path
;
579 *oldname
= q
->queue
[i
]->one
->path
;
580 *status
= q
->queue
[i
]->status
;
582 if(p_Rev
->diffstat
.files
)
585 for(j
=0;j
<p_Rev
->diffstat
.nr
;j
++)
587 if(strcmp(*newname
,p_Rev
->diffstat
.files
[j
]->name
)==0)
590 if( j
== p_Rev
->diffstat
.nr
)
598 *IsBin
= p_Rev
->diffstat
.files
[j
]->is_binary
;
600 *inc
= p_Rev
->diffstat
.files
[j
]->added
;
602 *dec
= p_Rev
->diffstat
.files
[j
]->deleted
;
613 int git_read_tree(GIT_HASH hash
,read_tree_fn_t fn
, void *context
)
617 reprepare_packed_git();
618 root
= parse_tree_indirect(hash
);
625 ret
= read_tree_recursive(root
, NULL
, 0, 0, NULL
, fn
, context
);
630 int git_add_exclude(const char *string
, const char *base
,
631 int baselen
, struct exclude_list
*which
, int lineno
)
633 add_exclude(string
, base
, baselen
, which
, lineno
);
637 int git_create_exclude_list(EXCLUDE_LIST
*which
)
639 *which
= malloc(sizeof(struct exclude_list
));
640 memset(*which
,0,sizeof(struct exclude_list
));
644 int git_free_exclude_list(EXCLUDE_LIST which
)
647 struct exclude_list
*p
= (struct exclude_list
*) which
;
649 for(i
=0; i
<p
->nr
;i
++)
651 free(p
->excludes
[i
]);
658 int git_check_excluded_1(const char *pathname
,
659 int pathlen
, const char *basename
, int *dtype
,
662 return is_excluded_from_list(pathname
, pathlen
, basename
, dtype
, el
);
665 int git_get_notes(GIT_HASH hash
, char **p_note
)
670 format_display_notes(hash
, &sb
, "utf-8", 1);
671 *p_note
= strbuf_detach(&sb
,&size
);
678 int (*fn
)(int, const char **, const char *);
682 #define RUN_SETUP (1<<0)
683 #define USE_PAGER (1<<1)
685 * require working tree to be present -- anything uses this needs
686 * RUN_SETUP for reading from the configuration file.
688 #define NEED_WORK_TREE (1<<2)
690 const char git_usage_string
[] =
691 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
692 " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
693 " [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]\n"
694 " [-c name=value] [--help]\n"
697 const char git_more_info_string
[] =
698 "See 'git help COMMAND' for more information on a specific command.";
700 static struct cmd_struct commands
[] = {
701 { "add", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
702 { "stage", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
703 { "annotate", cmd_annotate
, RUN_SETUP
},
704 { "apply", cmd_apply
},
705 { "archive", cmd_archive
},
706 { "bisect--helper", cmd_bisect__helper
, RUN_SETUP
| NEED_WORK_TREE
},
707 { "blame", cmd_blame
, RUN_SETUP
},
708 { "branch", cmd_branch
, RUN_SETUP
},
709 { "bundle", cmd_bundle
},
710 { "cat-file", cmd_cat_file
, RUN_SETUP
},
711 { "checkout", cmd_checkout
, RUN_SETUP
| NEED_WORK_TREE
},
712 { "checkout-index", cmd_checkout_index
,
713 RUN_SETUP
| NEED_WORK_TREE
},
714 { "check-ref-format", cmd_check_ref_format
},
715 { "check-attr", cmd_check_attr
, RUN_SETUP
},
716 { "cherry", cmd_cherry
, RUN_SETUP
},
717 { "cherry-pick", cmd_cherry_pick
, RUN_SETUP
| NEED_WORK_TREE
},
718 { "clone", cmd_clone
},
719 { "clean", cmd_clean
, RUN_SETUP
| NEED_WORK_TREE
},
720 { "commit", cmd_commit
, RUN_SETUP
| NEED_WORK_TREE
},
721 { "commit-tree", cmd_commit_tree
, RUN_SETUP
},
722 { "config", cmd_config
},
723 { "count-objects", cmd_count_objects
, RUN_SETUP
},
724 { "describe", cmd_describe
, RUN_SETUP
},
725 { "diff", cmd_diff
},
726 { "diff-files", cmd_diff_files
, RUN_SETUP
| NEED_WORK_TREE
},
727 { "diff-index", cmd_diff_index
, RUN_SETUP
},
728 { "diff-tree", cmd_diff_tree
, RUN_SETUP
},
729 { "fast-export", cmd_fast_export
, RUN_SETUP
},
730 { "fetch", cmd_fetch
, RUN_SETUP
},
731 { "fetch-pack", cmd_fetch_pack
, RUN_SETUP
},
732 { "fmt-merge-msg", cmd_fmt_merge_msg
, RUN_SETUP
},
733 { "for-each-ref", cmd_for_each_ref
, RUN_SETUP
},
734 { "format-patch", cmd_format_patch
, RUN_SETUP
},
735 { "fsck", cmd_fsck
, RUN_SETUP
},
736 { "fsck-objects", cmd_fsck
, RUN_SETUP
},
737 { "gc", cmd_gc
, RUN_SETUP
},
738 { "get-tar-commit-id", cmd_get_tar_commit_id
},
739 { "grep", cmd_grep
},
740 { "hash-object", cmd_hash_object
},
741 { "help", cmd_help
},
742 { "index-pack", cmd_index_pack
},
743 { "init", cmd_init_db
},
744 { "init-db", cmd_init_db
},
745 { "log", cmd_log
, RUN_SETUP
},
746 { "ls-files", cmd_ls_files
, RUN_SETUP
},
747 { "ls-tree", cmd_ls_tree
, RUN_SETUP
},
748 { "ls-remote", cmd_ls_remote
},
749 { "mailinfo", cmd_mailinfo
},
750 { "mailsplit", cmd_mailsplit
},
751 { "merge", cmd_merge
, RUN_SETUP
| NEED_WORK_TREE
},
752 { "merge-base", cmd_merge_base
, RUN_SETUP
},
753 { "merge-file", cmd_merge_file
},
754 { "merge-index", cmd_merge_index
, RUN_SETUP
},
755 { "merge-ours", cmd_merge_ours
, RUN_SETUP
},
756 { "merge-recursive", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
757 { "merge-recursive-ours", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
758 { "merge-recursive-theirs", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
759 { "merge-subtree", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
760 { "merge-tree", cmd_merge_tree
, RUN_SETUP
},
761 { "mktag", cmd_mktag
, RUN_SETUP
},
762 { "mktree", cmd_mktree
, RUN_SETUP
},
763 { "mv", cmd_mv
, RUN_SETUP
| NEED_WORK_TREE
},
764 { "name-rev", cmd_name_rev
, RUN_SETUP
},
765 { "notes", cmd_notes
, RUN_SETUP
},
766 { "pack-objects", cmd_pack_objects
, RUN_SETUP
},
767 { "pack-redundant", cmd_pack_redundant
, RUN_SETUP
},
768 { "patch-id", cmd_patch_id
},
769 { "pickaxe", cmd_blame
, RUN_SETUP
},
770 { "prune", cmd_prune
, RUN_SETUP
},
771 { "prune-packed", cmd_prune_packed
, RUN_SETUP
},
772 { "push", cmd_push
, RUN_SETUP
},
773 { "read-tree", cmd_read_tree
, RUN_SETUP
},
774 { "receive-pack", cmd_receive_pack
},
775 { "reflog", cmd_reflog
, RUN_SETUP
},
776 { "remote", cmd_remote
, RUN_SETUP
},
777 { "replace", cmd_replace
, RUN_SETUP
},
778 { "rerere", cmd_rerere
, RUN_SETUP
},
779 { "reset", cmd_reset
, RUN_SETUP
},
780 { "rev-list", cmd_rev_list
, RUN_SETUP
},
781 { "rev-parse", cmd_rev_parse
},
782 { "revert", cmd_revert
, RUN_SETUP
| NEED_WORK_TREE
},
783 { "rm", cmd_rm
, RUN_SETUP
},
784 { "send-pack", cmd_send_pack
, RUN_SETUP
},
785 { "shortlog", cmd_shortlog
, USE_PAGER
},
786 { "show-branch", cmd_show_branch
, RUN_SETUP
},
787 { "show", cmd_show
, RUN_SETUP
},
788 { "status", cmd_status
, RUN_SETUP
| NEED_WORK_TREE
},
789 { "stripspace", cmd_stripspace
},
790 { "symbolic-ref", cmd_symbolic_ref
, RUN_SETUP
},
791 { "tag", cmd_tag
, RUN_SETUP
},
792 { "unpack-file", cmd_unpack_file
, RUN_SETUP
},
793 { "unpack-objects", cmd_unpack_objects
, RUN_SETUP
},
794 { "update-index", cmd_update_index
, RUN_SETUP
},
795 { "update-ref", cmd_update_ref
, RUN_SETUP
},
796 { "update-server-info", cmd_update_server_info
, RUN_SETUP
},
797 { "upload-archive", cmd_upload_archive
},
799 { "verify-tag", cmd_verify_tag
, RUN_SETUP
},
800 { "version", cmd_version
},
801 { "whatchanged", cmd_whatchanged
, RUN_SETUP
},
802 { "write-tree", cmd_write_tree
, RUN_SETUP
},
803 { "verify-pack", cmd_verify_pack
},
804 { "show-ref", cmd_show_ref
, RUN_SETUP
},
805 { "pack-refs", cmd_pack_refs
, RUN_SETUP
},
808 int is_builtin(const char *s
)
811 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
812 struct cmd_struct
*p
= commands
+i
;
813 if (!strcmp(s
, p
->cmd
))
819 int git_run_cmd(char *cmd
, char *arg
)
828 for(i
=0;i
< sizeof(commands
) / sizeof(struct cmd_struct
);i
++)
830 if(strcmp(cmd
,commands
[i
].cmd
)==0)
834 argv
= strtoargv(arg
,&argc
);
836 ret
= commands
[i
].fn(argc
, argv
, NULL
);
852 int git_for_each_ref_in(const char * refname
, each_ref_fn fn
, void * data
)
855 invalidate_ref_cache(NULL
);
856 ret
= for_each_ref_in(refname
, fn
, data
);
861 const char *git_resolve_ref(const char *ref
, unsigned char *sha1
, int reading
, int *flag
)
863 invalidate_ref_cache(NULL
);
864 return resolve_ref_unsafe(ref
,sha1
,reading
, flag
);
866 int git_for_each_reflog_ent(const char *ref
, each_reflog_ent_fn fn
, void *cb_data
)
868 return for_each_reflog_ent(ref
,fn
,cb_data
);
871 int git_deref_tag(const unsigned char *tagsha1
, GIT_HASH refhash
)
873 struct object
*obj
= NULL
;
874 obj
= parse_object(tagsha1
);
878 if (obj
->type
== OBJ_TAG
)
880 obj
= deref_tag(obj
, "", 0);
884 memcpy(refhash
, obj
->sha1
, sizeof(GIT_HASH
));
891 static int update_some(const unsigned char *sha1
, const char *base
, int baselen
,
892 const char *pathname
, unsigned mode
, int stage
, void *context
)
894 struct cache_entry
*ce
;
895 UNREFERENCED_PARAMETER(stage
);
897 ce
= (struct cache_entry
*)context
;
900 return READ_TREE_RECURSIVE
;
902 hashcpy(ce
->sha1
, sha1
);
903 memcpy(ce
->name
, base
, baselen
);
904 memcpy(ce
->name
+ baselen
, pathname
, strlen(pathname
));
905 ce
->ce_flags
= create_ce_flags(strlen(pathname
) + baselen
);
906 ce
->ce_mode
= create_ce_mode(mode
);
911 int git_checkout_file(const char *ref
, const char *path
, const char *outputpath
)
913 struct cache_entry
*ce
;
917 struct checkout state
;
918 struct pathspec pathspec
;
919 const char *matchbuf
[1];
920 ret
= get_sha1(ref
, sha1
);
924 reprepare_packed_git();
925 root
= parse_tree_indirect(sha1
);
933 ce
= xcalloc(1, cache_entry_size(strlen(path
)));
936 parse_pathspec(&pathspec
, PATHSPEC_ALL_MAGIC
, PATHSPEC_PREFER_CWD
, path
, matchbuf
);
937 pathspec
.items
[0].nowildcard_len
= pathspec
.items
[0].len
;
938 ret
= read_tree_recursive(root
, "", 0, 0, &pathspec
, update_some
, ce
);
939 free_pathspec(&pathspec
);
947 memset(&state
, 0, sizeof(state
));
949 state
.refresh_cache
= 0;
951 ret
= write_entry(ce
, outputpath
, &state
, 0);
964 static int get_config(const char *key_
, const char *value_
, void *cb
)
966 struct config_buf
*buf
;
967 buf
=(struct config_buf
*)cb
;
968 if(strcmp(key_
, buf
->key
))
972 strncpy(buf
->buf
,value_
,buf
->size
);
986 // wchar_t wrapper for git_etc_gitconfig()
987 const wchar_t *wget_msysgit_etc(void)
989 static const wchar_t *etc_gitconfig
= NULL
;
990 wchar_t wpointer
[MAX_PATH
];
993 return etc_gitconfig
;
995 if (xutftowcs_path(wpointer
, git_etc_gitconfig()) < 0)
998 etc_gitconfig
= _wcsdup(wpointer
);
1000 return etc_gitconfig
;
1003 int git_get_config(const char *key
, char *buffer
, int size
)
1005 char *local
, *global
, *globalxdg
;
1006 const char *home
, *system
;
1007 struct config_buf buf
;
1014 home
= get_windows_home_directory();
1017 global
= xstrdup(mkpath("%s/.gitconfig", home
));
1018 globalxdg
= xstrdup(mkpath("%s/.config/git/config", home
));
1026 system
= git_etc_gitconfig();
1028 local
= git_pathdup("config");
1031 git_config_with_options(get_config
, &buf
, local
, NULL
, 1);
1032 if (!buf
.seen
&& global
)
1033 git_config_with_options(get_config
, &buf
, global
, NULL
, 1);
1034 if (!buf
.seen
&& globalxdg
)
1035 git_config_with_options(get_config
, &buf
, globalxdg
, NULL
, 1);
1036 if (!buf
.seen
&& system
)
1037 git_config_with_options(get_config
, &buf
, system
, NULL
, 1);
1049 // taken from msysgit: compat/mingw.c
1050 const char *get_windows_home_directory(void)
1052 static const char *home_directory
= NULL
;
1053 struct strbuf buf
= STRBUF_INIT
;
1056 return home_directory
;
1058 home_directory
= getenv("HOME");
1059 if (home_directory
&& *home_directory
)
1060 return home_directory
;
1062 strbuf_addf(&buf
, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
1063 home_directory
= strbuf_detach(&buf
, NULL
);
1065 return home_directory
;
1068 // wchar_t wrapper for get_windows_home_directory()
1069 const wchar_t *wget_windows_home_directory(void)
1071 static const wchar_t *home_directory
= NULL
;
1072 wchar_t wpointer
[MAX_PATH
];
1075 return home_directory
;
1077 if (xutftowcs_path(wpointer
, get_windows_home_directory()) < 0)
1080 home_directory
= _wcsdup(wpointer
);
1082 return home_directory
;
1085 int get_set_config(const char *key
, const char *value
, CONFIG_TYPE type
)
1087 char * config_exclusive_filename
= NULL
;
1092 config_exclusive_filename
= git_pathdup("config");
1095 case CONFIG_XDGGLOBAL
:
1097 const char *home
= get_windows_home_directory();
1100 if (type
== CONFIG_GLOBAL
)
1101 config_exclusive_filename
= xstrdup(mkpath("%s/.gitconfig", home
));
1103 config_exclusive_filename
= xstrdup(mkpath("%s/.config/git/config", home
));
1109 if(!config_exclusive_filename
)
1112 return git_config_set_multivar_in_file(config_exclusive_filename
, key
, value
, NULL
, 0);
1115 struct mailmap_info
{
1120 struct mailmap_entry
{
1121 /* name and email for the simple mail-only case */
1125 /* name and email for the complex mail and name matching case */
1126 struct string_list namemap
;
1129 int git_read_mailmap(GIT_MAILMAP
*mailmap
)
1131 struct string_list
*map
;
1138 if ((map
= (struct string_list
*)calloc(1, sizeof(struct string_list
))) == NULL
)
1141 if ((result
= read_mailmap(map
, NULL
)) != 0)
1148 const char * git_get_mailmap_author(GIT_MAILMAP mailmap
, const char *email2
, void *payload
, const char *(*author2_cb
)(void *))
1150 struct string_list
*map
;
1156 map
= (struct string_list
*)mailmap
;
1158 while (imax
>= imin
)
1160 int i
= imin
+ ((imax
- imin
) / 2);
1161 struct string_list_item
*si
= (struct string_list_item
*)&map
->items
[i
];
1162 struct mailmap_entry
*me
= (struct mailmap_entry
*)si
->util
;
1163 int comp
= strcmp(si
->string
, email2
);
1169 const char *author2
= author2_cb(payload
);
1171 for (j
= 0; j
< me
->namemap
.nr
; ++j
)
1173 struct string_list_item
*sj
= (struct string_list_item
*)&me
->namemap
.items
[j
];
1174 struct mailmap_info
*mi
= (struct mailmap_info
*)sj
->util
;
1176 if (!strcmp(sj
->string
, author2
))
1192 void git_free_mailmap(GIT_MAILMAP mailmap
)
1197 clear_mailmap((struct string_list
*)mailmap
);