p4 command line equivalent to “git log -p”?

2019-05-01 09:18发布

问题:

I often use git log -p <file> or git log -p <directory> to get a comprehensive summary of the changes in a file or set of files. It outputs a history of the commits affecting the files, together with a unified diff of each commit.

Using Perforce I can do p4 changes <file> or p4 changes <directory>/... to get a list of commits. But there does not seem to be an option to show the corresponding diffs.

Is there a Perforce equivalent I could use? If shell scripting is necessary, a fully working function would be nice.

If you want to know why I miss the feature, here are a few things that git log -p lets me do extremely quickly:

  • what was the last .cpp file to be modified?
  • find all commits where the string FOO has been added to a file.
  • a function just got deprecated; what did other developers replace it with?
  • in general, just know what happened recently in a given directory.

回答1:

Here's a reasonable approximation:

p4log () {
  p4 changes "$1" | awk '{print $2}' | xargs -i p4 describe -du {} | less -F
}

Note that unlike git log -p, an argument is mandatory. You can give a pattern like p4log ... to run it against everything under the current directory recursively.

Details

p4 changes "$1": Get one-line change summaries (most recent to oldest) for files matched by the pattern.

awk '{print $2}': Extract the change number.

p4 describe -du CHANGE [$CHANGE2 etc]: Output the complete change description and diffs. The -du specifies unified diff format, which is closest to git's diff format.

xargs -i p4 describe -du {}: Run the describe command with all the change numbers as its arguments.

less -F: Page if longer than one screen, dump to terminal otherwise. Git pipes most of its output through less -F by default