Merge branch 'jc/maint-filter-branch-epoch-date' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 30 Jul 2012 20:03:40 +0000 (13:03 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Jul 2012 20:04:18 +0000 (13:04 -0700)
In 1.7.9 era, we taught "git rebase" about the raw timestamp format
but we did not teach the same trick to "filter-branch", which rolled
a similar logic on its own.

* jc/maint-filter-branch-epoch-date:
t7003: add test to filter a branch with a commit at epoch
date.c: Fix off by one error in object-header date parsing
filter-branch: do not forget the '@' prefix to force git-timestamp

1  2 
date.c
git-filter-branch.sh
t/test-lib-functions.sh
diff --combined date.c
index 1fdcf7c6eae0386552c5d7e069f5242fca3c2e0e,67b3d66f66e6a6fe9938bae53baa7812d60f5a12..57331ed406e2391e0a2bf327fbb86d3b62012324
--- 1/date.c
--- 2/date.c
+++ b/date.c
@@@ -86,98 -86,83 +86,98 @@@ static int local_tzoffset(unsigned lon
        return offset * eastwest;
  }
  
 -const char *show_date_relative(unsigned long time, int tz,
 +void show_date_relative(unsigned long time, int tz,
                               const struct timeval *now,
 -                             char *timebuf,
 -                             size_t timebuf_size)
 +                             struct strbuf *timebuf)
  {
        unsigned long diff;
 -      if (now->tv_sec < time)
 -              return "in the future";
 +      if (now->tv_sec < time) {
 +              strbuf_addstr(timebuf, _("in the future"));
 +              return;
 +      }
        diff = now->tv_sec - time;
        if (diff < 90) {
 -              snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
 -              return timebuf;
 +              strbuf_addf(timebuf,
 +                       Q_("%lu second ago", "%lu seconds ago", diff), diff);
 +              return;
        }
        /* Turn it into minutes */
        diff = (diff + 30) / 60;
        if (diff < 90) {
 -              snprintf(timebuf, timebuf_size, "%lu minutes ago", diff);
 -              return timebuf;
 +              strbuf_addf(timebuf,
 +                       Q_("%lu minute ago", "%lu minutes ago", diff), diff);
 +              return;
        }
        /* Turn it into hours */
        diff = (diff + 30) / 60;
        if (diff < 36) {
 -              snprintf(timebuf, timebuf_size, "%lu hours ago", diff);
 -              return timebuf;
 +              strbuf_addf(timebuf,
 +                       Q_("%lu hour ago", "%lu hours ago", diff), diff);
 +              return;
        }
        /* We deal with number of days from here on */
        diff = (diff + 12) / 24;
        if (diff < 14) {
 -              snprintf(timebuf, timebuf_size, "%lu days ago", diff);
 -              return timebuf;
 +              strbuf_addf(timebuf,
 +                       Q_("%lu day ago", "%lu days ago", diff), diff);
 +              return;
        }
        /* Say weeks for the past 10 weeks or so */
        if (diff < 70) {
 -              snprintf(timebuf, timebuf_size, "%lu weeks ago", (diff + 3) / 7);
 -              return timebuf;
 +              strbuf_addf(timebuf,
 +                       Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
 +                       (diff + 3) / 7);
 +              return;
        }
        /* Say months for the past 12 months or so */
        if (diff < 365) {
 -              snprintf(timebuf, timebuf_size, "%lu months ago", (diff + 15) / 30);
 -              return timebuf;
 +              strbuf_addf(timebuf,
 +                       Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
 +                       (diff + 15) / 30);
 +              return;
        }
        /* Give years and months for 5 years or so */
        if (diff < 1825) {
                unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
                unsigned long years = totalmonths / 12;
                unsigned long months = totalmonths % 12;
 -              int n;
 -              n = snprintf(timebuf, timebuf_size, "%lu year%s",
 -                              years, (years > 1 ? "s" : ""));
 -              if (months)
 -                      snprintf(timebuf + n, timebuf_size - n,
 -                                      ", %lu month%s ago",
 -                                      months, (months > 1 ? "s" : ""));
 -              else
 -                      snprintf(timebuf + n, timebuf_size - n, " ago");
 -              return timebuf;
 +              if (months) {
 +                      struct strbuf sb = STRBUF_INIT;
 +                      strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
 +                      /* TRANSLATORS: "%s" is "<n> years" */
 +                      strbuf_addf(timebuf,
 +                               Q_("%s, %lu month ago", "%s, %lu months ago", months),
 +                               sb.buf, months);
 +                      strbuf_release(&sb);
 +              } else
 +                      strbuf_addf(timebuf,
 +                               Q_("%lu year ago", "%lu years ago", years), years);
 +              return;
        }
        /* Otherwise, just years. Centuries is probably overkill. */
 -      snprintf(timebuf, timebuf_size, "%lu years ago", (diff + 183) / 365);
 -      return timebuf;
 +      strbuf_addf(timebuf,
 +               Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
 +               (diff + 183) / 365);
  }
  
  const char *show_date(unsigned long time, int tz, enum date_mode mode)
  {
        struct tm *tm;
 -      static char timebuf[200];
 +      static struct strbuf timebuf = STRBUF_INIT;
  
        if (mode == DATE_RAW) {
 -              snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
 -              return timebuf;
 +              strbuf_reset(&timebuf);
 +              strbuf_addf(&timebuf, "%lu %+05d", time, tz);
 +              return timebuf.buf;
        }
  
        if (mode == DATE_RELATIVE) {
                struct timeval now;
 +
 +              strbuf_reset(&timebuf);
                gettimeofday(&now, NULL);
 -              return show_date_relative(time, tz, &now,
 -                                        timebuf, sizeof(timebuf));
 +              show_date_relative(time, tz, &now, &timebuf);
 +              return timebuf.buf;
        }
  
        if (mode == DATE_LOCAL)
        tm = time_to_tm(time, tz);
        if (!tm)
                return NULL;
 +
 +      strbuf_reset(&timebuf);
        if (mode == DATE_SHORT)
 -              sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
 +              strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
                                tm->tm_mon + 1, tm->tm_mday);
        else if (mode == DATE_ISO8601)
 -              sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
 +              strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
                                tm->tm_year + 1900,
                                tm->tm_mon + 1,
                                tm->tm_mday,
                                tm->tm_hour, tm->tm_min, tm->tm_sec,
                                tz);
        else if (mode == DATE_RFC2822)
 -              sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
 +              strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
                        weekday_names[tm->tm_wday], tm->tm_mday,
                        month_names[tm->tm_mon], tm->tm_year + 1900,
                        tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
        else
 -              sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
 +              strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
                                weekday_names[tm->tm_wday],
                                month_names[tm->tm_mon],
                                tm->tm_mday,
                                tm->tm_year + 1900,
                                (mode == DATE_LOCAL) ? 0 : ' ',
                                tz);
 -      return timebuf;
 +      return timebuf.buf;
  }
  
  /*
@@@ -569,35 -552,23 +569,35 @@@ static int match_digit(const char *date
  static int match_tz(const char *date, int *offp)
  {
        char *end;
 -      int offset = strtoul(date+1, &end, 10);
 -      int min, hour;
 -      int n = end - date - 1;
 +      int hour = strtoul(date + 1, &end, 10);
 +      int n = end - (date + 1);
 +      int min = 0;
  
 -      min = offset % 100;
 -      hour = offset / 100;
 +      if (n == 4) {
 +              /* hhmm */
 +              min = hour % 100;
 +              hour = hour / 100;
 +      } else if (n != 2) {
 +              min = 99; /* random crap */
 +      } else if (*end == ':') {
 +              /* hh:mm? */
 +              min = strtoul(end + 1, &end, 10);
 +              if (end - (date + 1) != 5)
 +                      min = 99; /* random crap */
 +      } /* otherwise we parsed "hh" */
  
        /*
 -       * Don't accept any random crap.. At least 3 digits, and
 -       * a valid minute. We might want to check that the minutes
 -       * are divisible by 30 or something too.
 +       * Don't accept any random crap. Even though some places have
 +       * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
 +       * UTC+14), there is something wrong if hour part is much
 +       * larger than that. We might also want to check that the
 +       * minutes are divisible by 15 or something too. (Offset of
 +       * Kathmandu, Nepal is UTC+5:45)
         */
 -      if (min < 60 && n > 2) {
 -              offset = hour*60+min;
 +      if (min < 60 && hour < 24) {
 +              int offset = hour * 60 + min;
                if (*date == '-')
                        offset = -offset;
 -
                *offp = offset;
        }
        return end - date;
@@@ -624,7 -595,7 +624,7 @@@ static int match_object_header_date(con
        unsigned long stamp;
        int ofs;
  
-       if (*date < '0' || '9' <= *date)
+       if (*date < '0' || '9' < *date)
                return -1;
        stamp = strtoul(date, &end, 10);
        if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-'))
diff --combined git-filter-branch.sh
index add2c0247fa91e0f629428c295fc581f19cf85e1,b9cded5a28e6f7329e9fed23300449b371c00952..178e45305d2b66bbe5a0c0902d989ea66888869b
@@@ -84,7 -84,7 +84,7 @@@ set_ident () 
                        s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
  
                        g
-                       s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
+                       s/^'$lid' [^<]* <[^>]*> \(.*\)$/@\1/
                        s/'\''/'\''\'\'\''/g
                        s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
  
@@@ -108,7 -108,9 +108,7 @@@ OPTIONS_SPEC
  . git-sh-setup
  
  if [ "$(is_bare_repository)" = false ]; then
 -      git diff-files --ignore-submodules --quiet &&
 -      git diff-index --cached --quiet HEAD -- ||
 -      die "Cannot rewrite branch(es) with a dirty working directory."
 +      require_clean_work_tree 'rewrite branches'
  fi
  
  tempdir=.git-rewrite
diff --combined t/test-lib-functions.sh
index 16397691d951864f760b832ee066a63cd83f64fc,0000000000000000000000000000000000000000..80daaca7806cbe1a6de0ddeba40400c8a811328e
mode 100644,000000..100644
--- /dev/null
@@@ -1,565 -1,0 +1,574 @@@
-       file=${2:-"$1.t"}
 +#!/bin/sh
 +#
 +# Copyright (c) 2005 Junio C Hamano
 +#
 +# 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 2 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 http://www.gnu.org/licenses/ .
 +
 +# The semantics of the editor variables are that of invoking
 +# sh -c "$EDITOR \"$@\"" files ...
 +#
 +# If our trash directory contains shell metacharacters, they will be
 +# interpreted if we just set $EDITOR directly, so do a little dance with
 +# environment variables to work around this.
 +#
 +# In particular, quoting isn't enough, as the path may contain the same quote
 +# that we're using.
 +test_set_editor () {
 +      FAKE_EDITOR="$1"
 +      export FAKE_EDITOR
 +      EDITOR='"$FAKE_EDITOR"'
 +      export EDITOR
 +}
 +
 +test_decode_color () {
 +      awk '
 +              function name(n) {
 +                      if (n == 0) return "RESET";
 +                      if (n == 1) return "BOLD";
 +                      if (n == 30) return "BLACK";
 +                      if (n == 31) return "RED";
 +                      if (n == 32) return "GREEN";
 +                      if (n == 33) return "YELLOW";
 +                      if (n == 34) return "BLUE";
 +                      if (n == 35) return "MAGENTA";
 +                      if (n == 36) return "CYAN";
 +                      if (n == 37) return "WHITE";
 +                      if (n == 40) return "BLACK";
 +                      if (n == 41) return "BRED";
 +                      if (n == 42) return "BGREEN";
 +                      if (n == 43) return "BYELLOW";
 +                      if (n == 44) return "BBLUE";
 +                      if (n == 45) return "BMAGENTA";
 +                      if (n == 46) return "BCYAN";
 +                      if (n == 47) return "BWHITE";
 +              }
 +              {
 +                      while (match($0, /\033\[[0-9;]*m/) != 0) {
 +                              printf "%s<", substr($0, 1, RSTART-1);
 +                              codes = substr($0, RSTART+2, RLENGTH-3);
 +                              if (length(codes) == 0)
 +                                      printf "%s", name(0)
 +                              else {
 +                                      n = split(codes, ary, ";");
 +                                      sep = "";
 +                                      for (i = 1; i <= n; i++) {
 +                                              printf "%s%s", sep, name(ary[i]);
 +                                              sep = ";"
 +                                      }
 +                              }
 +                              printf ">";
 +                              $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
 +                      }
 +                      print
 +              }
 +      '
 +}
 +
 +nul_to_q () {
 +      "$PERL_PATH" -pe 'y/\000/Q/'
 +}
 +
 +q_to_nul () {
 +      "$PERL_PATH" -pe 'y/Q/\000/'
 +}
 +
 +q_to_cr () {
 +      tr Q '\015'
 +}
 +
 +q_to_tab () {
 +      tr Q '\011'
 +}
 +
 +append_cr () {
 +      sed -e 's/$/Q/' | tr Q '\015'
 +}
 +
 +remove_cr () {
 +      tr '\015' Q | sed -e 's/Q$//'
 +}
 +
 +# In some bourne shell implementations, the "unset" builtin returns
 +# nonzero status when a variable to be unset was not set in the first
 +# place.
 +#
 +# Use sane_unset when that should not be considered an error.
 +
 +sane_unset () {
 +      unset "$@"
 +      return 0
 +}
 +
 +test_tick () {
 +      if test -z "${test_tick+set}"
 +      then
 +              test_tick=1112911993
 +      else
 +              test_tick=$(($test_tick + 60))
 +      fi
 +      GIT_COMMITTER_DATE="$test_tick -0700"
 +      GIT_AUTHOR_DATE="$test_tick -0700"
 +      export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
 +}
 +
 +# Stop execution and start a shell. This is useful for debugging tests and
 +# only makes sense together with "-v".
 +#
 +# Be sure to remove all invocations of this command before submitting.
 +
 +test_pause () {
 +      if test "$verbose" = t; then
 +              "$SHELL_PATH" <&6 >&3 2>&4
 +      else
 +              error >&5 "test_pause requires --verbose"
 +      fi
 +}
 +
 +# Call test_commit with the arguments "<message> [<file> [<contents>]]"
 +#
 +# This will commit a file with the given contents and the given commit
 +# message.  It will also add a tag with <message> as name.
 +#
 +# Both <file> and <contents> default to <message>.
 +
 +test_commit () {
-       test_tick &&
++      notick= &&
++      if test "z$1" = "z--notick"
++      then
++              notick=yes
++              shift
++      fi &&
++      file=${2:-"$1.t"} &&
 +      echo "${3-$1}" > "$file" &&
 +      git add "$file" &&
++      if test -z "$notick"
++      then
++              test_tick
++      fi &&
 +      git commit -m "$1" &&
 +      git tag "$1"
 +}
 +
 +# Call test_merge with the arguments "<message> <commit>", where <commit>
 +# can be a tag pointing to the commit-to-merge.
 +
 +test_merge () {
 +      test_tick &&
 +      git merge -m "$1" "$2" &&
 +      git tag "$1"
 +}
 +
 +# This function helps systems where core.filemode=false is set.
 +# Use it instead of plain 'chmod +x' to set or unset the executable bit
 +# of a file in the working directory and add it to the index.
 +
 +test_chmod () {
 +      chmod "$@" &&
 +      git update-index --add "--chmod=$@"
 +}
 +
 +# Unset a configuration variable, but don't fail if it doesn't exist.
 +test_unconfig () {
 +      git config --unset-all "$@"
 +      config_status=$?
 +      case "$config_status" in
 +      5) # ok, nothing to unset
 +              config_status=0
 +              ;;
 +      esac
 +      return $config_status
 +}
 +
 +# Set git config, automatically unsetting it after the test is over.
 +test_config () {
 +      test_when_finished "test_unconfig '$1'" &&
 +      git config "$@"
 +}
 +
 +test_config_global () {
 +      test_when_finished "test_unconfig --global '$1'" &&
 +      git config --global "$@"
 +}
 +
 +write_script () {
 +      {
 +              echo "#!${2-"$SHELL_PATH"}" &&
 +              cat
 +      } >"$1" &&
 +      chmod +x "$1"
 +}
 +
 +# Use test_set_prereq to tell that a particular prerequisite is available.
 +# The prerequisite can later be checked for in two ways:
 +#
 +# - Explicitly using test_have_prereq.
 +#
 +# - Implicitly by specifying the prerequisite tag in the calls to
 +#   test_expect_{success,failure,code}.
 +#
 +# The single parameter is the prerequisite tag (a simple word, in all
 +# capital letters by convention).
 +
 +test_set_prereq () {
 +      satisfied="$satisfied$1 "
 +}
 +satisfied=" "
 +
 +test_have_prereq () {
 +      # prerequisites can be concatenated with ','
 +      save_IFS=$IFS
 +      IFS=,
 +      set -- $*
 +      IFS=$save_IFS
 +
 +      total_prereq=0
 +      ok_prereq=0
 +      missing_prereq=
 +
 +      for prerequisite
 +      do
 +              total_prereq=$(($total_prereq + 1))
 +              case $satisfied in
 +              *" $prerequisite "*)
 +                      ok_prereq=$(($ok_prereq + 1))
 +                      ;;
 +              *)
 +                      # Keep a list of missing prerequisites
 +                      if test -z "$missing_prereq"
 +                      then
 +                              missing_prereq=$prerequisite
 +                      else
 +                              missing_prereq="$prerequisite,$missing_prereq"
 +                      fi
 +              esac
 +      done
 +
 +      test $total_prereq = $ok_prereq
 +}
 +
 +test_declared_prereq () {
 +      case ",$test_prereq," in
 +      *,$1,*)
 +              return 0
 +              ;;
 +      esac
 +      return 1
 +}
 +
 +test_expect_failure () {
 +      test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 +      test "$#" = 2 ||
 +      error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
 +      export test_prereq
 +      if ! test_skip "$@"
 +      then
 +              say >&3 "checking known breakage: $2"
 +              if test_run_ "$2" expecting_failure
 +              then
 +                      test_known_broken_ok_ "$1"
 +              else
 +                      test_known_broken_failure_ "$1"
 +              fi
 +      fi
 +      echo >&3 ""
 +}
 +
 +test_expect_success () {
 +      test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 +      test "$#" = 2 ||
 +      error "bug in the test script: not 2 or 3 parameters to test-expect-success"
 +      export test_prereq
 +      if ! test_skip "$@"
 +      then
 +              say >&3 "expecting success: $2"
 +              if test_run_ "$2"
 +              then
 +                      test_ok_ "$1"
 +              else
 +                      test_failure_ "$@"
 +              fi
 +      fi
 +      echo >&3 ""
 +}
 +
 +# test_external runs external test scripts that provide continuous
 +# test output about their progress, and succeeds/fails on
 +# zero/non-zero exit code.  It outputs the test output on stdout even
 +# in non-verbose mode, and announces the external script with "# run
 +# <n>: ..." before running it.  When providing relative paths, keep in
 +# mind that all scripts run in "trash directory".
 +# Usage: test_external description command arguments...
 +# Example: test_external 'Perl API' perl ../path/to/test.pl
 +test_external () {
 +      test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq=
 +      test "$#" = 3 ||
 +      error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
 +      descr="$1"
 +      shift
 +      export test_prereq
 +      if ! test_skip "$descr" "$@"
 +      then
 +              # Announce the script to reduce confusion about the
 +              # test output that follows.
 +              say_color "" "# run $test_count: $descr ($*)"
 +              # Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
 +              # to be able to use them in script
 +              export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
 +              # Run command; redirect its stderr to &4 as in
 +              # test_run_, but keep its stdout on our stdout even in
 +              # non-verbose mode.
 +              "$@" 2>&4
 +              if [ "$?" = 0 ]
 +              then
 +                      if test $test_external_has_tap -eq 0; then
 +                              test_ok_ "$descr"
 +                      else
 +                              say_color "" "# test_external test $descr was ok"
 +                              test_success=$(($test_success + 1))
 +                      fi
 +              else
 +                      if test $test_external_has_tap -eq 0; then
 +                              test_failure_ "$descr" "$@"
 +                      else
 +                              say_color error "# test_external test $descr failed: $@"
 +                              test_failure=$(($test_failure + 1))
 +                      fi
 +              fi
 +      fi
 +}
 +
 +# Like test_external, but in addition tests that the command generated
 +# no output on stderr.
 +test_external_without_stderr () {
 +      # The temporary file has no (and must have no) security
 +      # implications.
 +      tmp=${TMPDIR:-/tmp}
 +      stderr="$tmp/git-external-stderr.$$.tmp"
 +      test_external "$@" 4> "$stderr"
 +      [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
 +      descr="no stderr: $1"
 +      shift
 +      say >&3 "# expecting no stderr from previous command"
 +      if [ ! -s "$stderr" ]; then
 +              rm "$stderr"
 +
 +              if test $test_external_has_tap -eq 0; then
 +                      test_ok_ "$descr"
 +              else
 +                      say_color "" "# test_external_without_stderr test $descr was ok"
 +                      test_success=$(($test_success + 1))
 +              fi
 +      else
 +              if [ "$verbose" = t ]; then
 +                      output=`echo; echo "# Stderr is:"; cat "$stderr"`
 +              else
 +                      output=
 +              fi
 +              # rm first in case test_failure exits.
 +              rm "$stderr"
 +              if test $test_external_has_tap -eq 0; then
 +                      test_failure_ "$descr" "$@" "$output"
 +              else
 +                      say_color error "# test_external_without_stderr test $descr failed: $@: $output"
 +                      test_failure=$(($test_failure + 1))
 +              fi
 +      fi
 +}
 +
 +# debugging-friendly alternatives to "test [-f|-d|-e]"
 +# The commands test the existence or non-existence of $1. $2 can be
 +# given to provide a more precise diagnosis.
 +test_path_is_file () {
 +      if ! [ -f "$1" ]
 +      then
 +              echo "File $1 doesn't exist. $*"
 +              false
 +      fi
 +}
 +
 +test_path_is_dir () {
 +      if ! [ -d "$1" ]
 +      then
 +              echo "Directory $1 doesn't exist. $*"
 +              false
 +      fi
 +}
 +
 +test_path_is_missing () {
 +      if [ -e "$1" ]
 +      then
 +              echo "Path exists:"
 +              ls -ld "$1"
 +              if [ $# -ge 1 ]; then
 +                      echo "$*"
 +              fi
 +              false
 +      fi
 +}
 +
 +# test_line_count checks that a file has the number of lines it
 +# ought to. For example:
 +#
 +#     test_expect_success 'produce exactly one line of output' '
 +#             do something >output &&
 +#             test_line_count = 1 output
 +#     '
 +#
 +# is like "test $(wc -l <output) = 1" except that it passes the
 +# output through when the number of lines is wrong.
 +
 +test_line_count () {
 +      if test $# != 3
 +      then
 +              error "bug in the test script: not 3 parameters to test_line_count"
 +      elif ! test $(wc -l <"$3") "$1" "$2"
 +      then
 +              echo "test_line_count: line count for $3 !$1 $2"
 +              cat "$3"
 +              return 1
 +      fi
 +}
 +
 +# This is not among top-level (test_expect_success | test_expect_failure)
 +# but is a prefix that can be used in the test script, like:
 +#
 +#     test_expect_success 'complain and die' '
 +#           do something &&
 +#           do something else &&
 +#         test_must_fail git checkout ../outerspace
 +#     '
 +#
 +# Writing this as "! git checkout ../outerspace" is wrong, because
 +# the failure could be due to a segv.  We want a controlled failure.
 +
 +test_must_fail () {
 +      "$@"
 +      exit_code=$?
 +      if test $exit_code = 0; then
 +              echo >&2 "test_must_fail: command succeeded: $*"
 +              return 1
 +      elif test $exit_code -gt 129 -a $exit_code -le 192; then
 +              echo >&2 "test_must_fail: died by signal: $*"
 +              return 1
 +      elif test $exit_code = 127; then
 +              echo >&2 "test_must_fail: command not found: $*"
 +              return 1
 +      fi
 +      return 0
 +}
 +
 +# Similar to test_must_fail, but tolerates success, too.  This is
 +# meant to be used in contexts like:
 +#
 +#     test_expect_success 'some command works without configuration' '
 +#             test_might_fail git config --unset all.configuration &&
 +#             do something
 +#     '
 +#
 +# Writing "git config --unset all.configuration || :" would be wrong,
 +# because we want to notice if it fails due to segv.
 +
 +test_might_fail () {
 +      "$@"
 +      exit_code=$?
 +      if test $exit_code -gt 129 -a $exit_code -le 192; then
 +              echo >&2 "test_might_fail: died by signal: $*"
 +              return 1
 +      elif test $exit_code = 127; then
 +              echo >&2 "test_might_fail: command not found: $*"
 +              return 1
 +      fi
 +      return 0
 +}
 +
 +# Similar to test_must_fail and test_might_fail, but check that a
 +# given command exited with a given exit code. Meant to be used as:
 +#
 +#     test_expect_success 'Merge with d/f conflicts' '
 +#             test_expect_code 1 git merge "merge msg" B master
 +#     '
 +
 +test_expect_code () {
 +      want_code=$1
 +      shift
 +      "$@"
 +      exit_code=$?
 +      if test $exit_code = $want_code
 +      then
 +              return 0
 +      fi
 +
 +      echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
 +      return 1
 +}
 +
 +# test_cmp is a helper function to compare actual and expected output.
 +# You can use it like:
 +#
 +#     test_expect_success 'foo works' '
 +#             echo expected >expected &&
 +#             foo >actual &&
 +#             test_cmp expected actual
 +#     '
 +#
 +# This could be written as either "cmp" or "diff -u", but:
 +# - cmp's output is not nearly as easy to read as diff -u
 +# - not all diff versions understand "-u"
 +
 +test_cmp() {
 +      $GIT_TEST_CMP "$@"
 +}
 +
 +# This function can be used to schedule some commands to be run
 +# unconditionally at the end of the test to restore sanity:
 +#
 +#     test_expect_success 'test core.capslock' '
 +#             git config core.capslock true &&
 +#             test_when_finished "git config --unset core.capslock" &&
 +#             hello world
 +#     '
 +#
 +# That would be roughly equivalent to
 +#
 +#     test_expect_success 'test core.capslock' '
 +#             git config core.capslock true &&
 +#             hello world
 +#             git config --unset core.capslock
 +#     '
 +#
 +# except that the greeting and config --unset must both succeed for
 +# the test to pass.
 +#
 +# Note that under --immediate mode, no clean-up is done to help diagnose
 +# what went wrong.
 +
 +test_when_finished () {
 +      test_cleanup="{ $*
 +              } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
 +}
 +
 +# Most tests can use the created repository, but some may need to create more.
 +# Usage: test_create_repo <directory>
 +test_create_repo () {
 +      test "$#" = 1 ||
 +      error "bug in the test script: not 1 parameter to test-create-repo"
 +      repo="$1"
 +      mkdir -p "$repo"
 +      (
 +              cd "$repo" || error "Cannot setup test environment"
 +              "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 +              error "cannot run git init -- have you built things yet?"
 +              mv .git/hooks .git/hooks-disabled
 +      ) || exit
 +}