@monoweave/git 0.0.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ <!-- MONOWEAVE:BELOW -->
4
+
5
+ ## [0.0.2](https://github.com/monoweave/monoweave/compare/@monoweave/git@0.0.1...@monoweave/git@0.0.2) "@monoweave/git" (2024-02-29)<a name="0.0.2"></a>
6
+
7
+ ### Bug Fixes
8
+
9
+ * initial commit, forked from tophat/monodeploy under Apache 2.0 license (#1) ([2410d1c](https://github.com/monoweave/monoweave/commits/2410d1c))
10
+
11
+
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # Monoweave Git
2
+
3
+ This package is responsible for read/write interactions with git.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add @monoweave/git
9
+ ```
@@ -0,0 +1,2 @@
1
+ import type { CommitMessage, MonoweaveConfiguration, YarnContext } from '@monoweave/types';
2
+ export declare const getCommitMessages: (config: MonoweaveConfiguration, context?: YarnContext) => Promise<CommitMessage[]>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCommitMessages = void 0;
4
+ const gitCommands_1 = require("./gitCommands");
5
+ const DELIMITER = '-----------------monoweave-----------------';
6
+ const getCommitMessages = async (config, context) => {
7
+ const to = config.git.commitSha;
8
+ let from = config.git.baseBranch;
9
+ if (!from) {
10
+ const { sha, tag } = await (0, gitCommands_1.gitLastTaggedCommit)({
11
+ cwd: config.cwd,
12
+ context,
13
+ prerelease: config.prerelease,
14
+ });
15
+ from = sha;
16
+ if (to === from && tag !== null) {
17
+ // the latest commit is the tagged commit, this run of monoweave should be a no-op
18
+ return [];
19
+ }
20
+ }
21
+ const logOutput = await (0, gitCommands_1.gitLog)(from, to, {
22
+ cwd: config.cwd,
23
+ DELIMITER,
24
+ context,
25
+ });
26
+ return logOutput
27
+ .toString()
28
+ .split(`${DELIMITER}\n`)
29
+ .map((logEntry) => {
30
+ const [sha, ...msg] = logEntry.split('\n');
31
+ return { sha, body: msg.join('\n') };
32
+ })
33
+ .filter((msg) => msg.body);
34
+ };
35
+ exports.getCommitMessages = getCommitMessages;
@@ -0,0 +1,63 @@
1
+ import { type YarnContext } from '@monoweave/types';
2
+ export declare const gitCheckout: ({ files }: {
3
+ files: string[];
4
+ }, { cwd, context }: {
5
+ cwd: string;
6
+ context?: YarnContext | undefined;
7
+ }) => Promise<void>;
8
+ export declare const gitResolveSha: (ref: string, { cwd, context }: {
9
+ cwd: string;
10
+ context?: YarnContext | undefined;
11
+ }) => Promise<string>;
12
+ export declare const gitDiffTree: (ref: string, { cwd, context }: {
13
+ cwd: string;
14
+ context?: YarnContext | undefined;
15
+ }) => Promise<string>;
16
+ export declare const gitLog: (from: string, to: string, { cwd, DELIMITER, context }: {
17
+ cwd: string;
18
+ DELIMITER: string;
19
+ context?: YarnContext | undefined;
20
+ }) => Promise<string>;
21
+ export declare const gitTag: (tag: string, { cwd, context }: {
22
+ cwd: string;
23
+ context?: YarnContext | undefined;
24
+ }) => Promise<void>;
25
+ export declare const gitPushTags: ({ cwd, remote, context, dryRun, }: {
26
+ cwd: string;
27
+ remote: string;
28
+ context?: YarnContext | undefined;
29
+ dryRun?: boolean | undefined;
30
+ }) => Promise<void>;
31
+ export declare const gitPull: ({ cwd, remote, context, autostash, strategyOption, }: {
32
+ cwd: string;
33
+ remote: string;
34
+ context?: YarnContext | undefined;
35
+ autostash?: boolean | undefined;
36
+ strategyOption?: "theirs" | undefined;
37
+ }) => Promise<void>;
38
+ export declare const gitPush: ({ cwd, remote, context, dryRun, }: {
39
+ cwd: string;
40
+ remote: string;
41
+ context?: YarnContext | undefined;
42
+ dryRun?: boolean | undefined;
43
+ }) => Promise<void>;
44
+ export declare const gitLastTaggedCommit: ({ cwd, context, prerelease, }: {
45
+ cwd: string;
46
+ context?: YarnContext | undefined;
47
+ prerelease?: boolean | undefined;
48
+ }) => Promise<{
49
+ sha: string;
50
+ tag: string | null;
51
+ }>;
52
+ export declare const gitGlob: (globs: string[], { cwd, context }: {
53
+ cwd: string;
54
+ context?: YarnContext | undefined;
55
+ }) => Promise<string[]>;
56
+ export declare const gitAdd: (paths: string[], { cwd, context }: {
57
+ cwd: string;
58
+ context?: YarnContext | undefined;
59
+ }) => Promise<void>;
60
+ export declare const gitCommit: (message: string, { cwd, context }: {
61
+ cwd: string;
62
+ context?: YarnContext | undefined;
63
+ }) => Promise<void>;
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.gitCommit = exports.gitAdd = exports.gitGlob = exports.gitLastTaggedCommit = exports.gitPush = exports.gitPull = exports.gitPushTags = exports.gitTag = exports.gitLog = exports.gitDiffTree = exports.gitResolveSha = exports.gitCheckout = void 0;
30
+ const io_1 = require("@monoweave/io");
31
+ const logging_1 = __importStar(require("@monoweave/logging"));
32
+ const micromatch_1 = __importDefault(require("micromatch"));
33
+ const git = async (subcommand, { cwd, context }) => {
34
+ const command = `git ${subcommand}`;
35
+ logging_1.default.debug(`[Exec] ${command}`, { report: context?.report });
36
+ return await (0, io_1.exec)(command, { cwd, env: { GIT_TERMINAL_PROMPT: '0', ...process.env } });
37
+ };
38
+ const gitCheckout = async ({ files }, { cwd, context }) => {
39
+ const { stdout: branch } = await git('rev-parse --abbrev-ref --symbolic-full-name @\\{u\\}', {
40
+ cwd,
41
+ context,
42
+ });
43
+ await git(`checkout ${branch.trim()} -- ${files.map((f) => `"${f}"`).join(' ')}`, {
44
+ cwd,
45
+ context,
46
+ });
47
+ };
48
+ exports.gitCheckout = gitCheckout;
49
+ const gitResolveSha = async (ref, { cwd, context }) => {
50
+ const { stdout } = await git(`log --format=%H -n 1 ${ref}`, {
51
+ cwd,
52
+ context,
53
+ });
54
+ return stdout.trim();
55
+ };
56
+ exports.gitResolveSha = gitResolveSha;
57
+ const gitDiffTree = async (ref, { cwd, context }) => {
58
+ const { stdout } = await git(`diff-tree --no-commit-id --name-only -r --root ${ref.trim()}`, {
59
+ cwd,
60
+ context,
61
+ });
62
+ return stdout.trim();
63
+ };
64
+ exports.gitDiffTree = gitDiffTree;
65
+ const gitLog = async (from, to, { cwd, DELIMITER, context }) => {
66
+ let command = `log ${from}..${to} --format=%H%n%B%n${DELIMITER}`;
67
+ if (from === to) {
68
+ /* Special case where we'll just return a single log entry for "to". */
69
+ command = `log -1 ${to} --format=%H%n%B%n${DELIMITER}`;
70
+ }
71
+ const { stdout } = await git(command, { cwd, context });
72
+ return stdout;
73
+ };
74
+ exports.gitLog = gitLog;
75
+ const gitTag = async (tag, { cwd, context }) => {
76
+ (0, logging_1.assertProduction)();
77
+ await git(`tag ${tag} -m ${tag}`, { cwd, context });
78
+ };
79
+ exports.gitTag = gitTag;
80
+ const gitPushTags = async ({ cwd, remote, context, dryRun = false, }) => {
81
+ (0, logging_1.assertProduction)();
82
+ const args = ['--tags'];
83
+ if (dryRun)
84
+ args.push('--dry-run');
85
+ await git(`push ${args.join(' ')} ${remote}`, {
86
+ cwd,
87
+ context,
88
+ });
89
+ };
90
+ exports.gitPushTags = gitPushTags;
91
+ const gitPull = async ({ cwd, remote, context, autostash = false, strategyOption, }) => {
92
+ (0, logging_1.assertProduction)();
93
+ const args = ['--rebase', '--no-verify'];
94
+ if (autostash)
95
+ args.push('--autostash');
96
+ if (strategyOption)
97
+ args.push(`--strategy-option=${strategyOption}`);
98
+ await git(`pull ${args.join(' ')} ${remote}`, {
99
+ cwd,
100
+ context,
101
+ });
102
+ };
103
+ exports.gitPull = gitPull;
104
+ const gitPush = async ({ cwd, remote, context, dryRun = false, }) => {
105
+ (0, logging_1.assertProduction)();
106
+ const args = ['--no-verify'];
107
+ if (dryRun)
108
+ args.push('--dry-run');
109
+ await git(`push ${args.join(' ')} ${remote}`, {
110
+ cwd,
111
+ context,
112
+ });
113
+ };
114
+ exports.gitPush = gitPush;
115
+ const gitLastTaggedCommit = async ({ cwd, context, prerelease = false, }) => {
116
+ let command = "describe --abbrev=0 --match '*@*[[:digit:]]*.[[:digit:]]*.[[:digit:]]*'";
117
+ if (!prerelease) {
118
+ // The glob matches prerelease ranges. The 'complexity' comes from not wanting
119
+ // to be overeager in producing a false positive for a tag such as
120
+ // `@scope-with-hyphen/name.with.dot-and-hyphen`
121
+ command = `${command} --exclude '*@*[[:digit:]]*.[[:digit:]]*.[[:digit:]]*-*'`;
122
+ }
123
+ let tag = null;
124
+ try {
125
+ tag = (await git(command, { cwd, context })).stdout.trim();
126
+ }
127
+ catch (err) {
128
+ logging_1.default.warning('[Exec] Fetching most recent tag failed, falling back to HEAD', {
129
+ report: context?.report,
130
+ });
131
+ }
132
+ const sha = (await git(`rev-list -1 ${tag ?? 'HEAD'}`, {
133
+ cwd,
134
+ context,
135
+ })).stdout
136
+ .toString()
137
+ .trim();
138
+ return { sha, tag };
139
+ };
140
+ exports.gitLastTaggedCommit = gitLastTaggedCommit;
141
+ const gitGlob = async (globs, { cwd, context }) => {
142
+ const rawStdout = [
143
+ (await git('ls-files --modified', { cwd, context })).stdout,
144
+ (await git('ls-files -o --exclude-standard', { cwd, context })).stdout,
145
+ ].join('\n');
146
+ const files = rawStdout.split('\n').filter((v) => Boolean(v));
147
+ return (0, micromatch_1.default)(files, globs);
148
+ };
149
+ exports.gitGlob = gitGlob;
150
+ const gitAdd = async (paths, { cwd, context }) => {
151
+ (0, logging_1.assertProduction)();
152
+ await git(`add ${paths.join(' ')}`, { cwd, context });
153
+ };
154
+ exports.gitAdd = gitAdd;
155
+ const gitCommit = async (message, { cwd, context }) => {
156
+ (0, logging_1.assertProduction)();
157
+ await git(`commit -m "${message}" -n`, { cwd, context });
158
+ };
159
+ exports.gitCommit = gitCommit;
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './gitCommands';
2
+ export * from './getCommitMessages';
3
+ export * from './parseRepositoryProperty';
package/lib/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./gitCommands"), exports);
18
+ __exportStar(require("./getCommitMessages"), exports);
19
+ __exportStar(require("./parseRepositoryProperty"), exports);
@@ -0,0 +1,10 @@
1
+ import { type Workspace } from '@yarnpkg/core';
2
+ export interface RepositoryInfo {
3
+ host: string | null;
4
+ owner: string | null;
5
+ repository: string | null;
6
+ repoUrl: string | null;
7
+ }
8
+ export declare const parseRepositoryProperty: (workspace: Workspace, { fallbackToTopLevel }?: {
9
+ fallbackToTopLevel?: boolean | undefined;
10
+ }) => Promise<RepositoryInfo>;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseRepositoryProperty = void 0;
4
+ const core_1 = require("@yarnpkg/core");
5
+ const REPOSITORY_PATTERNS = [
6
+ [
7
+ /((?:git\+)?(https?:\/\/[^/]+)\/([^.]+)\/([^/.]+))(?:\.git)?/,
8
+ (m) => ({ repoUrl: m[1], host: m[2], owner: m[3], repository: m[4] }),
9
+ ],
10
+ [
11
+ /(?:git@)?([^:]+):([^.]+)\/([^/.]+)(?:\.git)?/,
12
+ (m) => ({
13
+ repoUrl: `https://${m[1]}/${m[2]}/${m[3]}`,
14
+ host: `https://${m[1]}`,
15
+ owner: m[2],
16
+ repository: m[3],
17
+ }),
18
+ ],
19
+ ];
20
+ const parseRepositoryProperty = async (workspace, { fallbackToTopLevel = true } = {}) => {
21
+ const rawManifest = workspace.manifest.raw;
22
+ const data = {
23
+ host: null,
24
+ owner: null,
25
+ repository: null,
26
+ repoUrl: null,
27
+ };
28
+ const repositoryUrl = (typeof rawManifest?.repository === 'string'
29
+ ? rawManifest?.repository
30
+ : rawManifest?.repository?.url) ?? '';
31
+ for (const [pattern, cb] of REPOSITORY_PATTERNS) {
32
+ const matches = repositoryUrl.match(pattern);
33
+ if (!matches)
34
+ continue;
35
+ try {
36
+ const result = cb(matches);
37
+ if (result) {
38
+ Object.assign(data, result);
39
+ break;
40
+ }
41
+ }
42
+ catch { }
43
+ }
44
+ if (fallbackToTopLevel &&
45
+ !core_1.structUtils.areDescriptorsEqual(workspace.anchoredDescriptor, workspace.project.topLevelWorkspace.anchoredDescriptor) &&
46
+ !data.repository) {
47
+ return await (0, exports.parseRepositoryProperty)(workspace.project.topLevelWorkspace);
48
+ }
49
+ return data;
50
+ };
51
+ exports.parseRepositoryProperty = parseRepositoryProperty;
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@monoweave/git",
3
+ "version": "0.0.2",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/monoweave/monoweave.git",
7
+ "directory": "packages/git"
8
+ },
9
+ "author": {
10
+ "name": "noahnu",
11
+ "url": "https://monoweave.github.io"
12
+ },
13
+ "license": "BSD-3",
14
+ "publishConfig": {
15
+ "main": "./lib/index.js",
16
+ "registry": "https://registry.npmjs.org/",
17
+ "types": "./lib/index.d.ts",
18
+ "access": "public"
19
+ },
20
+ "group": "monoweave",
21
+ "main": "./lib/index.js",
22
+ "files": [
23
+ "lib"
24
+ ],
25
+ "scripts": {
26
+ "clean": "run workspace:clean \"$(pwd)\"",
27
+ "types:watch": "run workspace:types:watch \"$(pwd)\"",
28
+ "test:watch": "run workspace:test:watch \"$(pwd)\"",
29
+ "build:watch": "run workspace:build:watch \"$(pwd)\"",
30
+ "prepack": "run workspace:build \"$(pwd)\""
31
+ },
32
+ "devDependencies": {
33
+ "@monoweave/io": "0.0.2",
34
+ "@monoweave/logging": "0.0.2",
35
+ "@monoweave/test-utils": "link:../../testUtils",
36
+ "@monoweave/types": "0.0.2",
37
+ "@types/jest": "^29.5.6",
38
+ "@types/micromatch": "^4.0.4",
39
+ "@types/node": "^18.18.6",
40
+ "@yarnpkg/core": "^4.0.3",
41
+ "@yarnpkg/fslib": "^3.0.2"
42
+ },
43
+ "dependencies": {
44
+ "micromatch": "^4.0.5"
45
+ },
46
+ "peerDependencies": {
47
+ "@monoweave/io": "^0.0.2",
48
+ "@monoweave/logging": "^0.0.2",
49
+ "@monoweave/types": "^0.0.2",
50
+ "@yarnpkg/core": "^4.0.3",
51
+ "@yarnpkg/fslib": "^3.0.2"
52
+ },
53
+ "types": "./lib/index.d.ts"
54
+ }