2018-03-21 02:34:03 +03:00
|
|
|
/*
|
2019-06-20 02:58:25 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-03-21 02:34:03 +03:00
|
|
|
*
|
2019-06-20 02:58:25 +03:00
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
2018-03-21 02:34:03 +03:00
|
|
|
*/
|
2019-10-11 15:26:59 +03:00
|
|
|
|
2018-03-21 02:34:03 +03:00
|
|
|
#include "eden/fs/store/Diff.h"
|
|
|
|
|
|
|
|
#include <folly/Portability.h>
|
|
|
|
#include <folly/Synchronized.h>
|
|
|
|
#include <folly/futures/Future.h>
|
2018-05-01 07:20:51 +03:00
|
|
|
#include <folly/logging/xlog.h>
|
2018-03-21 02:34:03 +03:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "eden/fs/model/Tree.h"
|
|
|
|
#include "eden/fs/model/TreeEntry.h"
|
2020-01-29 22:15:29 +03:00
|
|
|
#include "eden/fs/model/git/GitIgnoreStack.h"
|
2019-10-11 20:42:16 +03:00
|
|
|
#include "eden/fs/store/DiffContext.h"
|
2018-03-21 02:34:03 +03:00
|
|
|
#include "eden/fs/store/ObjectStore.h"
|
2019-09-24 01:19:29 +03:00
|
|
|
#include "eden/fs/store/ScmStatusDiffCallback.h"
|
2020-02-03 22:28:16 +03:00
|
|
|
#include "eden/fs/utils/Future.h"
|
2018-03-21 02:34:03 +03:00
|
|
|
#include "eden/fs/utils/PathFuncs.h"
|
|
|
|
|
|
|
|
using folly::Future;
|
|
|
|
using folly::makeFuture;
|
|
|
|
using folly::Try;
|
|
|
|
using folly::Unit;
|
|
|
|
using std::make_unique;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
/*
|
|
|
|
* In practice, while the functions in this file are comparing two source
|
|
|
|
* control Tree objects, they are used for comparing the current
|
|
|
|
* (non-materialized) working directory state (as wdTree) to its corresponding
|
|
|
|
* source control state (as scmTree).
|
|
|
|
*/
|
2018-03-21 02:34:03 +03:00
|
|
|
namespace {
|
|
|
|
|
2019-10-11 20:42:16 +03:00
|
|
|
struct ChildFutures {
|
|
|
|
void add(RelativePath&& path, Future<Unit>&& future) {
|
|
|
|
paths.emplace_back(std::move(path));
|
|
|
|
futures.emplace_back(std::move(future));
|
|
|
|
}
|
2018-03-21 02:34:03 +03:00
|
|
|
|
2019-10-11 20:42:16 +03:00
|
|
|
vector<RelativePath> paths;
|
|
|
|
vector<Future<Unit>> futures;
|
2018-03-21 02:34:03 +03:00
|
|
|
};
|
|
|
|
|
2019-10-11 20:42:16 +03:00
|
|
|
struct DiffState {
|
|
|
|
explicit DiffState(const ObjectStore* store)
|
|
|
|
: callback{}, context{&callback, store} {}
|
|
|
|
|
|
|
|
ScmStatusDiffCallback callback;
|
|
|
|
DiffContext context;
|
|
|
|
};
|
2018-03-21 02:34:03 +03:00
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
static constexpr PathComponentPiece kIgnoreFilename{".gitignore"};
|
2018-03-21 02:34:03 +03:00
|
|
|
|
2019-10-11 20:42:16 +03:00
|
|
|
Future<Unit> diffAddedTree(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece entryPath,
|
|
|
|
const Tree& wdTree,
|
|
|
|
const GitIgnoreStack* ignore,
|
|
|
|
bool isIgnored);
|
2019-10-11 20:42:16 +03:00
|
|
|
|
|
|
|
Future<Unit> diffRemovedTree(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece entryPath,
|
|
|
|
const Tree& scmTree);
|
2019-10-11 20:42:16 +03:00
|
|
|
|
|
|
|
void processAddedSide(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2019-10-11 20:42:16 +03:00
|
|
|
ChildFutures& childFutures,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const TreeEntry& wdEntry,
|
|
|
|
const GitIgnoreStack* ignore,
|
|
|
|
bool isIgnored);
|
2019-10-11 20:42:16 +03:00
|
|
|
|
|
|
|
void processRemovedSide(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2019-10-11 20:42:16 +03:00
|
|
|
ChildFutures& childFutures,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const TreeEntry& scmEntry);
|
2019-10-11 20:42:16 +03:00
|
|
|
|
|
|
|
void processBothPresent(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2019-10-11 20:42:16 +03:00
|
|
|
ChildFutures& childFutures,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const TreeEntry& scmEntry,
|
|
|
|
const TreeEntry& wdEntry,
|
|
|
|
const GitIgnoreStack* ignore,
|
|
|
|
bool isIgnored);
|
2019-10-11 20:42:16 +03:00
|
|
|
|
2020-02-06 00:13:11 +03:00
|
|
|
Future<Unit> waitOnResults(DiffContext* context, ChildFutures&& childFutures);
|
2019-10-11 20:42:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Diff two trees.
|
|
|
|
*
|
|
|
|
* The path argument specifies the path to these trees, and will be prefixed
|
|
|
|
* to all differences recorded in the results.
|
|
|
|
*
|
|
|
|
* The differences will be recorded using a callback provided by the caller.
|
|
|
|
*/
|
2020-01-29 22:15:29 +03:00
|
|
|
FOLLY_NODISCARD Future<Unit> computeTreeDiff(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const Tree& scmTree,
|
|
|
|
const Tree& wdTree,
|
|
|
|
std::unique_ptr<GitIgnoreStack> ignore,
|
|
|
|
bool isIgnored) {
|
2018-03-21 02:34:03 +03:00
|
|
|
// A list of Futures to wait on for our children's results.
|
|
|
|
ChildFutures childFutures;
|
|
|
|
|
|
|
|
// Walk through the entries in both trees.
|
|
|
|
// This relies on the fact that the entry list in each tree is always sorted.
|
2020-01-29 22:15:29 +03:00
|
|
|
const auto& scmEntries = scmTree.getTreeEntries();
|
|
|
|
const auto& wdEntries = wdTree.getTreeEntries();
|
|
|
|
size_t scmIdx = 0;
|
|
|
|
size_t wdIdx = 0;
|
2018-03-21 02:34:03 +03:00
|
|
|
while (true) {
|
2020-01-29 22:15:29 +03:00
|
|
|
if (scmIdx >= scmEntries.size()) {
|
|
|
|
if (wdIdx >= wdEntries.size()) {
|
2018-03-21 02:34:03 +03:00
|
|
|
// All Done
|
|
|
|
break;
|
|
|
|
}
|
2020-01-29 22:15:29 +03:00
|
|
|
// This entry is present in wdTree but not scmTree
|
|
|
|
processAddedSide(
|
|
|
|
context,
|
|
|
|
childFutures,
|
|
|
|
currentPath,
|
|
|
|
wdEntries[wdIdx],
|
|
|
|
ignore.get(),
|
|
|
|
isIgnored);
|
|
|
|
++wdIdx;
|
|
|
|
} else if (wdIdx >= wdEntries.size()) {
|
|
|
|
// This entry is present in scmTree but not wdTree
|
|
|
|
processRemovedSide(
|
|
|
|
context, childFutures, currentPath, scmEntries[scmIdx]);
|
|
|
|
++scmIdx;
|
|
|
|
} else if (scmEntries[scmIdx].getName() < wdEntries[wdIdx].getName()) {
|
|
|
|
processRemovedSide(
|
|
|
|
context, childFutures, currentPath, scmEntries[scmIdx]);
|
|
|
|
++scmIdx;
|
|
|
|
} else if (scmEntries[scmIdx].getName() > wdEntries[wdIdx].getName()) {
|
|
|
|
processAddedSide(
|
|
|
|
context,
|
|
|
|
childFutures,
|
|
|
|
currentPath,
|
|
|
|
wdEntries[wdIdx],
|
|
|
|
ignore.get(),
|
|
|
|
isIgnored);
|
|
|
|
++wdIdx;
|
2018-03-21 02:34:03 +03:00
|
|
|
} else {
|
2019-10-11 20:42:16 +03:00
|
|
|
processBothPresent(
|
2020-01-29 22:15:29 +03:00
|
|
|
context,
|
|
|
|
childFutures,
|
|
|
|
currentPath,
|
|
|
|
scmEntries[scmIdx],
|
|
|
|
wdEntries[wdIdx],
|
|
|
|
ignore.get(),
|
|
|
|
isIgnored);
|
|
|
|
++scmIdx;
|
|
|
|
++wdIdx;
|
2018-03-21 02:34:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
// Add an ensure() block that makes sure the ignore stack exists until all of
|
|
|
|
// our children results have finished processing
|
|
|
|
return waitOnResults(context, std::move(childFutures))
|
|
|
|
.ensure([ignore = std::move(ignore)] {});
|
|
|
|
}
|
|
|
|
|
|
|
|
FOLLY_NODISCARD Future<Unit> loadGitIgnoreThenDiffTrees(
|
|
|
|
const TreeEntry& gitIgnoreEntry,
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const Tree& scmTree,
|
|
|
|
const Tree& wdTree,
|
|
|
|
const GitIgnoreStack* parentIgnore,
|
|
|
|
bool isIgnored) {
|
|
|
|
// TODO: load file contents directly from context->store if gitIgnoreEntry is
|
|
|
|
// a regular file
|
|
|
|
auto loadFileContentsFromPath = context->getLoadFileContentsFromPath();
|
2020-02-06 00:13:11 +03:00
|
|
|
return loadFileContentsFromPath(
|
|
|
|
context->getFetchContext(), currentPath + gitIgnoreEntry.getName())
|
2020-01-29 22:15:29 +03:00
|
|
|
.thenError([entryPath = currentPath + gitIgnoreEntry.getName()](
|
|
|
|
const folly::exception_wrapper& ex) {
|
|
|
|
// TODO: add an API to DiffCallback to report user errors like this
|
|
|
|
// (errors that do not indicate a problem with EdenFS itself) that can
|
|
|
|
// be returned to the caller in a thrift response
|
|
|
|
XLOG(WARN) << "error loading gitignore at " << entryPath << ": "
|
|
|
|
<< folly::exceptionStr(ex);
|
|
|
|
return std::string{};
|
|
|
|
})
|
|
|
|
.thenValue([context,
|
|
|
|
currentPath = currentPath.copy(),
|
|
|
|
scmTree,
|
|
|
|
wdTree,
|
|
|
|
parentIgnore,
|
|
|
|
isIgnored](std::string&& ignoreFileContents) mutable {
|
|
|
|
return computeTreeDiff(
|
|
|
|
context,
|
|
|
|
currentPath,
|
|
|
|
scmTree,
|
|
|
|
wdTree,
|
|
|
|
make_unique<GitIgnoreStack>(parentIgnore, ignoreFileContents),
|
|
|
|
isIgnored);
|
|
|
|
});
|
2019-10-11 20:42:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FOLLY_NODISCARD Future<Unit> diffTrees(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const Tree& scmTree,
|
|
|
|
const Tree& wdTree,
|
|
|
|
const GitIgnoreStack* parentIgnore,
|
|
|
|
bool isIgnored) {
|
2020-01-29 22:15:29 +03:00
|
|
|
if (context->isCancelled()) {
|
|
|
|
XLOG(DBG7) << "diff() on directory " << currentPath
|
|
|
|
<< " cancelled due to client request no longer being active";
|
|
|
|
return makeFuture();
|
|
|
|
}
|
2020-01-29 22:15:29 +03:00
|
|
|
// If this directory is already ignored, we don't need to bother loading its
|
|
|
|
// .gitignore file. Everything inside this directory must also be ignored,
|
|
|
|
// unless it is explicitly tracked in source control.
|
|
|
|
//
|
|
|
|
// Explicit include rules cannot be used to unignore files inside an ignored
|
|
|
|
// directory.
|
|
|
|
//
|
|
|
|
// We check context->getLoadFileContentsFromPath() here as a way to see if we
|
|
|
|
// are processing gitIgnore files or not, since this is only set from code
|
|
|
|
// that enters through eden/fs/inodes/Diff.cpp. Either way, it is
|
|
|
|
// impossible to load file contents without this set.
|
|
|
|
if (isIgnored || !context->getLoadFileContentsFromPath()) {
|
|
|
|
// We can pass in a null GitIgnoreStack pointer here.
|
|
|
|
// Since the entire directory is ignored, we don't need to check ignore
|
|
|
|
// status for any entries that aren't already tracked in source control.
|
|
|
|
return computeTreeDiff(
|
|
|
|
context, currentPath, scmTree, wdTree, nullptr, isIgnored);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this directory has a .gitignore file, load it first.
|
|
|
|
const auto* gitIgnoreEntry = wdTree.getEntryPtr(kIgnoreFilename);
|
|
|
|
if (gitIgnoreEntry && !gitIgnoreEntry->isTree()) {
|
|
|
|
return loadGitIgnoreThenDiffTrees(
|
|
|
|
*gitIgnoreEntry,
|
2019-10-11 20:42:16 +03:00
|
|
|
context,
|
2020-01-29 22:15:29 +03:00
|
|
|
currentPath,
|
|
|
|
scmTree,
|
|
|
|
wdTree,
|
|
|
|
parentIgnore,
|
|
|
|
isIgnored);
|
2019-10-11 20:42:16 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
return computeTreeDiff(
|
|
|
|
context,
|
|
|
|
currentPath,
|
|
|
|
scmTree,
|
|
|
|
wdTree,
|
|
|
|
make_unique<GitIgnoreStack>(parentIgnore), // empty with no rules
|
|
|
|
isIgnored);
|
2019-10-11 20:42:16 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
FOLLY_NODISCARD Future<Unit> processAddedChildren(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const Tree& wdTree,
|
|
|
|
std::unique_ptr<GitIgnoreStack> ignore,
|
|
|
|
bool isIgnored) {
|
|
|
|
ChildFutures childFutures;
|
|
|
|
for (const auto& childEntry : wdTree.getTreeEntries()) {
|
|
|
|
processAddedSide(
|
|
|
|
context,
|
|
|
|
childFutures,
|
|
|
|
currentPath,
|
|
|
|
childEntry,
|
|
|
|
ignore.get(),
|
|
|
|
isIgnored);
|
2019-09-24 01:19:29 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
// Add an ensure() block that makes sure the ignore stack exists until all of
|
|
|
|
// our children results have finished processing
|
|
|
|
return waitOnResults(context, std::move(childFutures))
|
|
|
|
.ensure([ignore = std::move(ignore)] {});
|
2019-09-24 01:19:29 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
FOLLY_NODISCARD Future<Unit> loadGitIgnoreThenProcessAddedChildren(
|
|
|
|
const TreeEntry& gitIgnoreEntry,
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const Tree& wdTree,
|
|
|
|
const GitIgnoreStack* parentIgnore,
|
|
|
|
bool isIgnored) {
|
|
|
|
auto loadFileContentsFromPath = context->getLoadFileContentsFromPath();
|
2020-02-06 00:13:11 +03:00
|
|
|
return loadFileContentsFromPath(
|
|
|
|
context->getFetchContext(), currentPath + gitIgnoreEntry.getName())
|
2020-01-29 22:15:29 +03:00
|
|
|
.thenError([entryPath = currentPath + gitIgnoreEntry.getName()](
|
|
|
|
const folly::exception_wrapper& ex) {
|
|
|
|
XLOG(WARN) << "error loading gitignore at " << entryPath << ": "
|
|
|
|
<< folly::exceptionStr(ex);
|
|
|
|
return std::string{};
|
|
|
|
})
|
|
|
|
.thenValue([context,
|
|
|
|
currentPath = currentPath.copy(),
|
|
|
|
wdTree,
|
|
|
|
parentIgnore,
|
|
|
|
isIgnored](std::string&& ignoreFileContents) mutable {
|
|
|
|
return processAddedChildren(
|
|
|
|
context,
|
|
|
|
currentPath,
|
|
|
|
wdTree,
|
|
|
|
make_unique<GitIgnoreStack>(parentIgnore, ignoreFileContents),
|
|
|
|
isIgnored);
|
2018-03-21 02:34:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a Tree that is present only on one side of the diff.
|
|
|
|
*/
|
2020-01-29 22:15:29 +03:00
|
|
|
FOLLY_NODISCARD Future<Unit> diffAddedTree(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const Tree& wdTree,
|
|
|
|
const GitIgnoreStack* parentIgnore,
|
|
|
|
bool isIgnored) {
|
2020-01-29 22:15:29 +03:00
|
|
|
if (context->isCancelled()) {
|
|
|
|
XLOG(DBG7) << "diff() on directory " << currentPath
|
|
|
|
<< " cancelled due to client request no longer being active";
|
|
|
|
return makeFuture();
|
|
|
|
}
|
2019-09-24 01:19:29 +03:00
|
|
|
ChildFutures childFutures;
|
2020-01-29 22:15:29 +03:00
|
|
|
|
|
|
|
// If this directory is already ignored, we don't need to bother loading its
|
|
|
|
// .gitignore file. Everything inside this directory must also be ignored,
|
|
|
|
// unless it is explicitly tracked in source control.
|
|
|
|
//
|
|
|
|
// Also, if we are not honoring gitignored files, then do not bother loading
|
|
|
|
// its .gitignore file
|
|
|
|
//
|
|
|
|
// Explicit include rules cannot be used to unignore files inside an ignored
|
|
|
|
// directory.
|
|
|
|
//
|
|
|
|
// We check context->getLoadFileContentsFromPath() here as a way to see if we
|
|
|
|
// are processing gitIgnore files or not, since this is only set from code
|
|
|
|
// that enters through eden/fs/inodes/DiffTree.cpp. Either way, it is
|
|
|
|
// impossible to load file contents without this set.
|
|
|
|
if (isIgnored || !context->getLoadFileContentsFromPath()) {
|
|
|
|
// We can pass in a null GitIgnoreStack pointer here.
|
|
|
|
// Since the entire directory is ignored, we don't need to check ignore
|
|
|
|
// status for any entries that aren't already tracked in source control.
|
|
|
|
return processAddedChildren(
|
|
|
|
context, currentPath, wdTree, nullptr, isIgnored);
|
2019-09-24 01:19:29 +03:00
|
|
|
}
|
2020-01-29 22:15:29 +03:00
|
|
|
|
|
|
|
// If this directory has a .gitignore file, load it first.
|
|
|
|
const auto* gitIgnoreEntry = wdTree.getEntryPtr(kIgnoreFilename);
|
|
|
|
if (gitIgnoreEntry && !gitIgnoreEntry->isTree()) {
|
|
|
|
return loadGitIgnoreThenProcessAddedChildren(
|
|
|
|
*gitIgnoreEntry, context, currentPath, wdTree, parentIgnore, isIgnored);
|
|
|
|
}
|
|
|
|
|
|
|
|
return processAddedChildren(
|
|
|
|
context,
|
|
|
|
currentPath,
|
|
|
|
wdTree,
|
|
|
|
make_unique<GitIgnoreStack>(parentIgnore), // empty with no rules
|
|
|
|
isIgnored);
|
2019-09-24 01:19:29 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:42:16 +03:00
|
|
|
/**
|
|
|
|
* Process a Tree that is present only on one side of the diff.
|
|
|
|
*/
|
2020-01-29 22:15:29 +03:00
|
|
|
FOLLY_NODISCARD Future<Unit> diffRemovedTree(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const Tree& scmTree) {
|
2020-01-29 22:15:29 +03:00
|
|
|
if (context->isCancelled()) {
|
|
|
|
XLOG(DBG7) << "diff() on directory " << currentPath
|
|
|
|
<< " cancelled due to client request no longer being active";
|
|
|
|
return makeFuture();
|
|
|
|
}
|
2018-03-21 02:34:03 +03:00
|
|
|
ChildFutures childFutures;
|
2020-01-29 22:15:29 +03:00
|
|
|
for (const auto& childEntry : scmTree.getTreeEntries()) {
|
|
|
|
processRemovedSide(context, childFutures, currentPath, childEntry);
|
2018-03-21 02:34:03 +03:00
|
|
|
}
|
2019-10-11 20:42:16 +03:00
|
|
|
return waitOnResults(context, std::move(childFutures));
|
2018-03-21 02:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a TreeEntry that is present only on one side of the diff.
|
|
|
|
* We don't know yet if this TreeEntry refers to a Tree or a Blob.
|
|
|
|
*
|
|
|
|
* If we could not compute a result immediately we will add an entry to
|
|
|
|
* childFutures.
|
|
|
|
*/
|
2019-10-11 20:42:16 +03:00
|
|
|
void processRemovedSide(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2018-03-21 02:34:03 +03:00
|
|
|
ChildFutures& childFutures,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const TreeEntry& scmEntry) {
|
|
|
|
if (!scmEntry.isTree()) {
|
|
|
|
context->callback->removedFile(currentPath + scmEntry.getName());
|
2018-03-21 02:34:03 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-01-29 22:15:29 +03:00
|
|
|
auto entryPath = currentPath + scmEntry.getName();
|
|
|
|
auto childFuture = diffRemovedTree(context, entryPath, scmEntry.getHash());
|
|
|
|
childFutures.add(std::move(entryPath), std::move(childFuture));
|
2019-09-24 01:19:29 +03:00
|
|
|
}
|
2018-03-21 02:34:03 +03:00
|
|
|
|
2019-09-24 01:19:29 +03:00
|
|
|
/**
|
|
|
|
* Process a TreeEntry that is present only on one side of the diff.
|
|
|
|
* We don't know yet if this TreeEntry refers to a Tree or a Blob.
|
|
|
|
*
|
|
|
|
* If we could not compute a result immediately we will add an entry to
|
|
|
|
* childFutures.
|
|
|
|
*/
|
2019-10-11 20:42:16 +03:00
|
|
|
void processAddedSide(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2019-09-24 01:19:29 +03:00
|
|
|
ChildFutures& childFutures,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const TreeEntry& wdEntry,
|
|
|
|
const GitIgnoreStack* ignore,
|
|
|
|
bool isIgnored) {
|
|
|
|
bool entryIgnored = isIgnored;
|
|
|
|
auto entryPath = currentPath + wdEntry.getName();
|
|
|
|
if (!isIgnored && ignore) {
|
|
|
|
auto fileType =
|
|
|
|
wdEntry.isTree() ? GitIgnore::TYPE_DIR : GitIgnore::TYPE_FILE;
|
|
|
|
auto ignoreStatus = ignore->match(entryPath, fileType);
|
|
|
|
if (ignoreStatus == GitIgnore::HIDDEN) {
|
|
|
|
// Completely skip over hidden entries.
|
|
|
|
// This is used for reserved directories like .hg and .eden
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
entryIgnored = (ignoreStatus == GitIgnore::EXCLUDE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wdEntry.isTree()) {
|
|
|
|
if (!entryIgnored || context->listIgnored) {
|
|
|
|
auto childFuture = diffAddedTree(
|
|
|
|
context, entryPath, wdEntry.getHash(), ignore, entryIgnored);
|
|
|
|
childFutures.add(std::move(entryPath), std::move(childFuture));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!entryIgnored) {
|
|
|
|
context->callback->addedFile(entryPath);
|
|
|
|
} else if (context->listIgnored) {
|
|
|
|
context->callback->ignoredFile(entryPath);
|
|
|
|
} else {
|
|
|
|
// Don't bother reporting this ignored file since
|
|
|
|
// listIgnored is false.
|
|
|
|
}
|
2019-09-24 01:19:29 +03:00
|
|
|
}
|
2018-03-21 02:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process TreeEntry objects that exist on both sides of the diff.
|
|
|
|
*/
|
2019-10-11 20:42:16 +03:00
|
|
|
void processBothPresent(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2018-03-21 02:34:03 +03:00
|
|
|
ChildFutures& childFutures,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
const TreeEntry& scmEntry,
|
|
|
|
const TreeEntry& wdEntry,
|
|
|
|
const GitIgnoreStack* ignore,
|
|
|
|
bool isIgnored) {
|
|
|
|
bool entryIgnored = isIgnored;
|
|
|
|
auto entryPath = currentPath + scmEntry.getName();
|
|
|
|
// If wdEntry and scmEntry are both files (or symlinks) then we don't need
|
|
|
|
// to bother computing the ignore status: the file is explicitly tracked in
|
|
|
|
// source control, so we should report it's status even if it would normally
|
|
|
|
// be ignored.
|
|
|
|
if (!isIgnored && (wdEntry.isTree() || scmEntry.isTree()) && ignore) {
|
|
|
|
auto fileType =
|
|
|
|
wdEntry.isTree() ? GitIgnore::TYPE_DIR : GitIgnore::TYPE_FILE;
|
|
|
|
auto ignoreStatus = ignore->match(entryPath, fileType);
|
|
|
|
if (ignoreStatus == GitIgnore::HIDDEN) {
|
|
|
|
// This is rather unexpected. We don't expect to find entries in
|
|
|
|
// source control using reserved hidden names.
|
|
|
|
// Treat this as ignored for now.
|
|
|
|
entryIgnored = true;
|
|
|
|
} else if (ignoreStatus == GitIgnore::EXCLUDE) {
|
|
|
|
entryIgnored = true;
|
|
|
|
} else {
|
|
|
|
entryIgnored = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isTreeSCM = scmEntry.isTree();
|
|
|
|
bool isTreeWD = wdEntry.isTree();
|
|
|
|
|
|
|
|
if (isTreeSCM) {
|
|
|
|
if (isTreeWD) {
|
2018-03-21 02:34:03 +03:00
|
|
|
// tree-to-tree diff
|
2020-11-11 03:29:24 +03:00
|
|
|
XDCHECK_EQ(scmEntry.getType(), wdEntry.getType());
|
2020-01-29 22:15:29 +03:00
|
|
|
if (scmEntry.getHash() == wdEntry.getHash()) {
|
2018-03-21 02:34:03 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-01-29 22:15:29 +03:00
|
|
|
auto childFuture = diffTrees(
|
|
|
|
context,
|
|
|
|
entryPath,
|
|
|
|
scmEntry.getHash(),
|
|
|
|
wdEntry.getHash(),
|
|
|
|
ignore,
|
|
|
|
entryIgnored);
|
|
|
|
childFutures.add(std::move(entryPath), std::move(childFuture));
|
2018-03-21 02:34:03 +03:00
|
|
|
} else {
|
|
|
|
// tree-to-file
|
2020-01-29 22:15:29 +03:00
|
|
|
// Add a ADDED entry for this path
|
|
|
|
if (entryIgnored) {
|
|
|
|
if (context->listIgnored) {
|
|
|
|
context->callback->ignoredFile(entryPath);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
context->callback->addedFile(entryPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report everything in scmTree as REMOVED
|
|
|
|
auto childFuture =
|
|
|
|
diffRemovedTree(context, entryPath, scmEntry.getHash());
|
|
|
|
childFutures.add(std::move(entryPath), std::move(childFuture));
|
2018-03-21 02:34:03 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-29 22:15:29 +03:00
|
|
|
if (isTreeWD) {
|
2018-03-21 02:34:03 +03:00
|
|
|
// file-to-tree
|
|
|
|
// Add a REMOVED entry for this path
|
2020-01-29 22:15:29 +03:00
|
|
|
context->callback->removedFile(entryPath);
|
2019-09-24 01:19:29 +03:00
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
// Report everything in wdEntry as ADDED
|
|
|
|
auto childFuture = diffAddedTree(
|
|
|
|
context, entryPath, wdEntry.getHash(), ignore, entryIgnored);
|
|
|
|
childFutures.add(std::move(entryPath), std::move(childFuture));
|
2018-03-21 02:34:03 +03:00
|
|
|
} else {
|
|
|
|
// file-to-file diff
|
2019-11-22 19:07:59 +03:00
|
|
|
// Even if blobs have different hashes, they could have the same contents.
|
|
|
|
// For example, if between the two revisions being compared, if a file was
|
|
|
|
// changed and then later reverted. In that case, the contents would be
|
|
|
|
// the same but the blobs would have different hashes
|
|
|
|
// If the types are different, then this entry is definitely modified
|
2020-01-29 22:15:29 +03:00
|
|
|
if (scmEntry.getType() != wdEntry.getType()) {
|
|
|
|
context->callback->modifiedFile(entryPath);
|
2019-11-22 19:07:59 +03:00
|
|
|
} else {
|
|
|
|
// If Mercurial eventually switches to using blob IDs that are solely
|
|
|
|
// based on the file contents (as opposed to file contents + history)
|
|
|
|
// then we could drop this extra load of the blob SHA-1, and rely only
|
|
|
|
// on the blob ID comparison instead.
|
2020-01-29 22:15:29 +03:00
|
|
|
auto compareEntryContents =
|
|
|
|
folly::makeFutureWith([context,
|
|
|
|
entryPath = currentPath + scmEntry.getName(),
|
|
|
|
&scmEntry,
|
|
|
|
&wdEntry] {
|
2020-02-06 00:13:11 +03:00
|
|
|
auto scmFuture = context->store->getBlobSha1(
|
|
|
|
scmEntry.getHash(), context->getFetchContext());
|
|
|
|
auto wdFuture = context->store->getBlobSha1(
|
|
|
|
wdEntry.getHash(), context->getFetchContext());
|
2020-02-03 22:28:16 +03:00
|
|
|
return collectSafe(scmFuture, wdFuture)
|
2020-01-29 22:15:29 +03:00
|
|
|
.thenValue([entryPath = entryPath.copy(),
|
|
|
|
context](const std::tuple<Hash, Hash>& info) {
|
|
|
|
const auto& [scmHash, wdHash] = info;
|
|
|
|
if (scmHash != wdHash) {
|
|
|
|
context->callback->modifiedFile(entryPath);
|
2019-11-22 19:07:59 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-01-29 22:15:29 +03:00
|
|
|
childFutures.add(std::move(entryPath), std::move(compareEntryContents));
|
2018-03-21 02:34:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
FOLLY_NODISCARD Future<Unit> waitOnResults(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2019-10-11 20:42:16 +03:00
|
|
|
ChildFutures&& childFutures) {
|
2020-11-11 03:29:24 +03:00
|
|
|
XDCHECK_EQ(childFutures.paths.size(), childFutures.futures.size());
|
2018-03-21 02:34:03 +03:00
|
|
|
if (childFutures.futures.empty()) {
|
|
|
|
return makeFuture();
|
|
|
|
}
|
|
|
|
|
2020-04-04 03:12:59 +03:00
|
|
|
return folly::collectAll(std::move(childFutures.futures))
|
2018-05-26 23:54:11 +03:00
|
|
|
.toUnsafeFuture()
|
2019-10-11 20:42:16 +03:00
|
|
|
.thenValue([context, paths = std::move(childFutures.paths)](
|
2018-09-15 02:57:46 +03:00
|
|
|
vector<Try<Unit>>&& results) {
|
2020-11-11 03:29:24 +03:00
|
|
|
XDCHECK_EQ(paths.size(), results.size());
|
2018-03-21 02:34:03 +03:00
|
|
|
for (size_t idx = 0; idx < results.size(); ++idx) {
|
|
|
|
const auto& result = results[idx];
|
|
|
|
if (!result.hasException()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
XLOG(ERR) << "error computing SCM diff for " << paths.at(idx);
|
2019-10-11 20:42:16 +03:00
|
|
|
context->callback->diffError(paths.at(idx), result.exception());
|
2018-03-21 02:34:03 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:00:08 +03:00
|
|
|
/**
|
|
|
|
* Diff two commits.
|
|
|
|
*
|
|
|
|
* The differences will be recorded using a callback inside of DiffState and
|
|
|
|
* will be extracted and returned to the caller.
|
|
|
|
*/
|
|
|
|
FOLLY_NODISCARD Future<Unit>
|
2020-02-06 00:13:11 +03:00
|
|
|
diffCommits(DiffContext* context, Hash hash1, Hash hash2) {
|
|
|
|
auto future1 =
|
|
|
|
context->store->getTreeForCommit(hash1, context->getFetchContext());
|
|
|
|
auto future2 =
|
|
|
|
context->store->getTreeForCommit(hash2, context->getFetchContext());
|
2020-02-03 22:28:16 +03:00
|
|
|
return collectSafe(future1, future2)
|
2019-12-05 03:00:08 +03:00
|
|
|
.thenValue([context](std::tuple<
|
|
|
|
std::shared_ptr<const Tree>,
|
|
|
|
std::shared_ptr<const Tree>>&& tup) {
|
|
|
|
const auto& [tree1, tree2] = tup;
|
|
|
|
|
|
|
|
// This happens in the case in which the CLI (during eden doctor) calls
|
|
|
|
// getScmStatusBetweenRevisions() with the same hash in order to check
|
|
|
|
// if a commit hash is valid.
|
|
|
|
if (tree1->getHash() == tree2->getHash()) {
|
|
|
|
return makeFuture();
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
return diffTrees(
|
|
|
|
context, RelativePathPiece{}, *tree1, *tree2, nullptr, false);
|
2019-12-05 03:00:08 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-24 01:19:29 +03:00
|
|
|
Future<std::unique_ptr<ScmStatus>>
|
2019-10-11 20:42:16 +03:00
|
|
|
diffCommitsForStatus(const ObjectStore* store, Hash hash1, Hash hash2) {
|
2018-03-21 02:34:03 +03:00
|
|
|
return folly::makeFutureWith([&] {
|
2019-09-24 01:19:29 +03:00
|
|
|
auto state = std::make_unique<DiffState>(store);
|
|
|
|
auto statePtr = state.get();
|
2019-10-11 20:42:16 +03:00
|
|
|
auto contextPtr = &(statePtr->context);
|
|
|
|
return diffCommits(contextPtr, hash1, hash2)
|
2019-09-24 01:19:29 +03:00
|
|
|
.thenValue([state = std::move(state)](auto&&) {
|
|
|
|
return std::make_unique<ScmStatus>(state->callback.extractStatus());
|
2018-09-15 02:57:46 +03:00
|
|
|
});
|
2018-03-21 02:34:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:15:29 +03:00
|
|
|
FOLLY_NODISCARD Future<Unit> diffTrees(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
Hash scmHash,
|
|
|
|
Hash wdHash,
|
|
|
|
const GitIgnoreStack* ignore,
|
|
|
|
bool isIgnored) {
|
2020-02-06 00:13:11 +03:00
|
|
|
auto scmTreeFuture =
|
|
|
|
context->store->getTree(scmHash, context->getFetchContext());
|
|
|
|
auto wdTreeFuture =
|
|
|
|
context->store->getTree(wdHash, context->getFetchContext());
|
2020-01-29 22:15:29 +03:00
|
|
|
// Optimization for the case when both tree objects are immediately ready.
|
|
|
|
// We can avoid copying the input path in this case.
|
|
|
|
if (scmTreeFuture.isReady() && wdTreeFuture.isReady()) {
|
|
|
|
return diffTrees(
|
|
|
|
context,
|
|
|
|
currentPath,
|
|
|
|
*(std::move(scmTreeFuture).get()),
|
|
|
|
*(std::move(wdTreeFuture).get()),
|
|
|
|
ignore,
|
|
|
|
isIgnored);
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:28:16 +03:00
|
|
|
return collectSafe(scmTreeFuture, wdTreeFuture)
|
2020-01-29 22:15:29 +03:00
|
|
|
.thenValue([context, currentPath = currentPath.copy(), ignore, isIgnored](
|
|
|
|
std::tuple<
|
|
|
|
std::shared_ptr<const Tree>,
|
|
|
|
std::shared_ptr<const Tree>>&& tup) {
|
|
|
|
const auto& [scmTree, wdTree] = tup;
|
|
|
|
return diffTrees(
|
|
|
|
context, currentPath, *scmTree, *wdTree, ignore, isIgnored);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
FOLLY_NODISCARD Future<Unit> diffAddedTree(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
Hash wdHash,
|
|
|
|
const GitIgnoreStack* ignore,
|
|
|
|
bool isIgnored) {
|
2020-02-06 00:13:11 +03:00
|
|
|
auto wdFuture = context->store->getTree(wdHash, context->getFetchContext());
|
2020-01-29 22:15:29 +03:00
|
|
|
// Optimization for the case when the tree object is immediately ready.
|
|
|
|
// We can avoid copying the input path in this case.
|
|
|
|
if (wdFuture.isReady()) {
|
|
|
|
return diffAddedTree(
|
|
|
|
context, currentPath, *std::move(wdFuture).get(), ignore, isIgnored);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(wdFuture).thenValue(
|
|
|
|
[context, currentPath = currentPath.copy(), ignore, isIgnored](
|
|
|
|
std::shared_ptr<const Tree>&& wdTree) {
|
|
|
|
return diffAddedTree(context, currentPath, *wdTree, ignore, isIgnored);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
FOLLY_NODISCARD Future<Unit> diffRemovedTree(
|
2020-02-06 00:13:11 +03:00
|
|
|
DiffContext* context,
|
2020-01-29 22:15:29 +03:00
|
|
|
RelativePathPiece currentPath,
|
|
|
|
Hash scmHash) {
|
2020-02-06 00:13:11 +03:00
|
|
|
auto scmFuture = context->store->getTree(scmHash, context->getFetchContext());
|
2020-01-29 22:15:29 +03:00
|
|
|
// Optimization for the case when the tree object is immediately ready.
|
|
|
|
// We can avoid copying the input path in this case.
|
|
|
|
if (scmFuture.isReady()) {
|
|
|
|
return diffRemovedTree(context, currentPath, *(std::move(scmFuture).get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(scmFuture).thenValue(
|
|
|
|
[context,
|
|
|
|
currentPath = currentPath.copy()](std::shared_ptr<const Tree>&& tree) {
|
|
|
|
return diffRemovedTree(context, currentPath, *tree);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:34:03 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|