Make reference lookups apply precomposeunicode Before these changes, looking up a reference would return the same precomposed or decomposed form of the reference name that was used to look it up, so on MacOS which ignores the difference between the two, a single reference could be looked up either way and git_reference_name would return the form of the name that was used to look it up! This change makes lookup always return the precomposed name if core.precomposeunicode is set regardless of which version was used to look it up. The reference iterator was already returning the precomposed form from earlier work. This also updates the CMakeLists.txt rules for enabling iconv usage because the clar tests for this code were actually not being activated properly with the old version. Finally, this moves git_repository_reset_filesystem from include/ git2/repository.h to include/git2/sys/repository.h since it is not really a function that normal library users should have to think about very often.
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 62ebbf4..c741499 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -27,9 +27,14 @@ OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF )
OPTION( TAGS "Generate tags" OFF )
OPTION( PROFILE "Generate profiling information" OFF )
OPTION( ENABLE_TRACE "Enables tracing support" OFF )
-OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
+OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
-OPTION( ANDROID "Build for android NDK" OFF )
+OPTION( ANDROID "Build for android NDK" OFF )
+
+OPTION( USE_ICONV "Link with and use iconv library" OFF )
+IF(APPLE)
+ SET( USE_ICONV ON )
+ENDIF()
IF(MSVC)
# This option is only available when building with MSVC. By default, libgit2
@@ -58,8 +63,11 @@ FUNCTION(TARGET_OS_LIBRARIES target)
TARGET_LINK_LIBRARIES(${target} socket nsl)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
TARGET_LINK_LIBRARIES(${target} rt)
- ELSEIF(APPLE)
+ ENDIF()
+
+ IF(USE_ICONV)
TARGET_LINK_LIBRARIES(${target} iconv)
+ ADD_DEFINITIONS(-DGIT_USE_ICONV)
ENDIF()
IF(THREADSAFE)
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 4871e98..4041947 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -453,7 +453,7 @@ GIT_EXTERN(int) git_reference_is_remote(git_reference *ref);
GIT_EXTERN(int) git_reference_is_tag(git_reference *ref);
typedef enum {
- GIT_REF_FORMAT_NORMAL = 0,
+ GIT_REF_FORMAT_NORMAL = 0u,
/**
* Control whether one-level refnames are accepted
@@ -461,7 +461,7 @@ typedef enum {
* components). Those are expected to be written only using
* uppercase letters and underscore (FETCH_HEAD, ...)
*/
- GIT_REF_FORMAT_ALLOW_ONELEVEL = (1 << 0),
+ GIT_REF_FORMAT_ALLOW_ONELEVEL = (1u << 0),
/**
* Interpret the provided name as a reference pattern for a
@@ -470,14 +470,14 @@ typedef enum {
* in place of a one full pathname component
* (e.g., foo/<star>/bar but not foo/bar<star>).
*/
- GIT_REF_FORMAT_REFSPEC_PATTERN = (1 << 1),
+ GIT_REF_FORMAT_REFSPEC_PATTERN = (1u << 1),
/**
* Interpret the name as part of a refspec in shorthand form
* so the `ONELEVEL` naming rules aren't enforced and 'master'
* becomes a valid name.
*/
- GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1 << 2),
+ GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1u << 2),
} git_reference_normalize_t;
/**
diff --git a/include/git2/repository.h b/include/git2/repository.h
index 28d8400..b4d5619 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -288,25 +288,6 @@ GIT_EXTERN(int) git_repository_init_ext(
git_repository_init_options *opts);
/**
- * Update the filesystem config settings for an open repository
- *
- * When a repository is initialized, config values are set based on the
- * properties of the filesystem that the repository is on, such as
- * "core.ignorecase", "core.filemode", "core.symlinks", etc. If the
- * repository is moved to a new filesystem, these properties may no
- * longer be correct and API calls may not behave as expected. This
- * call reruns the phase of repository initialization that sets those
- * properties to compensate for the current filesystem of the repo.
- *
- * @param repo A repository object
- * @param recurse_submodules Should submodules be reset recursively
- * @returrn 0 on success, < 0 on error
- */
-GIT_EXTERN(int) git_repository_reset_filesystem(
- git_repository *repo,
- int recurse_submodules);
-
-/**
* Retrieve and resolve the reference pointed at by HEAD.
*
* The returned `git_reference` will be owned by caller and
diff --git a/include/git2/sys/repository.h b/include/git2/sys/repository.h
index ba3d65a..e77b1dc 100644
--- a/include/git2/sys/repository.h
+++ b/include/git2/sys/repository.h
@@ -27,7 +27,6 @@ GIT_BEGIN_DECL
*/
GIT_EXTERN(int) git_repository_new(git_repository **out);
-
/**
* Reset all the internal state in a repository.
*
@@ -42,6 +41,25 @@ GIT_EXTERN(int) git_repository_new(git_repository **out);
GIT_EXTERN(void) git_repository__cleanup(git_repository *repo);
/**
+ * Update the filesystem config settings for an open repository
+ *
+ * When a repository is initialized, config values are set based on the
+ * properties of the filesystem that the repository is on, such as
+ * "core.ignorecase", "core.filemode", "core.symlinks", etc. If the
+ * repository is moved to a new filesystem, these properties may no
+ * longer be correct and API calls may not behave as expected. This
+ * call reruns the phase of repository initialization that sets those
+ * properties to compensate for the current filesystem of the repo.
+ *
+ * @param repo A repository object
+ * @param recurse_submodules Should submodules be reset recursively
+ * @returrn 0 on success, < 0 on error
+ */
+GIT_EXTERN(int) git_repository_reset_filesystem(
+ git_repository *repo,
+ int recurse_submodules);
+
+/**
* Set the configuration file for this repository
*
* This configuration file will be used for all configuration
diff --git a/src/path.h b/src/path.h
index 1757569..eaf94d4 100644
--- a/src/path.h
+++ b/src/path.h
@@ -365,11 +365,6 @@ extern int git_path_set_error(
/* check if non-ascii characters are present in filename */
extern bool git_path_has_non_ascii(const char *path, size_t pathlen);
-/* only enable iconv on Mac for now */
-#ifdef __APPLE__
-#define GIT_USE_ICONV 1
-#endif
-
#define GIT_PATH_REPO_ENCODING "UTF-8"
#ifdef __APPLE__
diff --git a/src/refs.c b/src/refs.c
index c045ab9..0da02a6 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -138,6 +138,22 @@ int git_reference_name_to_id(
return 0;
}
+static int reference_normalize_for_repo(
+ char *out,
+ size_t out_size,
+ git_repository *repo,
+ const char *name)
+{
+ int precompose;
+ unsigned int flags = GIT_REF_FORMAT_ALLOW_ONELEVEL;
+
+ if (!git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) &&
+ precompose)
+ flags |= GIT_REF_FORMAT__PRECOMPOSE_UNICODE;
+
+ return git_reference_normalize_name(out, out_size, name, flags);
+}
+
int git_reference_lookup_resolved(
git_reference **ref_out,
git_repository *repo,
@@ -159,13 +175,13 @@ int git_reference_lookup_resolved(
else if (max_nesting < 0)
max_nesting = DEFAULT_NESTING_LEVEL;
- strncpy(scan_name, name, GIT_REFNAME_MAX);
scan_type = GIT_REF_SYMBOLIC;
- if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
- return -1;
+ if ((error = reference_normalize_for_repo(
+ scan_name, sizeof(scan_name), repo, name)) < 0)
+ return error;
- if ((error = git_reference__normalize_name_lax(scan_name, GIT_REFNAME_MAX, name)) < 0)
+ if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
return error;
for (nesting = max_nesting;
@@ -173,7 +189,7 @@ int git_reference_lookup_resolved(
nesting--)
{
if (nesting != max_nesting) {
- strncpy(scan_name, ref->target.symbolic, GIT_REFNAME_MAX);
+ strncpy(scan_name, ref->target.symbolic, sizeof(scan_name));
git_reference_free(ref);
}
@@ -711,17 +727,18 @@ static bool is_all_caps_and_underscore(const char *name, size_t len)
return true;
}
+/* Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100 */
int git_reference__normalize_name(
git_buf *buf,
const char *name,
unsigned int flags)
{
- // Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100
-
char *current;
int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC;
unsigned int process_flags;
bool normalize = (buf != NULL);
+ git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
+
assert(name);
process_flags = flags;
@@ -733,6 +750,13 @@ int git_reference__normalize_name(
if (normalize)
git_buf_clear(buf);
+ if ((flags & GIT_REF_FORMAT__PRECOMPOSE_UNICODE) != 0) {
+ size_t namelen = strlen(current);
+ if ((error = git_path_iconv_init_precompose(&ic)) < 0 ||
+ (error = git_path_iconv(&ic, ¤t, &namelen)) < 0)
+ goto cleanup;
+ }
+
while (true) {
segment_len = ensure_segment_validity(current);
if (segment_len < 0) {
@@ -809,6 +833,8 @@ cleanup:
if (error && normalize)
git_buf_free(buf);
+ git_path_iconv_clear(&ic);
+
return error;
}
@@ -983,9 +1009,9 @@ static int peel_error(int error, git_reference *ref, const char* msg)
}
int git_reference_peel(
- git_object **peeled,
- git_reference *ref,
- git_otype target_type)
+ git_object **peeled,
+ git_reference *ref,
+ git_otype target_type)
{
git_reference *resolved = NULL;
git_object *target = NULL;
@@ -1027,24 +1053,19 @@ cleanup:
return error;
}
-int git_reference__is_valid_name(
- const char *refname,
- unsigned int flags)
+int git_reference__is_valid_name(const char *refname, unsigned int flags)
{
- int error;
-
- error = git_reference__normalize_name(NULL, refname, flags) == 0;
- giterr_clear();
+ if (git_reference__normalize_name(NULL, refname, flags) < 0) {
+ giterr_clear();
+ return false;
+ }
- return error;
+ return true;
}
-int git_reference_is_valid_name(
- const char *refname)
+int git_reference_is_valid_name(const char *refname)
{
- return git_reference__is_valid_name(
- refname,
- GIT_REF_FORMAT_ALLOW_ONELEVEL);
+ return git_reference__is_valid_name(refname, GIT_REF_FORMAT_ALLOW_ONELEVEL);
}
const char *git_reference_shorthand(git_reference *ref)
diff --git a/src/refs.h b/src/refs.h
index 4b91c25..80c7703 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -46,6 +46,8 @@
#define GIT_STASH_FILE "stash"
#define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE
+#define GIT_REF_FORMAT__PRECOMPOSE_UNICODE (1u << 16)
+
#define GIT_REFNAME_MAX 1024
struct git_reference {
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index 82ec5c0..9f65953 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
+#include "git2/sys/repository.h"
void cl_git_report_failure(
int error, const char *file, int line, const char *fncall)
diff --git a/tests-clar/refs/unicode.c b/tests-clar/refs/unicode.c
index b29c63e..b560128 100644
--- a/tests-clar/refs/unicode.c
+++ b/tests-clar/refs/unicode.c
@@ -4,17 +4,13 @@ static git_repository *repo;
void test_refs_unicode__initialize(void)
{
- cl_fixture_sandbox("testrepo.git");
-
- cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+ repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_unicode__cleanup(void)
{
- git_repository_free(repo);
+ cl_git_sandbox_cleanup();
repo = NULL;
-
- cl_fixture_cleanup("testrepo.git");
}
void test_refs_unicode__create_and_lookup(void)
@@ -23,13 +19,12 @@ void test_refs_unicode__create_and_lookup(void)
git_repository *repo2;
const char *REFNAME = "refs/heads/" "\303\205" "ngstr" "\303\266" "m";
- const char *REFNAME_DECOMPOSED =
- "refs/heads/" "A" "\314\212" "ngstro" "\314\210" "m";
const char *master = "refs/heads/master";
/* Create the reference */
cl_git_pass(git_reference_lookup(&ref0, repo, master));
- cl_git_pass(git_reference_create(&ref1, repo, REFNAME, git_reference_target(ref0), 0));
+ cl_git_pass(git_reference_create(
+ &ref1, repo, REFNAME, git_reference_target(ref0), 0));
cl_assert_equal_s(REFNAME, git_reference_name(ref1));
git_reference_free(ref0);
@@ -45,6 +40,8 @@ void test_refs_unicode__create_and_lookup(void)
#if GIT_USE_ICONV
/* Lookup reference by decomposed unicode name */
+#define REFNAME_DECOMPOSED "refs/heads/" "A" "\314\212" "ngstro" "\314\210" "m"
+
cl_git_pass(git_reference_lookup(&ref2, repo2, REFNAME_DECOMPOSED));
cl_assert_equal_i(
0, git_oid_cmp(git_reference_target(ref1), git_reference_target(ref2)));