/// (c) 2007 James A Briant
///
/// Permission is granted to use or modify this source code provided that
/// a) this notice is kept intact
/// b) if you make any modications to this file you assign the
/// copyright of those modifications to James Briant.
/// c) you acknowledge that Mr. Briant offers no warranty
/// and will not be liable for any loss, damages, time, etc.
///
/// Any doubt, email me: firstname (nospace) lastname at persuasivesoftware.com
///
/// This package is inspired by Noel Llopis's P4AddIn (also available from
/// the perforce public depot), and also a similar package that I developed
/// at Bunkspeed, Inc ( www.bunkspeed.com ) makers of real-time raytracing
/// tools that anyone case use.
using System.IO;
using DevDriven.P4Vs;
namespace DevDriven.P4Vs.External
{
enum FileStatus
{
InDepot,
NotInDepot,
OpenedForAdd,
OpenedForEdit,
OpenedForDelete,
OpenedForBranch,
OpenedForIntegrate,
Error,
}
class P4Wrapper
{
private ICmdExec exec;
private ILogger log;
private const string p4cmd = "p4.exe";
public P4Wrapper(ICmdExec exec, ILogger log)
{
this.exec = exec;
this.log = log;
}
public int Add( string path)
{
string args = string.Format("add \"{0}\"", path);
string wd = Path.GetDirectoryName(path);
return Exec(args, wd);
}
private int Exec(string args, string wd)
{
string errors;
string output;
int rv = exec.Exec(p4cmd, args, wd, out output, out errors);
if (errors.Length>0)
{
log.Write(5, "Error: {0}", errors);
return 1;
}
return rv;
}
public int Delete( string path)
{
string args = string.Format("delete \"{0}\"", path);
string wd = Path.GetDirectoryName(path);
return Exec(args,wd);
}
public int Edit(string path)
{
if (Directory.Exists(path))
return 0;
string args = string.Format("edit \"{0}\"", path);
string wd = Path.GetDirectoryName(path);
return Exec(args,wd);
}
public int Revert(string path, bool dontRefreshClient)
{
string args = string.Format("revert {1} \"{0}\"", path, dontRefreshClient?"-k":"");
string wd = Path.GetDirectoryName(path);
return Exec(args,wd);
}
public int Integrate(string oldpath, string newpath)
{
string args = string.Format("integrate \"{0}\" \"{1}\"", oldpath, newpath);
string wd = Path.GetDirectoryName(newpath);
return Exec(args,wd);
}
public FileStatus GetFileStatus(string path)
{
string errors;
string output;
if (exec.Exec(p4cmd, string.Format("fstat \"{0}\"", path), Path.GetDirectoryName(path), out output, out errors) != 0)
{
log.WriteLine(5,"Failed to fstat file {0}:", path);
log.Write(5,"{0}",errors);
return FileStatus.Error;
}
else
{
if (output.Contains("... action add"))
{
return FileStatus.OpenedForAdd;
}
else if (output.Contains("... action edit"))
{
return FileStatus.OpenedForEdit;
}
else if (output.Contains("... action delete"))
{
return FileStatus.OpenedForDelete;
}
else if (output.Contains("... action branch"))
{
return FileStatus.OpenedForBranch;
}
else if (output.Contains("... action integrate"))
{
return FileStatus.OpenedForIntegrate;
}
else if (errors.Contains("no such file"))
{
return FileStatus.NotInDepot;
}
else if (errors.Length > 0)
{
return FileStatus.Error;
}
return FileStatus.InDepot;
}
}
}
}