config: Bring back `git_config_open_global` Scott commands, I obey.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
diff --git a/include/git2/config.h b/include/git2/config.h
index 7be45b1..022b0af 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -53,6 +53,34 @@ struct git_config_file {
};
/**
+ * Locate the path to the global configuration file
+ *
+ * The user or global configuration file is usually
+ * located in `$HOME/.gitconfig`.
+ *
+ * This method will try to guess the full path to that
+ * file, if the file exists. The returned path
+ * may be used on any `git_config` call to load the
+ * global configuration file.
+ *
+ * @param path Buffer of GIT_PATH_MAX length to store the path
+ * @return GIT_SUCCESS if a global configuration file has been
+ * found. Its path will be stored in `buffer`.
+ */
+GIT_EXTERN(int) git_config_find_global(char *global_config_path);
+
+/**
+ * Open the global configuration file
+ *
+ * Utility wrapper that calls `git_config_find_global`
+ * and opens the located file, if it exists.
+ *
+ * @param out Pointer to store the config instance
+ * @return GIT_SUCCESS on success; error code otherwise
+ */
+GIT_EXTERN(int) git_config_open_global(git_config **out);
+
+/**
* Create a configuration file backend for ondisk files
*
* These are the normal `.gitconfig` files that Core Git
@@ -61,7 +89,7 @@ struct git_config_file {
* variables.
*
* @param out the new backend
- * @path where the config file is located
+ * @param path where the config file is located
*/
GIT_EXTERN(int) git_config_file__ondisk(struct git_config_file **out, const char *path);
@@ -72,6 +100,7 @@ GIT_EXTERN(int) git_config_file__ondisk(struct git_config_file **out, const char
* can do anything with it.
*
* @param out pointer to the new configuration
+ * @return GIT_SUCCESS on success; error code otherwise
*/
GIT_EXTERN(int) git_config_new(git_config **out);
@@ -88,6 +117,7 @@ GIT_EXTERN(int) git_config_new(git_config **out);
* @param cfg the configuration to add the file to
* @param file the configuration file (backend) to add
* @param priority the priority the backend should have
+ * @return GIT_SUCCESS on success; error code otherwise
*/
GIT_EXTERN(int) git_config_add_file(git_config *cfg, git_config_file *file, int priority);
@@ -108,6 +138,7 @@ GIT_EXTERN(int) git_config_add_file(git_config *cfg, git_config_file *file, int
* @param cfg the configuration to add the file to
* @param file path to the configuration file (backend) to add
* @param priority the priority the backend should have
+ * @return GIT_SUCCESS on success; error code otherwise
*/
GIT_EXTERN(int) git_config_add_file_ondisk(git_config *cfg, const char *path, int priority);
@@ -122,6 +153,7 @@ GIT_EXTERN(int) git_config_add_file_ondisk(git_config *cfg, const char *path, in
*
* @param cfg The configuration instance to create
* @param path Path to the on-disk file to open
+ * @return GIT_SUCCESS on success; error code otherwise
*/
GIT_EXTERN(int) git_config_open_ondisk(git_config **cfg, const char *path);
diff --git a/src/config.c b/src/config.c
index b802ba5..cc31bda 100644
--- a/src/config.c
+++ b/src/config.c
@@ -319,3 +319,36 @@ int git_config_get_string(git_config *cfg, const char *name, const char **out)
return git__throw(error, "Config value '%s' not found", name);
}
+int git_config_find_global(char *global_config_path)
+{
+ const char *home;
+
+ home = getenv("HOME");
+
+#ifdef GIT_WIN32
+ if (home == NULL)
+ home = getenv("USERPROFILE");
+#endif
+
+ if (home == NULL)
+ return git__throw(GIT_EOSERR, "Failed to open global config file. Cannot locate the user's home directory");
+
+ git__joinpath(global_config_path, home, GIT_CONFIG_FILENAME);
+
+ if (gitfo_exists(global_config_path) < GIT_SUCCESS)
+ return git__throw(GIT_EOSERR, "Failed to open global config file. The file does not exist");
+
+ return GIT_SUCCESS;
+}
+
+int git_config_open_global(git_config **out)
+{
+ int error;
+ char global_path[GIT_PATH_MAX];
+
+ if ((error = git_config_find_global(global_path)) < GIT_SUCCESS)
+ return error;
+
+ return git_config_open_ondisk(out, global_path);
+}
+