Git plugin serializes the repository and graph (#874)

This modifies the behavior when loading the Git plugin so that it
serializes the Repository as well as the graph. This will allow us to
get extra information, like the commit headline, to the Git plugin in
the frontend.

As an added bonus, we can now refactor `loadRepositoryTest` to depend on
`sourcecred.js load` rather than `loadAndPrintRepository`. As this was
the only use for `loadAndPrintRepository`, we can safely delete it. This
improves our test quality because it means we are also testing the
actual CLI behavior.

Note that the switch from using `stringify` to `json.tool` for
pretty-printing has resulted in a trivial diff in the snapshot.

Test plan: `yarn test --full` passes.
This commit is contained in:
Dandelion Mané 2018-09-20 14:07:58 -07:00 committed by GitHub
parent 5b8ec53b13
commit e3f04c5079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 46 deletions

View File

@ -33,8 +33,5 @@ module.exports = {
"src/plugins/github/bin/fetchAndPrintGithubRepo.js" "src/plugins/github/bin/fetchAndPrintGithubRepo.js"
), ),
createExampleRepo: resolveApp("src/plugins/git/bin/createExampleRepo.js"), createExampleRepo: resolveApp("src/plugins/git/bin/createExampleRepo.js"),
loadAndPrintGitRepository: resolveApp(
"src/plugins/git/bin/loadAndPrintRepository.js"
),
}, },
}; };

View File

@ -1,36 +0,0 @@
/*
* Command-line utility to load a Git repository into memory and then
* print the resulting JSON representation.
*
* Usage:
*
* node bin/loadAndPrintGitRepository.js PATH [ROOT_REF]
*
* where PATH is the path on disk to a Git repository, and ROOT_REF is
* the revision to load (defaults to HEAD).
*/
// @flow
import stringify from "json-stable-stringify";
import {loadRepository} from "../loadRepository";
function parseArgs() {
const argv = process.argv.slice(2);
if (argv.length !== 1 && argv.length !== 2) {
const invocation = process.argv.slice(0, 2).join(" ");
throw new Error(`Usage: ${invocation} PATH`);
}
return {
repositoryPath: argv[0],
rootRef: argv.length > 1 ? argv[1] : "HEAD",
};
}
function main() {
const args = parseArgs();
const result = loadRepository(args.repositoryPath, args.rootRef);
console.log(stringify(result, {space: 4}));
}
main();

View File

@ -26,8 +26,7 @@
}, },
"c2b51945e7457546912a8ce158ed9d294558d294": { "c2b51945e7457546912a8ce158ed9d294558d294": {
"hash": "c2b51945e7457546912a8ce158ed9d294558d294", "hash": "c2b51945e7457546912a8ce158ed9d294558d294",
"parentHashes": [ "parentHashes": []
]
}, },
"d160cca97611e9dfed642522ad44408d0292e8ea": { "d160cca97611e9dfed642522ad44408d0292e8ea": {
"hash": "d160cca97611e9dfed642522ad44408d0292e8ea", "hash": "d160cca97611e9dfed642522ad44408d0292e8ea",

View File

@ -2,6 +2,7 @@
import fs from "fs-extra"; import fs from "fs-extra";
import path from "path"; import path from "path";
import stringify from "json-stable-stringify";
import type {Repo} from "../../core/repo"; import type {Repo} from "../../core/repo";
import cloneAndLoadRepository from "./cloneAndLoadRepository"; import cloneAndLoadRepository from "./cloneAndLoadRepository";
@ -18,7 +19,13 @@ export function loadGitData(options: Options): Promise<void> {
const repositories = options.repos.map((r) => cloneAndLoadRepository(r)); const repositories = options.repos.map((r) => cloneAndLoadRepository(r));
const repository = mergeRepository(repositories); const repository = mergeRepository(repositories);
const graph = createGraph(repository); const graph = createGraph(repository);
const blob = JSON.stringify(graph); function writeToFile(filename, serializable) {
const outputFilename = path.join(options.outputDirectory, "graph.json"); const blob = stringify(serializable);
return fs.writeFile(outputFilename, blob); const filePath = path.join(options.outputDirectory, filename);
return fs.writeFile(filePath, blob);
}
return Promise.all([
writeToFile("repository.json", repository),
writeToFile("graph.json", graph),
]).then(() => undefined);
} }

View File

@ -23,8 +23,11 @@ usage() {
fetch() { fetch() {
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
node "${SOURCECRED_BIN:-./bin}/createExampleRepo.js" "${tmpdir}" SOURCECRED_DIRECTORY="${tmpdir}" \
node "${SOURCECRED_BIN:-./bin}/loadAndPrintGitRepository.js" "${tmpdir}" node "${SOURCECRED_BIN:-./bin}/sourcecred.js" \
load --plugin git \
sourcecred/example-git
python -m json.tool "${tmpdir}"/data/sourcecred/example-git/git/repository.json
rm -rf "${tmpdir}" rm -rf "${tmpdir}"
} }