6

I want to design a shell script that will take the resolve decision based on the diff chunks shown by 'p4 resolve'

The current state of my script is something like -

p4 integ -b @=CL #This will open the files in my default CL.

p4 resolve #It will now resolve the files opened in my default CL one by one

Now my Problem is, with p4 resolve, I cannot redirect its output to some file and or read the diff chunks because, it shows diff chunks for one file and waits for the user input for resolve decision. I want the script to take the very obvious resolve decision.

So, is there any way to find the diff chunks for the files in my default CL? So that, I can read these diff chunks anyhow and make these obvious resolve decisions?

Manish Sharma
  • 173
  • 10
  • 2
    Are you planning on doing analysis of the diffs, or just applying the resolve method? The latter can be done with `p4 resolve -as` or `-am` to do safe or merge resolves, other options are available as well. If you are planning on doing further analysis with a script, you might want to use `p4 resolve -af` to get the conflicts embedded into the files and then perform your analysis by looking at the files for the conflict indicators (`>>>>>`,`<<<<<`...). – gaige Aug 08 '13 at 12:36
  • Thanks for the reply gaige. What I am trying to do is to catch the diff-chunks in the form of numbers in some file or a variables and perform some operation if this diff chunk is non-zero in case of conflict. about -af option, Perforce documentation says "Force Accept. Accept the merge file no matter what. If the merge file has conflict markers, they will be left in, and you'll need to remove them by editing the file." This is not my requirement. :) Please help... – Manish Sharma Mar 26 '14 at 10:51
  • The reason I suggested `-af` is to make place the conflict indicators into the file so that you can find the conflicts. You will have to process them using `grep` or another processor to figure out where the conflicts are. Unless you do this, you don't have any way of finding the conflicts directly. This, to my knowledge, is the only way to get the conflicts from `p4` without manually resolving the files. You wanted a way to get the conflicts so that you can read them, and `-af` is it. You can then write your code to seek the `<<<<<` and `>>>>>` to find and operate on the conflicts. – gaige Mar 26 '14 at 11:01

3 Answers3

2

I recently had a similar need, and hacked together a solution with a shell script.

The problem, for me, was that p4 resolve wants to be interactive, but I just wanted to see the conflicts in a file.

I didn't want to run p4 resolve -f since that would mark the file as resolved in Perforce.

I just wanted to view the conflicts, maybe email them to someone else, without really doing the resolve. You can do this interactively with the (e)dit and (s)kip commands of p4 resolve, but I wanted a hands-off version.

Save this script as p4-dump-conflicts:

#!/usr/bin/env bash

function cat_to_file() {
    cat "$@" > "$OUTFILE"
}
export -f cat_to_file
destFileName=$(dirname "$1")/CONFLICTS_$(basename "$1")
OUTFILE="$destFileName" P4EDITOR=cat_to_file p4 resolve "$1" <<END_INPUT
e
s
END_INPUT

If you run p4-dump-conflicts myfile, it will write out CONFLICTS_myfile, containing the conflict markers. It will leave the file un-resolved in Perforce, though.

Note: Only works for one file, as-is.

For your question in particular, you could process the CONFLICTS_xxx file however you want, then use the results of that for the final merge resolution.

jwd
  • 10,837
  • 3
  • 43
  • 67
  • This solution works but can you please explain how it works? How does changing P4EDITOR to cat_to_file p4 resolve $1 actually output the conflict markers to the file OUTFILE? – user2635911 Mar 29 '19 at 14:44
  • @user2635911: If I understand your comment, I think you misunderstand how P4EDITOR is being set. In general, for `bash` (and `sh` and others), if you write `X=foo mycommand` then it runs `mycommand` with the environment variable X having a value `foo` for that run. So, in my case, it does not "change P4EDITOR to cat_to_file p4 resolve $1". Rather, it changes P4EDITOR to `cat_to_file`, then invokes the `p4 resolve $1` command. That command then invokes the editor to do the work. We are using a non-interactive "editor" (just a script that dumps the file). – jwd Apr 29 '19 at 21:32
2

I wanted to do something similar; output the actual conflicts in a Jenkins job so that the information could be sent to the relevant development engineer.

I noticed the handy p4-dump-conficts script by jwd, based on that, perhaps the following is enough

echo -e "d\ns\n" | P4EDITOR=cat p4 resolve

(i.e. get p4 resolve to (d)iff and then (s)kip and use cat to output the results. This works for all 'pending' files in the workspace)

1

Yes, you can use p4 resolve -n to print the same output as p4 resolve, without prompting for input or taking any action. From the p4 resolve docs:

-n List the files that need resolving without actually performing the resolve.

benj
  • 713
  • 6
  • 12
  • But this only prints the list of files. It does not output the diff hunks or conflict details. – jwd Aug 27 '15 at 18:07