# -*- 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           github          1.0
PortGroup           legacysupport   1.1
PortGroup           perl5           1.0
PortGroup           conflicts_build 1.0

legacysupport.newest_darwin_requires_legacy 11

github.setup        Libre-WD-40 git 2.55.0 v -wd40
github.tarball_from archive
name                libre-wd-40-git
revision            0

description         A fork of Git with the Rust dependency removed

long_description    ${name} is a fork of Git, the fast, scalable, distributed \
                    open source version control system, that strips out \
                    Git's optional Rust-based components so that it can \
                    always be built with a C toolchain alone.

maintainers         {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

conflicts           git git-devel

patch.pre_args-replace  -p0 -p1

checksums           rmd160  19f33a34656115befea77f7770ec2d780d5fef3e \
                    sha256  afa37420e408677cb029d14422d082aeda4beb754923bddae4aee0c42e1500e0 \
                    size    12188087

depends_build-append \
                    port:asciidoc \
                    port:xmlto \
                    port:gettext

build.target        all man html

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

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} {
    patchfiles-append   patch-git-compat-util-no-builtin-add-overflow.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
}

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   {}

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}

        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} {
    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
}
