Perforce: How to checkout file same file from different version? - perforce

For example, I have 100s of files checked out in a changelist:
//Development/MyProject/Version1.0/Coord/File1.cs
//Development/MyProject/Version1.0/Coord/File2.cs
...
//Development/MyProject/Version1.0/Coord/Filen.cs
//Development/MyProject/Version1.0/Common/File1.cs
...
//Development/MyProject/Version1.0/Common/Filen.cs
Similarly in some more directories. Now I want to checkout similar files in Version2.0
//Development/MyProject/Version2.0/Coord/File1.cs
//Development/MyProject/Version2.0/Coord/File2.cs
...
//Development/MyProject/Version2.0/Coord/Filen.cs
//Development/MyProject/Version2.0/Common/File1.cs
...
//Development/MyProject/Version2.0/Common/Filen.cs
I can do this by looking at what files have been checked out in Version1.0 and go to each directory in Version2.0 and checkout those files. This is tedious job.
Any aboveboard way of doing this?

From the linux/Unix/macOS shell, I suggest the following method. It would be significantly different in Windows unless you had unix tools installed there.
Check out all your Version1.0 files.
cd to your Version1.0 directory
Run the following command line:
p4 opened ... | sed 's/\#[1-9].*$//' | sed 's/\/Version1.0\//\/Version2.0\//' |
p4 -x - edit
Note: this would need some tweaking if any of your files have '#' symbols in their names. It's a quick-and-dirty fix. And credit to Bryan Pendleton above, who basically said the same thing much more briefly.

Related

Find a string in Perforce file without syncing

Not sure if this is possible or not, but I figured I'd ask to see if anyone knows. Is it possible to find a file containing a string in a Perforce repository? Specifically, is it possible to do so without syncing the entire repository to a local directory first? (It's quite large - I don't think I'd have room even if I deleted lots of stuff - that's what the archive servers are for anyhow.)
There's any number of tools that can search through files in a local directory (I personally use Agent Ransack, but it's just one of many), but these will not search a remote Perforce directory, unless there's some (preferably free) tool I'm not aware of that has this capability, or maybe some hidden feature within Perforce itself?
p4 grep is your friend. From the perforce blog
'p4 grep' allows users to use simple file searches as well as regular
expressions to search through file contents of head as well as earlier
revisions of files stored on the server. While not every single option
of a standard grep is supported, the most important options are
available. Here is the syntax of the command according to 'p4 help
grep':
p4 grep [ -a -i -n -v -A after -B before -C context -l -L -t -s -F -G ] -e pattern file[revRange]...
See also, the manual page.
Update: Note that there is a limitation on the number of files that Perforce will search in a single p4 grep command. Presumably this is to help keep the load on the server down. This manifests as an error:
Grep revision limit exceeded (over 10000).
If you have sufficient perforce permissions, you can use p4 configure to increase the dm.grep.maxrevs setting from this default of 10K to something larger. e.g. to set to 1 million:
p4 configure set dm.grep.maxrevs=1M
If you do not have permission to change this, you can work around it by splitting the p4 grep up into multiple commands over the subdirectories. You may have need to split further into sub-subdirectories etc depending on your depot structure.
For example, this command can be used at a bash shell to search each subdirectory of //depot/trunk one at a time. Makes use of the p4 dirs command to obtain the list of subdirectories from the server.
for dir in $(p4 dirs //depot/trunk/*); do
p4 grep -s -i -e the_search_string $dir/...
done
Actually, solved this one myself. p4 grep indeed does the trick. Doc here. You have to carefully narrow it down before it'll work properly - on our server at least you have to get it down to < 10000 files. I also had to redirect the output to a file instead of printing it out in the console, adding > output.txt, because there's a limit of 4096 chars per line in the console and the file paths are quite long.
It's not something you can do with the standard perforce tools. One helpful command might be p4 print but it's not really faster than syncing I would think.
This is a big if but if you have access to the server you can run agent ransack on the perforce directory. Perforce stores all versioned files on disk, it's only the metadata that's in a database.

How can I open for edit 10k perforce files using list of files as parameter?

I have 10k perforce files mentioned in my file.txt.
I need to open them using p4 edit command.
I expect some command like "p4 edit ?????file.txt". Can you help me to check these files out?
You can use the -x flag on p4. This is assuming a UNIX shell.
cat file.txt | p4 -x - edit
I assume you have some copy of directories structure where you have changes, and now you need to add those files to a change list. Which is impossible to do without checking them out. Am I right?
If I needed to change that much amount of files, I would do like this:
Copy all files I wanted to check in replacing read-only files (Wondows Explorer can do that)
In P4V go to a directory you need to check out files in, and then call "Reconcile offline work".
In appeared dialog choose all files.
Get new changelist with changed files being checked out.
I used this solution a couple of times - it works for added, changed and deleted files.
Just use below command to edit all files present in file.txt
p4 -x file.txt edit

Perforce (P4): Is there such thing as a 'reverse sync', that pushes any/all changes from your local directory to the sever?

I know this is a strange question, but is there a p4 command that is the reverse of 'sync'? That is, I'd like whatever files are in my local workspace directory to be pushed to the depot.
I know your first thought is probably "but WHY?", and the answer is, it's complicated.
Reconciling offline work could help you. It pushes files that are added, or changed or deleted . Its a bit trickier with renamed files.
Out of curiosity. What exactly is "it's complicated"
Both 'submit' and 'shelve' can send file content from your local workspace to the depot.
In either case, you have to use 'add' or 'edit' first to mark the files to be sent to the depot. (How you do this depends on the client tool you're using.)
Here's a script, derived from the O'Reilly Perforce book
You probably want to start with
p4 sync -k ...
which makes perforce think it has synchronized to the current head, but in fact
makes no changes to the filesystem. So the below diffs will behave (to perforce) like changes to the current head.
# a reverse 'synchronize' (sync what's on disk with what's in the depot)
# see "practical perforce" (o'reilly) page 46
#
# changed files
p4 diff -se | p4 -x- edit
#
# deleted files
p4 diff -sd | p4 -x- delete
#
# added files
find . -type f -o -type l | p4 -x- add -f
I have not tested the above in isolation (it came from a longer script that has been widely used) but I believe it should work.

Is there a single Perforce command to display a list of all the check-ins to a file/dir along with the list of files that changed and the description?

p4 changes -l ... shows us the list of check-ins and the description, but it doesn't show the list of files that were modified in the check-in. Is there a way to do that in one command, without the need to create a wrapper script that combines the output of another command like p4 describe or p4 file?
In Subversion, I can do this by running svn log -v.
The 'files' command can do what you're looking for. An easy way is:
p4 files //...#=<changelist>
That example will list the files modified by that changelist, under the view specified.
You can use the "describe" command to get the description of a changelist, along with the files affected.
For example, p4 describe -s <changelist> will describe the changelist, and the "-s" will prevent it from displaying file diffs.
One liner, list all changes made to a branch, with description and list of affected files, without showing the diff. Thanks to a combination of answers. Works on windows with Unix utils
p4 changes -s submitted //depot/xxx/yyy/zzz/... | grep -o "^Change [0-9]*" | cut -f2 -d" " | p4 -x- describe -s
Output:
Change 1753385 by user#clientspec on 2019/03/08 06:29:44
Changing the world
Affected files ...
... //depot/xx/yy/zz.h#6 edit
Change 1751752 by name#clientspec on 2019/03/05 15:24:00
I made a change to a file
Affected files ...
... //depot/xx/yy/zz.h#3 integrate

How to show new files in Perforce workspace?

When I add new files to my project which is managed by Perforce, how can I get a list of the new files I've added (the ones Perforce does not know about yet) so I can make sure I don't miss any in my changeset?
Something like 'svn status | grep ^\?' or 'git status' to show the unstaged, unknown files.
Thanks!
-Casey
Find the similar question on SO and check the other answers and comments too. There is no direct command available from command-line. But you can use a powershell script or a GUI for it.
The best answer is provided in the similar question linked to by Teja. However, if you want an easy command line solution that deals specifically with added files, you can do the following
find directory -type f | p4 -x- add
for normal files, and
find directory -type l | p4 -x- add
for links. (Letter 'el', not 'one' in above command)
Perforce simply refuses to add any files that are already in version control. The only problem is that this adds everything, including binary files.
For P4V, right-click on a folder in the workspace tree and click "Find File...". In "All or part of a file name", type "*.cs". Then sort by filetype - new files will have filetype "none".
Unfortunately there doesn't seem to be a way to apply multiple filters, e.g., "*.cs; *.aspx". So you have to filter one filetype at a time. Also, you have to be patient if there are thousands of files - the sorting mechanism won't work properly until the asynchronous find process has completed.
(The "Reconcile Offline Work..." option isn't working for me. I'm using P4V June 2009.)
You might take advantage of the p4 files command
find . -type f | xargs p4 files 2>&1 | grep 'no such file(s)'
You could even filter on file type like this.
find . -type f | xargs p4 files 2>&1 | grep 'no such file(s)' | grep 'cpp'
Here's a unix one liner using find and p4, fstat and awk that saved my bacon.
How to find untracked files in a Perforce tree? (analogue of svn status)
The perforce "reconcile" command with the "add files" and "preview" options will show new, un-added files, without adding them:
p4 rec -an

Resources