/*
* px *
Copyright (c) 2008 Shawn Hladky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pxheaders.h"
#include "addrclientuser.h"
#include "addr.h"
ErrorId addr_usage = { ErrorOf( ES_CLIENT, 0, E_INFO, EV_USAGE, 0 ),
"\n"
" addr -- Recursively add local files to Perforce.\n"
"\n"
" px addr [-v] [-h] [path]...\n"
"\n"
" 'px addr' recursively opens for add local files.\n"
" addr must be given a local path with an ending ...\n"
" If no path is given (only a ...), the current directory\n"
" is added.\n"
"\n"
" The -v (verbose) flag causes addr to output messages\n"
" for files already in Perforce. By default those warnings\n"
" are suppressed.\n"
"\n"
" The -h flag causes hidden files to be added as well.\n"
"\n"};
void addrCommand::Help(Error *e)
{
e->Set(addr_usage);
}
void addrCommand::RecurseDir(FileSys* dir)
{
Error e;
StrArray* subfolders = dir->ScanDir(&e);
if (e.Test())
{
// throw an exception
printf("you suck\n");
}
PathSys* pathsys = PathSys::Create();
const StrPtr* subfile;
for(int i = 0; subfile = subfolders->Get(i); i++)
{
// ScanDir seems to give only the relative path
// so append paths here
pathsys->SetLocal(*(dir->Path()), *subfile);
FileSys* subFile = FileSys::Create(FST_BINARY);
subFile->Set(*pathsys);
// only scan hidden files/folders if the -h flag was specified
if ( ( (subFile->Stat() & FSF_HIDDEN) && addHidden) ||
( !(subFile->Stat() & FSF_HIDDEN)) )
{
// printf("%s: %d\n", pathsys->Text(), subFile->Stat());
if ((subFile->Stat() & FSF_DIRECTORY))
{
RecurseDir(subFile);
}
else
{
scannedFiles.Put()->Set(pathsys);
}
}
subFile->Close(&e);
}
}
void addrCommand::Run(ClientUser* cu, ClientApi* api, char *const *argv, int argc)
{
Options opts;
Error e;
int iargc = argc;
char** iargv = (char**) argv;
opts.Parse( iargc, iargv, "nfvhc:t:", OPT_ONE, addr_usage, &e);
if (e.Test())
{
cu->Message(&e);
return;
}
// the argument must end in ...
StrRef path(iargv[0]);
if(path.Length() < 3)
{
// throw an exception
printf("path too short\n");
return;
}
//printf("%s :%d\n", path.End() - 3, strcmp(path.End() - 3, "..."));
if (strcmp(path.End() - 3, "...") != 0)
{
// throw an exception
printf("must end in ...\n");
return;
}
StrBuf cwd, myDir;
HostEnv hostEnv;
hostEnv.GetCwd(cwd);
if (path.Length() > 3)
{
if (path[path.Length() - 4] != PATH_SEP)
{
printf("Must specify a directory\n");
return;
}
// add the local path for a full path
PathSys* pathsys = PathSys::Create();
pathsys->SetLocal(cwd, path);
// this only works if the path is in the form of:
// local\path\...
// if they've omitted the last path seperator
// we are one directory higher than they anticipated.
StrBuf shortPath;
pathsys->ToParent(&shortPath);
myDir.Set(pathsys);
}
else
{
// assume current directory
myDir.Set(cwd);
}
FileSys* topDir = FileSys::Create(FST_BINARY);
topDir->Set(myDir.Text());
if (!(topDir->Stat() & FSF_EXISTS))
{
// throw an exception
printf("Dir not found: %s\n", myDir.Text());
return;
}
if (!(topDir->Stat() & FSF_DIRECTORY))
{
// throw an exception
printf("not a directory\n");
return;
}
addHidden = false;
if (opts['h']) addHidden = true;
bool verbose = false;
if (opts['v']) verbose = true;
RecurseDir(topDir);
topDir->Close(&e);
// pass through the f, n, c, t options
if (opts['f']) api->SetVar("", "-f");
if (opts['n']) api->SetVar("", "-n");
if (opts['c'])
{
api->SetVar("", "-c");
api->SetVar("", opts['c']);
}
if (opts['t'])
{
api->SetVar("", "-t");
api->SetVar("", opts['t']);
}
// spin the scanned files and add them as arguments
const StrBuf* f;
for (int i=0; f = scannedFiles.Get(i); i++)
{
api->SetVar("", f);
}
addrClientUser acu(verbose, cu);
if(tagged) api->SetVar("tag", "");
api->Run("add", &acu);
}