@dotinc/ogre 0.3.0 → 0.3.1-canary.132.73cd488.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.
package/README.md CHANGED
@@ -11,43 +11,43 @@ keep the history around for a bit longer.
11
11
  - Checkout
12
12
  - Visualization via `@dotinc/ogre-react`
13
13
  - Merge
14
- - fast-forward
14
+ - fast-forward
15
15
 
16
16
  ## Usage
17
17
 
18
18
  ```typescript
19
- const repo = new Repository(new ComplexObject())
19
+ const repo = new Repository(new ComplexObject());
20
20
 
21
21
  // apply changes
22
- repo.data.name = 'my name'
23
- repo.data.description = 'now we have a description'
22
+ repo.data.name = "my name";
23
+ repo.data.description = "now we have a description";
24
24
 
25
- // commit changes
26
- const init = await repo.commit('initial commit', 'author <author@test.com>')
25
+ // commit changes
26
+ const init = await repo.commit("initial commit", "author <author@test.com>");
27
27
  // create a branch named savepoint pointing to the last commit
28
- repo.createBranch('savepoint')
28
+ repo.createBranch("savepoint");
29
29
 
30
30
  // switch to new branch
31
- repo.checkout('add_details', true)
31
+ repo.checkout("add_details", true);
32
32
 
33
33
  // apply changes
34
- repo.data.name = 'a fancier name'
34
+ repo.data.name = "a fancier name";
35
35
 
36
36
  // a) commit & merge
37
- await repo.commit('change name', 'author <author@test.com>')
38
- repo.checkout('main')
39
- repo.merge('add_details')
40
- repo.tag('v1.0.0')
37
+ await repo.commit("change name", "author <author@test.com>");
38
+ repo.checkout("main");
39
+ repo.merge("add_details");
40
+ repo.tag("v1.0.0");
41
41
 
42
42
  // or b) discard change and go back
43
- // by using the branch name
44
- repo.checkout('main')
45
- // by using the commit hash in a detached state
46
- repo.checkout(init)
43
+ // by using the branch name
44
+ repo.checkout("main");
45
+ // by using the commit hash in a detached state
46
+ repo.checkout(init);
47
47
  ```
48
48
 
49
49
  ## TODO
50
50
 
51
51
  - [ ] Merge
52
- - [ ] recursive
53
- - [ ] octopus
52
+ - [ ] recursive
53
+ - [ ] octopus
package/lib/commit.d.ts CHANGED
@@ -1,19 +1,18 @@
1
- import { Change } from './interfaces';
1
+ import { Operation } from "fast-json-patch";
2
2
  export interface Commit {
3
3
  hash: string;
4
4
  tree: string;
5
5
  message: string | undefined;
6
6
  author: string;
7
7
  parent: string | undefined;
8
- changes: Change[];
8
+ changes: Operation[];
9
9
  timestamp: Date;
10
- to: number;
11
10
  }
12
11
  export interface CommitHashContent {
13
12
  message: string;
14
13
  author: string;
15
14
  parentRef: string | undefined;
16
- changes: Change[];
15
+ changes: Operation[];
17
16
  timestamp: Date;
18
17
  }
19
18
  export declare function calculateCommitHash(content: CommitHashContent): Promise<string>;
@@ -0,0 +1,33 @@
1
+ import { History } from "./interfaces";
2
+ /**
3
+ * The function `formatGit2Json` takes a `history` object and returns an array of formatted commit
4
+ * objects.
5
+ * @param {History} history - The `history` parameter is an object that contains information about the
6
+ * Repository history.
7
+ * @returns The function `formatGit2Json` returns an array of objects. Each object represents a commit
8
+ * in the Repository history. The json representation is returned in `git2json` format based on:
9
+ * https://github.com/fabien0102/git2json/blob/e067166d2468018b6f3982a8fb44a2e54110ce02/src/git2json.js#L5-L22
10
+ */
11
+ export declare const formatGit2Json: <T = any>(history: History<T>) => {
12
+ refs: string[];
13
+ hash: string;
14
+ hashAbbrev: string;
15
+ tree: string;
16
+ treeAbbrev: string;
17
+ parents: string[];
18
+ parentsAbbrev: string[];
19
+ committer: {
20
+ name: string;
21
+ email: string;
22
+ date: number;
23
+ };
24
+ author: {
25
+ name: string;
26
+ email: string;
27
+ timestamp: number;
28
+ };
29
+ subject: string | undefined;
30
+ body: string;
31
+ notes: string;
32
+ stats: never[];
33
+ }[];
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatGit2Json = void 0;
4
+ const repository_1 = require("./repository");
5
+ const utils_1 = require("./utils");
6
+ const findRefs = (commit, refs) => {
7
+ const list = [];
8
+ const headRef = refs.get(repository_1.REFS_HEAD_KEY);
9
+ for (const [key, ref] of refs.entries()) {
10
+ if (ref.value === commit.hash) {
11
+ if ((0, repository_1.isTagRef)(key)) {
12
+ list.push(`tag: ${ref.name}`);
13
+ }
14
+ else {
15
+ list.push(ref.name);
16
+ }
17
+ // also check if HEAD is pointing to this ref
18
+ if (headRef && headRef.value === (0, repository_1.createHeadRefValue)(key)) {
19
+ list.push(headRef.name);
20
+ }
21
+ }
22
+ }
23
+ return list;
24
+ };
25
+ /**
26
+ * The function `formatGit2Json` takes a `history` object and returns an array of formatted commit
27
+ * objects.
28
+ * @param {History} history - The `history` parameter is an object that contains information about the
29
+ * Repository history.
30
+ * @returns The function `formatGit2Json` returns an array of objects. Each object represents a commit
31
+ * in the Repository history. The json representation is returned in `git2json` format based on:
32
+ * https://github.com/fabien0102/git2json/blob/e067166d2468018b6f3982a8fb44a2e54110ce02/src/git2json.js#L5-L22
33
+ */
34
+ const formatGit2Json = (history) => {
35
+ const { commits, refs } = history;
36
+ return commits.reverse().map((c) => {
37
+ const [name, email] = (0, utils_1.cleanAuthor)(c.author);
38
+ return {
39
+ refs: findRefs(c, refs),
40
+ hash: c.hash,
41
+ hashAbbrev: c.hash.substring(0, 8),
42
+ tree: c.tree,
43
+ treeAbbrev: c.tree.substring(0, 8),
44
+ // FIXME there is only one parent at the moment on ogre
45
+ parents: c.parent ? [c.parent] : [],
46
+ parentsAbbrev: c.parent ? [c.parent.substring(0, 8)] : [],
47
+ committer: {
48
+ name,
49
+ email,
50
+ date: c.timestamp.getMilliseconds(),
51
+ },
52
+ author: {
53
+ name,
54
+ email,
55
+ timestamp: c.timestamp.getMilliseconds(),
56
+ },
57
+ subject: c.message,
58
+ body: "",
59
+ notes: "",
60
+ // MAYBE? map changes to {additions: number, deletions: number, file: string}
61
+ stats: [],
62
+ };
63
+ });
64
+ };
65
+ exports.formatGit2Json = formatGit2Json;
package/lib/hash.js CHANGED
@@ -23,25 +23,29 @@ exports.digest = void 0;
23
23
  *
24
24
  * @returns a promise that resolves to a string with hexadecimal content.
25
25
  */
26
- function digest(obj, algorithm = 'SHA-256', isBrowser = false) {
27
- const algorithms = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'];
26
+ function digest(obj, algorithm = "SHA-256", isBrowser = false) {
27
+ // eslint-disable-line
28
+ const algorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];
28
29
  if (!algorithms.includes(algorithm)) {
29
30
  throw RangeError(`Valid hash algorithm values are any of ${JSON.stringify(algorithms)}`);
30
31
  }
31
32
  return (async function (obj, algorithm) {
32
33
  const encoder = new TextEncoder();
33
34
  const hashInput = encoder.encode(hashable(obj)).buffer;
34
- let digest = '';
35
+ let digest = "";
35
36
  if (isBrowser) {
36
37
  const buf = await crypto.subtle.digest(algorithm, hashInput);
37
- const h = '0123456789abcdef';
38
- (new Uint8Array(buf)).forEach((v) => {
38
+ const h = "0123456789abcdef";
39
+ new Uint8Array(buf).forEach((v) => {
39
40
  digest += h[v >> 4] + h[v & 15];
40
41
  });
41
42
  }
42
43
  else {
43
- const nodeAlg = algorithm.toLowerCase().replace('-', '');
44
- digest = require('crypto').createHash(nodeAlg).update(Buffer.from(hashInput)).digest('hex'); // eslint-disable-line
44
+ const nodeAlg = algorithm.toLowerCase().replace("-", "");
45
+ digest = require("crypto")
46
+ .createHash(nodeAlg)
47
+ .update(Buffer.from(hashInput))
48
+ .digest("hex"); // eslint-disable-line
45
49
  }
46
50
  /* eslint-enable no-lone-blocks */
47
51
  return digest;
@@ -49,7 +53,7 @@ function digest(obj, algorithm = 'SHA-256', isBrowser = false) {
49
53
  }
50
54
  exports.digest = digest;
51
55
  function isObject(val) {
52
- return (val != null) && (typeof val === 'object') && !(Array.isArray(val));
56
+ return val != null && typeof val === "object" && !Array.isArray(val);
53
57
  }
54
58
  function objectToArraySortedByKey(obj) {
55
59
  if (!isObject(obj) && !Array.isArray(obj)) {
package/lib/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- export * from './interfaces';
2
- export * from './repository';
3
- export { Commit } from './commit';
4
- export * from './ref';
5
- export * from './size';
6
- export * from './utils';
1
+ export * from "./interfaces";
2
+ export * from "./repository";
3
+ export { Commit } from "./commit";
4
+ export * from "./ref";
5
+ export * from "./size";
6
+ export * from "./utils";
7
+ export * from "./git2json";
package/lib/index.js CHANGED
@@ -6,3 +6,4 @@ tslib_1.__exportStar(require("./repository"), exports);
6
6
  tslib_1.__exportStar(require("./ref"), exports);
7
7
  tslib_1.__exportStar(require("./size"), exports);
8
8
  tslib_1.__exportStar(require("./utils"), exports);
9
+ tslib_1.__exportStar(require("./git2json"), exports);
@@ -1,14 +1,12 @@
1
- import { Commit } from './commit';
1
+ import { Commit } from "./commit";
2
2
  export interface Reference {
3
3
  name: string;
4
4
  value: string;
5
5
  }
6
- export interface Change {
7
- path: any[];
8
- newValue: any | undefined;
9
- oldValue: any | undefined;
10
- }
11
- export interface History {
6
+ export interface History<T extends {
7
+ [k: string]: any;
8
+ }> {
9
+ original: T;
12
10
  refs: Map<string, Reference>;
13
11
  commits: Commit[];
14
12
  }
package/lib/ref.js CHANGED
@@ -4,7 +4,7 @@ exports.validBranch = exports.validRef = void 0;
4
4
  const bad = /(^|[/.])([/.]|$)|^@$|@{|[\x00-\x20\x7f~^:?*[\\]|\.lock(\/|$)/;
5
5
  const badBranch = /^(-|HEAD$)/;
6
6
  function validRef(name, onelevel) {
7
- return !bad.test(name) && (onelevel || name.includes('/'));
7
+ return !bad.test(name) && (onelevel || name.includes("/"));
8
8
  }
9
9
  exports.validRef = validRef;
10
10
  function validBranch(name) {
@@ -1,30 +1,68 @@
1
- import { History } from './interfaces';
2
- import { Commit } from './commit';
1
+ import { Operation } from "fast-json-patch";
2
+ import { Commit } from "./commit";
3
+ import { History } from "./interfaces";
3
4
  export declare const REFS_HEAD_KEY = "HEAD";
4
5
  export declare const REFS_MAIN_KEY: string;
5
- export interface RepositoryOptions<T> {
6
- history?: History;
6
+ export interface RepositoryOptions<T extends {
7
+ [k: string]: any;
8
+ }> {
9
+ history?: History<T>;
7
10
  }
8
- export interface RepositoryObject<T> {
11
+ export interface RepositoryObject<T extends {
12
+ [k: string]: any;
13
+ }> {
9
14
  data: T;
10
- getHistory(): History;
15
+ getHistory(): History<T>;
16
+ /**
17
+ * Returns the diff between the current HEAD and provided shaish expression
18
+ *
19
+ * @param shaishFrom expression (e.g. refs (branches, tags), commitSha)
20
+ * @param shaishTo expression (e.g. refs (branches, tags), commitSha)
21
+ */
22
+ diff(shaishFrom: string, shaishTo?: string): Operation[];
11
23
  head(): string;
12
24
  ref(reference: string): string | undefined;
13
25
  commit(message: string, author: string, amend?: boolean): Promise<string>;
14
26
  checkout(shaish: string, createBranch?: boolean): void;
15
27
  logs(commits?: number): Commit[];
16
28
  createBranch(name: string): string;
17
- merge(source: string | RepositoryObject<T> | History): string;
29
+ merge(source: string | RepositoryObject<T> | History<T>): string;
30
+ /**
31
+ * Branch returns the current branch name
32
+ */
18
33
  branch(): string;
19
34
  tag(tag: string): string;
20
35
  }
21
- export interface RespositoryObjectType {
22
- new <T>(obj: T, options: RepositoryOptions<T>): RepositoryObject<T>;
23
- }
24
36
  /**
25
37
  * A repository recording and managing the state transitions of an object
26
38
  */
27
- export declare const Repository: RespositoryObjectType;
39
+ export declare class Repository<T extends {
40
+ [k: PropertyKey]: any;
41
+ }> implements RepositoryObject<T> {
42
+ constructor(obj: T, options: RepositoryOptions<T>);
43
+ private readonly original;
44
+ data: T;
45
+ private observer;
46
+ private readonly refs;
47
+ private readonly commits;
48
+ private moveTo;
49
+ branch(): string;
50
+ diff(shaishFrom: string, shaishTo?: string): Operation[];
51
+ checkout(shaish: string, createBranch?: boolean): void;
52
+ commit(message: string, author: string, amend?: boolean): Promise<string>;
53
+ commitAtHead(): Commit | undefined;
54
+ mustCommitAtHead(): Commit;
55
+ createBranch(name: string): string;
56
+ head(): string;
57
+ private collectCommits;
58
+ getHistory(): History<T>;
59
+ logs(numberOfCommits?: number): Commit[];
60
+ merge(source: string | RepositoryObject<T> | History<T>): string;
61
+ private moveRef;
62
+ private saveNewRef;
63
+ ref(reference: string): string | undefined;
64
+ tag(tag: string): string;
65
+ }
28
66
  export declare const createHeadRefValue: (refKey: string) => string;
29
67
  export declare const isTagRef: (refKey: string) => boolean;
30
68
  export declare const cleanRefValue: (ref: string) => string;
@@ -36,4 +74,6 @@ export declare const validateRef: (name: string, oneLevel?: boolean) => void;
36
74
  * Prints the underlying changelog of a repository
37
75
  * @param repository
38
76
  */
39
- export declare const printChangeLog: <T>(repository: RepositoryObject<T>) => void;
77
+ export declare const printChangeLog: <T extends {
78
+ [k: string]: any;
79
+ }>(repository: RepositoryObject<T>) => void;