Skip to content

Commit 7b5f99d

Browse files
authored
Merge pull request #192 from dotindustries/fix/reset_fn
Fix reset function
2 parents 701e767 + 1791ddc commit 7b5f99d

File tree

12 files changed

+113
-33
lines changed

12 files changed

+113
-33
lines changed

packages/ogre/src/checkout.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test } from "tap";
22
import {
33
addOneStep,
4-
ComplexObject,
4+
type ComplexObject,
55
getBaseline,
66
sumChanges,
77
testAuthor,

packages/ogre/src/commit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { test } from "tap";
22

33
import {
44
addOneStep,
5-
ComplexObject,
5+
type ComplexObject,
66
getBaseline,
77
sumChanges,
88
testAuthor,

packages/ogre/src/commit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { digest } from "./hash.js";
2-
import { Operation } from "fast-json-patch";
2+
import type {Operation} from "fast-json-patch";
33

44
export interface Commit {
55
/*The hash of the commit. Is a sha256 of:

packages/ogre/src/git2json.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Commit } from "./commit.js";
2-
import { History, Reference } from "./interfaces.js";
1+
import type {Commit} from "./commit.js";
2+
import type {History, Reference} from "./interfaces.js";
33
import {
44
cleanAuthor,
55
createHeadRefValue,

packages/ogre/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
export * from "./interfaces.js";
22
export * from "./repository.js";
3-
export { Commit } from "./commit.js";
3+
export type { Commit } from "./commit.js";
44
export * from "./ref.js";
55
export * from "./size.js";
66
export * from "./utils.js";
77
export * from "./git2json.js";
88

9-
export { compare, deepClone, Operation, JsonPatchError } from "fast-json-patch";
9+
export {compare, deepClone, JsonPatchError} from "fast-json-patch";
10+
export type { Operation } from "fast-json-patch";
11+

packages/ogre/src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Commit } from "./commit.js";
1+
import type {Commit} from "./commit.js";
22

33
export interface Reference {
44
name: string;

packages/ogre/src/repository.test.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { Repository } from "./repository.js";
55
import {
66
addOneStep,
77
addOneStep as addOneNested,
8-
ComplexObject,
8+
type ComplexObject,
99
getBaseline,
1010
sumChanges,
1111
testAuthor,
1212
updateHeaderData,
1313
} from "./test.utils.js";
14-
import { Reference } from "./interfaces.js";
14+
import type {Reference} from "./interfaces.js";
1515
import { compare } from "fast-json-patch";
1616
import {objectToTree, treeToObject} from "./serialize.js";
1717

@@ -60,6 +60,13 @@ test("restore", async (t) => {
6060
"main is pointing at wrong commit",
6161
);
6262
t.equal(history.commits.length, 2, "incorrect # of commits");
63+
const logs = repo.logs()
64+
t.equal(logs.length, 3, "incorrect # of logs")
65+
const logs1 = repo.logs(1)
66+
t.equal(logs1.length, 1, "incorrect # of logs retrieved")
67+
68+
const logs2 = repo.logs(100)
69+
t.equal(logs2.length, 3, "incorrect # of logs retrieved")
6370

6471
// start reconstruction
6572
const p = {};
@@ -280,7 +287,7 @@ test("history", async (t) => {
280287
});
281288

282289
test("reset", async (t) => {
283-
t.test("reset hard", async (t) => {
290+
t.test("discard uncommitted changes", async (t) => {
284291
const [repo, co] = await getBaseline();
285292
co.uuid = "asdf";
286293
const hash = await repo.commit("baseline", testAuthor);
@@ -292,10 +299,60 @@ test("reset", async (t) => {
292299
t.equal(diff.length, changes, "wrong # of changes in diff");
293300

294301
// reset
295-
repo.reset("hard");
302+
await repo.reset("hard");
296303
const diff2 = await repo.diff(hash);
297304
t.equal(diff2.length, 0, "failed to reset");
298305
});
306+
307+
t.test("reset to earlier commit", async t => {
308+
const [repo, co] = await getBaseline();
309+
co.uuid = "asdf";
310+
const hash = await repo.commit("baseline", testAuthor);
311+
const h1 = repo.getHistory();
312+
t.equal(h1.commits.length, 1);
313+
314+
// do changes
315+
const changes = updateHeaderData(co);
316+
const hash2 = await repo.commit("header data", testAuthor);
317+
const h2 = repo.getHistory();
318+
t.equal(h2.commits.length, 2);
319+
const diff = await repo.diff(hash);
320+
t.equal(diff.length, changes, "wrong # of changes in diff");
321+
t.equal(hash !== hash2, true, "hash should not be the same");
322+
323+
// reset
324+
await repo.reset("hard", hash);
325+
const diff2 = await repo.diff(hash);
326+
t.equal(diff2.length, 0, "failed to reset");
327+
const h3 = repo.getHistory()
328+
t.equal(h3.refs.get("refs/heads/main")!.value, hash, "main should point to first hash");
329+
})
330+
331+
t.test("reset to earlier version tag", async t => {
332+
const [repo, co] = await getBaseline();
333+
co.uuid = "asdf";
334+
const hash = await repo.commit("baseline", testAuthor);
335+
repo.tag("v0.1.0")
336+
const h1 = repo.getHistory();
337+
t.equal(h1.commits.length, 1);
338+
339+
// do changes
340+
const changes = updateHeaderData(co);
341+
const hash2 = await repo.commit("header data", testAuthor);
342+
repo.tag("v0.2.0")
343+
const h2 = repo.getHistory();
344+
t.equal(h2.commits.length, 2);
345+
const diff = await repo.diff(hash);
346+
t.equal(diff.length, changes, "wrong # of changes in diff");
347+
t.equal(hash !== hash2, true, "hash should not be the same");
348+
349+
// reset
350+
await repo.reset("hard", "v0.1.0");
351+
const diff2 = await repo.diff(hash);
352+
t.equal(diff2.length, 0, "failed to reset");
353+
const h3 = repo.getHistory()
354+
t.equal(h3.refs.get("refs/heads/main")!.value, hash, "main should point to first hash");
355+
})
299356
});
300357

301358
test("status", async (t) => {

packages/ogre/src/repository.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {
22
applyPatch,
3-
applyReducer,
43
compare,
54
deepClone,
65
generate,
76
JsonPatchError,
87
observe,
9-
Observer,
10-
Operation,
8+
type Observer,
9+
type Operation,
1110
unobserve,
1211
validate,
1312
} from "fast-json-patch";
@@ -22,12 +21,12 @@ import {
2221
headValueRefPrefix,
2322
immutableArrayCopy,
2423
immutableMapCopy,
24+
refKeysAtCommit,
2525
localHeadPathPrefix,
2626
mapPath,
2727
mutableMapCopy,
2828
REFS_HEAD_KEY,
2929
REFS_MAIN_KEY,
30-
refsAtCommit,
3130
shaishToCommit,
3231
tagToRef,
3332
validateBranchName,
@@ -380,14 +379,16 @@ export class Repository<T extends { [k: PropertyKey]: any }>
380379
const [commit] = shaishToCommit(shaish, this.refs, this.commits);
381380
await this.moveTo(commit);
382381

383-
const refs = refsAtCommit(this.refs, commit);
382+
const commitAtHEAD = this.mustCommitAtHead()
383+
const refs = refKeysAtCommit(this.refs, commitAtHEAD);
384+
384385
// reset only moves heads and not tags
385-
const moveableRefs = refs.filter((r) =>
386-
r.name.startsWith(localHeadPathPrefix()),
386+
const moveableRefs = refs.filter((ref) =>
387+
ref.startsWith(localHeadPathPrefix()),
387388
);
388389

389390
for (const ref of moveableRefs) {
390-
this.moveRef(ref.name, commit);
391+
this.moveRef(ref, commit);
391392
}
392393

393394
if (mode === "hard") {

packages/ogre/src/test.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { v4 as uuid4 } from "uuid";
2-
import { Repository, RepositoryObject } from "./repository.js";
3-
import { Commit } from "./commit.js";
2+
import { Repository, type RepositoryObject } from "./repository.js";
3+
import type {Commit} from "./commit.js";
44

55
export type NestedObject = {
66
name?: string;

packages/ogre/src/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test } from "tap";
22
import { cleanAuthor, mapPath, shaishToCommit } from "./utils.js";
33
import { Repository } from "./repository.js";
4-
import { ComplexObject, testAuthor } from "./test.utils.js";
4+
import {type ComplexObject, testAuthor } from "./test.utils.js";
55

66
test("author <email@domain.info>", (t) => {
77
const [name, email] = cleanAuthor("author <email@domain.info>");

0 commit comments

Comments
 (0)