ignoring folders in mercurial - regex

Caveat:
I try all the posibilities listed here: How can I ignore everything under a folder in Mercurial.
None works as I hope.
I want to ignore every thing under the folder test. But not ignore srcProject\test\TestManager
I try
syntax: glob
test/**
And it ignores test and srcProject\test\TestManager
With:
syntax: regexp
^/test/
It's the same thing.
Also with:
syntax: regexp
test\\*
I have install TortoiseHG 0.4rc2 with Mercurial-626cb86a6523+tortoisehg, Python-2.5.1, PyGTK-2.10.6, GTK-2.10.11 in Windows

Try it without the slash after the caret in the regexp version.
^test/
Here's a test:
~$ mkdir hg-folder-ignore
~$ cd hg-folder-ignore
~/hg-folder-ignore$ echo '^test/' > .hgignore
~/hg-folder-ignore$ hg init
~/hg-folder-ignore$ mkdir test
~/hg-folder-ignore$ touch test/ignoreme
~/hg-folder-ignore$ mkdir -p srcProject/test/TestManager
~/hg-folder-ignore$ touch srcProject/test/TestManager/dont-ignore
~/hg-folder-ignore$ hg stat
? .hgignore
? srcProject/test/TestManager/dont-ignore
Notice that ignoreme isn't showing up and dont-ignore is.

Both cases worked for me (on linux and windows):
syntax: regexp
^backup/ #root folder
nbproject/ #any folder
or
syntax: glob
./backup/* #root folder
nbproject/* #any folder
However, it wasn't before I added a link to .hgignore file to .hgrc file in my repo:
[ui]
ignore = .hg/.hgignore
Also worth mentioning that mercurial ignores files that it is not currently tracking, which are those added before you configured it to ignore them. So, don't be put off by hg status saying some filed are M (modified) or ! (missing) in the folders that you have just added to the ignore list!

You can use zero-width negative look-ahead and look-behind assertions to specify that you want to ignore test only when it's not preceded by srcProject and not followed by TestManager:
syntax: regexp
(?<!srcProject\\)test\\(?!TestManager)
Mercurial uses Python regular expressions, so you can find more info on zero-width assertions in the Python docs: https://docs.python.org/library/re.html#regular-expression-syntax

Create .hgignore file under root directory of the repository
Now add the following contents in the file .
syntax: glob
bin/**
*.DS_Store
This will remove the bin directory and all the *.DS_Store files from the repository

Related

Using regular expression in lftp to ignore some strings from file name

Get specific file with name like abc_yyyymmdd_hhmmss.csv from directory using mget.
Example files in a folder:
abc_20221202_145911.csv
abc_20221202_145921.csv
abc_20221202_145941.csv
abc_20181202_145941.csv
But, I want to ignore hhmmss part. I want to get all files with abc_20221202_*.csv
How to include * in mget.
My code below:
File=abc_
Date=20221202
Filename=$File$Date"_*".csv
// Assume I have sftp connection established and I am in directory //where files with above naming convention are present. As I can //download the file when hardcoding exact file name during testing
conn=`lftp $protocol://$user:$password#$sftp_server -p $port <<EOF>/error.log
cd $path
mget $Filename
EOF`
The script is able to find the file but not able to retrieve it from the server.
But, if I remove * and provide the entire file name abc_20221202_145941.csv it will download the file. Why is * causing issue in retrieving the file
Assuming mget actually accepts regex:
Currently your regexp is looking for files that match abc_20221202_(underscore any number of times).csv
Just add a . before the * so it matches any character after the underscore any number of times before the .csv
Like so:
Filename=$File$Date"_.*".csv
If mget doesn't actually support regex, just use wget instead:
wget -r -np -nH -A "abc_20221202_.*\.csv" --ftp-user=user --ftp-password=psd ftp://ip/*
I think the backtick symbol was causing the problem when using *. Once I removed the ` (backtick) and used below command, it worked fine.
lftp -p $port $protocol://$user:$password#$sftp_server <<EOF>/error.log
cd $path
lcd $targetPath
mget $Filename
EOF
You probably missed an underscore between File and Date. A good way to debug such problems is to enable debug (“debug” command) and command logging (set cmd:trace true)

.gitignore regex for emacs temporary files

I'm trying to .gitignore emacs temporary/autosave files. I'm using...
\.\#.*
in my .gitignore.
But git add -A run in a subfolder is still giving me:
# new file: .#make_collections.py
# new file: .#norm_collections.py
# new file: make_collections.py
# new file: norm_collections.py
even though
\.\#.*
is clearly getting the right file names and not the wrong ones when I test it with a regex tester.
You can also instruct emacs to save the autosave files in a different directory altogether by setting the variable auto-save-file-name-transforms, I have this in my init file
(setq auto-save-file-name-transforms
`((".*" ,(concat user-emacs-directory "auto-save/") t)))
This instructs emacs to store the auto-saves inside the auto-save folder in the user-emacs-directory (usually ~/.emacs.d).
To save backup files in a different directory set the variable backup-directory-alist, the following will save backup files inside backups folder in the user-emacs-directory
(setq backup-directory-alist
`(("." . ,(expand-file-name
(concat user-emacs-directory "backups")))))
gitignore doesn't use regular expressions. Instead it uses shell glob patters. The man page tells you two things important for this situation:
Otherwise, Git treats the pattern as a shell glob suitable for
consumption by fnmatch(3) with the FNM_PATHNAME flag.
and
A line starting with # serves as a comment. Put a backslash ("\")
in front of the first hash for patterns that begin with a hash.
This means that the pattern you want to use is simply .#*.
Now the second pattern that matov mentioned, #*, doesn't do anything as it is treated as a comment by git. Hence me quoting that second sentence from the man page.
Emacs autosave files are ignored with
\#*#
files are ignored with:
\#*\#
.\#*
If you want an easy way to ignore files, you can also use http://www.gitignore.io which helps create useful .gitignore files for your project.
Here is the emacs template: https://www.gitignore.io/api/emacs
There is also documentation demonstrating how to run gi from the command line.
To suppress the temporary Emacs files appearing on git status globally, you can do the following:
Configure git to use a global excludesfile
Since this is a common problem, git has a specific solution to that:
Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig
git config --global core.excludesfile ~/.gitignore_global
Create the respective file
cd
touch .gitignore_global
Paste the following template into the file
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
# Org-mode
.org-id-locations
*_archive
# flymake-mode
*_flymake.*
# eshell files
/eshell/history
/eshell/lastdir
# elpa packages
/elpa/
# reftex files
.rel
# AUCTeX auto folder
/auto/
# cask packages
.cask/
dist/
# Flycheck
flycheck_*.el
# server auth directory
/server/
# projectiles files
.projectile
# directory configuration
.dir-locals.el
# network security
/network-security.data
Watch git do its magic! :)

Bash: Moving Files with Regex that Matches Pattern of Script Arugment and Fixed Text

I'm trying to write a backup script that takes a directory and directory/file name as arguments to the script. The problem is curating the target directory of the backups. For safety, I'm currently moving the files into MacOS ~/.Trash/. The problem is that I want to support having spaces in the target directory's file name, but escaping the path in mv prevents shell expansion of *.
The script in question:
# Usage: backup-to-dropbox.sh "path/containing/target" "target dir"
cd "$1"
DATE=`date "+%Y%m%dT%H%M%S"`
SOURCE="$2"
mv "~/Dropbox/Backups/$SOURCE*.tgz" ~/.Trash/ ## Problem line here
tar -czf "$SOURCE $DATE.tgz" "$SOURCE/"
mv "$SOURCE $DATE.tgz" ~/Dropbox/Backups/
How can I match all the files with this known, arbitrary prefix and fixed extension?
Words can be partially quoted. Be sure not to quote anything expanded by the shell.
mv ~/"Dropbox/Backups/$SOURCE"*.tgz ~/.Trash/

hg: how to exclude "*.xll" file but not xll directory

In my .hgignore file, I am trying to ignore all generated xll files. I (unfortunately) have a directory called "xll" within the domain of the repository, and I do not want to ignore the directory itself.
I have tried:
syntax: regex
\.xll$
which I thought should mean "match all that ends in '.xll'"
and
syntax: regex
*.\.xll$
which I thought should mean "match all that have at least one arbitrary character, followed by '.xll'".
With either of the above, the directory is not ignored (yay) but neither is a file foobar.xll (darn). If I use a bare "xll" with regex, or "*.xll" with glob, both the directory and the file are ignored.
This is in linux (Ubuntu 10.04.4) with hg 2.6 (TortoiseHG 2.8) (I'm observing the effect in Nautilus via the presence or absence of "X" icons).
Thanks in advance!
EDIT
(adding comments in here as they are too long to fit in a comment...)
Thanks for all the responses. Turns out I was misinterpreting some things. So:
- because I used "regex" instead of "regexp" (and I had "glob" at top of file), whatever I put on the line that referred to "xll" was being interpreted by "glob", so the line did have an effect (which made me think, incorrectly, that the "syntax: regex" line was doing what I thought it was
- by coincidence, all the files in my "xll" directory were filtered out (as they should have been) by other lines in .hgignore, and not by the "*.xll" line
- consequently, in Nautilus, the xll directory was marked as "ignored", not because the filter ignoring the entire directory, but instead because other filters were filtering all files within that directory
Bottom line, the *.xll I had under "syntax: glob" was actually filtering out files exactly as desired. The feedback in Nautilus was just different than I expected.
It's .*\.xll$, not *.\.xll$.
Using glob syntax works well for me:
syntax: glob
*.xll
When I create a directory named xll with an untracked file, I still see the file in the output from hg status:
$ mkdir xll
$ touch a.xll x.txt xll/b.xll xll/y.txt
$ echo 'syntax: glob\n*.xll' > .hgignore
$ hg status
? .hgignore
? x.txt
? xll/y.txt
Using \.xll$ with syntax: regexp also works great for me.

Using two asterisks to add a file in git

I want to add a file which has a unique file name but a long preceding path (e.g. a/b/c/d/filename.java). Normally I would add this to my repository by doing
git add *filename.java.
However I have also done this before:
git add a/b/c/d/filename*
So I tried to combine the two:
git add *filename*
but this does something weird. It adds every untracked file. I can see possible reasons for failure but they all should occur in one of the previous two commands so I don't know why this is happening.
My question isn't so much about how to add a file to a git repository with just its file name (although that would be useful).
My question is what is my misunderstanding of the * operation which makes me think the above should work.
Info:
I am using Git Bash for Windows, which is based on minGW.
You're looking at globs
(not regular expressions, which are a different pattern-matching language), and they're expanded by your shell, not by git.
If you want to see how they're going to match, just pass the same glob to another command, eg.
$ ls -d *filename.java
vs
$ ls -d *filename*
(I've just added the -d so ls doesn't show the contents of any directories that match)
Since you're using git bash, and it's possible that glob expansion behaves differently from a regular shell, try
$ git add --dry-run --verbose -- *filename*
for example: this should show you how it really expands the glob and what effect that has.
Note the -- ... if you're using globs that might match a filename with a leading -, it's important to make sure git knows it's a filename and not an option.
Unfortunately, this will only show you the files which both match the glob, and have some difference between the index and working copy.
Answer from author:
The dry run helped a lot, here is what I found:
I was forgetting about the bin folder which I haven't added, so when I performed the dry run I realised it was finding two matches: filename.java and filename.class. When I changed the glob to *filename.j* it worked.
My next step was to remove the .class and try the command again: it worked! It is still unexplained why git bash added everything when it found two matches... since the dry run behaves differently from the actual run I think there must be a bug, but I think that discussion is to be held elsewhere (unless somebody thinks it isn't a bug).
You could try with git add ./**/*.java
Note: I tested with zsh, it should also work for bash as well.