# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           conflicts_build 1.0
PortGroup           legacysupport   1.1
PortGroup           perl5           1.0

legacysupport.newest_darwin_requires_legacy 11

# RELEASE VERSION
name                git
# NOTE: Update the DEVEL version first before updating this RELEASE version,
#       to verify that the intended version builds on all platforms.
version             2.55.0
revision            0

description         Fast version control system
long_description    Git is a fast, scalable, distributed open source version \
                    control system focusing on speed and efficiency.

# DEVELOPMENT VERSION
subport ${name}-devel {
    PortGroup       github  1.0

    github.setup    git git 2.55.0 v
    revision        0

    description     \
        {*}${description} (Development Version)

    long_description \
        {*}${long_description} (Development Version)
}

maintainers         {ciserlohn @ci42} \
                    {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

default_variants    +credential_osxkeychain +diff_highlight +doc +pcre

categories          devel
license             GPL-2 LGPL-2.1+
installs_libs       no

patch.pre_args-replace  -p0 -p1

if {${name} eq ${subport}} {
    # RELEASE VERSION
    homepage        https://git-scm.com

    master_sites    https://www.kernel.org/pub/software/scm/git/ \
                    https://cdn.kernel.org/pub/software/scm/git/

    distname        git-${version}
    use_xz          yes
    distfiles-append \
                    git-${version}${extract.suffix} \
                    git-manpages-${version}${extract.suffix}

    checksums       git-${version}${extract.suffix} \
                    rmd160  bb3a07257c182db0fc365233d69a5937284bb435 \
                    sha256  457fdb04dc8728e007d4688695e6912e6f680727920f2a40bf11eacc17505357 \
                    size    8177180 \
                    git-manpages-${version}${extract.suffix} \
                    rmd160  073ee83beee8d0911c15c3a4d6f3e70586224317 \
                    sha256  a32d432f80df46a14a05d1104c72d5a13fe27e9feba9aa0f017e54131db6b982 \
                    size    630172

    extract.only    git-${version}${extract.suffix} \
                    git-manpages-${version}${extract.suffix}

    conflicts       git-devel

    livecheck.type  regexm
    livecheck.regex {<span class="version">.*?(\d+\.\d+\.\d+).*?</span>}
} else {
    # DEVELOPMENT VERSION

    checksums       ${distname}${extract.suffix} \
                    rmd160  47cd5f9f82e846df374f3aa1ba2a7257d0410cc4 \
                    sha256  72923418db7b26dfddc21e2268660c5118e560bdfaa09b4489b67b38e9b69c49 \
                    size    12209821
    github.tarball_from archive

    depends_build-append \
                    port:asciidoc \
                    port:xmlto

    build.target    all man html

    conflicts       git
}

perl5.require_variant   false
perl5.conflict_variants yes
perl5.branches          5.28 5.30 5.32 5.34
perl5.default_branch    5.34
perl5.create_variants   ${perl5.branches}

conflicts_build     libuuid

depends_build-append \
                    port:gettext

depends_lib-append  port:curl \
                    port:expat \
                    port:gettext-runtime \
                    port:libiconv \
                    port:zlib

depends_run-append  path:bin/rsync:rsync

patchfiles-append   patch-Makefile.diff \
                    patch-git-subtree.1.diff \
                    patch-sha1dc-older-apple-gcc-versions.diff

# Old Clang versions (macOS < 10.12) lack __builtin_add_overflow;
# fall back to the manual overflow check.
if {${os.platform} eq "darwin" && ${os.major} < 16 && [string match clang ${configure.compiler}]} {
    patchfiles-append   patch-git-compat-util-no-builtin-add-overflow.diff
}

# TODO: perhaps restore a working one?
# Git 2.36.0 implements a new FSEvent listener that uses
# the API available in Yosemite and newer.
if {${os.platform} eq "darwin" && ${os.major} < 14} {
    patchfiles-append   patch-ignore-fsmonitor-daemon-backend.diff
}

# Git 2.40.0 fails to build on macOS 10.6 & 10.7 unless
# USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS is disabled.
# See: https://trac.macports.org/ticket/67091
# Also trash brewisms.
if {${os.platform} eq "darwin" && ${os.major} < 12} {
    patchfiles-append   patch-disable-enhanced-basic-regex.diff
}

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # Reverts breakage introduced by 51c15ac1b6ab15d8e29651d0bd364d8f62dc6509
    # and later commits.
    # TODO: consider convising upstream to restore fallback code.
    patchfiles-append   patch-Revert-breaking-osxkeychain.diff

    # Fix an extraction failure with some old versions of tar.
    # https://superuser.com/questions/1219214/permissions-cannot-be-restored-for-a-tar
    extract.post_args-append \
                    -m --touch --no-overwrite-dir
}

use_configure       no

compiler.c_standard 1999

variant universal   {}

# Apple gcc needs these flags, but Apple clang breaks on them.
if {[string match *gcc-4.* ${configure.compiler}]} {
    set CFLAGS      "${configure.cppflags} ${configure.cflags} -std=gnu99 [get_canonical_archflags cc]"
} else {
    set CFLAGS      "${configure.cflags} [get_canonical_archflags cc]"
}
set LDFLAGS         "${configure.ldflags} [get_canonical_archflags ld]"

build.args          CFLAGS="${CFLAGS}" \
                    LDFLAGS="${LDFLAGS}" \
                    CC=${configure.cc} \
                    prefix=${prefix} \
                    CURLDIR=${prefix} \
                    ICONVDIR=${prefix} \
                    NO_FINK=1 \
                    NO_DARWIN_PORTS=1 \
                    NO_R_TO_GCC_LINKER=1 \
                    NO_RUST=1 \
                    NO_PERL=1 \
                    V=1

if {[variant_isset ${perl5.variant}]} {
    depends_lib-delete  port:perl${perl5.major}
    depends_run-append \
                    port:perl${perl5.major} \
                    port:p${perl5.major}-authen-sasl \
                    port:p${perl5.major}-error \
                    port:p${perl5.major}-net-smtp-ssl \
                    port:p${perl5.major}-term-readkey \
                    port:p${perl5.major}-cgi

    build.args-delete \
                    NO_PERL=1

    build.args-append \
                    PERL_PATH="${perl5.bin}" \
                    NO_PERL_CPAN_FALLBACK=1
} elseif {[variant_isset svn]} {
    pre-configure {
        return -code error "+svn does not work without +perl5_xx"
    }
}

test.run            yes
test.cmd            make
test.target         test
test.dir            ${worksrcpath}
pre-test {
    test.args  {*}${build.args}
}

destroot.target     install
pre-destroot {
    destroot.args  {*}${build.args}
    xinstall -m 0644 \
        ${worksrcpath}/contrib/subtree/git-subtree.1 ${workpath}/man1
}

set docdestroot         ${destroot}${prefix}/share/doc/git-doc
set guidir              ${prefix}/share/git-gui/lib/Git\ Gui.app/Contents
set system_gitconfig    ${prefix}/etc/gitconfig

post-destroot {
    foreach f {1 5 7} {
        xinstall -d ${destroot}${prefix}/share/man/man${f}

        if {${name} eq ${subport}} {
            # RELEASE man pages
            foreach m [glob -directory ${workpath} man${f}/*.${f}] {
                xinstall ${m} ${destroot}${prefix}/share/man/man${f}
            }
        } else {
            # DEVEL man pages
            foreach m [glob -directory ${worksrcpath} Documentation/*.${f}] {
                xinstall ${m} ${destroot}${prefix}/share/man/man${f}
            }
        }
    }

    if {![variant_isset svn]} {
        system "rm ${destroot}${prefix}/libexec/git-core/git-svn*"
    }

    xinstall -d ${docdestroot}

    fs-traverse badfile ${destroot} {
        if {[string last perllocal.pod ${badfile}] != -1} {
            ui_info "Removing ${badfile}"
            file delete ${badfile}
        }
    }

    set completions_path ${destroot}${prefix}/share/bash-completion/completions
    xinstall -d ${completions_path}
    xinstall -m 0644 ${worksrcpath}/contrib/completion/git-completion.bash \
        ${completions_path}/git

    set share_path ${destroot}${prefix}/share/${name}
    xinstall -d ${share_path}
    xinstall -m 0644 ${worksrcpath}/contrib/completion/git-prompt.sh \
        ${share_path}/git-prompt.sh

    xinstall -d ${destroot}${prefix}/libexec/git-core
    xinstall -m 0755 ${worksrcpath}/contrib/subtree/git-subtree.sh \
        ${destroot}${prefix}/libexec/git-core/git-subtree

    # Tiger doesn't build the GUI, so check for its existence before mucking with it
    if {[file exists "${destroot}${guidir}"]} {
        # The executable could be "Wish" or "Wish Shell", depending on the OS version
        set wish_executable [exec defaults read ${destroot}${guidir}/Info CFBundleExecutable]
        # The system-provided Tk is now deprecated, so use the MacPorts version if available.
        system "echo exec wish \"'${guidir}/Resources/Scripts/AppMain.tcl'\" '\"$@\"' > \
            '${destroot}${guidir}/MacOS/${wish_executable}'"
    }

    file delete -force ${share_path}/contrib
    copy ${worksrcpath}/contrib ${share_path}
}

variant pcre description {Use pcre} {
    build.args-append       LIBPCREDIR=${prefix} USE_LIBPCRE2=1
    depends_lib-append      port:pcre2
}

variant doc description {Install HTML and plaintext documentation} {
    if {${name} eq ${subport}} {
        # RELEASE
        distfiles-append    git-htmldocs-${version}${extract.suffix}
        checksums-append    git-htmldocs-${version}${extract.suffix} \
                            rmd160  103b5d7f84999fb803c222a9d2dc56b96295c379 \
                            sha256  d1142c4e28b469d297d6df6519653e92a76c952f55202fde17a72a3b03d49437 \
                            size    1713768

        patchfiles-append   patch-git-subtree.html.diff

        post-extract {
            file mkdir ${workpath}/htmldocs
            system -W ${workpath}/htmldocs "${extract.cmd} ${extract.pre_args} \
                '${distpath}/git-htmldocs-${version}${extract.suffix}' \
                ${extract.post_args}"
        }

        pre-destroot {
            xinstall -m 0644 \
                ${worksrcpath}/contrib/subtree/git-subtree.html \
                ${workpath}/htmldocs
        }

        post-destroot {
            foreach f [glob ${workpath}/htmldocs/*] {
                file delete -force "${docdestroot}/[file tail ${f}]"
                file attribute ${f} -permissions ugo+r
                copy ${f} ${docdestroot}
            }

            fs-traverse f [list ${docdestroot}/howto ${docdestroot}/technical ${docdestroot}/RelNotes] {
                if {[file isdirectory ${f}]} {
                    file attribute ${f} -permissions ugo+rx
                } else {
                    file attribute ${f} -permissions ugo+r
                }
            }
        }

    } else {
        # DEVEL
        post-destroot {
            system -W ${worksrcpath} \
                "${destroot.cmd} install-html prefix=${destroot}/${prefix}"
        }
    }
}

variant gitweb description {Install gitweb.cgi} {
    depends_run-append  port:lighttpd

    build.target-append gitweb

    post-destroot {
        xinstall -d ${destroot}${prefix}/share/${name}/gitweb
        xinstall -W ${worksrcpath}/gitweb \
            gitweb.cgi \
            ${destroot}${prefix}/share/${name}/gitweb
        xinstall -m 0444 -W ${worksrcpath}/gitweb/static \
            gitweb.css \
            gitweb.js \
            git-favicon.png \
            git-logo.png \
            ${destroot}${prefix}/share/${name}/gitweb
        xinstall -d ${docdestroot}/gitweb
        xinstall -m 0444 -W ${worksrcpath}/gitweb README INSTALL \
            ${docdestroot}/gitweb
    }
}

variant svn description {Bi-directional subversion repository support} {
    depends_run-append  port:subversion \
                        port:p${perl5.major}-libwww-perl \
                        port:p${perl5.major}-svn-simple
}

variant credential_osxkeychain description {Install git credential-osxkeychain utility from contrib} {

    post-build {
        system -W "${worksrcpath}/contrib/credential/osxkeychain" "${build.cmd} [join ${build.args}]"
    }

    post-destroot {
        xinstall -m 0755 \
            "${worksrcpath}/contrib/credential/osxkeychain/git-credential-osxkeychain" \
            "${destroot}${prefix}/libexec/git-core/"
    }

    pre-activate {
        set osxkc_config        "\[credential]\n\thelper = osxkeychain"

        # Ensure the osxkeychain credential helper is configured in the
        # system-wide gitconfig.  If the gitconfig file already exists,
        # then append the configuration to the pre-existing file only
        # if the file is currently missing the desired config.

        if { ![file exists ${system_gitconfig}] } {
            set f [open ${system_gitconfig} w 0644]
            puts ${f} ${osxkc_config}
            close ${f}
        } else {
            if { [catch {exec grep "helper = osxkeychain" ${system_gitconfig}} result] } {
                set f [open ${system_gitconfig} a]
                puts ${f} ${osxkc_config}
                close ${f}
            }
        }
    }

    notes "
Configuration to enable the osxkeychain credential helper has been added to
the system-wide gitconfig at ${system_gitconfig}. If you do not wish to use
this credential helper, you can override this setting in your own personal
git config file \(\$HOME/.gitconfig\) with e.g.

    \[credential]
            helper = some_other_credential_helper

For more information, run

    git help credentials
"
}

variant diff_highlight description {Install git diff-highlight utility from contrib} {
    post-build {
        system -W "${worksrcpath}/contrib/diff-highlight" "${build.cmd} [join ${build.args}]"
    }

    post-destroot {
        xinstall -m 0755 \
            "${worksrcpath}/contrib/diff-highlight/diff-highlight" \
            "${destroot}${prefix}/bin/"
    }
}

# Python is only needed to enable certain tools for interacting with other
# version control systems, such as git-p4 (for interfacing with Perforce).
# This variant enables git to use MacPorts' Python as it builds these
# additional tools.
variant python description {Build with Python support} {
    depends_lib-append  port:python314
    build.args-append   PYTHON_PATH="${prefix}/bin/python3.14"
}

platform darwin 11 {
    # Add additional headers for open() on 10.7
    patchfiles-append    patch-osxkeychain-10.7.diff
}

platform darwin 8 {
    # https://trac.macports.org/ticket/66480
    depends_build-append port:gmake
    build.cmd  ${prefix}/bin/gmake
    test.cmd   ${prefix}/bin/gmake

    build.args-append   NO_APPLE_COMMON_CRYPTO=1
    build.args-append   CC_LD_DYNPATH=-R
}
