Force Mercurial (Hg) to add new files automatically - version-control

I'm looking for the opposite mechanism to the .ignore file, which should add all files of a given pattern automatically, for example *.tex for any LaTeX documentation project or *.def for any file that was added by OASIS (an MS Access addin for version control).

hg add with no arguments will add all files not explicitly ignored. You can also use hg commit --addremove to add all unknown files (and remove all deleted files, i.e. hg rm any which hg status lists as '!') to do this automatically when committing.

Adding files only happens when you run hg add. If you want to add all files matching a pattern, use the --include (-I) switch.

Related

Replace local file by remote file

How do I replace a local file by its latest version in the repository?
Is there also a way of replacing all local files which are conflicting with the corresponding files from the repository?
Both hg update -C and hg revert will do what you are looking for - replace a locally modified file with the clean version in the repository. Personally I prefer hg revert but hg up -C will also do the job
hg revert
Some further details from the help for hg revert
With no revision specified, revert the specified files or directories to
the contents they had in the parent of the working directory. This
restores the contents of files to an unmodified state and unschedules
adds, removes, copies, and renames. If the working directory has two
parents, you must explicitly specify a revision.
Using the -r/--rev or -d/--date options, revert the given files or
directories to their states as of a specific revision. Because revert does
not change the working directory parents, this will cause these files to
appear modified. This can be helpful to "back out" some or all of an
earlier change. See "hg backout" for a related method.
Modified files are saved with a .orig suffix before reverting. To disable
these backups, use --no-backup.
Hope that helps
Chris
svn update ?
Or delete your folder, and svn checkout...
Or try the option --force.

Prevent a directory from being pushed in mercurial

I created a new repository on my local computer, added files and committed giving me the following structure in my working copy:
-/a
-/b
I want to push my initial commit to a remote repository on Bitbucket. I however want to ignore directory b so that it doesn't get pushed. I added
syntax:
glob b/**
to my .hgignore file. The b directory gets pushed anyways.
How can I prevent this?
This cannot be done in Mercurial: you cannot push only an initial version of a file and then ignore later modifications. You can also not push part of a changeset — you either push the entire changeset or not. This way Mercurial makes sure you have a consistent snapshot of your project in all clones.
Preet is correct when he writes that the .hgignore file is a filter for untracked files. The .hgignore file is used to filter the output of commands like hg status and to filter the files added by running hg add with no further arguments.
in .hgignore add:
^b/.*
or b/* if you are using the inferior glob syntax (regex gives you much more control).
The syntax is defined on the first line, and all rules are defined in following seperate lines.
if you want globbing, the first line would be syntax: glob and if regex, the first line would be syntax: regexp (implied by default). Refer to the documentation.
Note that mercurial does not actually push empty directories, so the only way you will be able to test it is if you have a file in b/

Ignore changes to files that are already in repository

Adding files to .hgignore will prevent them from being tracked. But we have some files that we want to be in the repository - we just don't want users to ever commit changes. Is there a way to ignore changes to these files, so they won't get committed in an hg commit.
That cannot be done in Mercurial — a file is either tracked or untracked (and then optionally ignored). You should instead version a template file and then ignore the real file.
So add config.template to version control and add config to .hgignore. Ask your developers to copy the template to the real name and customize it as needed.

Mercurial: How to ignore changes to a tracked file

I have a file with database settings in my project which I have set to some defaults. The file is tracked by Mercurial and checked in. Since this file will be edited with different values various developer machines, is there a way I can tell Mercurial to ignore new changes to this file?
I tried adding the file to the .hgignore file, but since the file is tracked it isn't ignored. This is alright and good in other situations, but I am wondering if there is something I can do here?
Using a file template is definitely the best solution.
For example, if you have a database.ini file, commit a database.ini.template file and ignore database.ini in .hgignore
If you always want to ignore the file, you can add the -X option as a default for commit to your .hg/hgrc configuration file:
[defaults]
commit = -X program.conf
We wrote an extension for this called exclude. It will automatically add the -X options on the commands that support them -- so hg status and hg commit wont see the modified file. It works by reading a .hgexclude file from the root of your repository, just like the .hgignore file. You add the files that you want to exclude there:
syntax: glob
db.conf
The extension works quite well, but there is a known situation where it fails: merges and the commit that follows a merge (this is documented on the wiki). It would need to be improved so that it would save the modifications away to a temporary file and then restore them afterwards. Please get in contact if you need this feature.
There is no truly automated process, but you can try (as in this SO question) the -X option on hg commit:
% hg stat
M myfile
% hg commit -X 'myfile'
(other solutions might involve shelve or hq)
However, this is not the "right" solution. I would rather recommend versioning:
a file template
a script able to generate the final file (that you modify but can ignore altogether)
If you are using TortoiseHG, open the Settings for the repo, go to the Commit section (2nd icon down on the left) & add the file name(s) to the Auto Exclude list on the right (~ 3rd from the bottom in the list).
From https://tortoisehg.readthedocs.io/en/latest/settings.html#commit
Typically you would check in a reference copy of the file and track it then have the developers make a copy of that for local development, you wouldn't really want developers editing the source controlled file for their own environments.
If your configuration system supports it, it's even easier if you can use an override file that simply override values in the reference copy (e.g. the database connection string). That way devs only have to keep a very minimal local set of override values.
If the file is already being tracked, you can issue the Forget command to the file. If you're using TortoiseHg just right click the file during commit and select Forget. The file must also be already in the ignore list.
I had the same problem as yours, I file keeps on appearing on every commit even-though its already in the ignore list. I tried the Forget command and it did the trick.
You can try hg forget.
For more details, see the official manual about the same command.
It worked for me.
I think, something like this is closer to a correct answer to the original question Mercurial: How to ignore changes to a tracked file, rather than the others suggesting a template, etc.

What is the difference between hg forget and hg remove?

I want mercurial to remove several files from the current state of the repository. However, I want the files to exist in prior history.
How do forget and remove differ, and can they do what I want?
'hg forget' is just shorthand for 'hg remove -Af'. From the 'hg remove' help:
...and -Af can be used to remove files
from the next revision without
deleting them from the working
directory.
Bottom line: 'remove' deletes the file from your working copy on disk (unless you uses -Af) and 'forget' doesn't.
The best way to put is that hg forget is identical to hg remove except that it leaves the files behind in your working copy. The files are left behind as untracked files and can now optionally be ignored with a pattern in .hgignore.
In other words, I cannot tell if you used hg forget or hg remove when I pull from you. A file that you ran hg forget on will be deleted when I update to that changeset — just as if you had used hg remove instead.
From the documentation, you can apparently use either command to keep the file in the project history. Looks like you want remove, since it also deletes the file from the working directory.
From the Mercurial book at http://hgbook.red-bean.com/read/:
Removing a file does not affect its
history. It is important to
understand that removing a file has
only two effects. It removes the
current version of the file from the
working directory. It stops Mercurial
from tracking changes to the file,
from the time of the next commit.
Removing a file does not in any way
alter the history of the file.
The man page hg(1) says this about forget:
Mark the specified files so they will
no longer be tracked after the next
commit. This only removes files from
the current branch, not from the
entire project history, and it does
not delete them from the working
directory.
And this about remove:
Schedule the indicated files for
removal from the repository. This
only removes files from the current
branch, not from the entire project
history.
If you use "hg remove b" against a file with "A" status, which means it has been added but not commited, Mercurial will respond:
not removing b: file has been marked for add (use forget to undo)
This response is a very clear explication of the difference between remove and forget.
My understanding is that "hg forget" is for undoing an added but not committed file so that it is not tracked by version control; while "hg remove" is for taking out a committed file from version control.
This thread has a example for using hg remove against files of 7 different types of status.
A file can be tracked or not, you use hg add to track a file and
hg remove or hg forget to un-track it. Using hg remove without
flags will both delete the file and un-track it, hg forget will
simply un-track it without deleting it.