Can you "ignore" a directory in P4V?
Asked Answered
C

8

53

We use Visual Studio, which generates lots of bin and obj directories. When I use P4V's "Reconcile Offline Work" feature, these appear in the "Local files not in depot" list.

I'd like to exclude them. I found this question, but that talks about files, and when I try what it suggests (adding the following to my workspace view), it doesn't work:

//depot/Foo/... //Client/Foo/...
-//depot/Foo/.../*.user //Client/Foo/.../*.user
-//depot/Foo/.../bin/... //Client/Foo/.../bin/...
-//depot/Foo/.../obj/... //Client/Foo/.../obj/...

It doesn't actually seem to work for files, either: the foo.csproj.user files are still displayed in the list.

Can I exclude directories from P4V? If so, how? What am I doing wrong?

Cryogenics answered 23/6, 2010 at 6:54 Comment(0)
H
8

Updated answer: This has been somewhat tricky in the past, because there was no built-in way to exclude a directory from a Perforce command. Since 2012 this has changed. You can have a look at a nice perforce article for this problem.

As of 2012.1, the P4IGNORE environment variable can be set to designate a file to be used to exclude files from Perforce. Both the Perforce Server (p4d) and client (p4, P4V) need to be 2012.1 or greater. P4IGNORE's purpose is to ignore files when adding new files to the depot and reconciling workspaces. Like comparable features in other version control systems, P4IGNORE cannot be used to ignore files already under the repository's control.

P4Eclipse manages .p4ignore files on its own terms. (see the manual regarding this point here)

Using client views to exclude files and directories:

The traditional method for excluding files and directories in Perforce is to use exclusionary client mappings. See the command reference for Views for full documentation. Example view:

View:

//depot/... //test_ws/depot/...
-//depot/dir/etc/... //test_ws/depot/dir/etc/...

This view will prevent files in dir/etc from being added to the depot. If trying to exclude the directory from read-only queries, use client or relative syntax.

$ p4 files //depot/dir/etc/...
//depot/dir/etc/foo#1 - add change 1186 (text)
//depot/dir/etc/bar#1 - add change 1186 (text) 


$ p4 files //test_ws/dir/etc/...
//test_ws/test_ignore/ignoredir/... - file(s) not in client view. 

$ cd dir/etc
$ p4 files ...
... - file(s) not in client view.

Alternatively, you can use shell commands to filter your output as desired.

p4 files //depot/dir/... |
    awk -F# '{print $1}' |
    grep -v "//depot/dir/etc/" |
    p4 -x - fstat

which runs p4 fstat on all files under "//depot/dir/", except for those files under "//depot/dir/etc/". This directory exclusion is accomplished by listing all of the files, and then using grep to remove those files under the directory to be excluded. The trailing slash in "//depot/dir/etc/" is necessary to prevent matching directories under "//depot/dir/" that start with "etc" (for example, "//depot/dir/etc2009").

Note:

The awk command assumes there are no file names containing the "#" character. The grep command can also read its patterns from a file, which is useful if you need to exclude multiple directories. We use the '-x -' flags with the p4 command to use the input as arguments to the corresponding command; see the Perforce Command Line Global Options for more information.

Hamhung answered 23/6, 2010 at 7:0 Comment(5)
Gah. That's hideous, but might be the best way to do what I want. I'll give it a whirl later.Cryogenics
Why do companies pay money for this ?Juniorjuniority
This answer is no longer correct. See this answer.Taler
answer is updated to the actual state of perforce functionalityHamhung
nice copy-paste from the p4 websiteOpenhearth
T
77

As of version 2012.1, Perforce supports the P4IGNORE environment variable. This allows you to specify files and directories to ignore when using the commands that search for or add new files (p4 add, p4 status, and p4 reconcile).

To use an ignore file, create a file in the root of your workspace and give it some meaningful name. The convention seems to be something like .ignore or .p4ignore, but anything will do (I used p4ignore.txt so that I can edit it with a simple double-click). Then fill it with your ignore rules. The following will ignore the the unwanted debris generated by Visual Studio:

# directories
bin
obj

# files
*.suo
*.user

After you have created this file, set the P4IGNORE environment variable to point to it. At the command line, type something along the lines of this:

p4 set P4IGNORE=C:\somepath\p4ignore.txt

Be sure to use an absolute path! The Perforce documentation doesn't specify this and my first attempt did not work (on Windows anyway) because I didn't specify an absolute path.

After doing this, any attempt to add files or directories that are in the ignore list will be rejected and you'll see a warning such as this (which they do give you the option to suppress):

P4V 'ignored' files message


If you are using a version of the Perforce server previous to 2012.1, you can still do this in your client spec. The syntax of your exclusion rules is just a little off. What you want is this:

-//depot/Foo.../*.user //Client/Foo.../*.user
-//depot/Foo...bin/... //Client/Foo...bin/...
-//depot/Foo...obj/... //Client/Foo...obj/...

Note the missing slashes after "Foo" and before "bin" and "obj".

Taler answered 23/6, 2010 at 17:16 Comment(12)
That doesn't work for directories; I'm still seeing (e.g.) path\to\bin\Debug\Foo.pdb. It does work for the foo.csproj.user files, though (which it didn't seem to be yesterday) Odd.Cryogenics
But it does seem to work in the Advanced Reconcile box. What's going on here?Cryogenics
Works for me using P4V 2010.2Barre
This still requires all developers setting a P4IGNORE envar. Is there not a default ignore file that clients will honour? (Much like git's .gitignore file)Amandaamandi
You can use the Ignored field in a stream, but it doesn't support embedded wildcards, and doesn't you let you ignore everything in a folder and then un-ignore specific files -- you need to be explicit about what is ignored.Dewitt
You can use p4ignore.txt to ignore files and folders by name, but frustratingly wildcard rules are only applied to files. Buggy software :(Scharf
Do not set P4IGNORE to an absolute path. Set it to a file name. For example, p4 set P4IGNORE=.gitignore. Then you can put files of this name in any relevant directories and subdirectories, and Perforce will recognize them, similarly to git and .gitignore.Parrett
@ChuckBatson, the fact that an environment variable has to be set which points to a single file configuring the entire workspace still makes it far more cumbersome than creating a .gitignore.Curtal
@Curtal What I'm saying is that when you don't use an absolute path, Perforce appears to look in each directory for the file name (i.e., .gitignore) very much like git. In other words, you don't need a single file for the entire workspace. Try it... I was pleasantly surprised! (Note though that Perforce's parsing/processing is not the same as git's so the behavior isn't identical, especially with respect to wildcards and subdirectories.)Parrett
@ChuckBatson Huh, I stand corrected, thanks! I still think its reliance on an environment variable is asking for problems, but being to specify multiple ignore lists is nice.Curtal
Does this have to only be configured on the P4D server? I have set this up on my AWS instance but it doesn't seem to work when testing via P4V on the client and local workspace machine.Lippmann
NOTE: P4IGNORE=~/.p4ignore doesn't work because Perforce is too stupid to resolve the ~. Use P4IGNORE=/home/yourusername/.p4ignore instead.Whitford
H
8

Updated answer: This has been somewhat tricky in the past, because there was no built-in way to exclude a directory from a Perforce command. Since 2012 this has changed. You can have a look at a nice perforce article for this problem.

As of 2012.1, the P4IGNORE environment variable can be set to designate a file to be used to exclude files from Perforce. Both the Perforce Server (p4d) and client (p4, P4V) need to be 2012.1 or greater. P4IGNORE's purpose is to ignore files when adding new files to the depot and reconciling workspaces. Like comparable features in other version control systems, P4IGNORE cannot be used to ignore files already under the repository's control.

P4Eclipse manages .p4ignore files on its own terms. (see the manual regarding this point here)

Using client views to exclude files and directories:

The traditional method for excluding files and directories in Perforce is to use exclusionary client mappings. See the command reference for Views for full documentation. Example view:

View:

//depot/... //test_ws/depot/...
-//depot/dir/etc/... //test_ws/depot/dir/etc/...

This view will prevent files in dir/etc from being added to the depot. If trying to exclude the directory from read-only queries, use client or relative syntax.

$ p4 files //depot/dir/etc/...
//depot/dir/etc/foo#1 - add change 1186 (text)
//depot/dir/etc/bar#1 - add change 1186 (text) 


$ p4 files //test_ws/dir/etc/...
//test_ws/test_ignore/ignoredir/... - file(s) not in client view. 

$ cd dir/etc
$ p4 files ...
... - file(s) not in client view.

Alternatively, you can use shell commands to filter your output as desired.

p4 files //depot/dir/... |
    awk -F# '{print $1}' |
    grep -v "//depot/dir/etc/" |
    p4 -x - fstat

which runs p4 fstat on all files under "//depot/dir/", except for those files under "//depot/dir/etc/". This directory exclusion is accomplished by listing all of the files, and then using grep to remove those files under the directory to be excluded. The trailing slash in "//depot/dir/etc/" is necessary to prevent matching directories under "//depot/dir/" that start with "etc" (for example, "//depot/dir/etc2009").

Note:

The awk command assumes there are no file names containing the "#" character. The grep command can also read its patterns from a file, which is useful if you need to exclude multiple directories. We use the '-x -' flags with the p4 command to use the input as arguments to the corresponding command; see the Perforce Command Line Global Options for more information.

Hamhung answered 23/6, 2010 at 7:0 Comment(5)
Gah. That's hideous, but might be the best way to do what I want. I'll give it a whirl later.Cryogenics
Why do companies pay money for this ?Juniorjuniority
This answer is no longer correct. See this answer.Taler
answer is updated to the actual state of perforce functionalityHamhung
nice copy-paste from the p4 websiteOpenhearth
L
7

There is a way to do it directly in Perforce:

  • Connection menu
  • Edit Current Workspace...

Navigate the project tree. Right-click on the file or folder. Choose to include or exclude the file, the folder, the entire tree.

Laurentian answered 6/5, 2014 at 18:33 Comment(1)
This only works if your workspace is compatible with the tree view. The one in question wasn't.Cryogenics
O
3

This works great for me:

//depot/... //my_workspace_name/...
-//depot/.../.git/... //my_workspace_name/.../.git/...
-//depot/.../xcuserdata/... //my_workspace_name/.../xcuserdata/...
-//depot/.../xcschemes/... //my_workspace_name/.../xcschemes/...
-//depot/.../xcdebugger/... //my_workspace_name/.../xcdebugger/...
Orourke answered 29/11, 2012 at 18:55 Comment(1)
Note that I'm mapping the entire depot. (Although I only sync what I need.) Tested using P4V 2011.1 on OS X 10.7.4Orourke
D
1

This may not be relevant, but if there are spaces in any of your paths you need to ensure you have correctly enclosed them in "" but dont enclose the - inside the quotes.

Dulles answered 15/7, 2010 at 8:36 Comment(0)
R
0

When I added the folders to ignore, they were not applied because I specified the path to the folders, and it was a mistake, you just need to specify the name of the folder that you want to ignore.

Revelry answered 7/6, 2022 at 11:53 Comment(0)
G
0

Method won't work with local depot, but if you have a stream depot, you can create a virtual stream and exclude your binaries folders in centralized way for all developers.

Advanced path settings of virtual stream could be something like that:

share ...
exclude bin/...
exclude logs/...

https://www.perforce.com/manuals/p4v/Content/P4V/streams.virtual.html

Godinez answered 25/3, 2023 at 11:33 Comment(0)
M
-1

I would go ahead with the Reconcile Offline Work and do everything but the submit. Before the submit, do the following:

p4 revert //Client/Foo/.../*.user

p4 revert //Client/Foo/.../bin/...

p4 revert //Client/Foo/.../obj/...

It's not automated, but that's the best I can think of at the moment.

Masuria answered 9/7, 2010 at 22:17 Comment(5)
That's why I would like to use ignore, but yes, I'm manually reverting stuff (or may end up writing a shellscript for that), and this is a joke.Emaemaciate
@SzabolcsKurdi, Perforce now has a way to ignore directories and files, both via the P4IGNORE variable, and through the Ignored field in the stream specification. Don't have time to do an actual answer right now.Masuria
I tried to use P4Ignore but it didn't work in the gui at all (and I find it quite cumbersome, compared to gitignore for example); maybe it's me, but I couldn't find a proper solution. I'd be glad if you could tell me how you did it.Emaemaciate
@SzabolcsKurdi, are you using streams?Masuria
I use command line + p4v + vs plugin; hopefully I cleared it up with support though, ignore only kicks in during the add process. If I can't find a solution I'll open a question instead of spamming the comment thread.Emaemaciate

© 2022 - 2025 — McMap. All rights reserved.