# File lib/librarian/source/git.rb, line 76
      def cache!
        repository_cached? and return or repository_cached!

        unless repository.git?
          repository.path.rmtree if repository.path.exist?
          repository.path.mkpath
          repository.clone!(uri)
          raise Error, "failed to clone #{uri}" unless repository.git?
        end

        # Probably unnecessary: nobody should be writing to our cache but us.
        # Just a precaution.
        repository_clean_once!

        unless sha
          repository_update_once!
          self.sha = fetch_sha_memo
        end

        unless repository.checked_out?(sha)
          repository_update_once! unless repository.has_commit?(sha)
          repository.checkout!(sha)
          # Probably unnecessary: if git fails to checkout, it should exit
          # nonzero, and we should expect Librarian::Posix::CommandFailure.
          raise Error, "failed to checkout #{sha}" unless repository.checked_out?(sha)
        end
      end