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:
parent
5b8ec53b13
commit
e3f04c5079
|
@ -33,8 +33,5 @@ module.exports = {
|
|||
"src/plugins/github/bin/fetchAndPrintGithubRepo.js"
|
||||
),
|
||||
createExampleRepo: resolveApp("src/plugins/git/bin/createExampleRepo.js"),
|
||||
loadAndPrintGitRepository: resolveApp(
|
||||
"src/plugins/git/bin/loadAndPrintRepository.js"
|
||||
),
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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();
|
|
@ -26,8 +26,7 @@
|
|||
},
|
||||
"c2b51945e7457546912a8ce158ed9d294558d294": {
|
||||
"hash": "c2b51945e7457546912a8ce158ed9d294558d294",
|
||||
"parentHashes": [
|
||||
]
|
||||
"parentHashes": []
|
||||
},
|
||||
"d160cca97611e9dfed642522ad44408d0292e8ea": {
|
||||
"hash": "d160cca97611e9dfed642522ad44408d0292e8ea",
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import stringify from "json-stable-stringify";
|
||||
|
||||
import type {Repo} from "../../core/repo";
|
||||
import cloneAndLoadRepository from "./cloneAndLoadRepository";
|
||||
|
@ -18,7 +19,13 @@ export function loadGitData(options: Options): Promise<void> {
|
|||
const repositories = options.repos.map((r) => cloneAndLoadRepository(r));
|
||||
const repository = mergeRepository(repositories);
|
||||
const graph = createGraph(repository);
|
||||
const blob = JSON.stringify(graph);
|
||||
const outputFilename = path.join(options.outputDirectory, "graph.json");
|
||||
return fs.writeFile(outputFilename, blob);
|
||||
function writeToFile(filename, serializable) {
|
||||
const blob = stringify(serializable);
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -23,8 +23,11 @@ usage() {
|
|||
|
||||
fetch() {
|
||||
tmpdir="$(mktemp -d)"
|
||||
node "${SOURCECRED_BIN:-./bin}/createExampleRepo.js" "${tmpdir}"
|
||||
node "${SOURCECRED_BIN:-./bin}/loadAndPrintGitRepository.js" "${tmpdir}"
|
||||
SOURCECRED_DIRECTORY="${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}"
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue