1

Is there a way to use the hg commit command only when filenames are supplied?

This should work: hg commit file1 file2

This should not: hg commit

I don't want people to mistakenly commit files that aren't supposed to be commited.

Subbeh
  • 886
  • 2
  • 7
  • 14
  • I looked into it, but I don't think you can check the parameters you give to the hg command – Subbeh Dec 11 '12 at 14:49

3 Answers3

4

I'm not sure whether this is possible but it sounds like something is wrong with your workflow and/or knowledge of Mercurial so I am going to answer with how this problem is usually managed in Mercurial.

What do you mean when you say "commit files that aren't supposed to be committed"? I can get one of two meanings for that phrase:

  1. People add files to the repository that shouldn't be added
  2. People change files that shouldn't be changed and then commit them

If it's the first then you should either move the files out of the working copy or you should add them to the .hgignore file. This file is used to specify files (or file pattens such as *.obj) which should ignored by commands such as hg status and hg commit --addremove.

If it is the second then you and your team should review the changes that they have made before they commit them. They should use hg status to check which files have been changed and then use hg diff if they see anything that they might not have intended to commit. This is easier if you get into the habit of committing more frequently by only fixing one problem at a time or by breaking new features down into stages and commit after each stage.

Another possible problem is that you have a configuration file which would frequently be changed by the developer but shouldn't be committed to the repository. The typical solution to this problem is that you have a template configuration file in the repository that each person needs to copy to the correct name when they clone the repository.

Steve Kaye
  • 6,262
  • 2
  • 23
  • 27
  • Thanks for your answer Steve. The reason I want to commit only certain files, is that I want to push only those files to the central repository. The files that are not ready yet, or not part of the change set need to stay behind, if that makes sense... – Subbeh Dec 11 '12 at 14:48
  • 1
    I think that the answer referring to the `shelve` and `record` extensions is more what you're looking for then. I'd use the `shelve` extension to stash the changes that you don't want to commit before you do the commit. Either way, I think that it would introduce a lot of hassle into your workflow to force names to be specified on the commit command line. – Steve Kaye Dec 12 '12 at 07:05
2

If your working directory contains more than one specific change (the only reason I can think to commit only specific files), then it's possible that a single file may contain several changes - some you want to commit and some you don't.

Rather than limiting which files should be checked in, maybe you should consider using something like the Record extension or Shelve. Both allow you to specify which specific changes get committed, but in slightly different ways (I think Record would probably suit your need).

Personally however I would consider changing your workflow to make small changes and commit regularly. Your question suggests that you are trying to use Mercurial in an almost file-based way (à la CVS (or Subversion?)), whereas Mercurial is changeset-based, treating the state of the repository as a whole.

icabod
  • 6,992
  • 25
  • 41
  • Thanks, I think the record extension is going to help me do the trick. You're right about the workflow though, I'm using Mercurial in non standard way as I use it to keep our DTAP street in line containing Unix shell scripts. – Subbeh Dec 12 '12 at 11:55
2

It's pretty simple, actually. You can do it by defining commit as an alias to itself, but with an explicit argument. In the [alias] section of your hgrc file, add the following:

commit = commit $1

This can still handle multiple arguments: They are simply appended to the end of the alias expansion. But if you try to commit without an argument, you'll get an error similar to this:

abort: $1: No such file or directory

With a more elaborate alias you could give your own error message.

alexis
  • 48,685
  • 16
  • 101
  • 161