/*******************************************************************************
Copyright (c) 2011, Perforce Software, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
/*******************************************************************************
* Name : Repository.BranchSpec.cs
*
* Author : wjb
*
* Description : BranchSpec operations for the Repository.
*
******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Perforce.P4
{
public partial class Repository
{
///
/// Create a new branch in the repository.
///
/// Branch specification for the new branch
/// The '-i' flag is required when creating a new branch
/// The Branch object if new branch was created, null if creation failed
///
///
///
p4 help branch
///
///
branch -- Create, modify, or delete a branch view specification
///
///
p4 branch [-f] name
///
p4 branch -d [-f] name
///
p4 branch [ -S stream ] [ -P parent ] -o name
///
p4 branch -i [-f]
///
///
A branch specification ('spec') is a named, user-defined mapping of
///
depot files to depot files. It can be used with most of the commands
///
that operate on two sets of files ('copy', 'merge', 'integrate',
///
'diff2', etc.)
///
///
Creating a branch spec does not branch files. To branch files, use
///
'p4 copy', with or without a branch spec.
///
///
The 'branch' command puts the branch spec into a temporary file and
///
invokes the editor configured by the environment variable $P4EDITOR.
///
Saving the file creates or modifies the branch spec.
///
///
The branch spec contains the following fields:
///
///
Branch: The branch spec name (read only).
///
///
Owner: The user who created this branch spec. Can be changed.
///
///
Update: The date this branch spec was last modified.
///
///
Access: The date of the last command used with this spec.
///
///
Description: A description of the branch spec (optional).
///
///
Options: Flags to change the branch spec behavior. The defaults
///
are marked with *.
///
///
locked Permits only the owner to change the spec.
///
unlocked * Prevents the branch spec from being deleted.
///
///
View: Lines mapping of one view of depot files to another.
///
Both the left and right-hand sides of the mappings refer
///
to the depot namespace. See 'p4 help views' for more on
///
view syntax.
///
///
New branch specs are created with a default view that maps all depot
///
files to themselves. This view must be changed before the branch
///
spec can be saved.
///
///
The -d flag deletes the named branch spec.
///
///
The -o flag writes the branch spec to standard output. The user's
///
editor is not invoked.
///
///
The -i flag causes a branch spec to be read from the standard input.
///
The user's editor is not invoked.
///
///
The -f flag enables a user with 'admin' privilege to delete the spec
///
or set the 'last modified' date. By default, specs can be deleted
///
only by their owner.
///
///
A branch spec can also be used to expose the internally generated
///
mapping of a stream to its parent. (See 'p4 help stream' and 'p4
///
help streamintro'.)
///
///
The -S stream flag will expose the internally generated mapping.
///
The -P flag may be used with -S to treat the stream as if it were a
///
child of a different parent. The -o flag is required with -S.
///
///
///
///
/// To create a branch spec [-i]:
///
///
/// BranchSpec newBranchSpec = new BranchSpec();
/// newBranchSpec.Id = "newBranchSpec";
/// newBranchSpec.Owner = "admin";
/// newBranchSpec.Description = " created by perforce";
/// newBranchSpec.ViewMap = new ViewMap();
/// string v0 = "//depot/main/... //depot/rel1/...";
/// string v1 = "//depot/main/... //depot/rel2/...";
/// string v2 = "//depot/dev/... //depot/main/...";
/// newBranchSpec.ViewMap.Add(v0);
/// newBranchSpec.ViewMap.Add(v1);
/// newBranchSpec.ViewMap.Add(v2);
/// Options opts = new Options(BranchSpecCmdFlags.Input);
/// _repository.CreateBranchSpec(newBranchSpec, opts);
///
///
///
///
public BranchSpec CreateBranchSpec(BranchSpec branch, Options options)
{
if (branch == null)
{
throw new ArgumentNullException("branch");
}
P4Command cmd = new P4Command(this, "branch", true);
cmd.DataSet = branch.ToString();
if (options == null)
{
options = new Options((BranchSpecCmdFlags.Input), null, null);
}
if (options.ContainsKey("-i") == false)
{
options["-i"] = null;
}
P4CommandResult results = cmd.Run(options);
if (results.Success)
{
return branch;
}
else
{
P4Exception.Throw(results.ErrorList);
}
return null;
}
///
/// Create a new branch in the repository.
///
/// Branch specification for the new branch
/// The Branch object if new branch was created, null if creation failed
///
/// To create a basic branch spec:
///
///
/// BranchSpec newBranchSpec = new BranchSpec();
/// newBranchSpec.Id = "newBranchSpec";
/// newBranchSpec.ViewMap = new ViewMap();
/// string v0 = "//depot/main/... //depot/rel1/...";
/// newBranchSpec.ViewMap.Add(v0);
/// _repository.CreateBranchSpec(newBranchSpec);
///
///
///
public BranchSpec CreateBranchSpec(BranchSpec branch)
{
return CreateBranchSpec(branch, null);
}
///
/// Update the record for a branch in the repository
///
/// Branch specification for the branch being updated
/// The Branch object if new depot was saved, null if creation failed
///
/// To append a view to an existing branch spec:
///
///
/// BranchSpec updateBranchSpec = _repository.GetBranchSpec("newBranchSpec");
/// string v0 = "\"//depot/main/a file with spaces.txt\" \"//depot/rel1/a file with spaces.txt\"";
/// updateBranchSpec.ViewMap.Add(v0);
/// _repository.UpdateBranchSpec(updateBranchSpec);
///
///
/// To lock a branch spec:
///
///
/// BranchSpec updateBranchSpec = _repository.GetBranchSpec("newBranchSpec");
/// updateBranchSpec.Locked = true;
/// _repository.UpdateBranchSpec(updateBranchSpec);
///
///
///
public BranchSpec UpdateBranchSpec(BranchSpec branch)
{
return CreateBranchSpec(branch, null);
}
///
/// Get the record for an existing branch from the repository.
///
/// Branch name
/// The Branch object if branch was found, null if creation failed
///
/// To get a branch spec:
///
///
/// BranchSpec getBranchSpec = _repository.GetBranchSpec("newBranchSpec");
///
///
///
public BranchSpec GetBranchSpec(string branch, string stream, string parent, Options options)
{
if (branch == null)
{
throw new ArgumentNullException("branch");
}
P4Command cmd = new P4Command(this, "branch", true, branch);
if (options == null)
{
options = new Options((BranchSpecCmdFlags.Output), stream, parent);
}
if (options.ContainsKey("-o") == false)
{
options["-o"] = null;
}
P4CommandResult results = cmd.Run(options);
if (results.Success)
{
if ((results.TaggedOutput == null) || (results.TaggedOutput.Count <= 0))
{
return null;
}
BranchSpec value = new BranchSpec();
value.FromBranchSpecCmdTaggedOutput(results.TaggedOutput[0]);
return value;
}
else
{
P4Exception.Throw(results.ErrorList);
}
return null;
}
public BranchSpec GetBranchSpec(string branch)
{
return GetBranchSpec(branch, null, null, null);
}
///
/// Get a list of branches from the repository
///
/// A list containing the matching branches
///
///
p4 help branches
///
///
branches -- Display list of branch specifications
///
///
p4 branches [-t] [-u user] [[-e|-E] nameFilter -m max]
///
///
Lists branch specifications. (See 'p4 help branch'.)
///
///
The -t flag displays the time as well as the date.
///
///
The -u user flag lists branch specs owned by the specified user.
///
///
The -e nameFilter flag lists branch specs with a name that matches
///
the nameFilter pattern, for example: -e 'svr-dev-rel*'. -E makes
///
the matching case-insensitive.
///
///
The -m max flag limits output to the specified number of branch specs.
///
///
///
///
/// To get all branches and include timestamps [-t] (WARNING, will fetch all branches from the repository):
///
///
/// Options opts = new Options(BranchSpecsCmdFlags.Time, "", "", -1);
/// IList<Branch> branches = _repository.GetBranchSpecs(opts);
///
///
/// To get branches owned by 'Bob' [-u]:
///
///
/// Options opts = new Options(BranchSpecsCmdFlags.None, "Bob", "", -1);
/// IList<Branch> branches = _repository.GetBranchSpecs(opts);
///
///
/// To get the first 10 branches that start with the capital letter 'A' [-m] [-e]:
///
///
/// Options opts = new Options(BranchSpecsCmdFlags.None, "", "A*", 10);
/// IList<Branch> branches = _repository.GetBranchSpecs(opts);
///
///
/// To get the first 10 branches that start with the letter 'A' case insensitive [-m] [-E]:
///
///
/// Options opts = new Options(BranchSpecsCmdFlags.IgnoreCase, "", "A*", 10);
/// IList<Branch> branches = _repository.GetBranchSpecs(opts);
///
///
///
///
public IList GetBranchSpecs(Options options)
{
P4Command cmd = new P4Command(this, "branches", true);
P4CommandResult results = cmd.Run(options);
if (results.Success)
{
if ((results.TaggedOutput == null) || (results.TaggedOutput.Count <= 0))
{
return null;
}
bool dst_mismatch = false;
string offset = string.Empty;
if (Server != null && Server.Metadata != null)
{
offset = Server.Metadata.DateTimeOffset;
dst_mismatch = FormBase.DSTMismatch(Server.Metadata);
}
List value = new List();
foreach (TaggedObject obj in results.TaggedOutput)
{
BranchSpec branch = new BranchSpec();
branch.FromBranchSpecsCmdTaggedOutput(obj,offset,dst_mismatch);
value.Add(branch);
}
return value;
}
else
{
P4Exception.Throw(results.ErrorList);
}
return null;
}
///
/// Delete a branch from the repository
///
/// The branch to be deleted
/// The '-f' and '-d' flags are valid when deleting an
/// existing branch
///
/// To delete a branch spec owned by you [-d implied]:
///
///
/// BranchSpec deleteBranchSpec = new BranchSpec();
/// deleteBranchSpec.Id = "newBranchSpec";
/// _repository.DeleteBranchSpec(deleteBranchSpec, null);
///
///
/// To delete a branch owned by someone other than you [-d implied] [-f requires admin privileges]:
///
///
/// BranchSpec deleteBranchSpec = new BranchSpec();
/// deleteBranchSpec.Id = "newBranchSpec";
/// Options opts = new Options(BranchSpecsCmdFlags.Force);
/// _repository.DeleteBranchSpec(deleteBranchSpec, opts);
///
///
///
///
public void DeleteBranchSpec(BranchSpec branch, Options options)
{
if (branch == null)
{
throw new ArgumentNullException("branch");
}
P4Command cmd = new P4Command(this, "branch", true, branch.Id);
if (options == null)
{
options = new Options(BranchSpecCmdFlags.Delete, null, null);
}
if (options.ContainsKey("-d") == false)
{
options["-d"] = null;
}
P4CommandResult results = cmd.Run(options);
if (results.Success == false)
{
P4Exception.Throw(results.ErrorList);
}
}
}
}