Move `localGit`/`GitDriver` into Git utils (#150)

Summary:
A few reasons for this:
 1. This _is_ a utility, so it makes sense semantically.
 2. This unifies the utilities API; clients like `loadRepository.test`
    don’t have to keep around both a `git` and a `gitUtils`.
 3. Most importantly, further scripts and tests shouldn’t depend on
    `loadRepository` just for `localGit`. Depending on `gitUtils` makes
    much more sense.

(Note that `makeUtils` is no longer dependency-injectable, but that’s
okay; I considered this and favored YAGNI on this one.)

Test Plan:
Existing unit tests pass.

wchargin-branch: move-localgit
This commit is contained in:
William Chargin 2018-04-26 19:31:27 -07:00 committed by GitHub
parent dad8777e6c
commit 3679529bef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 44 deletions

View File

@ -1,19 +1,37 @@
// @flow // @flow
import {execFileSync} from "child_process";
import fs from "fs"; import fs from "fs";
import mkdirp from "mkdirp"; import mkdirp from "mkdirp";
import path from "path"; import path from "path";
import type {GitDriver} from "./loadRepository"; export interface Utils {
exec: GitDriver;
interface Utils {
head(): string; head(): string;
writeAndStage(filename: string, contents: string): void; writeAndStage(filename: string, contents: string): void;
deterministicCommit(message: string): void; deterministicCommit(message: string): void;
} }
export function makeUtils(git: GitDriver, repositoryPath: string): Utils { export type GitDriver = (args: string[], options?: ExecOptions) => string;
// `ExecOptions` is the type of the second argument to `execFileSync`.
// See here for details: https://nodejs.org/api/child_process.html
type ExecOptions = Object;
export function localGit(repositoryPath: string): GitDriver {
return function git(args: string[], options?: ExecOptions): string {
return execFileSync(
"git",
["-C", repositoryPath, ...args],
options
).toString();
};
}
export function makeUtils(repositoryPath: string): Utils {
const git = localGit(repositoryPath);
return { return {
exec: git,
head() { head() {
return git(["rev-parse", "HEAD"]).trim(); return git(["rev-parse", "HEAD"]).trim();
}, },

View File

@ -9,22 +9,9 @@
*/ */
// @flow // @flow
import {execFileSync} from "child_process"; import type {GitDriver} from "./gitUtils";
import type {Repository, Hash, Commit, Tree, TreeEntry} from "./types"; import type {Repository, Hash, Commit, Tree, TreeEntry} from "./types";
import {localGit} from "./gitUtils";
export type GitDriver = (args: string[], options?: ExecOptions) => string;
type ExecOptions = Object; // close enough
export function localGit(repositoryPath: string): GitDriver {
return function git(args: string[], options?: ExecOptions): string {
// Throws an Error on shell failure.
return execFileSync(
"git",
["-C", repositoryPath, ...args],
options
).toString();
};
}
/** /**
* Load a Git repository from disk into memory. The `rootRef` should be * Load a Git repository from disk into memory. The `rootRef` should be

View File

@ -2,9 +2,8 @@
import tmp from "tmp"; import tmp from "tmp";
import type {GitDriver} from "./loadRepository";
import {makeUtils} from "./gitUtils"; import {makeUtils} from "./gitUtils";
import {localGit, loadRepository} from "./loadRepository"; import {loadRepository} from "./loadRepository";
const cleanups: (() => void)[] = []; const cleanups: (() => void)[] = [];
afterAll(() => { afterAll(() => {
@ -21,34 +20,33 @@ function mkdtemp() {
function createRepository(): {path: string, commits: string[]} { function createRepository(): {path: string, commits: string[]} {
const repositoryPath = mkdtemp(); const repositoryPath = mkdtemp();
const git = localGit(repositoryPath); const git = makeUtils(repositoryPath);
const gitUtils = makeUtils(git, repositoryPath);
git(["init"]); git.exec(["init"]);
gitUtils.writeAndStage("README.txt", "Amazing physics going on...\n"); git.writeAndStage("README.txt", "Amazing physics going on...\n");
gitUtils.deterministicCommit("Initial commit"); git.deterministicCommit("Initial commit");
const commit1 = gitUtils.head(); const commit1 = git.head();
gitUtils.writeAndStage("src/index.py", "import antigravity\n"); git.writeAndStage("src/index.py", "import antigravity\n");
gitUtils.writeAndStage( git.writeAndStage(
"src/quantum_gravity.py", "src/quantum_gravity.py",
'raise NotImplementedError("TODO(physicists)")\n' 'raise NotImplementedError("TODO(physicists)")\n'
); );
gitUtils.writeAndStage("TODOS.txt", "1. Resolve quantum gravity\n"); git.writeAndStage("TODOS.txt", "1. Resolve quantum gravity\n");
gitUtils.deterministicCommit("Discover gravity"); git.deterministicCommit("Discover gravity");
const commit2 = gitUtils.head(); const commit2 = git.head();
gitUtils.writeAndStage( git.writeAndStage(
"src/quantum_gravity.py", "src/quantum_gravity.py",
"import random\nif random.random() < 0.5:\n import antigravity\n" "import random\nif random.random() < 0.5:\n import antigravity\n"
); );
gitUtils.deterministicCommit("Solve quantum gravity"); git.deterministicCommit("Solve quantum gravity");
const commit3 = gitUtils.head(); const commit3 = git.head();
git(["rm", "TODOS.txt"]); git.exec(["rm", "TODOS.txt"]);
gitUtils.deterministicCommit("Clean up TODOS"); git.deterministicCommit("Clean up TODOS");
const commit4 = gitUtils.head(); const commit4 = git.head();
return { return {
path: repositoryPath, path: repositoryPath,
@ -88,16 +86,15 @@ describe("loadRepository", () => {
it("works with submodules", () => { it("works with submodules", () => {
const repositoryPath = mkdtemp(); const repositoryPath = mkdtemp();
const git = localGit(repositoryPath); const git = makeUtils(repositoryPath);
const gitUtils = makeUtils(git, repositoryPath);
const subproject = createRepository(); const subproject = createRepository();
git(["init"]); git.exec(["init"]);
git(["submodule", "--quiet", "add", subproject.path, "physics"]); git.exec(["submodule", "--quiet", "add", subproject.path, "physics"]);
gitUtils.deterministicCommit("Initial commit"); git.deterministicCommit("Initial commit");
const head = gitUtils.head(); const head = git.head();
const repository = loadRepository(repositoryPath, "HEAD"); const repository = loadRepository(repositoryPath, "HEAD");
const commit = repository.commits.get(head); const commit = repository.commits.get(head);