Arch Linux Git – How to Modify PKGBUILD for Shallow Clone

arch linuxgit

The other day I tried installing opencv-git from the AUR with makepkg on Arch Linux. Of course it pulls from the git repository as the name indicates. This pulls 1Gb. I am reading about making a shallow clone with git. When I look at the PKGBUILD file, using grep git PKGBUILD, I see:

pkgname="opencv-git"
makedepends=('git' 'cmake' 'python2-numpy' 'mesa' 'eigen2')
provides=("${pkgname%-git}")
conflicts=("${pkgname%-git}")
source=("${pkgname%-git}::git+http://github.com/Itseez/opencv.git"
    cd "${srcdir}/${pkgname%-git}"
    git describe --long | sed -r 's/([^-]*-g)/r\1/;s/-/./g'
    cd "${srcdir}/${pkgname%-git}"
    cd "${srcdir}/${pkgname%-git}"
    cd "${srcdir}/${pkgname%-git}"
    install -Dm644 "LICENSE" "${pkgdir}/usr/share/licenses/${pkgname%-git}/LICENSE"

Is there a way to modify the recipe or the makepkg command to pull only a shallow clone (the latest version of the source is what I want) and not the full repository to save space and bandwidth? Reading man 5 PKGBUILD doesn't provide the insight I'm looking for. Also looked quickly through the makepkg and pacman manpages – can't seem to find how to do that.

Best Answer

This can be done by using a custom dlagent. I do not really understand Arch packaging or how the dlagents work, so I only have a hack answer, but it gets the job done.

The idea is to modify the PKGBUILD to use a custom download agent. I modified the source

"${pkgname%-git}::git+http://github.com/Itseez/opencv.git"

into

"${pkgname%-git}::mygit://opencv.git"

and then defined a new dlagent called mygit which does a shallow clone. I did this by adding to the DLAGENTS array in /etc/makepkg.conf the following dlagent:

'mygit::/usr/bin/git clone --depth 1 http://github.com/Itseez/opencv.git'

My guess is you could probably define this download agent somewhere else, but I do not know how. Also notice that the repository that is being cloned is hard coded into the command. Again, this can probably be avoided. Finally, the download location is not what the PKGBUILD expects. To work around this, I simply move the repository after downloading it. I do this by adding

mv "${srcdir}/../mygit:/opencv.git" "${srcdir}/../${pkgname%-git}"

at the beginning of the pkgver function.

I think the cleaner solution would be to figure out what the git+http dlagent is doing and redfine that temporarily. This should avoid all the hack aspects of the solution.

Related Question