name: Linux

on:
  push:
    branches:
      - master
  pull_request:

env:
  CARGO_INCREMENTAL: 0
  RUST_BACKTRACE: short

jobs:
  build-git:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        # Use a tag from https://github.com/git/git/tags
        # Make sure to update `git-version` in the `run-tests` step as well.
        git-version: ["v2.24.3", "v2.29.2", "v2.31.1", "v2.33.1"]

    steps:
      - uses: actions/checkout@v3
        with:
          repository: git/git
          ref: ${{ matrix.git-version }}

      - uses: actions/cache@v3
        id: cache-git-build
        with:
          key: ${{ runner.os }}-git-${{ matrix.git-version }}
          path: |
            git
            git-*

      - name: Build Git ${{ matrix.git-version }}
        if: steps.cache-git-build.outputs.cache-hit != 'true'
        run: |
          sudo apt-get update --fix-missing
          # List of dependencies from https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
          sudo apt-get install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
          make

      - name: Package Git
        run: tar -czf git.tar.gz git git-*

      - name: "Upload artifact: git"
        uses: actions/upload-artifact@v2
        with:
          name: git-${{ matrix.git-version }}
          path: git.tar.gz
          if-no-files-found: error

  run-tests:
    runs-on: ubuntu-latest
    needs: build-git

    strategy:
      matrix:
        git-version: ["v2.24.3", "v2.29.2", "v2.31.1", "v2.33.1"]

    steps:
      - uses: actions/checkout@v3
      - name: "Download artifact: git"
        uses: actions/download-artifact@v2
        with:
          name: git-${{ matrix.git-version }}

      - name: "Unpack artifact: git"
        run: tar -xf git.tar.gz

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: 1.56.0

      - name: Cache dependencies
        uses: Swatinem/rust-cache@842ef286fff290e445b90b4002cc9807c3669641

      - name: Compile
        run: cargo build --benches --tests

      - name: Run Rust tests (all features)
        timeout-minutes: 10
        run: |
          export TEST_GIT="$PWD"/git
          export TEST_GIT_EXEC_PATH=$(dirname "$TEST_GIT")
          cargo test --all-features

      - name: Run Rust tests (no default features)
        timeout-minutes: 10
        run: |
          export TEST_GIT="$PWD"/git
          export TEST_GIT_EXEC_PATH=$(dirname "$TEST_GIT")
          cargo test --no-default-features
