This is a current and controversial subject and it is one about who should have rights over the territory. Geographically the area is part of mainland Spain but then a certain treaty sometime in 1700 gave this rock to Britain. If this went to an ordinary court the judge would have to take into account the circumstances at the time and whether international law at the time is binding now?
At the end of the day the debate should be about whether the people who live there want to be independent or whether they wish to be part of something bigger? The best solution, in my opinion, is to allow Gibraltar to be independent and for the two EU partners to have some protection rights? It is ridiculous that Spain and the UK are part of Merkels United States of Europe yet they are in dispute. Britain is behaving in an imperialist way and Spain needs to tone the hostile gestures down a bit?