@dotinc/ogre 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/.tap/processinfo/0799a6bd-022f-41d1-8be9-6338667041c0.json +245 -0
  2. package/.tap/processinfo/4cdc23d3-f7ce-4075-922f-722bf0d687fc.json +245 -0
  3. package/.tap/processinfo/6d907291-2f1e-4670-b04c-3a667271c352.json +245 -0
  4. package/.tap/processinfo/7dc6b1af-ac78-47bc-9b62-630e387e8755.json +245 -0
  5. package/.tap/processinfo/8c48e5e9-6eca-4afe-a7b5-04e26b765d29.json +241 -0
  6. package/.tap/processinfo/8ca695ae-d038-441a-b4ab-2a242d22d416.json +245 -0
  7. package/.tap/processinfo/98475c49-5ee5-4c2c-b8e2-85bd2af528c9.json +245 -0
  8. package/.tap/test-results/src/branch.test.ts.tap +40 -0
  9. package/.tap/test-results/src/checkout.test.ts.tap +52 -0
  10. package/.tap/test-results/src/commit.test.ts.tap +105 -0
  11. package/.tap/test-results/src/merge.test.ts.tap +16 -0
  12. package/.tap/test-results/src/repository.test.ts.tap +63 -0
  13. package/.tap/test-results/src/tag.test.ts.tap +19 -0
  14. package/.tap/test-results/src/utils.test.ts.tap +43 -0
  15. package/CHANGELOG.md +34 -1
  16. package/README.md +6 -4
  17. package/lib/commit.d.ts +0 -0
  18. package/lib/commit.js +0 -0
  19. package/lib/git2json.d.ts +0 -0
  20. package/lib/git2json.js +0 -0
  21. package/lib/hash.d.ts +0 -0
  22. package/lib/hash.js +0 -0
  23. package/lib/index.d.ts +0 -0
  24. package/lib/index.js +0 -0
  25. package/lib/interfaces.d.ts +0 -0
  26. package/lib/interfaces.js +0 -0
  27. package/lib/ref.d.ts +0 -0
  28. package/lib/ref.js +0 -0
  29. package/lib/repository.d.ts +7 -0
  30. package/lib/repository.js +32 -0
  31. package/lib/size.d.ts +0 -0
  32. package/lib/size.js +0 -0
  33. package/lib/test.utils.d.ts +9 -9
  34. package/lib/test.utils.js +8 -12
  35. package/lib/utils.d.ts +0 -0
  36. package/lib/utils.js +0 -0
  37. package/package.json +8 -20
  38. package/src/branch.test.ts +17 -17
  39. package/src/checkout.test.ts +29 -26
  40. package/src/commit.test.ts +60 -69
  41. package/src/merge.test.ts +13 -9
  42. package/src/repository.test.ts +160 -82
  43. package/src/repository.ts +63 -10
  44. package/src/tag.test.ts +6 -6
  45. package/src/test.utils.ts +18 -13
  46. package/src/utils.test.ts +21 -14
package/src/repository.ts CHANGED
@@ -59,6 +59,13 @@ export interface RepositoryObject<T extends { [k: string]: any }> {
59
59
  branch(): string;
60
60
 
61
61
  tag(tag: string): string;
62
+
63
+ /**
64
+ * Moves the HEAD and the branch to a specific shaish (commit or tag)
65
+ * @param mode hard - discard changes
66
+ * @param shaish
67
+ */
68
+ reset(mode?: "soft" | "hard", shaish?: string): void;
62
69
  }
63
70
 
64
71
  /**
@@ -84,12 +91,22 @@ export class Repository<T extends { [k: PropertyKey]: any }>
84
91
  ]);
85
92
 
86
93
  this.commits = options.history?.commits ?? [];
94
+
95
+ if (options.history) {
96
+ const commit = this.commitAtHead();
97
+ if (!commit) {
98
+ return;
99
+ }
100
+ this.moveTo(commit);
101
+ }
87
102
  }
88
103
 
89
104
  private readonly original: T;
90
105
 
91
106
  data: T;
107
+
92
108
  private observer: Observer<T>;
109
+
93
110
  private readonly refs: Map<string, Reference>;
94
111
  private readonly commits: Commit[];
95
112
 
@@ -104,6 +121,32 @@ export class Repository<T extends { [k: PropertyKey]: any }>
104
121
  this.observer = observe(this.data);
105
122
  }
106
123
 
124
+ reset(
125
+ mode: "soft" | "hard" | undefined = "hard",
126
+ shaish: string | undefined = REFS_HEAD_KEY,
127
+ ): void {
128
+ if (mode === "hard") {
129
+ unobserve(this.data, this.observer);
130
+ }
131
+
132
+ const [commit] = shaishToCommit(shaish, this.refs, this.commits);
133
+ this.moveTo(commit);
134
+
135
+ const refs = refsAtCommit(this.refs, commit);
136
+ // reset only moves heads and not tags
137
+ const moveableRefs = refs.filter((r) =>
138
+ r.name.startsWith(headsRefPathPrefix),
139
+ );
140
+
141
+ for (const ref of moveableRefs) {
142
+ this.moveRef(ref.name, commit);
143
+ }
144
+
145
+ if (mode === "hard") {
146
+ this.observer = observe(this.data);
147
+ }
148
+ }
149
+
107
150
  branch(): string {
108
151
  const currentHeadRef = this.refs.get(REFS_HEAD_KEY);
109
152
  if (!currentHeadRef) {
@@ -145,12 +188,12 @@ export class Repository<T extends { [k: PropertyKey]: any }>
145
188
  const [commit, isRef, refKey] = shaishToCommit(
146
189
  shaish,
147
190
  this.refs,
148
- this.commits
191
+ this.commits,
149
192
  );
150
193
  this.moveTo(commit);
151
194
  this.moveRef(
152
195
  REFS_HEAD_KEY,
153
- isRef && refKey !== undefined ? refKey : commit
196
+ isRef && refKey !== undefined ? refKey : commit,
154
197
  );
155
198
  }
156
199
  }
@@ -158,7 +201,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
158
201
  async commit(
159
202
  message: string,
160
203
  author: string,
161
- amend?: boolean
204
+ amend?: boolean,
162
205
  ): Promise<string> {
163
206
  let parent = this.commitAtHead();
164
207
  if (amend && !parent) {
@@ -196,7 +239,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
196
239
  });
197
240
 
198
241
  const treeHash = Buffer.from(
199
- compressSync(strToU8(JSON.stringify(this.data)), { level: 6, mem: 8 })
242
+ compressSync(strToU8(JSON.stringify(this.data)), { level: 6, mem: 8 }),
200
243
  ).toString("base64");
201
244
  const commit = {
202
245
  hash: sha,
@@ -307,7 +350,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
307
350
  throw new Error(
308
351
  `fatal: source type (${
309
352
  source instanceof Repository ? "Repository" : "History"
310
- }) not implemented`
353
+ }) not implemented`,
311
354
  );
312
355
  }
313
356
 
@@ -374,7 +417,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
374
417
  throw new Error(`unreachable: HEAD not present`);
375
418
  }
376
419
  throw new Error(
377
- `fatal: not a valid object name: '${getLastItem(headRef)}'`
420
+ `fatal: not a valid object name: '${getLastItem(headRef)}'`,
378
421
  );
379
422
  }
380
423
  this.refs.set(refKey, { name: name, value: headCommit.hash });
@@ -430,7 +473,7 @@ const traverseAndCollectChangelog = (commit: Commit, commitsList: Commit[]) => {
430
473
  const mapPath = (
431
474
  from: Commit,
432
475
  to: Commit,
433
- commits: Commit[]
476
+ commits: Commit[],
434
477
  ): [isAncestor: boolean] => {
435
478
  let c: Commit | undefined = to;
436
479
  while (c !== undefined) {
@@ -451,7 +494,7 @@ const mapPath = (
451
494
  const commitAtRefIn = (
452
495
  ref: string,
453
496
  references: Map<string, Reference>,
454
- commitsList: Commit[]
497
+ commitsList: Commit[],
455
498
  ) => {
456
499
  const reference = references.get(ref);
457
500
  if (!reference) {
@@ -477,6 +520,16 @@ const commitAtRefIn = (
477
520
  return undefined;
478
521
  };
479
522
 
523
+ const refsAtCommit = (references: Map<string, Reference>, commit: Commit) => {
524
+ const list: Array<Reference> = [];
525
+ for (const [name, ref] of references.entries()) {
526
+ if (ref.value === commit.hash) {
527
+ list.push(ref);
528
+ }
529
+ }
530
+ return list;
531
+ };
532
+
480
533
  /**
481
534
  * Accepts a shaish expression (e.g. refs (branches, tags), commitSha) and returns
482
535
  * - a commit of type Commit
@@ -486,7 +539,7 @@ const commitAtRefIn = (
486
539
  const shaishToCommit = (
487
540
  shaish: string,
488
541
  references: Map<string, Reference>,
489
- commitsList: Commit[]
542
+ commitsList: Commit[],
490
543
  ): [commit: Commit, isRef: boolean, ref: string | undefined] => {
491
544
  let sha = shaish;
492
545
  let isRef = false;
@@ -557,7 +610,7 @@ export const validateRef = (name: string, oneLevel: boolean = true) => {
557
610
  * @param repository
558
611
  */
559
612
  export const printChangeLog = <T extends { [k: string]: any }>(
560
- repository: RepositoryObject<T>
613
+ repository: RepositoryObject<T>,
561
614
  ) => {
562
615
  console.log("----------------------------------------------------------");
563
616
  console.log(`Changelog at ${repository.head()}`);
package/src/tag.test.ts CHANGED
@@ -1,4 +1,4 @@
1
- import test from "ava";
1
+ import { test } from "tap";
2
2
  import { getBaseline, testAuthor } from "./test.utils";
3
3
 
4
4
  test("cannot tag on an empty repo", async (t) => {
@@ -8,7 +8,7 @@ test("cannot tag on an empty repo", async (t) => {
8
8
  () => {
9
9
  repo.tag("v1.0.0");
10
10
  },
11
- { message: `fatal: failed to resolve 'HEAD' as a valid ref.` }
11
+ { message: `fatal: failed to resolve 'HEAD' as a valid ref.` },
12
12
  );
13
13
  });
14
14
 
@@ -21,10 +21,10 @@ test("can create simple tag pointing to HEAD", async (t) => {
21
21
  const tagRef = repo.tag(tag);
22
22
 
23
23
  let expectedRefKey = `refs/tags/${tag}`;
24
- t.is(tagRef, expectedRefKey);
25
- t.is(repo.ref(tagRef), commit, "tag is not pointing to expected commit");
24
+ t.equal(tagRef, expectedRefKey);
25
+ t.equal(repo.ref(tagRef), commit, "tag is not pointing to expected commit");
26
26
  const { refs } = repo.getHistory();
27
- t.true(refs.has(expectedRefKey), "reference was not present in history");
27
+ t.ok(refs.has(expectedRefKey), "reference was not present in history");
28
28
  });
29
29
 
30
30
  test("cannot create tag with whitespace", async (t) => {
@@ -37,6 +37,6 @@ test("cannot create tag with whitespace", async (t) => {
37
37
  () => {
38
38
  repo.tag(tag);
39
39
  },
40
- { message: `fatal: '${tag}' is not a valid tag name` }
40
+ { message: `fatal: '${tag}' is not a valid tag name` },
41
41
  );
42
42
  });
package/src/test.utils.ts CHANGED
@@ -2,24 +2,29 @@ import { v4 as uuid4 } from "uuid";
2
2
  import { Repository, RepositoryObject } from "./repository";
3
3
  import { Commit } from "./commit";
4
4
 
5
- export class NestedObject {
6
- public name: string | undefined;
7
- public uuid: string | undefined;
8
- }
9
-
10
- export class ComplexObject {
11
- public uuid: string | undefined;
12
- public name: string | undefined;
13
- public description: string | undefined;
14
- public nested: NestedObject[] = [];
15
- }
5
+ export type NestedObject = {
6
+ name?: string;
7
+ uuid?: string;
8
+ };
9
+
10
+ export type ComplexObject = {
11
+ uuid?: string;
12
+ name?: string;
13
+ description?: string;
14
+ nested: NestedObject[];
15
+ };
16
16
 
17
17
  export const testAuthor = "User name <name@domain.com>";
18
18
 
19
19
  export async function getBaseline(): Promise<
20
20
  [RepositoryObject<ComplexObject>, ComplexObject]
21
21
  > {
22
- const co = new ComplexObject();
22
+ const co: ComplexObject = {
23
+ uuid: undefined,
24
+ name: undefined,
25
+ description: undefined,
26
+ nested: [],
27
+ };
23
28
  const repo = new Repository(co, {});
24
29
  return [repo, co];
25
30
  }
@@ -33,7 +38,7 @@ export function updateHeaderData(wrapped: ComplexObject) {
33
38
  }
34
39
 
35
40
  export function addOneStep(wrapped: ComplexObject) {
36
- const pe = new NestedObject();
41
+ const pe: NestedObject = {};
37
42
  pe.uuid = uuid4();
38
43
  pe.name = "first name";
39
44
 
package/src/utils.test.ts CHANGED
@@ -1,40 +1,46 @@
1
- import test from "ava";
1
+ import { test } from "tap";
2
2
  import { cleanAuthor } from "./utils";
3
3
 
4
4
  test("author <email@domain.info>", (t) => {
5
5
  const [name, email] = cleanAuthor("author <email@domain.info>");
6
- t.is(name, "author");
7
- t.is(email, "email@domain.info");
6
+ t.equal(name, "author");
7
+ t.equal(email, "email@domain.info");
8
+ t.end();
8
9
  });
9
10
 
10
11
  test("author with space <email@domain.info>", (t) => {
11
12
  const [name, email] = cleanAuthor("author with space <email@domain.info>");
12
- t.is(name, "author with space");
13
- t.is(email, "email@domain.info");
13
+ t.equal(name, "author with space");
14
+ t.equal(email, "email@domain.info");
15
+ t.end();
14
16
  });
15
17
 
16
18
  test("author @handle", (t) => {
17
19
  const [name, email] = cleanAuthor("author @handle");
18
- t.is(name, "author");
19
- t.is(email, "@handle");
20
+ t.equal(name, "author");
21
+ t.equal(email, "@handle");
22
+ t.end();
20
23
  });
21
24
 
22
25
  test("author with space @handle", (t) => {
23
26
  const [name, email] = cleanAuthor("author with space @handle");
24
- t.is(name, "author with space");
25
- t.is(email, "@handle");
27
+ t.equal(name, "author with space");
28
+ t.equal(email, "@handle");
29
+ t.end();
26
30
  });
27
31
 
28
32
  test("email@domain.info", (t) => {
29
33
  const [name, email] = cleanAuthor("email@domain.info");
30
- t.is(name, "");
31
- t.is(email, "email@domain.info");
34
+ t.equal(name, "");
35
+ t.equal(email, "email@domain.info");
36
+ t.end();
32
37
  });
33
38
 
34
39
  test("@handle", (t) => {
35
40
  const [name, email] = cleanAuthor("@handle");
36
- t.is(name, "@handle");
37
- t.is(email, "");
41
+ t.equal(name, "@handle");
42
+ t.equal(email, "");
43
+ t.end();
38
44
  });
39
45
 
40
46
  test("empty author", (t) => {
@@ -42,6 +48,7 @@ test("empty author", (t) => {
42
48
  () => {
43
49
  cleanAuthor("");
44
50
  },
45
- { message: "author not provided" }
51
+ { message: "author not provided" },
46
52
  );
53
+ t.end();
47
54
  });