# -*- 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           legacysupport   1.1
PortGroup           perl5           1.0
PortGroup           conflicts_build 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.51.1
revision            0

description         A 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.51.1 v
    github.tarball_from archive
    revision            0

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

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

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

default_variants    +doc +pcre +credential_osxkeychain +diff_highlight

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  87a0ace578cdaf934e0e5a27f25a1e5a790035b2 \
                    sha256  a83fd9ffaed7eee679ed92ceb06f75b4615ebf66d3ac4fbdbfbc9567dc533f4a \
                    size    7865216 \
                    git-manpages-${version}${extract.suffix} \
                    rmd160  3b2a8f34efbfb397fdd84848dcf7020a04f7729e \
                    sha256  07a3fd7d3f9c305643b60d7899807d9c0474dc96f7d2a73c7887f327c258f22e \
                    size    602552

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

    conflicts       git-devel

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

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

    checksums       ${distname}${extract.suffix} \
                    rmd160  e449e5374c77ed3aee1eafdf859acb63f5fe2370 \
                    sha256  586fc2c8ba466c1f2122d44047e5fb575a5c0a7620840d3ab5f6943fc281c8c4 \
                    size    11761560

    depends_build-append \
                    port:asciidoc \
                    port:xmlto

    build.target    all man html

    conflicts       git

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

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:zlib \
                    port:expat \
                    port:gettext-runtime \
                    port:libiconv

depends_run-append  path:bin/rsync:rsync

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

# 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
if {${os.platform} eq "darwin" && ${os.major} < 12} {
    patchfiles-append   patch-disable-enhanced-basic-regex.diff
}

# Fix an extraction failure with some old versions of tar.
# https://superuser.com/questions/1219214/permissions-cannot-be-restored-for-a-tar
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    extract.post_args-append \
                    -m --touch --no-overwrite-dir
}

use_configure       no

compiler.c_standard 1999

variant universal   {}

set CFLAGS          "${configure.cflags} -std=gnu99 [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_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  89458fc6b9bc45e92ef0dd16664a1d986eb919eb \
                            sha256  524122af16a2c816dc6298f0cea1b761a840d76977e74de2bfffcdbdabc9dc0d \
                            size    1629412

        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" "make [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" "make [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:python312
    build.args-append   PYTHON_PATH="${prefix}/bin/python3.12"
}

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-apple
    build.cmd  ${prefix}/bin/gmake-apple

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

