Skip to content

Commit

Permalink
Merge branch 'git-objects-develop' into split-git
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed May 29, 2020
2 parents 742cf88 + a1f5760 commit 20c0a35
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
1 change: 0 additions & 1 deletion foo/hello.txt

This file was deleted.

47 changes: 15 additions & 32 deletions src/libutil/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,49 +93,32 @@ std::pair<GitMode, Hash> dumpGitHashInternal(
{
auto hashSink = makeHashSink();
struct stat st;
GitMode perm;
if (lstat(path.c_str(), &st))
throw SysError(format("getting attributes of path '%1%'") % path);

if (S_ISREG(st.st_mode)) {
auto s = (format("blob %d\0%s"s) % std::to_string(st.st_size) % readFile(path)).str();

vector<unsigned char> v;
std::copy(s.begin(), s.end(), std::back_inserter(v));
sink(v.data(), v.size());
return st.st_mode & S_IXUSR
? 100755
: 100644;
}

else if (S_ISDIR(st.st_mode)) {
std::string s1 = "";

std::map<string, string> entries;
perm = dumpGitBlob(path, st, *hashSink);
} else if (S_ISDIR(st.st_mode)) {
GitTree entries;
for (auto & i : readDirectory(path))
entries[i.name] = i.name;

for (auto & i : entries)
if (filter(path + "/" + i.first)) {
HashSink hashSink(htSHA1);
unsigned int perm = dumpGitInternal(path + "/" + i.first, hashSink, filter);
auto hash = hashSink.finish().first;
s1 += (format("%6d %s\0%s"s) % perm % i.first % hash.hash).str();
if (filter(path + "/" + i.name)) {
entries[i.name] = dumpGitHashInternal(makeHashSink, path + "/" + i.name, filter);
}

std::string s2 = (format("tree %d\0%s"s) % s1.size() % s1).str();

vector<unsigned char> v;
std::copy(s2.begin(), s2.end(), std::back_inserter(v));
sink(v.data(), v.size());
return 40000;
perm = dumpGitTree(entries, *hashSink);
} else {
throw Error(format("file '%1%' has an unsupported type") % path);
}

else throw Error(format("file '%1%' has an unsupported type") % path);
auto hash = hashSink->finish().first;
return std::pair { perm, hash };
}

void dumpGit(const Path & path, Sink & sink, PathFilter & filter)
Hash dumpGitHash(
std::function<std::unique_ptr<AbstractHashSink>()> makeHashSink,
const Path & path, PathFilter & filter)
{
dumpGitInternal(path, sink, filter);
return dumpGitHashInternal(makeHashSink, path, filter).second;
}

}
16 changes: 15 additions & 1 deletion src/libutil/git.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ void restoreGit(const Path & path, Source & source);

void parseGit(ParseSink & sink, Source & source);

void dumpGit(const Path & path, Sink & sink, PathFilter & filter = defaultPathFilter);
// Dumps a single file to a sink
GitMode dumpGitBlob(const Path & path, const struct stat st, Sink & sink);

typedef std::map<string, std::pair<GitMode, Hash>> GitTree;

// Dumps a representation of a git tree to a sink
GitMode dumpGitTree(const GitTree & entries, Sink & sink);

// Recursively dumps path, hashing as we go
Hash dumpGitHash(
std::function<std::unique_ptr<AbstractHashSink>()>,
const Path & path,
PathFilter & filter = defaultPathFilter);

// N.B. There is no way to recursively dump to a sink, as that doesn't make
// sense with the git hash/data model where the information is Merklized.
}
4 changes: 1 addition & 3 deletions src/nix/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ struct CmdHash : Command
break;
}
case FileIngestionMethod::Git:
auto hashSink = makeHashSink();
dumpGit(path, *hashSink);
h = hashSink->finish().first;
h = dumpGitHash(makeHashSink, path);
break;
}

Expand Down

0 comments on commit 20c0a35

Please sign in to comment.