Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions p4-fusion/branch_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
static const std::string EMPTY_STRING = "";
static const std::array<std::string, 2> INVALID_BRANCH_PATH { EMPTY_STRING, EMPTY_STRING };

std::vector<std::string> BranchedFileGroup::GetRelativeFileNames()
{
std::vector<std::string> ret;
for (auto& fileData : files)
{
ret.push_back(fileData.GetRelativePath());
}
return ret;
}

ChangedFileGroups::ChangedFileGroups()
: totalFileCount(0)
{
Expand Down Expand Up @@ -295,7 +285,7 @@ std::unique_ptr<ChangedFileGroups> BranchSet::ParseAffectedFiles(const std::vect

// It's a valid destination to a branch.
// Make sure the relative path is set.
fileData.SetRelativePath(branchPath[1]);
fileData.SetRelativeDepotPath(branchPath[1]);

bool needsHandling = true;
if (fileData.IsIntegrated())
Expand Down Expand Up @@ -326,7 +316,7 @@ std::unique_ptr<ChangedFileGroups> BranchSet::ParseAffectedFiles(const std::vect
{
// It's a non-branching setup.
// Make sure the relative path is set.
fileData.SetRelativePath(relativeDepotPath);
fileData.SetRelativeDepotPath(relativeDepotPath);
branchMap.addTarget(EMPTY_STRING, fileData);
}
}
Expand Down
199 changes: 98 additions & 101 deletions p4-fusion/branch_set.h
Original file line number Diff line number Diff line change
@@ -1,101 +1,98 @@
/*
* Copyright (c) 2022 Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
#pragma once

#include <string>
#include <vector>
#include <array>
#include <memory>
#include <stdexcept>

#include "commands/file_map.h"
#include "commands/file_data.h"
#include "commands/stream_result.h"
#include "utils/std_helpers.h"

struct BranchedFileGroup
{
// If a BranchedFiles collection hasSource == true,
// then all files in this collection MUST be a merge
// from the given source branch to the target branch.
// These branch names will be the Git branch names.
std::string sourceBranch;
std::string targetBranch;
bool hasSource;
std::vector<FileData> files;

// Get all the relative file names from each of the file data.
std::vector<std::string> GetRelativeFileNames();
};

struct ChangedFileGroups
{
private:
ChangedFileGroups();

public:
std::vector<BranchedFileGroup> branchedFileGroups;
int totalFileCount;

// When all the file groups have finished being used,
// only then can we safely clear out the data.
void Clear();

ChangedFileGroups(std::vector<BranchedFileGroup>& groups, int totalFileCount);

static std::unique_ptr<ChangedFileGroups> Empty() { return std::unique_ptr<ChangedFileGroups>(new ChangedFileGroups); };
};

struct Branch
{
public:
const std::string depotBranchPath;
const std::string gitAlias;

Branch(const std::string& branch, const std::string& alias);

// splitBranchPath If the relativeDepotPath matches, returns {branch alias, branch file path}.
// Otherwise, returns {"", ""}
std::array<std::string, 2> SplitBranchPath(const std::string& relativeDepotPath) const;
};

// A singular view on the branches and a base view (acts as a filter to trim down affected files).
// Maps a changed file state to a list of resulting branches and affected files.
struct BranchSet
{
private:
// Technically, these should all be const.
const bool m_includeBinaries;
std::string m_basePath;
const std::vector<Branch> m_branches;
const std::vector<StreamResult::MappingData> m_mappings;
const std::vector<StreamResult::MappingData> m_exclusions;
FileMap m_view;

// stripBasePath remove the base path from the depot path, or "" if not in the base path.
std::string stripBasePath(const std::string& depotPath) const;

// splitBranchPath extract the branch name and path under the branch (no leading '/' on the path)
// relativeDepotPath - already stripped from running stripBasePath.
std::array<std::string, 2> splitBranchPath(const std::string& relativeDepotPath) const;

public:
BranchSet(std::vector<std::string>& clientViewMapping, const std::string& baseDepotPath, const std::vector<std::string>& branches, const std::vector<StreamResult::MappingData>& mappings, const std::vector<StreamResult::MappingData>& exclusions, const bool includeBinaries);

// HasMergeableBranch is there a branch model that requires integration history?
bool HasMergeableBranch() const { return !m_branches.empty(); };

int Count() const { return m_branches.size(); };

// ParseAffectedFiles create collections of merges and commits.
// Breaks up the files into those that are within the view, with each item in the
// list is its own target Git branch.
// This also has the side-effect of populating the relative path value in the file data.
// ... the FileData object is copied, but it's underlying shared data is shared. So, this
// breaks the const.
std::unique_ptr<ChangedFileGroups> ParseAffectedFiles(const std::vector<FileData>& cl) const;
};
/*
* Copyright (c) 2022 Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
#pragma once

#include <string>
#include <vector>
#include <array>
#include <memory>
#include <stdexcept>

#include "commands/file_map.h"
#include "commands/file_data.h"
#include "commands/stream_result.h"
#include "utils/std_helpers.h"

struct BranchedFileGroup
{
// If a BranchedFiles collection hasSource == true,
// then all files in this collection MUST be a merge
// from the given source branch to the target branch.
// These branch names will be the Git branch names.
std::string sourceBranch;
std::string targetBranch;
bool hasSource;
std::vector<FileData> files;
};

struct ChangedFileGroups
{
private:
ChangedFileGroups();

public:
std::vector<BranchedFileGroup> branchedFileGroups;
int totalFileCount;

// When all the file groups have finished being used,
// only then can we safely clear out the data.
void Clear();

ChangedFileGroups(std::vector<BranchedFileGroup>& groups, int totalFileCount);

static std::unique_ptr<ChangedFileGroups> Empty() { return std::unique_ptr<ChangedFileGroups>(new ChangedFileGroups); };
};

struct Branch
{
public:
const std::string depotBranchPath;
const std::string gitAlias;

Branch(const std::string& branch, const std::string& alias);

// splitBranchPath If the relativeDepotPath matches, returns {branch alias, branch file path}.
// Otherwise, returns {"", ""}
std::array<std::string, 2> SplitBranchPath(const std::string& relativeDepotPath) const;
};

// A singular view on the branches and a base view (acts as a filter to trim down affected files).
// Maps a changed file state to a list of resulting branches and affected files.
struct BranchSet
{
private:
// Technically, these should all be const.
const bool m_includeBinaries;
std::string m_basePath;
const std::vector<Branch> m_branches;
const std::vector<StreamResult::MappingData> m_mappings;
const std::vector<StreamResult::MappingData> m_exclusions;
FileMap m_view;

// stripBasePath remove the base path from the depot path, or "" if not in the base path.
std::string stripBasePath(const std::string& depotPath) const;

// splitBranchPath extract the branch name and path under the branch (no leading '/' on the path)
// relativeDepotPath - already stripped from running stripBasePath.
std::array<std::string, 2> splitBranchPath(const std::string& relativeDepotPath) const;

public:
BranchSet(std::vector<std::string>& clientViewMapping, const std::string& baseDepotPath, const std::vector<std::string>& branches, const std::vector<StreamResult::MappingData>& mappings, const std::vector<StreamResult::MappingData>& exclusions, const bool includeBinaries);

// HasMergeableBranch is there a branch model that requires integration history?
bool HasMergeableBranch() const { return !m_branches.empty(); };

int Count() const { return m_branches.size(); };

// ParseAffectedFiles create collections of merges and commits.
// Breaks up the files into those that are within the view, with each item in the
// list is its own target Git branch.
// This also has the side-effect of populating the relative path value in the file data.
// ... the FileData object is copied, but it's underlying shared data is shared. So, this
// breaks the const.
std::unique_ptr<ChangedFileGroups> ParseAffectedFiles(const std::vector<FileData>& cl) const;
};
2 changes: 1 addition & 1 deletion p4-fusion/commands/file_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void FileData::SetPendingDownload()
}
}

void FileData::SetRelativePath(std::string& relativePath)
void FileData::SetRelativeDepotPath(const std::string& relativePath)
{
m_data->relativePath = relativePath;
}
Expand Down
4 changes: 2 additions & 2 deletions p4-fusion/commands/file_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct FileData
FileData& operator=(FileData& other);

void SetFromDepotFile(const std::string& fromDepotFile, const std::string& fromRevision);
void SetRelativePath(std::string& relativePath);
void SetRelativeDepotPath(const std::string& relativePath);
void SetFakeIntegrationDeleteAction() { m_data->SetAction(FAKE_INTEGRATION_DELETE_ACTION_NAME); };

// moves the argument's data into this file data structure.
Expand All @@ -88,7 +88,7 @@ struct FileData
const std::string& GetDepotFile() const { return m_data->depotFile; };
const std::string& GetRevision() const { return m_data->revision; };
const FileAction GetAction() const { return m_data->actionCategory; };
const std::string& GetRelativePath() const { return m_data->relativePath; };
const std::string& GetRelativeDepotPath() const { return m_data->relativePath; };
const std::vector<char>& GetContents() const { return m_data->contents; };
bool IsDeleted() const { return m_data->isDeleted; };
bool IsIntegrated() const { return m_data->isIntegrated; };
Expand Down
6 changes: 4 additions & 2 deletions p4-fusion/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "utils/std_helpers.h"
#include "utils/timer.h"
#include "utils/arguments.h"
#include "utils/p4_helpers.h"

#include "thread_pool.h"
#include "p4_api.h"
Expand Down Expand Up @@ -390,13 +391,14 @@ int Main(int argc, char** argv)

for (auto& file : branchGroup.files)
{
const std::string unescapedPath = P4Unescape(file.GetRelativeDepotPath());
if (file.IsDeleted())
{
git.RemoveFileFromIndex(file.GetRelativePath());
git.RemoveFileFromIndex(unescapedPath);
}
else
{
git.AddFileToIndex(file.GetRelativePath(), file.GetContents(), file.IsExecutable());
git.AddFileToIndex(unescapedPath, file.GetContents(), file.IsExecutable());
}

// No use for keeping the contents in memory once it has been added
Expand Down
19 changes: 19 additions & 0 deletions p4-fusion/utils/p4_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2022 Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
#include "p4_helpers.h"

#include "std_helpers.h"

std::string P4Unescape(std::string p4path)
{
STDHelpers::ReplaceAll(p4path, "%40", "@");
STDHelpers::ReplaceAll(p4path, "%23", "#");
STDHelpers::ReplaceAll(p4path, "%2A", "*");
// Replace %25 last to avoid interfering with other escape sequences
STDHelpers::ReplaceAll(p4path, "%25", "%");
return p4path;
}
11 changes: 11 additions & 0 deletions p4-fusion/utils/p4_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2022 Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
#pragma once

#include <string>

std::string P4Unescape(std::string p4path);
19 changes: 19 additions & 0 deletions p4-fusion/utils/std_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ void STDHelpers::StripSurrounding(std::string& source, const char c)
}
}

std::string STDHelpers::ToLower(const std::string& source)
{
std::string result = source;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}

void STDHelpers::ReplaceAll(std::string& str, const std::string& find, const std::string& replace)
{
std::string::size_type pos;
std::string::size_type startPos = 0;

while ((pos = str.find(find, startPos)) != std::string::npos)
{
str.replace(pos, find.length(), replace);
startPos = pos + replace.length();
}
}

std::array<std::string, 2> STDHelpers::SplitAt(const std::string& source, const char c, const size_t startAt)
{
size_t pos = source.find(c, startAt);
Expand Down
Loading
Loading