5

In perforce how can I delete files from a directory in my workspace where the files I want deleted are not part of the workspace?

I have a directory on my file system with files it gets from perforce but after some processes run, it creates some new files in those directories.

Is there a perforce command to remove these generated files that are not part of my workspace?

Matt
  • 25,943
  • 66
  • 198
  • 303

7 Answers7

4

Perforce 2013.2 has a p4 status command, which is an alias for p4 reconcile -n

I don't know if it was available in earlier versions.

cmcginty
  • 113,384
  • 42
  • 163
  • 163
3

This is my quick implementation of p4 nothave.

#!/bin/bash
#
# List files present, but not in perforce.

tmpd="$(mktemp -d /tmp/nothave.XXXXXXXX)"
trap "rm -rf $tmpd" EXIT HUP INT QUIT TERM

p4 have | awk -F' - ' '{print $2}' | sort >$tmpd/have.txt

root=$(p4 info | awk -F': ' '$1=="Client root"{print $2}')
find $root -type f | sort >$tmpd/find.txt

comm -13 $tmpd/have.txt $tmpd/find.txt

This will produce a list of files, which can then be piped into xargs rm in order to be deleted. Be careful to inspect the list before deleting though. You may well find things you didn't expect in there.

Dominic Mitchell
  • 11,861
  • 4
  • 29
  • 30
3

You can also use the "Reconcile Offline Work" option in p4v and p4 to accomplish this without any scripting. In p4v, right click any directory in Depot view and select "Reconcile Offline Work".

The middle pane will have "Local files not in depot". You can select any or all of these, right click and choose "Delete Local file".

Easy!

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • This works nicely - thanks! I notice that this method leaves all the resulting empty directories, if any, on the local system. – omelet Jul 03 '13 at 14:18
  • On a Unix based system `find /dir -empty -type d -delete` can solve that one easily. – BeeOnRope Jul 03 '13 at 23:34
2

If using Linux/Unix, in the Bourne shell family, you can type

find . -type f | p4 -x- files 2>&1| grep "no such file" | cut -f1 -d" " | xargs rm

Similarly, for symbolic links, type

find . -type l | p4 -x- files 2>&1| grep "no such file" | cut -f1 -d" " | xargs rm

Above is letter 'el', not 'one'.

Whether there is an equivalent in Windows I do not know.

Chance
  • 2,653
  • 2
  • 26
  • 33
  • This answer did not work for me on linux. The "p4 files ..." does not list any files that are not part of the workspace. I think the original question was misinterpreted. Try it: "mkdir tmp; cd tmp; touch not_in_workspace; p4 files ..." – engtech Feb 09 '11 at 18:55
1

You cannot modify/delete files that are not part of your workspace from within perforce. You will have to write external scripts for that.

341008
  • 9,862
  • 11
  • 52
  • 84
  • I think he used the term 'workspace' in two different ways. The files he's trying to delete are within his workspace, but not checked in (as is evidenced by the fact that perforce is putting other files alongside the files in question). – gdw2 Mar 13 '12 at 19:26
1

This is a little awkward in powershell, because p4 sends the can't-find-file message to the error stream rather than std out.

ps > $error.clear()
ps > dir | %{p4 fstat -T "haveRev" $_} 2>$null
ps > $error | %{$_.Exception.ToString().Split()[1]} | %{del $_}

The second line should produces errors such as "FILENAME.EXT - no such file(s)." It can be a bit flaky if you have anything other than these exceptions in the error stream, so beware.

tenpn
  • 4,556
  • 5
  • 43
  • 63
  • ...still learning powershell, so if there's a quicker way to do this, let me know. – tenpn Jul 10 '10 at 12:50
  • 2
    You can redirect stderr in PowerShell, and it will nicely wrap the errors in ErrorRecords (add "-r" to dir to search the whole tree): dir | % {p4.exe fstat $_.FullName 2>&1} | ? {$_ -is [System.Management.Automation.ErrorRecord]} | %{$_.Exception.ToString().Split()[1]} | % {del $_} – JasonMArcher Nov 23 '10 at 18:50
-4

You can use p4 nothave to determine the files that are not managed by perforce. Depending on the output of p4 nothave you might be able to do p4 nothave | xargs rm, but be sure to check the result of p4 nothave before running that (so that you don't delete something important by accident).

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200