CREATE OR REPLACE FUNCTION git."AccessType" (
        gituserid integer,
        gitrepo character varying
)
RETURNS git.gitrepoaccesstype
LANGUAGE plpgsql
AS $code$
DECLARE
        selectedrepo git.gitrepoaccesstype;
BEGIN

		IF gitrepo LIKE '/%'
		THEN gitrepo := SUBSTRING(gitrepo,1,LENGTH(gitrepo)-1);
		END IF;
		/* user.repo */
        SELECT MAX("accessType")
        INTO selectedrepo
        FROM git.perms
        JOIN git.repos ON "repoID"=repos."ID"
        WHERE repos.path=gitrepo AND "userID"=gituserid;
		
        IF selectedrepo != NULL
        THEN RETURN selectedrepo;
        END IF;
		RAISE WARNING 'Le repo na pas marche 2';
		/* groupe.repo */
        SELECT MAX(perms."accessType")
        INTO selectedrepo
        FROM git.perms
        JOIN git.repos ON "repoID"=repos."ID"
		JOIN git.appartenances ON appartenances."groupeID"=-perms."userID"
        WHERE repos.path=gitrepo AND appartenances."utilisateurID"=gituserid;

		
        IF selectedrepo != NULL
        THEN RETURN selectedrepo;
        END IF;
		RAISE WARNING 'Le repo na pas marche 3';
		
		/* user.repoGroup */
        SELECT MAX("accessType")
        INTO selectedrepo
        FROM git.perms
        JOIN git.repos ON "repoID"=-repos."groupeID"
        WHERE repos.path=gitrepo AND "userID"=gituserid;
		
        IF selectedrepo != NULL
        THEN RETURN selectedrepo;
        END IF;
		RAISE WARNING 'Le repo na pas marche 4';
		/* groupe.repoGroupe */
        SELECT MAX("accessType")
        INTO selectedrepo
        FROM git.perms
		JOIN git.appartenances ON appartenances."groupeID"=-perms."userID"
        JOIN git.repos ON "repoID"=-repos."groupeID"
        WHERE repos.path=gitrepo AND "userID"=gituserid;
		
        RETURN selectedRepo;
		
END
$code$
