# file      : tests/rep-fetch-git-refname.testscript
# license   : MIT; see accompanying LICENSE file

: fetch
:
{
  $clone_root_cfg && $rep_add "$rep/state0/libfoo.git$fragment"

  if ($git_protocol == 'https-dumb')
    info = "$info_ref_hist$reason_dumb$info_dumb"

  $* 2>>~"%EOE%"
    %fetching git:.+libfoo$fragment%
    %querying .+libfoo\.git%
    %fetching .+libfoo\.git%
    $info
    %fetched git:.+libfoo$fragment%
    1 package\(s\) in 1 repository\(s\)
    EOE
}

: re-fetch
:
{{
  : unchanged
  :
  {
    $clone_root_cfg && $rep_add "$rep/state0/libfoo.git$fragment"

    $* 2>!

    $* 2>>~"%EOE%"
      %fetching git:.+libfoo$fragment%
      %querying .+libfoo\.git%
      %fetched git:.+libfoo$fragment%
      1 package\(s\) in 1 repository\(s\)
      EOE
  }

  : changed
  :
  {
    g = [cmdline] git -C
    u = "$rep_git/state1"
    d = cfg/libfoo-1.0.0

    $clone_root_cfg && $rep_add "$rep/state0/libfoo.git$fragment"

    # Extract the repository path from the output line like this:
    #
    # fetching from <url> in 'cfg/.bpkg/tmp/4bde15f59461'...
    #
    $* -v 2>&1                                         | \
      sed -n -e "s/fetching .+ in '\(.+\)'/\$1/p" | \
      sed -n -e 's%(.+[\\/])tmp([\\/].+)%$1repos$2%p'  | \
      set r

    # Note that the commit for doc/style/basic submodule is not at the branch
    # tip and so is not advertised.
    #
    info1 = '%.{0}'
    info2 = '%.{0}'
    info3 = '%.{0}'
    info4 = '%.{0}'

    if (($git_protocol == 'local' && !$rep_git_local_unadv) || \
        $git_protocol == 'https-smart'                      || \
        $git_protocol == 'git'                              || \
        $git_protocol == 'git-unadv'                        || \
        $git_protocol == 'ssh'                              || \
        $git_protocol == 'ssh-unadv')
    {
      info2 = "$info_repo_hist for submodule 'doc/style/basic'$reason_unadv"
    }
    elif ($git_protocol == 'https-dumb')
    {
      info1 = "$info_ref_hist for submodule 'doc/style'$reason_dumb$info_dumb"
      info2 = "$info_repo_hist for submodule 'doc/style/basic'$reason_dumb$info_dumb"
      info3 = "$info_ref_hist for submodule 'libbar'$reason_dumb$info_dumb"
      info4 = "$info_ref_hist for submodule 'libbar/extras'$reason_dumb$info_dumb"
    }

    $pkg_checkout libfoo/1.0.0 2>>~"%EOE%"
      checking out libfoo/1.0.0 \(local cache\)
      %querying .+style\.git%?
      %fetching .+style\.git for submodule 'doc/style'%
      $info1
      %submodule path 'doc/style': checked out .+%
      %querying .+style-basic\.git%?
      %fetching .+style-basic\.git for submodule 'doc/style/basic'%
      $info2
      %submodule path 'doc/style/basic': checked out .+%
      %querying .+libbar\.git%?
      %fetching .+libbar\.git for submodule 'libbar'%
      $info3
      %submodule path 'libbar': checked out .+%
      %skipped fetching and validating .+style-basic.git .memory cache.%?
      %fetching .+style-basic\.git+ for submodule 'libbar/extras'%
      $info4
      %submodule path 'libbar/extras': checked out .+%
      verifying symlinks
      %fixing up symlinks%?
      distributing libfoo/1.0.0
      checked out libfoo/1.0.0
      EOE

    # While at it, check that the fetch was shallow (and the repository
    # contains a single commit), unless the protocol is dumb.
    #
    if ($git_protocol != 'https-dumb')
      $g "$r" log '--pretty=format:%d %s' >>:~%EOO%
        % \(.+\) .+%
        EOO

    $g "$r" config remote.origin.url "$u/libfoo.git"

    # Preconditions.
    #
    test -f $d/tests/TODO
    test -f $d/libbar/libbar/manifest
    test -f $d/doc/style/README != 0
    test -d $d/libbaz != 0

    if ($git_protocol == 'https-dumb')
      info = "$info_ref_hist$reason_dumb$info_dumb"

    $* 2>>~"%EOE%" 1>&2
      %fetching git:.+libfoo$fragment%
      %querying .+libfoo\.git%
      %fetching .+libfoo\.git%
      $info
      %fetched git:.+libfoo$fragment%
      1 package\(s\) in 1 repository\(s\)
      EOE

    $pkg_drop libfoo

    info1 = '%.{0}'
    info2 = '%.{0}'

    if ($git_protocol == 'https-dumb')
    {
      info1 = "$info_ref_hist for submodule 'doc/style'$reason_dumb$info_dumb"
      info2 = "$info_ref_hist for submodule 'libbaz'$reason_dumb$info_dumb"
    }

    $pkg_checkout libfoo/1.0.0 2>>~"%EOE%"
      checking out libfoo/1.0.0 \(local cache\)
      %querying .+style\.git%?
      %fetching .+style\.git for submodule 'doc/style'%
      $info1
      %submodule path 'doc/style': checked out .+%
      %skipped fetching .+style-basic\.git for submodule 'doc/style/basic'%
      %submodule path 'doc/style/basic': checked out .+%
      %querying .+libbaz\.git%?
      %fetching .+libbaz\.git for submodule 'libbaz'%
      $info2
      %submodule path 'libbaz': checked out .+%
      verifying symlinks
      %fixing up symlinks%?
      distributing libfoo/1.0.0
      checked out libfoo/1.0.0
      EOE

    # Postconditions.
    #
    test -d $d/tests != 0
    test -d $d/libbar != 0
    test -f $d/doc/style/README
    test -f $d/libbaz/manifest

    $pkg_drop libfoo
  }

  : no-progress
  :
  {
    $clone_root_cfg && $rep_add "$rep/state0/libfoo.git$fragment"

    $* --no-progress 2>>~"%EOE%"
      %fetched git:.+libfoo$fragment%
      1 package\(s\) in 1 repository\(s\)
      EOE

    $pkg_checkout --no-progress libfoo/1.0.0 2>>EOE
      checked out libfoo/1.0.0
      EOE

    $pkg_drop libfoo
  }
}}
