Can I change the username on a mercurial changeset?

后端 未结 4 1092
挽巷
挽巷 2020-11-29 18:12

I didn\'t set the username on my development computer and made a few commits. Can I retroactively change the username so it\'s clear who committed these changesets?

4条回答
  •  独厮守ぢ
    2020-11-29 18:45

    I've tried a couple of different methods (including the Convert Extension, which I found created an unrelated repository). The Mercurial wiki instructions for editing history using MQ were the ones I found most helpful. (There are of course the usual caveats about editing any publicly-known history being a bad idea, but local-changesets that only you have are OK to edit).

    I'll summarise the crucial steps here, and clarify the mechanics of changing the author. Assuming the first wrong author commit is at revision BAD (and you haven't published your changes anywhere of course), you should be able to do the following (I'll assume you're at the repository root):

    Enable MQ by adding this to $HOME/.hg/hgrc

    [extensions]
    hgext.mq=
    

    Convert the recent changesets into patches:

    $ hg qimport -r BAD:tip
    

    (They can now be found at .hg/patches)

    "Unapply" all the patches (assume they've been applied, and reverse them), to get your repository into the state of the revision before BAD:

    $ hg qpop -a
    

    If you look at your patches, you'll see that the author is encoded in a kind of comment line in all the patches:

    $ grep User .hg/patches/*
    .hg/patches/102.diff:# User Firstname Lastname 
    

    Now use your favourite search/replace tool to fix the patches (I'm using Perl here). Let's assume you want the commit name to be f.lastname@righturl.example.com:

    $ perl -pi -e 's/f\.lastname\@oops\.wrongurl\.example\.com/f.lastname\@righturl.example.com/' .hg/patches/*.diff
    

    Now check that you have successfully changed the author name, and re-apply the patches:

    $ hg qpush -a
    

    Then convert the applied patches into proper changesets:

    $ hg qfinish -a
    

    And you're done. Your repository is still listed as related, so you won't get any complaints about pushing.

提交回复
热议问题