exclude: split basename matching code into a separate function