int git_config_pathname(const char **dest, const char *var, const char *value) { if (!value) return config_error_nonbool(var); *dest = expand_user_path(value); if (!*dest) die("Failed to expand user dir in: '%s'", value); return 0; }
static char *get_socket_path(void) { struct stat sb; char *old_dir, *socket; old_dir = expand_user_path("~/.git-credential-cache", 0); if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode)) socket = xstrfmt("%s/socket", old_dir); else socket = xdg_cache_home("credential/socket"); free(old_dir); return socket; }
int main(int argc, char **argv) { const char * const usage[] = { "git credential-store [<options>] <action>", NULL }; const char *op; struct credential c = CREDENTIAL_INIT; struct string_list fns = STRING_LIST_INIT_DUP; char *file = NULL; struct option options[] = { OPT_STRING(0, "file", &file, "path", "fetch and store credentials in <path>"), OPT_END() }; umask(077); argc = parse_options(argc, (const char **)argv, NULL, options, usage, 0); if (argc != 1) usage_with_options(usage, options); op = argv[0]; if (file) { string_list_append(&fns, file); } else { if ((file = expand_user_path("~/.git-credentials"))) string_list_append_nodup(&fns, file); file = xdg_config_home("credentials"); if (file) string_list_append_nodup(&fns, file); } if (!fns.nr) die("unable to set up default path; use --file"); if (credential_read(&c, stdin) < 0) die("unable to read credential"); if (!strcmp(op, "get")) lookup_credential(&fns, &c); else if (!strcmp(op, "erase")) remove_credential(&fns, &c); else if (!strcmp(op, "store")) store_credential(&fns, &c); else ; /* Ignore unknown operation. */ string_list_clear(&fns, 0); return 0; }
hashmap * _p11_conf_load_modules (int mode, const char *system_dir, const char *user_dir) { hashmap *configs; char *path; int error = 0; /* A hash table of name -> config */ configs = hash_create (hash_string_hash, hash_string_equal, free, (hash_destroy_func)hash_free); /* Load each user config first, if user config is allowed */ if (mode != CONF_USER_NONE) { path = expand_user_path (user_dir); if (!path) error = errno; else if (load_configs_from_directory (path, configs) < 0) error = errno; free (path); if (error != 0) { hash_free (configs); errno = error; return NULL; } } /* * Now unless user config is overriding, load system modules. * Basically if a value for the same config name is not already * loaded above (in the user configs) then they're loaded here. */ if (mode != CONF_USER_ONLY) { if (load_configs_from_directory (system_dir, configs) < 0) { error = errno; hash_free (configs); errno = error; return NULL; } } return configs; }
int main(int argc, const char **argv) { const char * const usage[] = { "git credential-store [options] <action>", NULL }; const char *op; struct credential c = CREDENTIAL_INIT; char *file = NULL; struct option options[] = { OPT_STRING(0, "file", &file, "path", "fetch and store credentials in <path>"), OPT_END() }; umask(077); argc = parse_options(argc, argv, NULL, options, usage, 0); if (argc != 1) usage_with_options(usage, options); op = argv[0]; if (!file) file = expand_user_path("~/.git-credentials"); if (!file) die("unable to set up default path; use --file"); if (credential_read(&c, stdin) < 0) die("unable to read credential"); if (!strcmp(op, "get")) lookup_credential(file, &c); else if (!strcmp(op, "erase")) remove_credential(file, &c); else if (!strcmp(op, "store")) store_credential(file, &c); else ; /* Ignore unknown operation. */ return 0; }
int main(int argc, const char **argv) { char *socket_path = NULL; int timeout = 900; const char *op; const char * const usage[] = { "git credential-cache [<options>] <action>", NULL }; struct option options[] = { OPT_INTEGER(0, "timeout", &timeout, "number of seconds to cache credentials"), OPT_STRING(0, "socket", &socket_path, "path", "path of cache-daemon socket"), OPT_END() }; argc = parse_options(argc, argv, NULL, options, usage, 0); if (!argc) usage_with_options(usage, options); op = argv[0]; if (!socket_path) socket_path = expand_user_path("~/.git-credential-cache/socket"); if (!socket_path) die("unable to find a suitable socket path; use --socket"); if (!strcmp(op, "exit")) do_cache(socket_path, op, timeout, 0); else if (!strcmp(op, "get") || !strcmp(op, "erase")) do_cache(socket_path, op, timeout, FLAG_RELAY); else if (!strcmp(op, "store")) do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN); else ; /* ignore unknown operation */ return 0; }
/* * First, one directory to try is determined by the following algorithm. * * (0) If "strict" is given, the path is used as given and no DWIM is * done. Otherwise: * (1) "~/path" to mean path under the running user's home directory; * (2) "~user/path" to mean path under named user's home directory; * (3) "relative/path" to mean cwd relative directory; or * (4) "/absolute/path" to mean absolute directory. * * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git" * in this order. We select the first one that is a valid git repository, and * chdir() to it. If none match, or we fail to chdir, we return NULL. * * If all goes well, we return the directory we used to chdir() (but * before ~user is expanded), avoiding getcwd() resolving symbolic * links. User relative paths are also returned as they are given, * except DWIM suffixing. */ const char *enter_repo(const char *path, int strict) { static struct strbuf validated_path = STRBUF_INIT; static struct strbuf used_path = STRBUF_INIT; if (!path) return NULL; if (!strict) { static const char *suffix[] = { "/.git", "", ".git/.git", ".git", NULL, }; const char *gitfile; int len = strlen(path); int i; while ((1 < len) && (path[len-1] == '/')) len--; /* * We can handle arbitrary-sized buffers, but this remains as a * sanity check on untrusted input. */ if (PATH_MAX <= len) return NULL; strbuf_reset(&used_path); strbuf_reset(&validated_path); strbuf_add(&used_path, path, len); strbuf_add(&validated_path, path, len); if (used_path.buf[0] == '~') { char *newpath = expand_user_path(used_path.buf); if (!newpath) return NULL; strbuf_attach(&used_path, newpath, strlen(newpath), strlen(newpath)); } for (i = 0; suffix[i]; i++) { struct stat st; size_t baselen = used_path.len; strbuf_addstr(&used_path, suffix[i]); if (!stat(used_path.buf, &st) && (S_ISREG(st.st_mode) || (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) { strbuf_addstr(&validated_path, suffix[i]); break; } strbuf_setlen(&used_path, baselen); } if (!suffix[i]) return NULL; gitfile = read_gitfile(used_path.buf); if (gitfile) { strbuf_reset(&used_path); strbuf_addstr(&used_path, gitfile); } if (chdir(used_path.buf)) return NULL; path = validated_path.buf; } else { const char *gitfile = read_gitfile(path); if (gitfile) path = gitfile; if (chdir(path)) return NULL; } if (is_git_directory(".")) { set_git_dir("."); check_repository_format(); return path; } return NULL; }
/* * First, one directory to try is determined by the following algorithm. * * (0) If "strict" is given, the path is used as given and no DWIM is * done. Otherwise: * (1) "~/path" to mean path under the running user's home directory; * (2) "~user/path" to mean path under named user's home directory; * (3) "relative/path" to mean cwd relative directory; or * (4) "/absolute/path" to mean absolute directory. * * Unless "strict" is given, we try access() for existence of "%s.git/.git", * "%s/.git", "%s.git", "%s" in this order. The first one that exists is * what we try. * * Second, we try chdir() to that. Upon failure, we return NULL. * * Then, we try if the current directory is a valid git repository. * Upon failure, we return NULL. * * If all goes well, we return the directory we used to chdir() (but * before ~user is expanded), avoiding getcwd() resolving symbolic * links. User relative paths are also returned as they are given, * except DWIM suffixing. */ const char *enter_repo(const char *path, int strict) { static char used_path[PATH_MAX]; static char validated_path[PATH_MAX]; if (!path) return NULL; if (!strict) { static const char *suffix[] = { "/.git", "", ".git/.git", ".git", NULL, }; const char *gitfile; int len = strlen(path); int i; while ((1 < len) && (path[len-1] == '/')) len--; if (PATH_MAX <= len) return NULL; strncpy(used_path, path, len); used_path[len] = 0 ; strcpy(validated_path, used_path); if (used_path[0] == '~') { char *newpath = expand_user_path(used_path); if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { free(newpath); return NULL; } /* * Copy back into the static buffer. A pity * since newpath was not bounded, but other * branches of the if are limited by PATH_MAX * anyway. */ strcpy(used_path, newpath); free(newpath); } else if (PATH_MAX - 10 < len) return NULL; len = strlen(used_path); for (i = 0; suffix[i]; i++) { struct stat st; strcpy(used_path + len, suffix[i]); if (!stat(used_path, &st) && (S_ISREG(st.st_mode) || (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) { strcat(validated_path, suffix[i]); break; } } if (!suffix[i]) return NULL; gitfile = read_gitfile(used_path) ; if (gitfile) strcpy(used_path, gitfile); if (chdir(used_path)) return NULL; path = validated_path; } else if (chdir(path)) return NULL; if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && validate_headref("HEAD") == 0) { set_git_dir("."); check_repository_format(); return path; } return NULL; }
hashmap * _p11_conf_load_globals (const char *system_conf, const char *user_conf, int *user_mode) { hashmap *config = NULL; hashmap *uconfig = NULL; hashmap *result = NULL; char *path = NULL; int error = 0; int mode; /* * This loads the system and user configs. This depends on the user-config * value in both the system and user configs. A bit more complex than * you might imagine, since user-config can be set to 'none' in the * user configuration, essentially turning itself off. */ /* Load the main configuration */ config = _p11_conf_parse_file (system_conf, CONF_IGNORE_MISSING); if (!config) goto finished; /* Whether we should use or override from user directory */ mode = user_config_mode (config, CONF_USER_NONE); if (mode == CONF_USER_INVALID) { error = EINVAL; goto finished; } if (mode != CONF_USER_NONE) { path = expand_user_path (user_conf); if (!path) { error = errno; goto finished; } /* Load up the user configuration */ uconfig = _p11_conf_parse_file (path, CONF_IGNORE_MISSING); if (!uconfig) { error = errno; goto finished; } /* Figure out what the user mode is, defaulting to system mode if not set */ mode = user_config_mode (uconfig, mode); if (mode == CONF_USER_INVALID) { error = EINVAL; goto finished; } /* If merging, then supplement user config with system values */ if (mode == CONF_USER_MERGE) { if (_p11_conf_merge_defaults (uconfig, config) < 0) { error = errno; goto finished; } } /* If user config valid at all, then replace system with what we have */ if (mode != CONF_USER_NONE) { hash_free (config); config = uconfig; uconfig = NULL; } } if (user_mode) *user_mode = mode; result = config; config = NULL; finished: free (path); hash_free (config); hash_free (uconfig); errno = error; return result; }