# Template file for 'git'
pkgname=git
version=2.55.0
revision=1
build_helper=rust
hostmakedepends="asciidoc gettext perl pkg-config tk xmlto rust cargo"
makedepends="libglib-devel libcurl-devel libsecret-devel pcre2-devel tk-devel
 rust-std"
# Required by https://
depends="ca-certificates perl-Authen-SASL perl-MIME-tools perl-Net-SMTP-SSL"
checkdepends="gnupg cvs cvsps2 perl-DBD-SQLite subversion subversion-perl
 perl-Term-ReadKey perl-CGI perl-IO-Tty"
short_desc="Git Tree History Storage Tool"
maintainer="Đoàn Trần Công Danh <congdanhqx@gmail.com>"
license="GPL-2.0-only"
homepage="https://git-scm.com/"
changelog="https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/${version}.adoc"
distfiles="https://www.kernel.org/pub/software/scm/git/git-${version}.tar.xz"
checksum=457fdb04dc8728e007d4688695e6912e6f680727920f2a40bf11eacc17505357
replaces="git-perl>=0"
python_version=3

subpackages="git-cvs git-svn gitk git-gui git-all git-libsecret git-netrc
 git-scalar"

do_configure() {
	cat <<-EOF >config.mak
	prefix = /usr
	CC = $CC
	AR = $AR
	TAR = bsdtar
	CFLAGS = $CFLAGS
	LDFLAGS = $LDFLAGS
	USE_LIBPCRE2=Yes
	NO_INSTALL_HARDLINKS=Yes
	INSTALLDIRS=vendor
	perllibdir=/usr/share/perl5/vendor_perl
	PYTHON_PATH=/usr/bin/python3
	DEFAULT_TEST_TARGET = prove
	GIT_PROVE_OPTS = $makejobs --nocolor --nocount -s
	# firstword (uname -m)
	HOST_CPU = ${XBPS_TARGET_MACHINE%-musl}
	# this flag will skip shipping unneeded symlinks
	# those symlinks were kept for scripts written before 2010
	SKIP_DASHED_BUILT_INS = Yes
	GIT_TEST_UTF8_LOCALE=C.UTF-8
	NO_STRLCPY =
	NEEDS_LIBRT =
	# https://lore.kernel.org/git/xmqqr02wbtdn.fsf@gitster.g/
	CSPRNG_METHOD = getrandom
	CARGO_ARGS = --release --target ${RUST_TARGET}
	RUST_TARGET_DIR = target/${RUST_TARGET}/release
	EOF

	if [ "$XBPS_TARGET_LIBC" = musl ]; then
		cat <<-EOF >>config.mak
		ICONV_OMITS_BOM = Yes
		NO_REGEX = Yes
		EOF
	fi
}

do_build() {
	make ${makejobs} all man
	make ${makejobs} -C contrib/contacts all git-contacts.1
	make ${makejobs} -C contrib/diff-highlight all
	make ${makejobs} -C contrib/subtree all man
	make ${makejobs} -C contrib/credential/libsecret
	make ${makejobs} -C contrib/credential/netrc
}

do_check() {
	make test ${makejobs}
	GIT_TEST_DEFAULT_HASH=sha256 make test ${makejobs}
	# svn and cvs, fail intermittent with reftable
	GIT_TEST_DEFAULT_REF_FORMAT=reftable GIT_SKIP_TESTS='t9[16]??' \
		make test ${makejobs}
	make -C contrib/diff-highlight test
	make -C contrib/subtree test
	# 2 failures but passes in local run, unchecked
	# make -C contrib/credential/netrc testverbose
}

do_install() {
	make DESTDIR=${DESTDIR} install install-man
	vinstall contrib/completion/git-completion.bash 644 \
		usr/share/bash-completion/completions git
	vinstall contrib/completion/git-prompt.sh 644 usr/share/git
	make -C contrib/contacts DESTDIR=${DESTDIR} install install-man
	vbin contrib/diff-highlight/diff-highlight
	vdoc contrib/diff-highlight/README diff-highlight
	vinstall contrib/git-jump/git-jump 755 usr/libexec/git-core
	vdoc contrib/git-jump/README git-jump
	make -C contrib/subtree DESTDIR=${DESTDIR} install install-man
	vinstall contrib/credential/libsecret/git-credential-libsecret 755 \
		usr/libexec/git-core
	make -C contrib/credential/netrc DESTDIR=${DESTDIR} install
}

git-scalar_package() {
	depends="git"
	short_desc="Tool for managing large Git repositories"
	pkg_install() {
		vmove usr/bin/scalar
		vmove usr/libexec/git-core/scalar
		vmove usr/share/man/man1/scalar.1
	}
}

git-cvs_package() {
	depends="${sourcepkg}>=${version}_${revision} cvs cvsps2 perl-DBD-SQLite"
	short_desc+=" - CVS support"
	pkg_install() {
		vmove usr/bin/git-cvsserver
		vmove usr/libexec/git-core/git-cvsexportcommit
		vmove usr/libexec/git-core/git-cvsimport
		vmove usr/libexec/git-core/git-cvsserver
		vmove usr/share/man/man1/git-cvsexportcommit.1
		vmove usr/share/man/man1/git-cvsimport.1
		vmove usr/share/man/man1/git-cvsserver.1
		vmove usr/share/man/man7/gitcvs-migration.7
	}
}

git-svn_package() {
	depends="${sourcepkg}>=${version}_${revision} subversion-perl perl-Term-ReadKey"
	short_desc+=" - Subversion support"
	pkg_install() {
		vmove "usr/share/perl5/vendor_perl/Git/SVN*"
		vmove usr/libexec/git-core/git-svn
		vmove usr/share/man/man1/git-svn.1
	}
}

gitk_package() {
	depends="${sourcepkg}>=${version}_${revision} tk"
	short_desc="Git repository browser"
	license="GPL-2.0-or-later"
	pkg_install() {
		vmove usr/bin/gitk
		vmove usr/share/gitk
		vmove usr/share/man/man1/gitk.1
	}
}

git-gui_package() {
	depends="${sourcepkg}>=${version}_${revision} tk"
	short_desc+=" - GUI tool"
	license="GPL-2.0-or-later"
	pkg_install() {
		vmove usr/libexec/git-core/git-gui--askpass
		vmove usr/libexec/git-core/git-gui
		vmove usr/libexec/git-core/git-citool
		vmove usr/share/man/man1/git-gui.1
		vmove usr/share/man/man1/git-citool.1
		vmove usr/share/git-gui
	}
}

git-all_package() {
	metapackage=yes
	depends="${subpackages/git-all/}"
	short_desc+=" - meta-package for complete Git installation"
}

git-libsecret_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - libsecret credential helper"
	license="GPL-2.0-or-later"
	pkg_install() {
		vmove usr/libexec/git-core/git-credential-libsecret
	}
}

git-netrc_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - netrc credential helper"
	pkg_install() {
		vmove usr/libexec/git-core/git-credential-netrc
	}
}
