简体   繁体   中英

How can I hook Mercurial to ignore differences in parts of a file?

I have a couple of repositories that I use for keeping track of scripts for various purposes in our system. For example, several of the scripts are sql scripts that are useful to pull up, modify, and execute. Very frequently, these scripts will contain a set of parameters near the top that are changed to look at a specific set of records.

Unfortunately, as soon as I use the file, it always shows up as a change in hg status. I would really like a way to indicate to Mercurial that certain parts of the file aren't interesting for purposes of detecting changes.

I'm aware of (and frequently use) the MQ, record, and shelve extensions -- but all of those require that I do something to ignore those lines. I'd really prefer just to have them ignored.

Any suggestions?

You could probably use a pre-commit and post-commit hook to do the following:

  1. On pre-commit, copy the file out to some temporary known file, then overwrite it with a pristine unmodified version
  2. Let the commit proceed
  3. On post-commit, copy the file back from its temporary location

However, this is brittle, and will require everyone that has this problem to ensure they have the same hooks installed. Remember, hooks are not committed to the repository but must be set up manually.

The recommended way of doing this is to instead commit a template file into the repository, with a different filename, and then ask Mercurial to ignore the entire actual file that you keep modifying.

Then it's a simple matter of just copying out a usable version from the template when you set up a new working folder (which I would assume happens infrequently). This step you can even automate if you're running the script automated, by a if not exists ... copy ... step somewhere.

You can not, however, ask Mercurial to automatically ignore parts of a file on each commit.

With MQ, you can:

  • move all your (local) edits into MQ-patch
  • create alias, which "unapply all patches before commit and commit" in single command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM