An agreement has been reached between land owners and a tenant farmer facing eviction after more than 20 years.

Andrew Stoddart's tenancy at Colstoun Mains farm, near Haddington in East Lothian, was due to end on November 28.

Compensation for his eviction has been agreed with Coulston Trust following mediation, while he will also be allowed to stay on until January 2016.

Mr Stoddart believed legislation on agricultural holdings passed by the Scottish Parliament in 2003 would strengthen his security of tenure, but the Supreme Court ruled in 2013 that it breached the human rights of landlords.

Legislation to correct the defect - which prevented landlords from recovering vacant possession of their farms - has since been passed and the Colstoun Trust, which owns the farm, wants it back.

A statement issued on behalf of both parties said: "Mediation has taken place between Andrew Stoddart and the Trustees.

"Representatives of the Scottish Government were in attendance but have not accepted liability for claims already intimated to them by either Mr Stoddart or the Trustees and did not mediate on these aspects.

"Mr Stoddart and the Trustees have agreed the terms for payment of Mr Stoddart's waygoing claim. They have agreed that Mr Stoddart may continue to have the use of the house, grazing land and farm buildings at Colstoun Mains Farm, Haddington, until January 2016."

Environment Secretary Richard Lochhead welcomed the agreement.

He said: "There is no doubt that this has been an incredibly complex case with many different factors involved ranging from flawed legislation to a breakdown in relationship between landlord and tenant over many years.

"We understand that more than 300 tenant farmers were originally affected by the flawed 2003 Agricultural Holdings legislation introduced by the previous administration.

"Agreement between landlord and tenant has been reached in almost all of the cases although seven remain unresolved. I hope that the mediation process that we are facilitating will also now help in these cases.

"The Scottish Government will also continue to reflect on the circumstances of Mr Stoddart's case and I am determined that lessons are learnt so that the rights of tenant farmers are protected insofar as possible."

Green MSP Alison Johnstone said the case is "a clear sign that tenant farmers in Scotland need better legal protections".

She said: "While I am relieved to hear Andrew and his family won't be pushed into destitution by their wrongful eviction, the Scottish Government must take his case as a clear sign that they need to go further on land reform.

"Our legal system exists to protect people from injustices like this, and it is clear that at the moment the law on land ownership is not working."

David Johnstone, chairman of Scottish Land and Estates, said: "We are pleased that agreement has been reached between the Trust and Mr Stoddart and we are aware there was a willingness on the part of the landlord to deal with the issues involved.

"It is a great pity that this case was surrounded by so much ill-informed rhetoric.

"It was very regrettable that Mr Stoddart was, as were other tenants in Scotland, given false hope by defective legislation brought forward by the then-Scottish Executive many years ago.

"Both landlords and tenants continue to be affected by the consequences and we would appeal for very careful consideration of complex agricultural legislation which forms part of the current Land Reform Bill."