The first publication of the song was as a ballad sheet by Sanderson in Edinburgh - it seems he got it from Lady John Scott, who covered her tracks saying it was taken from a boy street singer in Edinburgh. I'm pretty sure that version has been posted here, possibly by me. A thread merge might be in order.
Nothing remotely like it is in Hogg's "Jacobite Relics" (1817-21) and he was thorough. It can't have existed before then.