1 /* 2 * Copyright (C) the libgit2 contributors. All rights reserved. 3 * 4 * This file is part of libgit2, distributed under the GNU GPL v2 with 5 * a Linking Exception. For full terms see the included COPYING file. 6 */ 7 /** 8 * License: GPL-2.0(Linking Exception) 9 */ 10 module libgit2.sys.merge; 11 12 13 private static import libgit2.buffer; 14 private static import libgit2.index; 15 private static import libgit2.merge; 16 private static import libgit2.types; 17 private import libgit2.common: GIT_EXTERN; 18 19 /* 20 * @file git2/sys/merge.h 21 * @brief Git merge driver backend and plugin routines 22 * @defgroup git_merge Git merge driver APIs 23 * @ingroup Git 24 * @{ 25 */ 26 extern (C): 27 nothrow @nogc: 28 29 /** 30 * Look up a merge driver by name 31 * 32 * Params: 33 * name = The name of the merge driver 34 * 35 * Returns: Pointer to the merge driver object or null if not found 36 */ 37 @GIT_EXTERN 38 .git_merge_driver* git_merge_driver_lookup(const (char)* name); 39 40 enum GIT_MERGE_DRIVER_TEXT = "text"; 41 enum GIT_MERGE_DRIVER_BINARY = "binary"; 42 enum GIT_MERGE_DRIVER_UNION = "union"; 43 44 /** 45 * A merge driver source represents the file to be merged 46 */ 47 struct git_merge_driver_source; 48 49 /** 50 * Get the repository that the source data is coming from. 51 */ 52 @GIT_EXTERN 53 libgit2.types.git_repository* git_merge_driver_source_repo(const (.git_merge_driver_source)* src); 54 55 /** 56 * Gets the ancestor of the file to merge. 57 */ 58 @GIT_EXTERN 59 const (libgit2.index.git_index_entry)* git_merge_driver_source_ancestor(const (.git_merge_driver_source)* src); 60 61 /** 62 * Gets the ours side of the file to merge. 63 */ 64 @GIT_EXTERN 65 const (libgit2.index.git_index_entry)* git_merge_driver_source_ours(const (.git_merge_driver_source)* src); 66 67 /** 68 * Gets the theirs side of the file to merge. 69 */ 70 @GIT_EXTERN 71 const (libgit2.index.git_index_entry)* git_merge_driver_source_theirs(const (.git_merge_driver_source)* src); 72 73 /** 74 * Gets the merge file options that the merge was invoked with 75 */ 76 @GIT_EXTERN 77 const (libgit2.merge.git_merge_file_options)* git_merge_driver_source_file_options(const (.git_merge_driver_source)* src); 78 79 /** 80 * Initialize callback on merge driver 81 * 82 * Specified as `driver.initialize`, this is an optional callback invoked 83 * before a merge driver is first used. It will be called once at most 84 * per library lifetime. 85 * 86 * If non-null, the merge driver's `initialize` callback will be invoked 87 * right before the first use of the driver, so you can defer expensive 88 * initialization operations (in case libgit2 is being used in a way that 89 * doesn't need the merge driver). 90 */ 91 alias git_merge_driver_init_fn = int function(.git_merge_driver* self); 92 93 /** 94 * Shutdown callback on merge driver 95 * 96 * Specified as `driver.shutdown`, this is an optional callback invoked 97 * when the merge driver is unregistered or when libgit2 is shutting down. 98 * It will be called once at most and should release resources as needed. 99 * This may be called even if the `initialize` callback was not made. 100 * 101 * Typically this function will free the `git_merge_driver` object itself. 102 */ 103 alias git_merge_driver_shutdown_fn = void function(.git_merge_driver* self); 104 105 /** 106 * Callback to perform the merge. 107 * 108 * Specified as `driver.apply`, this is the callback that actually does the 109 * merge. If it can successfully perform a merge, it should populate 110 * `path_out` with a pointer to the filename to accept, `mode_out` with 111 * the resultant mode, and `merged_out` with the buffer of the merged file 112 * and then return 0. If the driver returns `git_error_code.GIT_PASSTHROUGH`, then the 113 * default merge driver should instead be run. It can also return 114 * `git_error_code.GIT_EMERGECONFLICT` if the driver is not able to produce a merge result, 115 * and the file will remain conflicted. Any other errors will fail and 116 * return to the caller. 117 * 118 * The `filter_name` contains the name of the filter that was invoked, as 119 * specified by the file's attributes. 120 * 121 * The `src` contains the data about the file to be merged. 122 */ 123 alias git_merge_driver_apply_fn = int function(.git_merge_driver* self, const (char)** path_out, uint* mode_out, libgit2.buffer.git_buf* merged_out, const (char)* filter_name, const (.git_merge_driver_source)* src); 124 125 /** 126 * Merge driver structure used to register custom merge drivers. 127 * 128 * To associate extra data with a driver, allocate extra data and put the 129 * `git_merge_driver` struct at the start of your data buffer, then cast 130 * the `self` pointer to your larger structure when your callback is invoked. 131 */ 132 struct git_merge_driver 133 { 134 /** 135 * The `version` should be set to `GIT_MERGE_DRIVER_VERSION`. 136 */ 137 uint version_; 138 139 /** 140 * Called when the merge driver is first used for any file. 141 */ 142 .git_merge_driver_init_fn initialize; 143 144 /** 145 * Called when the merge driver is unregistered from the system. 146 */ 147 .git_merge_driver_shutdown_fn shutdown; 148 149 /** 150 * Called to merge the contents of a conflict. If this function 151 * returns `git_error_code.GIT_PASSTHROUGH` then the default (`text`) merge driver 152 * will instead be invoked. If this function returns 153 * `git_error_code.GIT_EMERGECONFLICT` then the file will remain conflicted. 154 */ 155 .git_merge_driver_apply_fn apply; 156 } 157 158 enum GIT_MERGE_DRIVER_VERSION = 1; 159 160 /** 161 * Register a merge driver under a given name. 162 * 163 * As mentioned elsewhere, the initialize callback will not be invoked 164 * immediately. It is deferred until the driver is used in some way. 165 * 166 * Currently the merge driver registry is not thread safe, so any 167 * registering or deregistering of merge drivers must be done outside of 168 * any possible usage of the drivers (i.e. during application setup or 169 * shutdown). 170 * 171 * Params: 172 * name = The name of this driver to match an attribute. Attempting to register with an in-use name will return git_error_code.GIT_EEXISTS. 173 * driver = The merge driver definition. This pointer will be stored as is by libgit2 so it must be a durable allocation (either static or on the heap). 174 * 175 * Returns: 0 on successful registry, error code <0 on failure 176 */ 177 @GIT_EXTERN 178 int git_merge_driver_register(const (char)* name, .git_merge_driver* driver); 179 180 /** 181 * Remove the merge driver with the given name. 182 * 183 * Attempting to remove the builtin libgit2 merge drivers is not permitted 184 * and will return an error. 185 * 186 * Currently the merge driver registry is not thread safe, so any 187 * registering or deregistering of drivers must be done outside of any 188 * possible usage of the drivers (i.e. during application setup or shutdown). 189 * 190 * Params: 191 * name = The name under which the merge driver was registered 192 * 193 * Returns: 0 on success, error code <0 on failure 194 */ 195 @GIT_EXTERN 196 int git_merge_driver_unregister(const (char)* name); 197 198 /* @} */