Needed to add a bunch of files to my (mercurial) hg repo and realized that a command I use all the time in subversion (svn) repos.

hg st | grep ? | sed ‘s/? *//’ | xargs hg add

Break it down for ya.  Remember the “|” unix character takes the results of stdout and pipes that as the inputs to the next chain in the command.  For the purposes of explanation, assume I were to create a file called testfile.js in the “js” directory in my repo directory.

1. hg st – this displays the files in an hg repo that are currently not versioned in the repo.  Results are sent to stdout.

Results:

? js/testfile.js

2. grep ? – This looks at each line and returns it valid if there is a ? in the string.

Results:

? js/testfile.js

3. sed ‘s/? *//’ – sed is an awesome text tool for performing operations on text,  in this case a executing a regex on each line that removes that ? from the text.  This is important because the next command does not recognize “?” as in the filename.

Results:

js/testfile.js

4. xargs hg add – Finally, we execute the command on the sanitized string.

A js/testfile.js

Enjoy!