How to find untracked files in a Perforce tree? (analogue of svn status)

前端 未结 16 2066
独厮守ぢ
独厮守ぢ 2020-12-02 06:44

Anybody have a script or alias to find untracked (really: unadded) files in a Perforce tree?

EDIT: I updated the accepted answer on this one since it looks like P4V

相关标签:
16条回答
  • 2020-12-02 07:35

    EDIT: Please use p4 status now. There is no need for jumping through hoops anymore. See @ColonelPanic's answer.

    In the Jan 2009 version of P4V, you can right-click on any folder in your workspace tree and click "reconcile offline work..."

    This will do a little processing then bring up a split-tree view of files that are not checked out but have differences from the depot version, or not checked in at all. There may even be a few other categories it brings up.

    You can right-click on files in this view and check them out, add them, or even revert them.

    It's a very handy tool that's saved my ass a few times.

    EDIT: ah the question asked about scripts specifically, but I'll leave this answer here just in case.

    0 讨论(0)
  • 2020-12-02 07:36

    The p4 fstat command lets you test if a file exists in the workspace, combine with find to locate files to check as in the following Perl example:

    // throw the output of p4 fstat to a 'output file'
    // find:
    //  -type f  :-  only look at files,
    //  -print0  :-  terminate strings with \0s to support filenames with spaces
    // xargs:
    //  Groups its input into command lines,
    //  -0       :-  read input strings terminated with \0s
    // p4:
    //  fstat    :-  fetch workspace stat on files
    
    my $status=system "(find . -type f -print0 | xargs -0 p4 fstat > /dev/null) >& $outputFile";
    
    // read output file
    open F1, $outputFile or die "$!\n";
    
    // iterate over all the lines in F1
    while (<F1>) {
      // remove trailing whitespace
      chomp $_;
    
      // grep lines which has 'no such file' or 'not in client'
      if($_ =~ m/no such file/ || $_ =~ m/not in client/){
    
         // Remove the content after '-'
         $_=~ s/-\s.*//g;
    
         // below line is optional. Check ur output file for more clarity.
         $_=~ s/^.\///g;
    
         print "$_\n";
      }
    }
    
    close F1;
    

    Or you can use p4 reconcile -n -m ...

    If it is 'opened for delete' then it has been removed from the workspace. Note that the above command is running in preview mode (-n).

    0 讨论(0)
  • 2020-12-02 07:38

    I feel impelled to add an answer, since the accepted answer, and some of the others, have what I think is a significant problem: they do not understand the difference between a read-only query command, and a command that makes changes.

    I don't expect any credit for this answer, but I hope that it will help others avoid wasting time and making mistakes by following the accepted but IMHO incorrect answer.

    ---+ BRIEF

    Probably the most convenient way to find all untracked files in a perforce workspace is p4 reconcile -na.

    -a says "give me files that are not in the repository, i.e. that should be added".

    -n says "make no changes" - i.e. a dry-run. (Although the messages may say "opened for add", mentally you must interpret that as "would be opened for add if not -n")

    Probably the most convenient way to find all local changes made while offline - not just files that might need to be added, but also files that might need to be deleted, or which have been changed without being opened for editing via p4 edit, is p4 reconcile -n.

    Several answers provided scripts, often involving p4 fstat. While I have not verified all of those scripts, I often use similar scripts to make up for the deficiencies of perforce commands such as p4 reconcile -n - e.g. often I find that I want local paths rather than Perforce depot paths or workspace paths.

    ---+ WARNING

    p4 status is NOT the counterpart to the status commands on other version control systems.

    p4 status is NOT a read-only query. p4 status actually finds the same sort of changes that p4 reconcile does, and adds them to the repository. p4 status does not seem to have a -n dry-run option like p4 reconcile does.

    If you do p4 status, look at the files and think "Oh, I don't need those", then you will have to p4 revert them if you want to continue editing in the same workspace. Or else the changes that p4 status added to your changeset will be checked in the next time.

    There seems to be little or no reason to use p4 status rather than p4 reconcile -n, except for some details of local workspace vs depot pathname.

    I can only imagine that whoever chose 'status' for a non-read-only command had limited command of the English language and other version control tools.

    ---+ P4V GUI

    In the GUI p4v, the reconcile command finds local changes that may need to be added, deleted, or opened for editing. Fortunately it does not add them to a changelist by default; but you still may want to be careful to close the reconcile window after inspecting it, if you don't want to commit the changes.

    0 讨论(0)
  • 2020-12-02 07:38

    Fast method, but little orthodox. If the codebase doesn't add new files / change view too often, you could create a local 'git' repository out of your checkout. From a clean perforce sync, git init, add and commit all files locally. Git status is fast and will show files not previously committed.

    0 讨论(0)
  • 2020-12-02 07:39

    The following commands produce status-like output, but none is quite equivalent to svn status or git status, providing a one-line summary of the status of each file:

    • p4 status
    • p4 opened
    • p4 diff -ds
    0 讨论(0)
  • 2020-12-02 07:41

    I use the following in my tool that backs up any files in the workspace that differ from the repository (for Windows). It handles some odd cases that Perforce doesn't like much, like embedded blanks, stars, percents, and hashmarks:

    dir /S /B /A-D | sed -e "s/%/%25/g" -e "s/@/%40/g" -e "s/#/%23/g" -e "s/\*/%2A/g" | p4 -x- have 1>NUL:
    

    "dir /S /B /A-D" lists all files at or below this folder (/S) in "bare" format (/B) excluding directories (/A-D). The "sed" changes dangerous characters to their "%xx" form (a la HTML), and the "p4 have" command checks this list ("-x-") against the server discarding anything about files it actually locates in the repository ("1>NUL:"). The result is a bunch of lines like:

    Z:\No_Backup\Workspaces\full\depot\Projects\Archerfish\Portal\Main\admin\html\images\nav\navxx_background.gif - file(s) not on client.
    

    Et voilà!

    0 讨论(0)
提交回复
热议问题