Perforce Public Knowledge Base - Integrating Perforce into the Gnome Nautilus Browser
Reset Search
 

 

Article

Integrating Perforce into the Gnome Nautilus Browser

« Go Back

Information

 
Problem

I like P4EXP. Is there anything like it for Linux?  How do I integrate Perforce into my Gnome desktop?


Solution

While Windows users have a choice of either using P4V or using the Windows shell Perforce plugin, P4EXP; users on Linux are limited to using P4V, P4WSAD (the Eclipse plug-in), or a terminal window to perform Perforce actions such as opening a file for edit, syncing a file, or submitting a changelist.

The following article describes how to set up a small, but functional, set of Perforce operations in Nautilus, the Gnome file browser.

Prerequisites

You do not strictly need to be running Gnome for this to work, but you do need to use the Nautilus file browser.

You will also need the Nautilus extension "Nautilus-Actions". Most modern distributions of Linux include this extension in their repositories. For example, on Ubuntu, you can install it with:

apt-get install nautilus-actions

 

If it is not available in your distribution's repository, it is directly available here: http://www.grumz.net/?q=taxonomy/term/2/9

The scripts also use the "Zenity" utility, a part of the Gnome toolkit, to pop up messages to the user. If you do not have Zenity, or do not want notification, you can omit the Zenity lines from the scripts.

Setting up the Scripts for Nautilus-Actions

"Nautilus-actions" provides hooks in the Nautilus file browser that allow users to directly call external programs on files, folders or sets of files. Because the shell spawned by the extension lacks the needed environment variable settings, you need to set up a small collection of shell scripts to wrap the Perforce operations you want.

Start by creating a location for your nautilus shell scripts. A good choice would be $HOME/bin/nautilus/ (that will be the default location for the rest of this article). If you choose a different location, remember to substitute it in for $HOME/bin/nautilus in the scripts.

You will set up the following scripts in your $HOME/bin/nautilus directory, and then set up Nautilus to use them.

Script Name Purpose
p4env.sh Sets up basic settings that the other scripts use
p4edit.sh Check out file(s) for edit
p4revert.sh Revert file(s)
p4submit.sh Submit a changelist
p4sync.sh Update a file from the depot
p4add.sh Add a file to the depot

The p4env.sh Script

This script defines the common settings for the other scripts, so you do not have to change multiple scripts each time you want to change a server setting, client setting, and so on. Add the appropriate values for P4PORT, P4USER and P4CLIENT.

#!/bin/bash
P4USER=
P4CLIENT=
P4PORT=
P4=p4
P4EDITOR=gedit # Whatever editor you would like to use to edit the submit form
OPTS="-u $P4USER -c $P4CLIENT -p $P4PORT"

 

The p4edit.sh Script

This script opens the selected files for edit.

#!/bin/bash
source $HOME/bin/nautilus/p4env.sh
FILES="\n"
for FILE in "$@"
do
  $P4 $OPTS edit $FILE
  FILES=$FILES" $FILE\n"

done
zenity --info --text "Files Opened for Edit: $FILES"

The p4revert.sh Script

This script reverts files that are currently opened for edit.

#!/bin/bash
source $HOME/bin/nautilus/p4env.sh
FILES="\n"
for FILE in "$@"
do
    $P4 $OPTS revert $FILE
    FILES=$FILES" $FILE\n"
done
zenity --info --text "Files Reverted: $FILES"

The p4submit.sh Script

The p4submit.sh script takes care of submitting changelists. It opens the editor specified in P4EDITOR in the p4env.sh script so the user can enter a description.

#!/bin/bash
source $HOME/bin/nautilus/p4env.sh
export P4EDITOR=$P4EDITOR
$P4 $OPTS submit
zenity --info --text "Submit Complete"

The p4sync.sh Script

The p4sync.sh script updates the selected files from the depot. Note that it does not handle resolves or conflicts at this time.

#!/bin/bash
source $HOME/bin/nautilus/p4env.sh
FILES="\n"
for FILE in "$@"
do
    $P4 $OPTS sync $FILE
    FILES=$FILES" $FILE\n"
done
zenity --info --text "Files Updated: $FILES"

The p4add.sh Script

The p4add.sh script adds the selected files to the depot. Note that it does not handle resolves or conflicts at this time.

#!/bin/bash
source $HOME/bin/nautilus/p4env.sh
FILES="\n"
for FILE in "$@"
do
    $P4 $OPTS add $FILE
    FILES=$FILES" $FILE\n"
done
zenity --info --text "Files Added: $FILES"

Hooking the Scripts into Nautilus

Once the scripts have been set up, they need to be hooked into Nautilus. This is done through the "nautilus-actions-config" program.

$ nautilus-actions-config

This will open the "Nautilus Actions" dialog. For each script that you have, you will add a corresponding context action to Nautilus.

  1. Click the +Add button.
  2. Set the Label name to the appropriate action. For example, 'p4 edit'.
  3. Set an icon if you desire. There is a 'P4' icon available in the P4V files in <p4vdir>/lib/p4v/P4VResources/icons/
  4. Select the Main profile and click Edit. (The profile option may not be available in some circumstances)
  5. In the Action tab, set Path to the location of your p4 executable - for example, /usr/bin/p4
  6. Set Parameters to %M. (Note: omit this step for the p4 submit script)
  7. Click on the Conditions tab. Check Appears if selection has multiple files or folders.
  8. Click on Ok. The action will now appear in the Nautilus context menu!
And that is it - you can now sync, add, edit, revert and submit files through Nautilus by right-clicking on a file or group of files. You can easily expand these scripts and actions to include other Perforce actions (such as delete) by creating additional scripts and hooks.

Troubleshooting
If for whatever reason a script does not appear to be working, change the
$P4 $OPTS...

line in the script to

xterm -e "$P4 $OPTS... ; cat"
This will spawn an xterm that contains the error message.


Related Links

Feedback

 

Was this article helpful?


   

Feedback

Please tell us how we can make this article more useful.

Characters Remaining: 255