Represent submodule commits as normal commits (#423)

Summary:
Closes #417. Submodule commits are dead; long live commits. The ontology
is now:

  - A tree includes tree entries.
  - A tree entry may have a blob as contents.
  - A tree entry may have a tree as contents.
  - A tree entry may have a commit as contents.

Test Plan:
Existing unit tests suffice, especially `#commits yields all commits`.

wchargin-branch: git-remove-submodule-commits
This commit is contained in:
William Chargin 2018-06-27 12:01:07 -07:00 committed by GitHub
parent 38c364c916
commit 518d5b819c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 60 additions and 261 deletions

View File

@ -121,7 +121,7 @@ Array [
"3dfb84795e07341b05fad3a0d5a55f8304b2d7d8",
"pygravitydefier",
],
"dstIndex": 15,
"dstIndex": 10,
"srcIndex": 29,
},
Object {
@ -186,7 +186,7 @@ Array [
"569e1d383759903134df75230d63c0090196d4cb",
"pygravitydefier",
],
"dstIndex": 15,
"dstIndex": 10,
"srcIndex": 34,
},
Object {
@ -303,7 +303,7 @@ Array [
"7be3ecfee5314ffa9b2d93fc4377792b2d6d70ed",
"pygravitydefier",
],
"dstIndex": 14,
"dstIndex": 7,
"srcIndex": 43,
},
Object {
@ -381,7 +381,7 @@ Array [
"819fc546cea489476ce8dc90785e9ba7753d0a8f",
"pygravitydefier",
],
"dstIndex": 14,
"dstIndex": 7,
"srcIndex": 49,
},
Object {
@ -459,7 +459,7 @@ Array [
"bbf3b8b3d26a4f884b5c022d46851f593d329192",
"pygravitydefier",
],
"dstIndex": 14,
"dstIndex": 7,
"srcIndex": 55,
},
Object {
@ -513,8 +513,8 @@ Array [
"COMMIT",
"69c5aad50eec8f2a0a07c988c3b283a6490eb45b",
],
"dstIndex": 8,
"srcIndex": 7,
"dstIndex": 9,
"srcIndex": 8,
},
Object {
"address": Array [
@ -528,8 +528,8 @@ Array [
"COMMIT",
"e8b7a8f19701cd5a25e4a097d513ead60e5f8bcc",
],
"dstIndex": 13,
"srcIndex": 8,
"dstIndex": 15,
"srcIndex": 9,
},
Object {
"address": Array [
@ -543,8 +543,8 @@ Array [
"COMMIT",
"c08ee3a4edea384d5291ffcbf06724a13ed72325",
],
"dstIndex": 10,
"srcIndex": 9,
"dstIndex": 12,
"srcIndex": 11,
},
Object {
"address": Array [
@ -558,8 +558,8 @@ Array [
"COMMIT",
"c2b51945e7457546912a8ce158ed9d294558d294",
],
"dstIndex": 11,
"srcIndex": 10,
"dstIndex": 13,
"srcIndex": 12,
},
Object {
"address": Array [
@ -573,8 +573,8 @@ Array [
"COMMIT",
"8d287c3bfbf8455ef30187bf5153ffc1b6eef268",
],
"dstIndex": 9,
"srcIndex": 12,
"dstIndex": 11,
"srcIndex": 14,
},
Object {
"address": Array [
@ -588,8 +588,8 @@ Array [
"COMMIT",
"d160cca97611e9dfed642522ad44408d0292e8ea",
],
"dstIndex": 12,
"srcIndex": 13,
"dstIndex": 14,
"srcIndex": 15,
},
Object {
"address": Array [
@ -601,7 +601,7 @@ Array [
"3715ddfb8d4c4fd2a6f6af75488c82f84c92ec2f",
],
"dstIndex": 21,
"srcIndex": 7,
"srcIndex": 8,
},
Object {
"address": Array [
@ -613,7 +613,7 @@ Array [
"69c5aad50eec8f2a0a07c988c3b283a6490eb45b",
],
"dstIndex": 23,
"srcIndex": 8,
"srcIndex": 9,
},
Object {
"address": Array [
@ -625,7 +625,7 @@ Array [
"8d287c3bfbf8455ef30187bf5153ffc1b6eef268",
],
"dstIndex": 17,
"srcIndex": 9,
"srcIndex": 11,
},
Object {
"address": Array [
@ -637,7 +637,7 @@ Array [
"c08ee3a4edea384d5291ffcbf06724a13ed72325",
],
"dstIndex": 16,
"srcIndex": 10,
"srcIndex": 12,
},
Object {
"address": Array [
@ -649,7 +649,7 @@ Array [
"c2b51945e7457546912a8ce158ed9d294558d294",
],
"dstIndex": 24,
"srcIndex": 11,
"srcIndex": 13,
},
Object {
"address": Array [
@ -661,7 +661,7 @@ Array [
"d160cca97611e9dfed642522ad44408d0292e8ea",
],
"dstIndex": 18,
"srcIndex": 12,
"srcIndex": 14,
},
Object {
"address": Array [
@ -673,7 +673,7 @@ Array [
"e8b7a8f19701cd5a25e4a097d513ead60e5f8bcc",
],
"dstIndex": 22,
"srcIndex": 13,
"srcIndex": 15,
},
Object {
"address": Array [
@ -1161,6 +1161,12 @@ Array [
"BLOB",
"f1f2514ca6d7a6a1a0511957021b1995bf9ace1c",
],
Array [
"sourcecred",
"git",
"COMMIT",
"29ef158bc982733e2ba429fcf73e2f7562244188",
],
Array [
"sourcecred",
"git",
@ -1173,6 +1179,12 @@ Array [
"COMMIT",
"69c5aad50eec8f2a0a07c988c3b283a6490eb45b",
],
Array [
"sourcecred",
"git",
"COMMIT",
"762c062fbdc7ec198cd693e95d55b374a08ff3e3",
],
Array [
"sourcecred",
"git",
@ -1203,20 +1215,6 @@ Array [
"COMMIT",
"e8b7a8f19701cd5a25e4a097d513ead60e5f8bcc",
],
Array [
"sourcecred",
"git",
"SUBMODULE_COMMIT",
"https://github.com/sourcecred/example-git-submodule.git",
"29ef158bc982733e2ba429fcf73e2f7562244188",
],
Array [
"sourcecred",
"git",
"SUBMODULE_COMMIT",
"https://github.com/sourcecred/example-git-submodule.git",
"762c062fbdc7ec198cd693e95d55b374a08ff3e3",
],
Array [
"sourcecred",
"git",

View File

@ -30,23 +30,6 @@ Object {
}
`;
exports[`plugins/git/nodes snapshots as expected: submoduleCommit 1`] = `
Object {
"address": Array [
"sourcecred",
"git",
"SUBMODULE_COMMIT",
"https://github.com/sourcecred/example-git-submodule.git",
"29ef158bc982733e2ba429fcf73e2f7562244188",
],
"structured": Object {
"commitHash": "29ef158bc982733e2ba429fcf73e2f7562244188",
"submoduleUrl": "https://github.com/sourcecred/example-git-submodule.git",
"type": "SUBMODULE_COMMIT",
},
}
`;
exports[`plugins/git/nodes snapshots as expected: tree 1`] = `
Object {
"address": Array [

View File

@ -24,11 +24,8 @@ class GraphCreator {
}
addRepository(repository: GT.Repository) {
const treeAndNameToSubmoduleUrls = this.treeAndNameToSubmoduleUrls(
repository
);
for (const treeHash of Object.keys(repository.trees)) {
this.addTree(repository.trees[treeHash], treeAndNameToSubmoduleUrls);
this.addTree(repository.trees[treeHash]);
}
for (const commitHash of Object.keys(repository.commits)) {
this.addCommit(repository.commits[commitHash]);
@ -36,38 +33,6 @@ class GraphCreator {
this.addBecomesEdges(repository);
}
treeAndNameToSubmoduleUrls(repository: GT.Repository) {
const result: {[tree: GT.Hash]: {[name: string]: string[]}} = {};
Object.keys(repository.commits).forEach((commitHash) => {
const {treeHash: rootTreeHash, submoduleUrls} = repository.commits[
commitHash
];
Object.keys(submoduleUrls).forEach((path) => {
const parts = path.split("/");
const [treePath, name] = [
parts.slice(0, parts.length - 1),
parts[parts.length - 1],
];
let tree = repository.trees[rootTreeHash];
for (const pathComponent of treePath) {
tree = repository.trees[tree.entries[pathComponent].hash];
if (tree == null) {
return;
}
}
if (result[tree.hash] == null) {
result[tree.hash] = {};
}
const url = submoduleUrls[path];
if (result[tree.hash][name] == null) {
result[tree.hash][name] = [];
}
result[tree.hash][name].push(url);
});
});
return result;
}
addCommit(commit: GT.Commit) {
const node: GN.CommitAddress = {type: GN.COMMIT_TYPE, hash: commit.hash};
const tree: GN.TreeAddress = {type: GN.TREE_TYPE, hash: commit.treeHash};
@ -81,7 +46,7 @@ class GraphCreator {
}
}
addTree(tree: GT.Tree, treeAndNameToSubmoduleUrls) {
addTree(tree: GT.Tree) {
const treeNode: GN.TreeAddress = {type: GN.TREE_TYPE, hash: tree.hash};
this.graph.addNode(GN.toRaw(treeNode));
for (const name of Object.keys(tree.entries)) {
@ -93,34 +58,25 @@ class GraphCreator {
};
this.graph.addNode(GN.toRaw(entryNode));
this.graph.addEdge(GE.createEdge.includes(treeNode, entryNode));
let targets: GN.TreeEntryContentsAddress[] = [];
let target: ?GN.TreeEntryContentsAddress = null;
switch (entry.type) {
case "blob":
targets.push({type: GN.BLOB_TYPE, hash: entry.hash});
target = {type: GN.BLOB_TYPE, hash: entry.hash};
break;
case "tree":
targets.push({type: GN.TREE_TYPE, hash: entry.hash});
target = {type: GN.TREE_TYPE, hash: entry.hash};
break;
case "commit":
// One entry for each possible URL.
const urls = treeAndNameToSubmoduleUrls[tree.hash][name];
for (const url of urls) {
targets.push({
type: GN.SUBMODULE_COMMIT_TYPE,
submoduleUrl: url,
commitHash: entry.hash,
});
}
// Submodule commit.
target = {type: GN.COMMIT_TYPE, hash: entry.hash};
break;
default:
// eslint-disable-next-line no-unused-expressions
(entry.type: empty);
throw new Error(String(entry.type));
}
for (const target of targets) {
this.graph.addNode(GN.toRaw(target));
this.graph.addEdge(GE.createEdge.hasContents(entryNode, target));
}
this.graph.addNode(GN.toRaw(target));
this.graph.addEdge(GE.createEdge.hasContents(entryNode, target));
}
}

View File

@ -37,13 +37,11 @@ describe("plugins/git/createGraph", () => {
hash: "commit1",
parentHashes: [],
treeHash: beforeTree,
submoduleUrls: {},
},
commit2: {
hash: "commit2",
parentHashes: ["commit1"],
treeHash: afterTree,
submoduleUrls: {},
},
},
trees,

View File

@ -5,9 +5,6 @@
"parentHashes": [
"69c5aad50eec8f2a0a07c988c3b283a6490eb45b"
],
"submoduleUrls": {
"pygravitydefier": "https://github.com/sourcecred/example-git-submodule.git"
},
"treeHash": "7be3ecfee5314ffa9b2d93fc4377792b2d6d70ed"
},
"69c5aad50eec8f2a0a07c988c3b283a6490eb45b": {
@ -15,9 +12,6 @@
"parentHashes": [
"e8b7a8f19701cd5a25e4a097d513ead60e5f8bcc"
],
"submoduleUrls": {
"pygravitydefier": "https://github.com/sourcecred/example-git-submodule.git"
},
"treeHash": "bbf3b8b3d26a4f884b5c022d46851f593d329192"
},
"8d287c3bfbf8455ef30187bf5153ffc1b6eef268": {
@ -25,9 +19,6 @@
"parentHashes": [
"c08ee3a4edea384d5291ffcbf06724a13ed72325"
],
"submoduleUrls": {
"pygravitydefier": "https://github.com/sourcecred/example-git-submodule.git"
},
"treeHash": "3dfb84795e07341b05fad3a0d5a55f8304b2d7d8"
},
"c08ee3a4edea384d5291ffcbf06724a13ed72325": {
@ -35,16 +26,12 @@
"parentHashes": [
"c2b51945e7457546912a8ce158ed9d294558d294"
],
"submoduleUrls": {
},
"treeHash": "2f7155e359fd0ecb96ffdca66fa45b6ed5792809"
},
"c2b51945e7457546912a8ce158ed9d294558d294": {
"hash": "c2b51945e7457546912a8ce158ed9d294558d294",
"parentHashes": [
],
"submoduleUrls": {
},
"treeHash": "bdff5d94193170015d6cbb549b7b630649428b1f"
},
"d160cca97611e9dfed642522ad44408d0292e8ea": {
@ -52,9 +39,6 @@
"parentHashes": [
"8d287c3bfbf8455ef30187bf5153ffc1b6eef268"
],
"submoduleUrls": {
"pygravitydefier": "https://github.com/sourcecred/example-git-submodule.git"
},
"treeHash": "569e1d383759903134df75230d63c0090196d4cb"
},
"e8b7a8f19701cd5a25e4a097d513ead60e5f8bcc": {
@ -62,9 +46,6 @@
"parentHashes": [
"d160cca97611e9dfed642522ad44408d0292e8ea"
],
"submoduleUrls": {
"pygravitydefier": "https://github.com/sourcecred/example-git-submodule.git"
},
"treeHash": "819fc546cea489476ce8dc90785e9ba7753d0a8f"
}
},

View File

@ -19,11 +19,6 @@ describe("plugins/git/edges", () => {
type: GN.COMMIT_TYPE,
hash: "69c5aad50eec8f2a0a07c988c3b283a6490eb45b",
}),
submoduleCommit: (): GN.SubmoduleCommitAddress => ({
type: GN.SUBMODULE_COMMIT_TYPE,
submoduleUrl: "https://github.com/sourcecred/example-git-submodule.git",
commitHash: "29ef158bc982733e2ba429fcf73e2f7562244188",
}),
tree: (): GN.TreeAddress => ({
type: GN.TREE_TYPE,
hash: "7be3ecfee5314ffa9b2d93fc4377792b2d6d70ed",

View File

@ -172,10 +172,7 @@ export class GraphView {
homs: [
{srcPrefix: GN._Prefix.treeEntry, dstPrefix: GN._Prefix.blob},
{srcPrefix: GN._Prefix.treeEntry, dstPrefix: GN._Prefix.tree},
{
srcPrefix: GN._Prefix.treeEntry,
dstPrefix: GN._Prefix.submoduleCommit,
},
{srcPrefix: GN._Prefix.treeEntry, dstPrefix: GN._Prefix.commit},
],
},
};

View File

@ -4,6 +4,7 @@ import cloneDeep from "lodash.clonedeep";
import {EdgeAddress, Graph, NodeAddress, edgeToString} from "../../core/graph";
import {createGraph} from "./createGraph";
import * as exampleRepo from "./demoData/exampleRepo";
import {GraphView} from "./graphView";
import type {Repository} from "./types";
@ -32,7 +33,13 @@ describe("plugins/git/graphView", () => {
});
it("#commits yields all commits", () => {
const expectedHashes = Object.keys(makeData().commits);
const extraSubmoduleCommits = [
exampleRepo.SUBMODULE_COMMIT_1,
exampleRepo.SUBMODULE_COMMIT_2,
];
const expectedHashes = Object.keys(makeData().commits).concat(
extraSubmoduleCommits
);
const actualHashes = Array.from(view.commits()).map((a) => a.hash);
expectEqualMultisets(actualHashes, expectedHashes);
});
@ -104,12 +111,10 @@ describe("plugins/git/graphView", () => {
const actual = Array.from(
view.contents(entryByName("pygravitydefier"))
);
const expected: $ReadOnlyArray<GN.SubmoduleCommitAddress> = [
const expected: $ReadOnlyArray<GN.CommitAddress> = [
{
type: GN.SUBMODULE_COMMIT_TYPE,
submoduleUrl:
"https://github.com/sourcecred/example-git-submodule.git",
commitHash: "29ef158bc982733e2ba429fcf73e2f7562244188",
type: GN.COMMIT_TYPE,
hash: "29ef158bc982733e2ba429fcf73e2f7562244188",
},
];
expect(actual).toEqual(expected);

View File

@ -43,85 +43,10 @@ function findCommits(git: GitDriver, rootRef: string): Commit[] {
.filter((line) => line.length > 0)
.map((line) => {
const [hash, treeHash, ...parentHashes] = line.trim().split(" ");
const submoduleUrls = loadSubmoduleUrls(git, hash);
return {hash, parentHashes, treeHash, submoduleUrls};
return {hash, parentHashes, treeHash};
});
}
const GITMODULES_SUBMODULES_KEY_RE = /^submodule\.(.*)\.(path|url)$/;
function loadSubmoduleUrls(
git: GitDriver,
commitHash: Hash
): {[path: string]: string} {
const gitmodulesRef = `${commitHash}:.gitmodules`;
const gitmodulesBlob: string | null = (() => {
try {
return git(["rev-parse", "--quiet", "--verify", gitmodulesRef]).trim();
} catch (e) {
if (e.status === 1) {
// No .gitmodules file here.
return null;
} else {
throw e;
}
}
})();
if (gitmodulesBlob == null) {
// No problem; there just weren't any submodules at this commit.
return {};
}
// The output format of the following is `${key}\n${value}\0...`, as
// specified in `git help config`'s section about the `-z` option.
// The format is safe because keys are strictly validated; see the
// function `git_config_parse_key` in `git/git:config.c`.
const rawConfig = git(["config", "--blob", gitmodulesBlob, "--list", "-z"]);
const configKeyValuePairs = rawConfig
.split("\0")
.filter((line) => line.length > 0)
.map((line) => {
const separator = line.indexOf("\n");
if (separator < 0) {
// Shouldn't happen, according to Git docs. Guard anyway.
throw new Error(`Bad .gitmodules line at ${commitHash}: ${line}`);
}
return {
key: line.substring(0, separator),
value: line.substring(separator + 1),
};
});
const submoduleInfoByKey: {
[submoduleKey: string]: {path: string | null, url: string | null},
} = {};
configKeyValuePairs.forEach(({key, value}) => {
const match = key.match(GITMODULES_SUBMODULES_KEY_RE);
if (!match) {
return;
}
const [_, submoduleKey, kind] = match;
if (submoduleInfoByKey[submoduleKey] == null) {
submoduleInfoByKey[submoduleKey] = {path: null, url: null};
}
if (kind !== "path" && kind !== "url") {
throw new Error(`Invariant violation: bad kind: ${kind}`);
}
submoduleInfoByKey[submoduleKey][kind] = value;
});
const result = {};
Object.keys(submoduleInfoByKey).forEach((submoduleKey) => {
const {path, url} = submoduleInfoByKey[submoduleKey];
if (path != null && url != null) {
result[path] = url;
} else {
console.warn(`Partial submodule at ${commitHash}: ${submoduleKey}`);
}
});
return result;
}
function findTrees(git: GitDriver, rootTrees: Set<Hash>): Tree[] {
const result: Tree[] = [];
const visited: Set<Hash> = new Set();

View File

@ -12,7 +12,6 @@ export function _gitAddress(...parts: string[]): RawAddress {
export const BLOB_TYPE: "BLOB" = "BLOB";
export const COMMIT_TYPE: "COMMIT" = "COMMIT";
export const SUBMODULE_COMMIT_TYPE: "SUBMODULE_COMMIT" = "SUBMODULE_COMMIT";
export const TREE_TYPE: "TREE" = "TREE";
export const TREE_ENTRY_TYPE: "TREE_ENTRY" = "TREE_ENTRY";
@ -20,7 +19,6 @@ export const _Prefix = Object.freeze({
base: GIT_PREFIX,
blob: _gitAddress(BLOB_TYPE),
commit: _gitAddress(COMMIT_TYPE),
submoduleCommit: _gitAddress(SUBMODULE_COMMIT_TYPE),
tree: _gitAddress(TREE_TYPE),
treeEntry: _gitAddress(TREE_ENTRY_TYPE),
});
@ -33,11 +31,6 @@ export type CommitAddress = {|
+type: typeof COMMIT_TYPE,
+hash: Hash,
|};
export type SubmoduleCommitAddress = {|
+type: typeof SUBMODULE_COMMIT_TYPE,
+submoduleUrl: string,
+commitHash: Hash,
|};
export type TreeAddress = {|
+type: typeof TREE_TYPE,
+hash: Hash,
@ -52,13 +45,12 @@ export type TreeEntryAddress = {|
// addresses.
export type TreeEntryContentsAddress =
| BlobAddress
| TreeAddress
| SubmoduleCommitAddress;
| CommitAddress
| TreeAddress;
export type StructuredAddress =
| BlobAddress
| CommitAddress
| SubmoduleCommitAddress
| TreeAddress
| TreeEntryAddress;
@ -82,11 +74,6 @@ export function fromRaw(x: RawAddress): StructuredAddress {
const [hash] = rest;
return {type: COMMIT_TYPE, hash};
}
case "SUBMODULE_COMMIT": {
if (rest.length !== 2) throw fail();
const [submoduleUrl, commitHash] = rest;
return {type: SUBMODULE_COMMIT_TYPE, submoduleUrl, commitHash};
}
case "TREE": {
if (rest.length !== 1) throw fail();
const [hash] = rest;
@ -110,12 +97,6 @@ export function toRaw(x: StructuredAddress): RawAddress {
return NodeAddress.append(_Prefix.blob, x.hash);
case COMMIT_TYPE:
return NodeAddress.append(_Prefix.commit, x.hash);
case SUBMODULE_COMMIT_TYPE:
return NodeAddress.append(
_Prefix.submoduleCommit,
x.submoduleUrl,
x.commitHash
);
case TREE_TYPE:
return NodeAddress.append(_Prefix.tree, x.hash);
case TREE_ENTRY_TYPE:

View File

@ -14,11 +14,6 @@ describe("plugins/git/nodes", () => {
type: GN.COMMIT_TYPE,
hash: "3715ddfb8d4c4fd2a6f6af75488c82f84c92ec2f",
}),
submoduleCommit: (): GN.SubmoduleCommitAddress => ({
type: GN.SUBMODULE_COMMIT_TYPE,
submoduleUrl: "https://github.com/sourcecred/example-git-submodule.git",
commitHash: "29ef158bc982733e2ba429fcf73e2f7562244188",
}),
tree: (): GN.TreeAddress => ({
type: GN.TREE_TYPE,
hash: "7be3ecfee5314ffa9b2d93fc4377792b2d6d70ed",
@ -129,20 +124,6 @@ describe("plugins/git/nodes", () => {
examples.treeEntry().name,
"wat",
]);
expectBadAddress("submodule commit with no fields", [
GN.SUBMODULE_COMMIT_TYPE,
]);
expectBadAddress("submodule commit with only URL", [
GN.SUBMODULE_COMMIT_TYPE,
examples.submoduleCommit().submoduleUrl,
]);
expectBadAddress("submodule commit with extra field", [
GN.SUBMODULE_COMMIT_TYPE,
examples.submoduleCommit().submoduleUrl,
examples.submoduleCommit().commitHash,
"wat",
]);
});
describe("toRaw(...) with", () => {

View File

@ -9,7 +9,6 @@ export type Commit = {|
+hash: Hash,
+parentHashes: $ReadOnlyArray<Hash>,
+treeHash: Hash,
+submoduleUrls: {[path: string]: string},
|};
export type Tree = {|
+hash: Hash,