Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

17.1 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

require "spec_helper"

RSpec.describe Repository::Git do
  let(:encoding) { "UTF-8" }
  let(:instance) { build(:repository_git, path_encoding: encoding) }
  let(:adapter) { instance.scm }
  let(:config) { {} }
  let(:enabled_scm) { %w[git] }

  before do
    allow(Setting).to receive(:enabled_scm).and_return(enabled_scm)
    allow(instance).to receive(:scm).and_return(adapter)
    allow(adapter.class).to receive(:config).and_return(config)
  end

  describe "when disabled" do
    let(:enabled_scm) { [] }

    it "does not allow creating a repository" do
      expect { instance.save! }.to raise_error ActiveRecord::RecordInvalid
    end
  end

  describe "available types" do
    it "allow local by default" do
      expect(instance.class.available_types).to eq([:local])
    end

    context "with disabled types" do
      let(:config) { { disabled_types: %i[local managed] } }

      it "does not have any types" do
        expect(instance.class.available_types).to be_empty
      end
    end

    context "with mixed disabled types" do
      let(:config) { { disabled_types: [:local, "managed"] } }

      it "does not have any types" do
        expect(instance.class.available_types).to be_empty
      end
    end
  end

  describe "managed git" do
    let(:managed_path) { "/tmp/managed_git" }

    it "is not manageable unless configured explicitly" do
      expect(instance.manageable?).to be false
    end

    context "with managed config" do
      let(:config) { { manages: managed_path } }
      let(:project) { build(:project) }
      let(:identifier) { project.identifier + ".git" }

      it "is manageable" do
        expect(instance.manageable?).to be true
        expect(instance.class.available_types).to eq(%i[local managed])
      end

      context "with disabled managed typed" do
        let(:config) { { disabled_types: [:managed] } }

        it "is no longer manageable" do
          expect(instance.class.available_types).to eq([:local])
          expect(instance.manageable?).to be false
        end
      end

      context "with string disabled types",
              with_config: { "scm" => { "git" => { "disabled_types" => %w[managed local] } } } do
        before do
          allow(adapter.class).to receive(:config).and_call_original
        end

        it "is no longer manageable" do
          expect(instance.class.available_types).to eq([])
          expect(instance.class.disabled_types).to eq(%i[managed local])
          expect(instance.manageable?).to be false
        end
      end

      context "and associated project" do
        before do
          instance.project = project
        end

        it "outputs valid managed paths" do
          expect(instance.repository_identifier).to eq(identifier)
          path = File.join(managed_path, identifier)
          expect(instance.managed_repository_path).to eq(path)
          expect(instance.managed_repository_url).to eq(path)
        end
      end

      context "and associated project with parent" do
        let(:parent) { build(:project) }
        let(:project) { build(:project, parent:) }

        before do
          instance.project = project
        end

        it "outputs the correct hierarchy path" do
          expect(instance.managed_repository_path)
            .to eq(File.join(managed_path, identifier))
        end
      end
    end
  end

  describe "URL validation" do
    let(:instance) { build(:repository_git, url:) }

    shared_examples "repository url is valid" do
      it "is valid" do
        expect(instance).to be_valid
      end
    end

    context "file:// URLs" do
      let(:url) { "file:///foo/bar" }

      it_behaves_like "repository url is valid"
    end

    context "absolute paths" do
      let(:url) { "file:///foo/bar" }

      it_behaves_like "repository url is valid"
    end

    context "https URLs" do
      let(:url) { "https://localhost/git/foo" }

      it_behaves_like "repository url is valid"
    end

    context "SSH URLs" do
      let(:url) { "ssh://-oProxyCommand=echo/wat" }

      it "is invalid" do
        expect(instance).not_to be_valid
        expect(instance.errors.full_messages).to eq(["URL must not be an SSH url."])
      end
    end
  end

  describe "with an actual repository" do
    with_git_repository do |repo_dir|
      let(:url) { repo_dir }
      let(:instance) do
        create(:repository_git,
               path_encoding: encoding,
               url:,
               root_url: url)
      end

      before do
        instance.fetch_changesets
        instance.reload
      end

      it "is available" do
        expect(instance.scm).to be_available
      end

      describe "#entries" do
        let(:entries) { instance.entries }

        it "lists 10 entries" do
          expect(entries.size).to eq 10
        end

        describe "with limit: 5" do
          let(:directories) { entries.select { |e| e.kind == "dir" } }
          let(:files) { entries.select { |e| e.kind == "file" } }

          let(:limited_entries) { instance.entries limit: 5 }

          before do
            expect(directories.size).to eq 3
          end

          it "lists 5 entries only, directories first" do
            expected_entries = (directories + files.take(2)).map(&:path)

            expect(limited_entries.map(&:path)).to eq expected_entries
          end

          it "indicates 5 omitted entries" do
            expect(limited_entries.truncated).to eq 5
          end
        end
      end

      it "fetches changesets from scratch" do
        expect(instance.changesets.count).to eq(22)
        # This test fails on macs since they count file changes to be 33, *nix system count 34
        expect(instance.file_changes.count).to be_between(33, 34)

        commit = instance.changesets.reorder(Arel.sql("committed_on ASC")).first
        expect(commit.comments).to eq("Initial import.\nThe repository contains 3 files.")
        expect(commit.committer).to eq("jsmith <[email protected]>")
        # assert_equal User.find_by_login('jsmith'), commit.user
        # TODO: add a commit with commit time <> author time to the test repository
        expect(commit.committed_on).to eq("2007-12-14 09:22:52")
        expect(commit.commit_date).to eq("2007-12-14".to_date)
        expect(commit.revision).to eq("7234cb2750b63f47bff735edc50a1c0a433c2518")
        expect(commit.scmid).to eq("7234cb2750b63f47bff735edc50a1c0a433c2518")
        expect(commit.file_changes.count).to eq(3)

        change = commit.file_changes.min_by(&:path)
        expect(change.path).to eq("README")
        expect(change.action).to eq("A")
      end

      it "fetches changesets incremental" do
        # Remove the 3 latest changesets
        instance.changesets.order(Arel.sql("committed_on DESC")).limit(8).each(&:destroy)
        instance.reload
        expect(instance.changesets.count).to eq(14)

        rev_a_commit = instance.changesets.order(Arel.sql("committed_on DESC")).first
        expect(rev_a_commit.revision).to eq("ed5bb786bbda2dee66a2d50faf51429dbc043a7b")
        expect(rev_a_commit.scmid).to eq("ed5bb786bbda2dee66a2d50faf51429dbc043a7b")
        # Mon Jul 5 22:34:26 2010 +0200
        committed_on = Time.gm(2010, 9, 18, 19, 59, 46)
        expect(rev_a_commit.committed_on).to eq(committed_on)
        expect(instance.latest_changeset.committed_on).to eq(committed_on)

        instance.fetch_changesets
        expect(instance.changesets.count).to eq(22)
      end

      describe ".latest_changesets" do
        it "fetches changesets with limits" do
          changesets = instance.latest_changesets("", nil, 2)
          expect(changesets.size).to eq(2)
        end

        it "fetches changesets with paths" do
          changesets = instance.latest_changesets("images", nil)
          expect(changesets.map(&:revision))
            .to eq(["deff712f05a90d96edbd70facc47d944be5897e3",
                    "899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])

          changesets = instance.latest_changesets("README", nil)
          expect(changesets.map(&:revision))
            .to eq(["32ae898b720c2f7eec2723d5bdd558b4cb2d3ddf",
                    "4a07fe31bffcf2888791f3e6cbc9c4545cefe3e8",
                    "713f4944648826f558cf548222f813dabe7cbb04",
                    "61b685fbe55ab05b5ac68402d5720c1a6ac973d1",
                    "899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])
        end

        it "fetches changesets with path, revision and limit" do
          changesets = instance.latest_changesets("images", "899a15dba")
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])

          changesets = instance.latest_changesets("images", "899a15dba", 1)
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9"])

          changesets = instance.latest_changesets("README", "899a15dba")
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])

          changesets = instance.latest_changesets("README", "899a15dba", 1)
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9"])
        end

        it "fetches changesets with tag" do
          changesets = instance.latest_changesets("images", "tag01.annotated")
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])

          changesets = instance.latest_changesets("README", "899a15dba", 1)
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9"])

          changesets = instance.latest_changesets("README", "tag01.annotated")
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])

          changesets = instance.latest_changesets("README", "tag01.annotated", 1)
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9"])
        end

        it "fetches changesets with path, branch, and limit" do
          changesets = instance.latest_changesets("images", "test_branch")
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])

          changesets = instance.latest_changesets("images", "test_branch", 1)
          expect(changesets.map(&:revision))
            .to eq(["899a15dba03a3b350b89c3f537e4bbe02a03cdc9"])

          changesets = instance.latest_changesets("README", "test_branch")
          expect(changesets.map(&:revision))
            .to eq(["713f4944648826f558cf548222f813dabe7cbb04",
                    "61b685fbe55ab05b5ac68402d5720c1a6ac973d1",
                    "899a15dba03a3b350b89c3f537e4bbe02a03cdc9",
                    "7234cb2750b63f47bff735edc50a1c0a433c2518"])

          changesets = instance.latest_changesets("README", "test_branch", 2)
          expect(changesets.map(&:revision))
            .to eq(["713f4944648826f558cf548222f813dabe7cbb04",
                    "61b685fbe55ab05b5ac68402d5720c1a6ac973d1"])
        end
      end

      it "finds changeset by name" do
        ["7234cb2750b63f47bff735edc50a1c0a433c2518", "7234cb2750b"].each do |r|
          expect(instance.find_changeset_by_name(r).revision)
            .to eq("7234cb2750b63f47bff735edc50a1c0a433c2518")
        end
      end

      it "finds changeset by empty name" do
        ["", " ", nil].each do |r|
          expect(instance.find_changeset_by_name(r)).to be_nil
        end
      end

      it "assigns scmid to identifier" do
        c = instance.changesets.where(revision: "7234cb2750b63f47bff735edc50a1c0a433c2518").first
        expect(c.scmid).to eq(c.identifier)
      end

      it "formats identifier" do
        c = instance.changesets.where(revision: "7234cb2750b63f47bff735edc50a1c0a433c2518").first
        expect(c.format_identifier).to eq("7234cb27")
      end

      it "finds previous changeset" do
        %w|1ca7f5ed374f3cb31a93ae5215c2e25cc6ec5127 1ca7f5ed|.each do |r1|
          changeset = instance.find_changeset_by_name(r1)
          %w|64f1f3e89ad1cb57976ff0ad99a107012ba3481d 64f1f3e89ad1|.each do |r2|
            expect(instance.find_changeset_by_name(r2)).to eq(changeset.previous)
          end
        end
      end

      it "returns nil when no previous changeset" do
        %w|7234cb2750b63f47bff735edc50a1c0a433c2518 7234cb2|.each do |r1|
          changeset = instance.find_changeset_by_name(r1)
          expect(changeset.previous).to be_nil
        end
      end

      it "finds next changeset" do
        %w|64f1f3e89ad1cb57976ff0ad99a107012ba3481d 64f1f3e89ad1|.each do |r2|
          changeset = instance.find_changeset_by_name(r2)
          %w|1ca7f5ed374f3cb31a93ae5215c2e25cc6ec5127 1ca7f5ed|.each do |r1|
            expect(instance.find_changeset_by_name(r1)).to eq(changeset.next)
          end
        end
      end

      it "nexts nil" do
        %w|71e5c1d3dca6304805b143b9d0e6695fb3895ea4 71e5c1d3|.each do |r1|
          changeset = instance.find_changeset_by_name(r1)
          expect(changeset.next).to be_nil
        end
      end

      context "with an admin browsing activity" do
        let(:user) { create(:admin) }
        let(:project) { create(:project) }

        def find_events(user, options = {})
          options[:scope] = ["changesets"]
          fetcher = Activities::Fetcher.new(user, options)
          fetcher.events(from: 30.days.ago, to: 1.day.from_now)
        end

        it "activitieses" do
          Changeset.create(repository: instance,
                           committed_on: Time.now,
                           revision: "abc7234cb2750b63f47bff735edc50a1c0a433c2",
                           scmid: "abc7234cb2750b63f47bff735edc50a1c0a433c2",
                           comments: "test")

          event = find_events(user).first
          assert event.event_title.include?("abc7234c:")
          assert event.event_path =~ /\?rev=abc7234cb2750b63f47bff735edc50a1c0a433c2$/
        end
      end

      describe "encoding" do
        let(:felix_hex) { "Felix Sch\xC3\xA4fer" }

        it "displays UTF-8" do
          c = instance.changesets.where(revision: "ed5bb786bbda2dee66a2d50faf51429dbc043a7b").first
          expect(c.committer).to eq("#{felix_hex} <[email protected]>")
          expect(c.committer).to eq("Felix Schäfer <[email protected]>")
        end

        context "with latin-1 encoding" do
          let (:encoding) { "ISO-8859-1" }
          let (:char1_hex) { (+"\xc3\x9c").force_encoding("UTF-8") }

          it "latests changesets latin 1 dir" do
            instance.fetch_changesets
            instance.reload
            changesets = instance.latest_changesets(
              "latin-1-dir/test-#{char1_hex}-subdir", "1ca7f5ed"
            )
            expect(changesets.map(&:revision))
              .to eq(["1ca7f5ed374f3cb31a93ae5215c2e25cc6ec5127"])
          end

          it "browses changesets" do
            changesets = instance.latest_changesets(
              "latin-1-dir/test-#{char1_hex}-2.txt", "64f1f3e89"
            )
            expect(changesets.map(&:revision))
              .to eq(["64f1f3e89ad1cb57976ff0ad99a107012ba3481d",
                      "4fc55c43bf3d3dc2efb66145365ddc17639ce81e"])

            changesets = instance.latest_changesets(
              "latin-1-dir/test-#{char1_hex}-2.txt", "64f1f3e89", 1
            )
            expect(changesets.map(&:revision))
              .to eq(["64f1f3e89ad1cb57976ff0ad99a107012ba3481d"])
          end
        end
      end

      it_behaves_like "is a countable repository" do
        let(:repository) { instance }
      end
    end
  end

  it_behaves_like "repository can be relocated", :git
end