UNPKG

5.25 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const exec = require("./exec");
9const logger = require("./logger");
10const git_command = process.env.GH_GIT_COMMAND || 'git';
11const testing = process.env.NODE_ENV === 'testing';
12function checkout(branch, newBranch) {
13 var args = ['checkout', branch];
14 if (newBranch) {
15 args.push('-B', newBranch);
16 }
17 return !testing && exec.spawnSyncStream(git_command, args);
18}
19exports.checkout = checkout;
20function clone(url, folder) {
21 var args = ['clone', url];
22 if (folder) {
23 args.push(folder);
24 }
25 return !testing && exec.spawnSyncStream(git_command, args);
26}
27exports.clone = clone;
28function _merge(branch, type) {
29 try {
30 const args = [type, branch];
31 !testing && exec.spawnSyncStream(git_command, [...args]);
32 }
33 catch (err) {
34 if (err.code && err.code !== 0) {
35 !testing && exec.spawnSyncStream(git_command, [type, '--abort']);
36 throw err;
37 }
38 }
39}
40exports._merge = _merge;
41function merge(branch) {
42 return !testing && this._merge(branch, 'merge');
43}
44exports.merge = merge;
45function rebase(branch) {
46 return !testing && this._merge(branch, 'rebase');
47}
48exports.rebase = rebase;
49function push(remote, branch) {
50 var args = ['push', remote];
51 if (branch) {
52 args.push(branch);
53 }
54 return !testing && exec.spawnSyncStream(git_command, args);
55}
56exports.push = push;
57function fetch(repoUrl, headBranch, pullBranch) {
58 var args = ['fetch', repoUrl, `${headBranch}:${pullBranch}`, '--no-tags'];
59 return !testing && exec.spawnSyncStream(git_command, args);
60}
61exports.fetch = fetch;
62function countUserAdjacentCommits() {
63 let git;
64 let params;
65 let commits = 0;
66 const user = getConfig('user.name');
67 let author;
68 do {
69 params = ['log', '-1', `--skip=${commits}`, '--pretty=%an'];
70 git = exec.spawnSync(git_command, params);
71 if (git.status !== 0) {
72 logger.error(git.stderr);
73 }
74 author = git.stdout;
75 commits += 1;
76 } while (author === user);
77 commits -= 1;
78 return commits;
79}
80exports.countUserAdjacentCommits = countUserAdjacentCommits;
81function deleteBranch(branch) {
82 if (testing) {
83 return;
84 }
85 var git = exec.spawnSync(git_command, ['branch', '-d', branch]);
86 if (git.status !== 0) {
87 logger.debug(git.stderr);
88 }
89 return git.stdout;
90}
91exports.deleteBranch = deleteBranch;
92function findRoot() {
93 return exec.spawnSync(git_command, ['rev-parse', '--show-toplevel']).stdout;
94}
95exports.findRoot = findRoot;
96function getCommitMessage(branch, number) {
97 let git;
98 const params = ['log'];
99 if (!number) {
100 number = 1;
101 }
102 params.push(`-${number}`, '--first-parent', '--no-merges', '--pretty=%s');
103 if (branch) {
104 params.push(branch);
105 }
106 params.push('--');
107 git = exec.spawnSync(git_command, params);
108 if (git.status !== 0) {
109 logger.debug("Can't get commit message.");
110 return;
111 }
112 return git.stdout;
113}
114exports.getCommitMessage = getCommitMessage;
115function getConfig(key) {
116 var git = exec.spawnSync(git_command, ['config', '--get', key]);
117 if (git.status !== 0) {
118 throw new Error(`No git config found for ${key}\n`);
119 }
120 return git.stdout;
121}
122exports.getConfig = getConfig;
123function getCurrentBranch() {
124 if (testing) {
125 return 'master';
126 }
127 var git = exec.spawnSync(git_command, ['symbolic-ref', '--short', 'HEAD']);
128 if (git.status !== 0) {
129 logger.debug("Can't get current branch.");
130 return;
131 }
132 return git.stdout;
133}
134exports.getCurrentBranch = getCurrentBranch;
135function getLastCommitMessage(branch) {
136 return getCommitMessage(branch, 1);
137}
138exports.getLastCommitMessage = getLastCommitMessage;
139function getLastCommitSHA() {
140 var git = exec.spawnSync(git_command, ['rev-parse', '--short', 'HEAD']);
141 if (git.status !== 0) {
142 throw new Error("Can't retrieve last commit.");
143 }
144 return git.stdout;
145}
146exports.getLastCommitSHA = getLastCommitSHA;
147function getRemoteUrl(remote) {
148 try {
149 return getConfig(`remote.${remote}.url`);
150 }
151 catch (e) {
152 logger.debug("Can't get remote URL.");
153 return;
154 }
155}
156exports.getRemoteUrl = getRemoteUrl;
157function getRepoFromRemoteURL(url) {
158 var parsed = parseRemoteUrl(url);
159 return parsed && parsed[1];
160}
161exports.getRepoFromRemoteURL = getRepoFromRemoteURL;
162function getUserFromRemoteUrl(url) {
163 var parsed = parseRemoteUrl(url);
164 return parsed && parsed[0];
165}
166exports.getUserFromRemoteUrl = getUserFromRemoteUrl;
167function getRepo(remote) {
168 return getRepoFromRemoteURL(getRemoteUrl(remote));
169}
170exports.getRepo = getRepo;
171function getUser(remote) {
172 return getUserFromRemoteUrl(getRemoteUrl(remote));
173}
174exports.getUser = getUser;
175function parseRemoteUrl(url) {
176 var parsed = /[\/:]([\w-]+)\/(.*?)(?:\.git)?$/.exec(url);
177 if (parsed) {
178 parsed.shift();
179 }
180 return parsed;
181}
182exports.parseRemoteUrl = parseRemoteUrl;
183//# sourceMappingURL=git.js.map
\No newline at end of file