This is a better name
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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2f5c559..188678c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -243,12 +243,13 @@ IF (BUILD_CLAR)
INCLUDE_DIRECTORIES(${CLAR_PATH})
FILE(GLOB_RECURSE SRC_TEST ${CLAR_PATH}/*/*.c)
- SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_helpers.c" "${CLAR_PATH}/clar/clar.c")
+ SET(SRC_CLAR "${CLAR_PATH}/main.c" "${CLAR_PATH}/clar_libgit2.c" "${CLAR_PATH}/clar/clar.c")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-missing-prototypes")
ADD_CUSTOM_COMMAND(
OUTPUT ${CLAR_PATH}/clar/clar.c
COMMAND git submodule update --init tests-clar/clar
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
ADD_CUSTOM_COMMAND(
diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c
deleted file mode 100644
index ce3ec4a..0000000
--- a/tests-clar/clar_helpers.c
+++ /dev/null
@@ -1,311 +0,0 @@
-#include "clar_libgit2.h"
-#include "posix.h"
-#include "path.h"
-
-void cl_git_mkfile(const char *filename, const char *content)
-{
- int fd;
-
- fd = p_creat(filename, 0666);
- cl_assert(fd != 0);
-
- if (content) {
- cl_must_pass(p_write(fd, content, strlen(content)));
- } else {
- cl_must_pass(p_write(fd, filename, strlen(filename)));
- cl_must_pass(p_write(fd, "\n", 1));
- }
-
- cl_must_pass(p_close(fd));
-}
-
-void cl_git_write2file(
- const char *filename, const char *new_content, int flags, unsigned int mode)
-{
- int fd = p_open(filename, flags, mode);
- cl_assert(fd >= 0);
- if (!new_content)
- new_content = "\n";
- cl_must_pass(p_write(fd, new_content, strlen(new_content)));
- cl_must_pass(p_close(fd));
-}
-
-void cl_git_append2file(const char *filename, const char *new_content)
-{
- cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_APPEND, 0644);
-}
-
-void cl_git_rewritefile(const char *filename, const char *new_content)
-{
- cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-}
-
-#ifdef GIT_WIN32
-
-#include "win32/utf-conv.h"
-
-char *cl_getenv(const char *name)
-{
- wchar_t name_utf16[GIT_WIN_PATH];
- DWORD alloc_len;
- wchar_t *value_utf16;
- char *value_utf8;
-
- git__utf8_to_16(name_utf16, GIT_WIN_PATH, name);
- alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0);
- if (alloc_len <= 0)
- return NULL;
-
- alloc_len = GIT_WIN_PATH;
- cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t)));
-
- GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len);
-
- cl_assert(value_utf8 = git__malloc(alloc_len));
- git__utf16_to_8(value_utf8, value_utf16);
-
- git__free(value_utf16);
-
- return value_utf8;
-}
-
-int cl_setenv(const char *name, const char *value)
-{
- wchar_t name_utf16[GIT_WIN_PATH];
- wchar_t value_utf16[GIT_WIN_PATH];
-
- git__utf8_to_16(name_utf16, GIT_WIN_PATH, name);
-
- if (value != NULL)
- git__utf8_to_16(value_utf16, GIT_WIN_PATH, value);
-
- /* Windows XP returns 0 (failed) when passing NULL for lpValue when lpName
- * does not exist in the environment block. This behavior seems to have changed
- * in later versions. Don't fail when SetEnvironmentVariable fails, if we passed
- * NULL for lpValue. */
- cl_assert(SetEnvironmentVariableW(name_utf16, value ? value_utf16 : NULL) || !value);
- return 0;
-}
-
-/* This function performs retries on calls to MoveFile in order
- * to provide enhanced reliability in the face of antivirus
- * agents that may be scanning the source (or in the case that
- * the source is a directory, a child of the source). */
-int cl_rename(const char *source, const char *dest)
-{
- wchar_t source_utf16[GIT_WIN_PATH];
- wchar_t dest_utf16[GIT_WIN_PATH];
- unsigned retries = 1;
-
- git__utf8_to_16(source_utf16, GIT_WIN_PATH, source);
- git__utf8_to_16(dest_utf16, GIT_WIN_PATH, dest);
-
- while (!MoveFileW(source_utf16, dest_utf16)) {
- /* Only retry if the error is ERROR_ACCESS_DENIED;
- * this may indicate that an antivirus agent is
- * preventing the rename from source to target */
- if (retries > 5 ||
- ERROR_ACCESS_DENIED != GetLastError())
- return -1;
-
- /* With 5 retries and a coefficient of 10ms, the maximum
- * delay here is 550 ms */
- Sleep(10 * retries * retries);
- retries++;
- }
-
- return 0;
-}
-
-#else
-
-#include <stdlib.h>
-char *cl_getenv(const char *name)
-{
- return getenv(name);
-}
-
-int cl_setenv(const char *name, const char *value)
-{
- return (value == NULL) ? unsetenv(name) : setenv(name, value, 1);
-}
-
-int cl_rename(const char *source, const char *dest)
-{
- return p_rename(source, dest);
-}
-
-#endif
-
-static const char *_cl_sandbox = NULL;
-static git_repository *_cl_repo = NULL;
-
-git_repository *cl_git_sandbox_init(const char *sandbox)
-{
- /* Copy the whole sandbox folder from our fixtures to our test sandbox
- * area. After this it can be accessed with `./sandbox`
- */
- cl_fixture_sandbox(sandbox);
- _cl_sandbox = sandbox;
-
- cl_git_pass(p_chdir(sandbox));
-
- /* If this is not a bare repo, then rename `sandbox/.gitted` to
- * `sandbox/.git` which must be done since we cannot store a folder
- * named `.git` inside the fixtures folder of our libgit2 repo.
- */
- if (p_access(".gitted", F_OK) == 0)
- cl_git_pass(cl_rename(".gitted", ".git"));
-
- /* If we have `gitattributes`, rename to `.gitattributes`. This may
- * be necessary if we don't want the attributes to be applied in the
- * libgit2 repo, but just during testing.
- */
- if (p_access("gitattributes", F_OK) == 0)
- cl_git_pass(cl_rename("gitattributes", ".gitattributes"));
-
- /* As with `gitattributes`, we may need `gitignore` just for testing. */
- if (p_access("gitignore", F_OK) == 0)
- cl_git_pass(cl_rename("gitignore", ".gitignore"));
-
- cl_git_pass(p_chdir(".."));
-
- /* Now open the sandbox repository and make it available for tests */
- cl_git_pass(git_repository_open(&_cl_repo, sandbox));
-
- return _cl_repo;
-}
-
-void cl_git_sandbox_cleanup(void)
-{
- if (_cl_repo) {
- git_repository_free(_cl_repo);
- _cl_repo = NULL;
- }
- if (_cl_sandbox) {
- cl_fixture_cleanup(_cl_sandbox);
- _cl_sandbox = NULL;
- }
-}
-
-bool cl_toggle_filemode(const char *filename)
-{
- struct stat st1, st2;
-
- cl_must_pass(p_stat(filename, &st1));
- cl_must_pass(p_chmod(filename, st1.st_mode ^ 0100));
- cl_must_pass(p_stat(filename, &st2));
-
- return (st1.st_mode != st2.st_mode);
-}
-
-bool cl_is_chmod_supported(void)
-{
- static int _is_supported = -1;
-
- if (_is_supported < 0) {
- cl_git_mkfile("filemode.t", "Test if filemode can be modified");
- _is_supported = cl_toggle_filemode("filemode.t");
- cl_must_pass(p_unlink("filemode.t"));
- }
-
- return _is_supported;
-}
-
-const char* cl_git_fixture_url(const char *fixturename)
-{
- return cl_git_path_url(cl_fixture(fixturename));
-}
-
-const char* cl_git_path_url(const char *path)
-{
- static char url[4096];
-
- const char *in_buf;
- git_buf path_buf = GIT_BUF_INIT;
- git_buf url_buf = GIT_BUF_INIT;
-
- cl_git_pass(git_path_prettify_dir(&path_buf, path, NULL));
- cl_git_pass(git_buf_puts(&url_buf, "file://"));
-
-#ifdef _MSC_VER
- /*
- * A FILE uri matches the following format: file://[host]/path
- * where "host" can be empty and "path" is an absolute path to the resource.
- *
- * In this test, no hostname is used, but we have to ensure the leading triple slashes:
- *
- * *nix: file:///usr/home/...
- * Windows: file:///C:/Users/...
- */
- cl_git_pass(git_buf_putc(&url_buf, '/'));
-#endif
-
- in_buf = git_buf_cstr(&path_buf);
-
- /*
- * A very hacky Url encoding that only takes care of escaping the spaces
- */
- while (*in_buf) {
- if (*in_buf == ' ')
- cl_git_pass(git_buf_puts(&url_buf, "%20"));
- else
- cl_git_pass(git_buf_putc(&url_buf, *in_buf));
-
- in_buf++;
- }
-
- strncpy(url, git_buf_cstr(&url_buf), 4096);
- git_buf_free(&url_buf);
- git_buf_free(&path_buf);
- return url;
-}
-
-typedef struct {
- const char *filename;
- size_t filename_len;
-} remove_data;
-
-static int remove_placeholders_recurs(void *_data, git_buf *path)
-{
- remove_data *data = (remove_data *)_data;
- size_t pathlen;
-
- if (git_path_isdir(path->ptr) == true)
- return git_path_direach(path, remove_placeholders_recurs, data);
-
- pathlen = path->size;
-
- if (pathlen < data->filename_len)
- return 0;
-
- /* if path ends in '/'+filename (or equals filename) */
- if (!strcmp(data->filename, path->ptr + pathlen - data->filename_len) &&
- (pathlen == data->filename_len ||
- path->ptr[pathlen - data->filename_len - 1] == '/'))
- return p_unlink(path->ptr);
-
- return 0;
-}
-
-int cl_git_remove_placeholders(const char *directory_path, const char *filename)
-{
- int error;
- remove_data data;
- git_buf buffer = GIT_BUF_INIT;
-
- if (git_path_isdir(directory_path) == false)
- return -1;
-
- if (git_buf_sets(&buffer, directory_path) < 0)
- return -1;
-
- data.filename = filename;
- data.filename_len = strlen(filename);
-
- error = remove_placeholders_recurs(&data, &buffer);
-
- git_buf_free(&buffer);
-
- return error;
-}
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
new file mode 100644
index 0000000..ce3ec4a
--- /dev/null
+++ b/tests-clar/clar_libgit2.c
@@ -0,0 +1,311 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "path.h"
+
+void cl_git_mkfile(const char *filename, const char *content)
+{
+ int fd;
+
+ fd = p_creat(filename, 0666);
+ cl_assert(fd != 0);
+
+ if (content) {
+ cl_must_pass(p_write(fd, content, strlen(content)));
+ } else {
+ cl_must_pass(p_write(fd, filename, strlen(filename)));
+ cl_must_pass(p_write(fd, "\n", 1));
+ }
+
+ cl_must_pass(p_close(fd));
+}
+
+void cl_git_write2file(
+ const char *filename, const char *new_content, int flags, unsigned int mode)
+{
+ int fd = p_open(filename, flags, mode);
+ cl_assert(fd >= 0);
+ if (!new_content)
+ new_content = "\n";
+ cl_must_pass(p_write(fd, new_content, strlen(new_content)));
+ cl_must_pass(p_close(fd));
+}
+
+void cl_git_append2file(const char *filename, const char *new_content)
+{
+ cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_APPEND, 0644);
+}
+
+void cl_git_rewritefile(const char *filename, const char *new_content)
+{
+ cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+}
+
+#ifdef GIT_WIN32
+
+#include "win32/utf-conv.h"
+
+char *cl_getenv(const char *name)
+{
+ wchar_t name_utf16[GIT_WIN_PATH];
+ DWORD alloc_len;
+ wchar_t *value_utf16;
+ char *value_utf8;
+
+ git__utf8_to_16(name_utf16, GIT_WIN_PATH, name);
+ alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0);
+ if (alloc_len <= 0)
+ return NULL;
+
+ alloc_len = GIT_WIN_PATH;
+ cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t)));
+
+ GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len);
+
+ cl_assert(value_utf8 = git__malloc(alloc_len));
+ git__utf16_to_8(value_utf8, value_utf16);
+
+ git__free(value_utf16);
+
+ return value_utf8;
+}
+
+int cl_setenv(const char *name, const char *value)
+{
+ wchar_t name_utf16[GIT_WIN_PATH];
+ wchar_t value_utf16[GIT_WIN_PATH];
+
+ git__utf8_to_16(name_utf16, GIT_WIN_PATH, name);
+
+ if (value != NULL)
+ git__utf8_to_16(value_utf16, GIT_WIN_PATH, value);
+
+ /* Windows XP returns 0 (failed) when passing NULL for lpValue when lpName
+ * does not exist in the environment block. This behavior seems to have changed
+ * in later versions. Don't fail when SetEnvironmentVariable fails, if we passed
+ * NULL for lpValue. */
+ cl_assert(SetEnvironmentVariableW(name_utf16, value ? value_utf16 : NULL) || !value);
+ return 0;
+}
+
+/* This function performs retries on calls to MoveFile in order
+ * to provide enhanced reliability in the face of antivirus
+ * agents that may be scanning the source (or in the case that
+ * the source is a directory, a child of the source). */
+int cl_rename(const char *source, const char *dest)
+{
+ wchar_t source_utf16[GIT_WIN_PATH];
+ wchar_t dest_utf16[GIT_WIN_PATH];
+ unsigned retries = 1;
+
+ git__utf8_to_16(source_utf16, GIT_WIN_PATH, source);
+ git__utf8_to_16(dest_utf16, GIT_WIN_PATH, dest);
+
+ while (!MoveFileW(source_utf16, dest_utf16)) {
+ /* Only retry if the error is ERROR_ACCESS_DENIED;
+ * this may indicate that an antivirus agent is
+ * preventing the rename from source to target */
+ if (retries > 5 ||
+ ERROR_ACCESS_DENIED != GetLastError())
+ return -1;
+
+ /* With 5 retries and a coefficient of 10ms, the maximum
+ * delay here is 550 ms */
+ Sleep(10 * retries * retries);
+ retries++;
+ }
+
+ return 0;
+}
+
+#else
+
+#include <stdlib.h>
+char *cl_getenv(const char *name)
+{
+ return getenv(name);
+}
+
+int cl_setenv(const char *name, const char *value)
+{
+ return (value == NULL) ? unsetenv(name) : setenv(name, value, 1);
+}
+
+int cl_rename(const char *source, const char *dest)
+{
+ return p_rename(source, dest);
+}
+
+#endif
+
+static const char *_cl_sandbox = NULL;
+static git_repository *_cl_repo = NULL;
+
+git_repository *cl_git_sandbox_init(const char *sandbox)
+{
+ /* Copy the whole sandbox folder from our fixtures to our test sandbox
+ * area. After this it can be accessed with `./sandbox`
+ */
+ cl_fixture_sandbox(sandbox);
+ _cl_sandbox = sandbox;
+
+ cl_git_pass(p_chdir(sandbox));
+
+ /* If this is not a bare repo, then rename `sandbox/.gitted` to
+ * `sandbox/.git` which must be done since we cannot store a folder
+ * named `.git` inside the fixtures folder of our libgit2 repo.
+ */
+ if (p_access(".gitted", F_OK) == 0)
+ cl_git_pass(cl_rename(".gitted", ".git"));
+
+ /* If we have `gitattributes`, rename to `.gitattributes`. This may
+ * be necessary if we don't want the attributes to be applied in the
+ * libgit2 repo, but just during testing.
+ */
+ if (p_access("gitattributes", F_OK) == 0)
+ cl_git_pass(cl_rename("gitattributes", ".gitattributes"));
+
+ /* As with `gitattributes`, we may need `gitignore` just for testing. */
+ if (p_access("gitignore", F_OK) == 0)
+ cl_git_pass(cl_rename("gitignore", ".gitignore"));
+
+ cl_git_pass(p_chdir(".."));
+
+ /* Now open the sandbox repository and make it available for tests */
+ cl_git_pass(git_repository_open(&_cl_repo, sandbox));
+
+ return _cl_repo;
+}
+
+void cl_git_sandbox_cleanup(void)
+{
+ if (_cl_repo) {
+ git_repository_free(_cl_repo);
+ _cl_repo = NULL;
+ }
+ if (_cl_sandbox) {
+ cl_fixture_cleanup(_cl_sandbox);
+ _cl_sandbox = NULL;
+ }
+}
+
+bool cl_toggle_filemode(const char *filename)
+{
+ struct stat st1, st2;
+
+ cl_must_pass(p_stat(filename, &st1));
+ cl_must_pass(p_chmod(filename, st1.st_mode ^ 0100));
+ cl_must_pass(p_stat(filename, &st2));
+
+ return (st1.st_mode != st2.st_mode);
+}
+
+bool cl_is_chmod_supported(void)
+{
+ static int _is_supported = -1;
+
+ if (_is_supported < 0) {
+ cl_git_mkfile("filemode.t", "Test if filemode can be modified");
+ _is_supported = cl_toggle_filemode("filemode.t");
+ cl_must_pass(p_unlink("filemode.t"));
+ }
+
+ return _is_supported;
+}
+
+const char* cl_git_fixture_url(const char *fixturename)
+{
+ return cl_git_path_url(cl_fixture(fixturename));
+}
+
+const char* cl_git_path_url(const char *path)
+{
+ static char url[4096];
+
+ const char *in_buf;
+ git_buf path_buf = GIT_BUF_INIT;
+ git_buf url_buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_path_prettify_dir(&path_buf, path, NULL));
+ cl_git_pass(git_buf_puts(&url_buf, "file://"));
+
+#ifdef _MSC_VER
+ /*
+ * A FILE uri matches the following format: file://[host]/path
+ * where "host" can be empty and "path" is an absolute path to the resource.
+ *
+ * In this test, no hostname is used, but we have to ensure the leading triple slashes:
+ *
+ * *nix: file:///usr/home/...
+ * Windows: file:///C:/Users/...
+ */
+ cl_git_pass(git_buf_putc(&url_buf, '/'));
+#endif
+
+ in_buf = git_buf_cstr(&path_buf);
+
+ /*
+ * A very hacky Url encoding that only takes care of escaping the spaces
+ */
+ while (*in_buf) {
+ if (*in_buf == ' ')
+ cl_git_pass(git_buf_puts(&url_buf, "%20"));
+ else
+ cl_git_pass(git_buf_putc(&url_buf, *in_buf));
+
+ in_buf++;
+ }
+
+ strncpy(url, git_buf_cstr(&url_buf), 4096);
+ git_buf_free(&url_buf);
+ git_buf_free(&path_buf);
+ return url;
+}
+
+typedef struct {
+ const char *filename;
+ size_t filename_len;
+} remove_data;
+
+static int remove_placeholders_recurs(void *_data, git_buf *path)
+{
+ remove_data *data = (remove_data *)_data;
+ size_t pathlen;
+
+ if (git_path_isdir(path->ptr) == true)
+ return git_path_direach(path, remove_placeholders_recurs, data);
+
+ pathlen = path->size;
+
+ if (pathlen < data->filename_len)
+ return 0;
+
+ /* if path ends in '/'+filename (or equals filename) */
+ if (!strcmp(data->filename, path->ptr + pathlen - data->filename_len) &&
+ (pathlen == data->filename_len ||
+ path->ptr[pathlen - data->filename_len - 1] == '/'))
+ return p_unlink(path->ptr);
+
+ return 0;
+}
+
+int cl_git_remove_placeholders(const char *directory_path, const char *filename)
+{
+ int error;
+ remove_data data;
+ git_buf buffer = GIT_BUF_INIT;
+
+ if (git_path_isdir(directory_path) == false)
+ return -1;
+
+ if (git_buf_sets(&buffer, directory_path) < 0)
+ return -1;
+
+ data.filename = filename;
+ data.filename_len = strlen(filename);
+
+ error = remove_placeholders_recurs(&data, &buffer);
+
+ git_buf_free(&buffer);
+
+ return error;
+}