13

In Perforce, how can I create a patch file which has the changes that are in the file as currently saved, compared to the have revision? Instructions for either the GUI client or the CLI client would be great.

I can see how to get the diffs between two changelists, but not how to just get the difference between the last changelist and the current state of the files. Additionally, I can see that in the GUI client, I can right-click on a file and select "Diff Against Have Revision" from the contextual menu, which shows me what I'm looking for, but I can't figure out how to get that into a file.

I'm basically looking for the equivalent of what git diff <file> > patch.diff does in Git-land.

Garrett Albright
  • 2,844
  • 3
  • 26
  • 45
  • `p4 diff FILE > patch.diff`? – jamesdlin Oct 31 '13 at 18:14
  • Oh, well, crap, that's straightforward. Go ahead and post that as an answer and I'll accept it. My search engine-ing was leading me down a `p4 diff2` path, and that wasn't ending up where I was wanting it to. – Garrett Albright Oct 31 '13 at 18:29
  • By the way, when you are in P4V, and it does a task, and you want to figure out how to do that in the command line, look for the P4V "log window", which reports the underlying commands that P4V is running on your behalf. It's a nice way to learn about the command line power, while still getting the elegance and ease-of-use of the GUI. – Bryan Pendleton Nov 01 '13 at 00:12
  • Bryan, that's generally a good idea, but I don't see anything being printed to the log window when I run "Diff Against Have Revision" on a file in p4v. It just launches the result in p4merge without logging anything that I can see. – Garrett Albright Nov 01 '13 at 15:11
  • @GarrettAlbright: You are right. One has to see the TaskManager(if on windows) to see that the perforce command used for showing diff between files is actually is p4merge! Although i am late, but thought might help. – goldenmean Oct 16 '14 at 12:30
  • 1
    On a side note, if you need a single patch description/diff of a changelist (e.g. 1001) involving multiple changed files you can use: p4 describe -du 1001 > patch_diff.txt – Guest Mar 10 '17 at 13:52

3 Answers3

12

I think that p4 diff -du FILE > patch.diff should do what you want.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 1
    @GarrettAlbright Oops! I forgot about that since I have `P4DIFF=diff -du` set in my environment. ;) – jamesdlin Oct 31 '13 at 21:45
  • 6
    This doesn't really solve the problem of generating a patch file if there are new files, or a lot of files (new files don't diff, and there's no recursion). – eclectic923 Feb 26 '14 at 22:47
  • 1
    @eclectic923 The original question asked about diffing a single file. You could run `p4 diff` with no arguments to generate a diff for everything in your client. (As you noted, though, `p4 diff` does not work for added files.) – jamesdlin Feb 27 '14 at 00:34
  • How to view this file? – Kanagavelu Sugumar Nov 13 '14 at 07:52
  • @KanagaveluSugumar Uh, you'd view it as you'd view any other text file. `p4 diff` generates textual output, and `>` redirects that output into a file... – jamesdlin Nov 13 '14 at 09:08
  • oooh, can't i view this file perforce diff/merge viewer..? mmm. – Kanagavelu Sugumar Nov 13 '14 at 09:48
  • @KanagaveluSugumar If you want to view the diffs for a change in P4V, you could just ask P4V to show you that directly. – jamesdlin Nov 13 '14 at 10:54
  • @jamesdlin :) thanks for your response. Actually after code change i want that to send for review (before merging). We both (me, reviewer) has perforce but no diff tool. So just excited how to use Perforce to acting as diff tool on both of our side. Anyhow Thanks for your response. +1. – Kanagavelu Sugumar Nov 13 '14 at 11:11
  • @KanagaveluSugumar If you're doing code review, you should use something like [Review Board](https://www.reviewboard.org/). – jamesdlin Nov 13 '14 at 11:56
  • 3
    This doesn't work on Linux if the P4DIFF variable is already set. In this case, you need to temporarily unset it: "`P4DIFF= p4 diff -du` _filename_" –  Aug 20 '18 at 22:11
6

Single file

p4 diff -du file > file.patch.diff

Every file

p4 diff -du > patch.diff

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
5

The problem with p4 diff is that it doesn't take into account of new files. Also you can't get files that are only in a certain pending change list.

If there is a pending changelist, you can actually use an open source perforce patcher tool, which I have created for my project needs. You can directly download from this link of github.

In case you want to checkout the source, go to the github repo.

Documentation for the tool can be found here.

Buddha
  • 4,339
  • 2
  • 27
  • 51