Branch
Hash :
26fd3545
Author :
Date :
2025-09-16T23:08:01
stringeq: prefer streq to strcmp in other modules * lib/acl-internal.c, lib/argmatch.c, lib/argp-help.c: * lib/argp-parse.c, lib/bcp47.c, lib/c32_get_mapping.c: * lib/c32_get_type_test.c, lib/concat-filename.c: * lib/cpu-supports.h, lib/dfa.c, lib/duplocale.c, lib/exclude.c: * lib/findprog-in.c, lib/findprog.c, lib/fopen.c, lib/freopen.c: * lib/fts.c, lib/gen-uni-tables.c, lib/getfilecon.c: * lib/getlogin.c, lib/getugroups.c, lib/hard-locale.c: * lib/hashcode-named-file.c, lib/hashkey-string.c: * lib/iconv_open.c, lib/idcache.c, lib/lc-charset-unicode.c: * lib/localcharset.c, lib/localename-environ.c: * lib/localename-unsafe.c, lib/mbs_endswith.c, lib/mountlist.c: * lib/newlocale.c, lib/nl_langinfo.c, lib/nproc.c, lib/open.c: * lib/os2-spawn.c, lib/parse-datetime.y, lib/popen.c: * lib/progreloc.c, lib/propername.c, lib/pt_chown.c, lib/putenv.c: * lib/qcopy-acl.c, lib/readutmp.c, lib/rpmatch.c: * lib/setlocale-fixes.c, lib/setlocale.c, lib/sig2str.c: * lib/spawni.c, lib/stat.c, lib/str_endswith.c, lib/streq.h: * lib/striconveha.c, lib/struniq.h, lib/supersede.c: * lib/time_rz.c, lib/unicodeio.c, lib/vasnprintf.c: * lib/vc-mtime.c, lib/wctrans-impl.h, lib/wctype-impl.h: Prefer streq to strcmp when either will do. Do not make this change to files shared with glibc. Do not make the change to test files, at least not for now. * lib/gen-uni-tables.c (streq): New static function, in same style. * modules/acl-permissions, modules/argp, modules/c32_get_mapping: * modules/c32_get_type_test, modules/concat-filename: * modules/cpu-supports, modules/dfa, modules/duplocale: * modules/exclude, modules/findprog, modules/findprog-in: * modules/fopen, modules/fopen-gnu, modules/freopen, modules/fts: * modules/getlocalename_l-simple, modules/getlocalename_l-unsafe: * modules/getlogin, modules/getugroups, modules/hard-locale: * modules/hashcode-named-file, modules/hashkey-string: * modules/iconv_open, modules/idcache, modules/localcharset: * modules/localename, modules/localename-unsafe: * modules/mbs_endswith, modules/mountlist, modules/newlocale: * modules/nl_langinfo, modules/nproc, modules/open: * modules/parse-datetime, modules/popen: * modules/posix_spawn-internal, modules/propername: * modules/pt_chown, modules/putenv-gnu, modules/qcopy-acl: * modules/rpmatch, modules/selinux-h, modules/setenv: * modules/setlocale, modules/setlocale-fixes, modules/sig2str: * modules/spawn-pipe, modules/stat, modules/str_endswith: * modules/streq, modules/striconveha, modules/supersede: * modules/time_rz, modules/uchar-h, modules/unicodeio: * modules/uniconv/base, modules/wctrans, modules/wctype: (Depends-on): Add stringeq.
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 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
/* Return the version-control based modification time of a file.
Copyright (C) 2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2025. */
#include <config.h>
/* Specification. */
#include "vc-mtime.h"
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <error.h>
#include "spawn-pipe.h"
#include "wait-process.h"
#include "execute.h"
#include "safe-read.h"
#include "xstrtol.h"
#include "stat-time.h"
#include "windows-cygpath.h"
#include "filename.h"
#include "xalloc.h"
#include "xgetcwd.h"
#include "xvasprintf.h"
#include "gl_map.h"
#include "gl_xmap.h"
#include "gl_hash_map.h"
#include "hashkey-string.h"
#if USE_UNLOCKED_IO
# include "unlocked-io.h"
#endif
#include "gettext.h"
#define _(msgid) dgettext (GNULIB_TEXT_DOMAIN, msgid)
/* ========================================================================== */
static const char *git_version;
/* Determines whether git is present, and sets git_version if so. */
static bool
is_git_present (void)
{
static bool git_tested;
static bool git_present;
if (!git_tested)
{
/* Test for presence of git:
"git --version 2>/dev/null" */
const char *argv[3];
pid_t child;
int fd[1];
argv[0] = "git";
argv[1] = "--version";
argv[2] = NULL;
child = create_pipe_in ("git", "git", argv, NULL, NULL,
DEV_NULL, true, true, false, fd);
if (child == -1)
git_present = false;
else
{
/* Retrieve its result. */
FILE *fp = fdopen (fd[0], "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, _("fdopen() failed"));
char *line = NULL;
size_t linesize = 0;
size_t linelen = getline (&line, &linesize, fp);
if (linelen == (size_t)(-1))
{
fclose (fp);
wait_subprocess (child, "git", true, true, true, false, NULL);
git_present = false;
}
else
{
if (linelen > 0 && line[linelen - 1] == '\n')
line[linelen - 1] = '\0';
/* Read until EOF (otherwise the child process may get a SIGPIPE
signal). */
while (getc (fp) != EOF)
;
fclose (fp);
/* Remove zombie process from process list, and retrieve exit
status. */
int exitstatus =
wait_subprocess (child, "git", true, true, true, false, NULL);
if (exitstatus != 0)
{
free (line);
git_present = false;
}
else
{
/* The version starts at the first digit in the line. */
const char *p = line;
for (; *p != '0'; p++)
if (*p >= '0' && *p <= '9')
break;
git_version = p;
git_present = true;
}
}
}
git_tested = true;
}
return git_present;
}
/* Determines whether the specified file is under version control. */
static bool
git_vc_controlled (const char *filename)
{
/* Run "git ls-files FILENAME" and return true if the exit code is 0
and the output is non-empty. */
const char *argv[4];
pid_t child;
int fd[1];
argv[0] = "git";
argv[1] = "ls-files";
argv[2] = filename;
argv[3] = NULL;
child = create_pipe_in ("git", "git", argv, NULL, NULL,
DEV_NULL, true, true, false, fd);
if (child == -1)
return false;
/* Read the subprocess output, and test whether it is non-empty. */
ptrdiff_t count = 0;
for (;;)
{
char buf[1024];
ptrdiff_t n = safe_read (fd[0], buf, sizeof (buf));
if (n > 0)
count += n;
else
break;
}
close (fd[0]);
/* Remove zombie process from process list, and retrieve exit status. */
int exitstatus =
wait_subprocess (child, "git", false, true, true, false, NULL);
return (exitstatus == 0 && count > 0);
}
/* Determines whether the specified file is unmodified, compared to the
last version in version control. */
static bool
git_unmodified (const char *filename)
{
/* Run "git diff --quiet -- HEAD FILENAME"
(or "git diff --quiet HEAD FILENAME")
and return true if the exit code is 0.
The '--' option is for the case that the specified file was removed. */
const char *argv[7];
int exitstatus;
argv[0] = "git";
argv[1] = "diff";
argv[2] = "--quiet";
argv[3] = "--";
argv[4] = "HEAD";
argv[5] = filename;
argv[6] = NULL;
exitstatus = execute ("git", "git", argv, NULL, NULL,
false, false, true, true,
true, false, NULL);
return (exitstatus == 0);
}
/* Stores in *MTIME the time of last modification in version control of the
specified file, and returns 0.
Upon failure, it returns -1. */
static int
git_mtime (struct timespec *mtime, const char *filename)
{
/* Run "git log -1 --format=%ct -- FILENAME". It prints the time of last
modification (the 'CommitDate', not the 'AuthorDate' which merely
represents the time at which the author locally committed the first version
of the change), as the number of seconds since the Epoch.
The '--' option is for the case that the specified file was removed. */
const char *argv[7];
pid_t child;
int fd[1];
argv[0] = "git";
argv[1] = "log";
argv[2] = "-1";
argv[3] = "--format=%ct";
argv[4] = "--";
argv[5] = filename;
argv[6] = NULL;
child = create_pipe_in ("git", "git", argv, NULL, NULL,
DEV_NULL, true, true, false, fd);
if (child == -1)
return -1;
/* Retrieve its result. */
FILE *fp;
char *line;
size_t linesize;
size_t linelen;
fp = fdopen (fd[0], "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, _("fdopen() failed"));
line = NULL; linesize = 0;
linelen = getline (&line, &linesize, fp);
if (linelen == (size_t)(-1))
{
error (0, 0, _("%s subprocess I/O error"), "git");
fclose (fp);
wait_subprocess (child, "git", true, false, true, false, NULL);
}
else
{
int exitstatus;
if (linelen > 0 && line[linelen - 1] == '\n')
line[linelen - 1] = '\0';
fclose (fp);
/* Remove zombie process from process list, and retrieve exit status. */
exitstatus =
wait_subprocess (child, "git", true, false, true, false, NULL);
if (exitstatus == 0)
{
char *endptr;
unsigned long git_log_time;
if (xstrtoul (line, &endptr, 10, &git_log_time, NULL) == LONGINT_OK
&& endptr == line + strlen (line))
{
mtime->tv_sec = git_log_time;
mtime->tv_nsec = 0;
free (line);
return 0;
}
}
}
free (line);
return -1;
}
int
vc_mtime (struct timespec *mtime, const char *filename)
{
if (is_git_present ()
&& git_vc_controlled (filename)
&& git_unmodified (filename))
{
if (git_mtime (mtime, filename) == 0)
return 0;
}
struct stat statbuf;
if (stat (filename, &statbuf) == 0)
{
*mtime = get_stat_mtime (&statbuf);
return 0;
}
return -1;
}
/* ========================================================================== */
/* Maximum length of a command that is guaranteed to work. */
#if defined _WIN32 || defined __CYGWIN__
/* Windows */
# define MAX_COMMAND_LENGTH 8192
#else
/* Unix platforms */
# define MAX_COMMAND_LENGTH 32768
#endif
/* Keep some safe distance to this maximum. */
#define MAX_CMD_LEN ((int) (MAX_COMMAND_LENGTH * 0.8))
/* The directory separator in canonicalized file names. */
#if defined _WIN32 && !defined __CYGWIN__
# define SLASH '\\'
#else
# define SLASH '/'
#endif
/* Returns the directory name of the git checkout that contains tha current
directory, as an absolute file name, or NULL if the current directory is
not in a git checkout. */
static char *
abs_git_checkout (void)
{
/* Run "git rev-parse --show-toplevel 2>/dev/null" and return its output,
without the trailing newline. */
const char *argv[4];
pid_t child;
int fd[1];
argv[0] = "git";
argv[1] = "rev-parse";
argv[2] = "--show-toplevel";
argv[3] = NULL;
child = create_pipe_in ("git", "git", argv, NULL, NULL,
DEV_NULL, true, true, false, fd);
if (child == -1)
return NULL;
/* Retrieve its result. */
FILE *fp = fdopen (fd[0], "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, _("fdopen() failed"));
char *line = NULL;
size_t linesize = 0;
size_t linelen = getline (&line, &linesize, fp);
if (linelen == (size_t)(-1))
{
fclose (fp);
wait_subprocess (child, "git", true, true, true, false, NULL);
return NULL;
}
else
{
int exitstatus;
if (linelen > 0 && line[linelen - 1] == '\n')
line[linelen - 1] = '\0';
/* Read until EOF (otherwise the child process may get a SIGPIPE signal). */
while (getc (fp) != EOF)
;
fclose (fp);
/* Remove zombie process from process list, and retrieve exit status. */
exitstatus =
wait_subprocess (child, "git", true, true, true, false, NULL);
if (exitstatus == 0)
{
char *line_converted = windows_cygpath_w (line);
free (line);
return line_converted;
}
}
free (line);
return NULL;
}
/* Given an absolute canonicalized directory DIR1 and an absolute canonicalized
directory DIR2, returns N where DIR1 = DIR2 "/.." ... "/.." with N times
"/..", or -1 if DIR1 is not an ancestor directory of DIR2. */
static long
ancestor_level (const char *dir1, const char *dir2)
{
if (streq (dir1, "/"))
dir1 = "";
if (streq (dir2, "/"))
dir2 = "";
size_t dir1_len = strlen (dir1);
if (strncmp (dir1, dir2, dir1_len) == 0)
{
/* DIR2 starts with DIR1. */
const char *p = dir2 + dir1_len;
if (*p == '\0')
/* DIR2 and DIR1 are the same. */
return 0;
if (ISSLASH (*p))
{
/* Return the number of slashes in the tail of DIR2 that starts
at P. */
long n = 1;
p++;
for (; *p != '\0'; p++)
if (ISSLASH (*p))
n++;
return n;
}
}
return -1;
}
/* Given an absolute canolicalized FILENAME that starts with DIR1, returns the
same file name relative to DIR2, where DIR1 = DIR2 "/.." ... "/.." with
N times "/..", as a freshly allocated string. */
static char *
relativize (const char *filename,
unsigned long n, const char *dir1, const char *dir2)
{
if (streq (dir1, "/"))
dir1 = "";
size_t dir1_len = strlen (dir1);
if (!(strncmp (filename, dir1, dir1_len) == 0
&& (filename[dir1_len] == '\0' || ISSLASH (filename[dir1_len]))))
/* Invalid argument. */
abort ();
if (streq (dir2, "/"))
dir2 = "";
dir2 += dir1_len;
filename += dir1_len;
for (;;)
{
/* Invariant: The result will be N times "../" followed by FILENAME. */
if (*filename == '\0')
break;
if (!ISSLASH (*filename))
abort ();
filename++;
if (*dir2 == '\0')
break;
if (!ISSLASH (*dir2))
abort ();
dir2++;
/* Skip one component in DIR2. */
const char *dir2_s;
for (dir2_s = dir2; *dir2_s != '\0'; dir2_s++)
if (ISSLASH (*dir2_s))
break;
/* Skip one component in FILENAME, at P. */
const char *filename_s;
for (filename_s = filename; *filename_s != '\0'; filename_s++)
if (ISSLASH (*filename_s))
break;
/* Did the components match? */
if (!(filename_s - filename == dir2_s - dir2
&& memeq (filename, dir2, dir2_s - dir2)))
break;
dir2 = dir2_s;
filename = filename_s;
n--;
}
if (n == 0 && *filename == '\0')
return xstrdup (".");
char *result = (char *) xmalloc (3 * n + strlen (filename) + 1);
{
char *q = result;
for (; n > 0; n--)
{
q[0] = '.'; q[1] = '.'; q[2] = '/'; q += 3;
}
strcpy (q, filename);
}
return result;
}
/* Accumulating mtimes. */
struct accumulator
{
bool has_some_mtimes;
struct timespec max_of_mtimes;
};
static void
accumulate (struct accumulator *accu, struct timespec mtime)
{
if (accu->has_some_mtimes)
{
/* Compute the maximum of accu->max_of_mtimes and mtime. */
if (accu->max_of_mtimes.tv_sec < mtime.tv_sec
|| (accu->max_of_mtimes.tv_sec == mtime.tv_sec
&& accu->max_of_mtimes.tv_nsec < mtime.tv_nsec))
accu->max_of_mtimes = mtime;
}
else
{
accu->max_of_mtimes = mtime;
accu->has_some_mtimes = true;
}
}
int
max_vc_mtime (struct timespec *max_of_mtimes,
size_t nfiles, const char * const *filenames)
{
if (nfiles == 0)
/* Invalid argument. */
abort ();
struct accumulator accu = { false };
/* Determine which of the specified files are under version control,
and which are duplicates. (The case of duplicates is rare, but it needs
special attention, because 'git ls-files' eliminates duplicates.)
vc_controlled[n] = 1 means that filenames[n] is under version control.
vc_controlled[n] = 0 means that filenames[n] is not under version control.
vc_controlled[n] = -1 means that filenames[n] is a duplicate. */
signed char *vc_controlled = XNMALLOC (nfiles, signed char);
for (size_t n = 0; n < nfiles; n++)
vc_controlled[n] = 0;
if (is_git_present ())
{
/* Since 'git ls-files' produces an error when at least one of the files
is outside the git checkout that contains tha current directory, we
need to filter out such files. This is most easily done by converting
each file name to a canonical file name first and then comparing with
the directory name of said git checkout. */
char *git_checkout = abs_git_checkout ();
if (git_checkout != NULL)
{
char *currdir = xgetcwd ();
if (currdir != NULL)
{
/* git_checkout is expected to be an ancestor directory of the
current directory. */
long ancestor = ancestor_level (git_checkout, currdir);
if (ancestor >= 0)
{
char **canonical_filenames = XNMALLOC (nfiles, char *);
for (size_t n = 0; n < nfiles; n++)
{
char *canonical = canonicalize_file_name (filenames[n]);
if (canonical == NULL)
{
if (errno == ENOMEM)
xalloc_die ();
/* The file filenames[n] does not exist. */
for (size_t k = n; k > 0; )
free (canonical_filenames[--k]);
free (canonical_filenames);
free (currdir);
free (git_checkout);
free (vc_controlled);
return -1;
}
canonical_filenames[n] = canonical;
}
/* Test which of these absolute file names are outside of the
git_checkout. */
char *git_checkout_slash =
(streq (git_checkout, "/")
? xstrdup (git_checkout)
: xasprintf ("%s%c", git_checkout, SLASH));
char **checkout_relative_filenames = XNMALLOC (nfiles, char *);
char **currdir_relative_filenames = XNMALLOC (nfiles, char *);
for (size_t n = 0; n < nfiles; n++)
{
if (str_startswith (canonical_filenames[n], git_checkout_slash))
{
vc_controlled[n] = 1;
checkout_relative_filenames[n] =
relativize (canonical_filenames[n],
0, git_checkout, git_checkout);
currdir_relative_filenames[n] =
relativize (canonical_filenames[n],
ancestor, git_checkout, currdir);
}
else
{
vc_controlled[n] = 0;
checkout_relative_filenames[n] = NULL;
currdir_relative_filenames[n] = NULL;
}
}
/* Room for passing arguments to git commands. */
const char **argv = XNMALLOC (7 + nfiles + 1, const char *);
{
/* Put the relative file names into a hash table. This is needed
because 'git ls-files' returns the files in a different order
than the one we provide in the command. */
gl_map_t relative_filenames_ht =
gl_map_create_empty (GL_HASH_MAP,
hashkey_string_equals, hashkey_string_hash,
NULL, NULL);
for (size_t n = 0; n < nfiles; n++)
if (currdir_relative_filenames[n] != NULL)
{
if (gl_map_get (relative_filenames_ht, currdir_relative_filenames[n]) != NULL)
{
/* It's already in the table. */
vc_controlled[n] = -1;
}
else
gl_map_put (relative_filenames_ht, currdir_relative_filenames[n], &vc_controlled[n]);
}
/* Run "git ls-files -c -o -t -z FILE1..." for as many files as
possible, and inspect the output. */
size_t n0 = 0;
do
{
size_t i = 0;
argv[i++] = "git";
argv[i++] = "ls-files";
argv[i++] = "-c";
argv[i++] = "-o";
argv[i++] = "-t";
argv[i++] = "-z";
size_t i0 = i;
size_t n = n0;
size_t cmd_len = 25;
for (; n < nfiles; n++)
if (vc_controlled[n] == 1)
{
if (cmd_len + strlen (currdir_relative_filenames[n]) >= MAX_CMD_LEN
&& i > i0)
break;
argv[i++] = currdir_relative_filenames[n];
cmd_len += 1 + strlen (currdir_relative_filenames[n]);
}
if (i > i0)
{
pid_t child;
int fd[1];
argv[i] = NULL;
child = create_pipe_in ("git", "git", argv, NULL, NULL,
DEV_NULL, true, true, false, fd);
if (child == -1)
break;
/* Read the subprocess output. It is expected to be of the form
T1 <space> <currdir_relative_filename1> NUL
T2 <space> <currdir_relative_filename2> NUL
...
where the relative filenames correspond to the given file
names (because we have already relativized them). */
FILE *fp = fdopen (fd[0], "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, _("fdopen() failed"));
char *fn = NULL;
size_t fn_size = 0;
for (;;)
{
int status = fgetc (fp);
if (status == EOF)
break;
/* status is a status tag, as documented in
"man git-ls-files". */
int space = fgetc (fp);
if (space != ' ')
{
fprintf (stderr, "vc-mtime: git ls-files output not as expected\n");
break;
}
if (getdelim (&fn, &fn_size, '\0', fp) == -1)
{
if (errno == ENOMEM)
xalloc_die ();
fprintf (stderr, "vc-mtime: failed to read git ls-files output\n");
break;
}
signed char *vc_controlled_p =
(signed char *) gl_map_get (relative_filenames_ht, fn);
if (vc_controlled_p == NULL)
fprintf (stderr, "vc-mtime: git ls-files returned an unexpected file name: %s\n", fn);
else
*vc_controlled_p = (status == 'H' ? 1 : 0);
}
free (fn);
fclose (fp);
/* Remove zombie process from process list, and retrieve exit status. */
int exitstatus =
wait_subprocess (child, "git", false, true, true, false, NULL);
if (exitstatus != 0)
fprintf (stderr, "vc-mtime: git ls-files failed with exit code %d\n", exitstatus);
}
n0 = n;
}
while (n0 < nfiles);
gl_map_free (relative_filenames_ht);
}
{
/* Put the relative file names into a hash table. This is needed
because 'git diff' returns the files in a different order
than the one we provide in the command. */
gl_map_t relative_filenames_ht =
gl_map_create_empty (GL_HASH_MAP,
hashkey_string_equals, hashkey_string_hash,
NULL, NULL);
for (size_t n = 0; n < nfiles; n++)
if (vc_controlled[n] == 1)
{
/* No need to test for duplicates here. We have already set
vc_controlled[n] to -1 for duplicates, above. */
gl_map_put (relative_filenames_ht, checkout_relative_filenames[n], &vc_controlled[n]);
}
/* Run "git diff --name-only --no-relative -z HEAD -- FILE1..." for
as many files as possible, and inspect the output. */
size_t n0 = 0;
do
{
size_t i = 0;
argv[i++] = "git";
argv[i++] = "diff";
argv[i++] = "--name-only";
/* With git versions >= 2.28, we pass option --no-relative,
in order to neutralize any possible customization of the
"diff.relative" property. With git versions < 2.28, this
is not needed, and the option --no-relative does not
exist. */
if (!(git_version[0] <= '1'
|| (git_version[0] == '2' && git_version[1] == '.'
&& ((git_version[2] >= '0' && git_version[2] <= '9'
&& !(git_version[3] >= '0' && git_version[3] <= '9'))
|| (((git_version[2] == '1'
&& git_version[3] >= '0' && git_version[3] <= '9')
|| (git_version[2] == '2'
&& git_version[3] >= '0' && git_version[3] <= '7'))
&& !(git_version[4] >= '0' && git_version[4] <= '9'))))))
argv[i++] = "--no-relative";
argv[i++] = "-z";
argv[i++] = "HEAD";
argv[i++] = "--";
size_t i0 = i;
size_t n = n0;
size_t cmd_len = 46;
for (; n < nfiles; n++)
if (vc_controlled[n] == 1)
{
if (cmd_len + strlen (currdir_relative_filenames[n]) >= MAX_CMD_LEN
&& i > i0)
break;
argv[i++] = currdir_relative_filenames[n];
cmd_len += 1 + strlen (currdir_relative_filenames[n]);
}
if (i > i0)
{
pid_t child;
int fd[1];
argv[i] = NULL;
child = create_pipe_in ("git", "git", argv, NULL, NULL,
DEV_NULL, true, true, false, fd);
if (child == -1)
break;
/* Read the subprocess output. It is expected to be of the form
<checkout_relative_filename1> NUL
<checkout_relative_filename2> NUL
...
where the relative filenames are relative to the git
checkout dir, not to currdir! */
FILE *fp = fdopen (fd[0], "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, _("fdopen() failed"));
char *fn = NULL;
size_t fn_size = 0;
for (;;)
{
/* Test for EOF. */
int c = fgetc (fp);
if (c == EOF)
break;
ungetc (c, fp);
if (getdelim (&fn, &fn_size, '\0', fp) == -1)
{
if (errno == ENOMEM)
xalloc_die ();
fprintf (stderr, "vc-mtime: failed to read git diff output\n");
break;
}
signed char *vc_controlled_p =
(signed char *) gl_map_get (relative_filenames_ht, fn);
if (vc_controlled_p == NULL)
fprintf (stderr, "vc-mtime: git diff returned an unexpected file name: %s\n", fn);
else
/* filenames[n] is under version control but is modified.
Treat it like a file not under version control. */
*vc_controlled_p = 0;
}
free (fn);
fclose (fp);
/* Remove zombie process from process list, and retrieve exit status. */
int exitstatus =
wait_subprocess (child, "git", false, true, true, false, NULL);
if (exitstatus != 0)
fprintf (stderr, "vc-mtime: git diff failed with exit code %d\n", exitstatus);
}
n0 = n;
}
while (n0 < nfiles);
gl_map_free (relative_filenames_ht);
}
{
/* Run "git log -1 --format=%ct -- FILE1...". It prints the
time of last modification (the 'CommitDate', not the
'AuthorDate' which merely represents the time at which the
author locally committed the first version of the change),
as the number of seconds since the Epoch. The '--' option
is for the case that the specified file was removed. */
size_t n0 = 0;
do
{
size_t i = 0;
argv[i++] = "git";
argv[i++] = "log";
argv[i++] = "-1";
argv[i++] = "--format=%ct";
argv[i++] = "--";
size_t i0 = i;
size_t n = n0;
size_t cmd_len = 27;
for (; n < nfiles; n++)
if (vc_controlled[n] == 1)
{
if (cmd_len + strlen (currdir_relative_filenames[n]) >= MAX_CMD_LEN
&& i > i0)
break;
argv[i++] = currdir_relative_filenames[n];
cmd_len += 1 + strlen (currdir_relative_filenames[n]);
}
if (i > i0)
{
pid_t child;
int fd[1];
argv[i] = NULL;
child = create_pipe_in ("git", "git", argv, NULL, NULL,
DEV_NULL, true, true, false, fd);
if (child == -1)
break;
/* Read the subprocess output. It is expected to be a
single line, containing a positive integer. */
FILE *fp = fdopen (fd[0], "r");
if (fp == NULL)
error (EXIT_FAILURE, errno, _("fdopen() failed"));
char *line = NULL;
size_t linesize = 0;
size_t linelen = getline (&line, &linesize, fp);
if (linelen == (size_t)(-1))
{
if (errno == ENOMEM)
xalloc_die ();
fprintf (stderr, "vc-mtime: failed to read git log output\n");
git_log_fail1:
free (line);
fclose (fp);
wait_subprocess (child, "git", true, false, true, false, NULL);
git_log_fail2:
free (argv);
for (size_t k = nfiles; k > 0; )
free (currdir_relative_filenames[--k]);
free (currdir_relative_filenames);
for (size_t k = nfiles; k > 0; )
free (checkout_relative_filenames[--k]);
free (checkout_relative_filenames);
free (git_checkout_slash);
for (size_t k = nfiles; k > 0; )
free (canonical_filenames[--k]);
free (canonical_filenames);
free (currdir);
free (git_checkout);
free (vc_controlled);
return -1;
}
if (linelen > 0 && line[linelen - 1] == '\n')
line[linelen - 1] = '\0';
char *endptr;
unsigned long git_log_time;
if (!(xstrtoul (line, &endptr, 10, &git_log_time, NULL) == LONGINT_OK
&& endptr == line + strlen (line)))
{
fprintf (stderr, "vc-mtime: git log output not as expected\n");
goto git_log_fail1;
}
struct timespec mtime;
mtime.tv_sec = git_log_time;
mtime.tv_nsec = 0;
accumulate (&accu, mtime);
free (line);
fclose (fp);
/* Remove zombie process from process list, and retrieve exit status. */
int exitstatus =
wait_subprocess (child, "git", false, true, true, false, NULL);
if (exitstatus != 0)
{
fprintf (stderr, "vc-mtime: git log failed with exit code %d\n", exitstatus);
goto git_log_fail2;
}
}
n0 = n;
}
while (n0 < nfiles);
}
free (argv);
for (size_t k = nfiles; k > 0; )
free (currdir_relative_filenames[--k]);
free (currdir_relative_filenames);
for (size_t k = nfiles; k > 0; )
free (checkout_relative_filenames[--k]);
free (checkout_relative_filenames);
free (git_checkout_slash);
for (size_t k = nfiles; k > 0; )
free (canonical_filenames[--k]);
free (canonical_filenames);
}
free (currdir);
}
free (git_checkout);
}
}
/* For the files that are not under version control, or that are modified
compared to HEAD, use the file's time stamp. */
for (size_t n = 0; n < nfiles; n++)
if (vc_controlled[n] == 0)
{
struct stat statbuf;
if (stat (filenames[n], &statbuf) < 0)
{
free (vc_controlled);
return -1;
}
struct timespec mtime = get_stat_mtime (&statbuf);
accumulate (&accu, mtime);
}
free (vc_controlled);
/* Since nfiles > 0, we must have accumulated at least one mtime. */
if (!accu.has_some_mtimes)
abort ();
*max_of_mtimes = accu.max_of_mtimes;
return 0;
}
/* ========================================================================== */
#ifdef TEST
#include <assert.h>
#include <stdio.h>
#include <time.h>
/* Some unit tests for internal functions. */
static void
test_ancestor_level (void)
{
assert (ancestor_level ("/home/user/projects/gnulib", "/home/user/projects/gnulib") == 0);
assert (ancestor_level ("/", "/") == 0);
assert (ancestor_level ("/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto") == 2);
assert (ancestor_level ("/", "/home/user") == 2);
assert (ancestor_level ("/home/user/.local", "/home/user/projects/gnulib") == -1);
assert (ancestor_level ("/.local", "/home/user") == -1);
assert (ancestor_level ("/.local", "/") == -1);
}
static void
test_relativize (void)
{
assert (streq (relativize ("/home/user/projects/gnulib",
0, "/home/user/projects/gnulib", "/home/user/projects/gnulib"),
"."));
assert (streq (relativize ("/home/user/projects/gnulib/NEWS",
0, "/home/user/projects/gnulib", "/home/user/projects/gnulib"),
"NEWS"));
assert (streq (relativize ("/home/user/projects/gnulib/doc/Makefile",
0, "/home/user/projects/gnulib", "/home/user/projects/gnulib"),
"doc/Makefile"));
assert (streq (relativize ("/",
0, "/", "/"),
"."));
assert (streq (relativize ("/swapfile",
0, "/", "/"),
"swapfile"));
assert (streq (relativize ("/etc/passwd",
0, "/", "/"),
"etc/passwd"));
assert (streq (relativize ("/home/user/projects/gnulib",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"../../"));
assert (streq (relativize ("/home/user/projects/gnulib/lib",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"../"));
assert (streq (relativize ("/home/user/projects/gnulib/lib/crypto",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"."));
assert (streq (relativize ("/home/user/projects/gnulib/lib/malloc",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"../malloc"));
assert (streq (relativize ("/home/user/projects/gnulib/lib/cr",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"../cr"));
assert (streq (relativize ("/home/user/projects/gnulib/lib/cryptography",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"../cryptography"));
assert (streq (relativize ("/home/user/projects/gnulib/doc",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"../../doc"));
assert (streq (relativize ("/home/user/projects/gnulib/doc/Makefile",
2, "/home/user/projects/gnulib", "/home/user/projects/gnulib/lib/crypto"),
"../../doc/Makefile"));
assert (streq (relativize ("/",
2, "/", "/home/user"),
"../../"));
assert (streq (relativize ("/home",
2, "/", "/home/user"),
"../"));
assert (streq (relativize ("/home/user",
2, "/", "/home/user"),
"."));
assert (streq (relativize ("/home/root",
2, "/", "/home/user"),
"../root"));
assert (streq (relativize ("/home/us",
2, "/", "/home/user"),
"../us"));
assert (streq (relativize ("/home/users",
2, "/", "/home/user"),
"../users"));
assert (streq (relativize ("/etc",
2, "/", "/home/user"),
"../../etc"));
assert (streq (relativize ("/etc/passwd",
2, "/", "/home/user"),
"../../etc/passwd"));
}
/* Usage: ./a.out FILE[...]
*/
int
main (int argc, char *argv[])
{
test_ancestor_level ();
test_relativize ();
if (argc == 1)
{
fprintf (stderr, "Usage: ./a.out FILE[...]\n");
return 1;
}
struct timespec mtime;
int ret = max_vc_mtime (&mtime, argc - 1, (const char **) argv + 1);
if (ret == 0)
{
time_t t = mtime.tv_sec;
struct tm *gmt = gmtime (&t);
printf ("mtime = %04d-%02d-%02d %02d:%02d:%02d UTC\n",
gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
return 0;
}
else
{
printf ("failed\n");
return 1;
}
}
/*
* Local Variables:
* compile-command: "gcc -ggdb -DTEST -Wall -I. -I.. vc-mtime.c libgnu.a"
* End:
*/
#endif