1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | const exec = require("./exec");
|
9 | const logger = require("./logger");
|
10 | const git_command = process.env.GH_GIT_COMMAND || 'git';
|
11 | const testing = process.env.NODE_ENV === 'testing';
|
12 | function 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 | }
|
19 | exports.checkout = checkout;
|
20 | function 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 | }
|
27 | exports.clone = clone;
|
28 | function _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 | }
|
40 | exports._merge = _merge;
|
41 | function merge(branch) {
|
42 | return !testing && this._merge(branch, 'merge');
|
43 | }
|
44 | exports.merge = merge;
|
45 | function rebase(branch) {
|
46 | return !testing && this._merge(branch, 'rebase');
|
47 | }
|
48 | exports.rebase = rebase;
|
49 | function 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 | }
|
56 | exports.push = push;
|
57 | function fetch(repoUrl, headBranch, pullBranch) {
|
58 | var args = ['fetch', repoUrl, `${headBranch}:${pullBranch}`, '--no-tags'];
|
59 | return !testing && exec.spawnSyncStream(git_command, args);
|
60 | }
|
61 | exports.fetch = fetch;
|
62 | function 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 | }
|
80 | exports.countUserAdjacentCommits = countUserAdjacentCommits;
|
81 | function 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 | }
|
91 | exports.deleteBranch = deleteBranch;
|
92 | function findRoot() {
|
93 | return exec.spawnSync(git_command, ['rev-parse', '--show-toplevel']).stdout;
|
94 | }
|
95 | exports.findRoot = findRoot;
|
96 | function 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 | }
|
114 | exports.getCommitMessage = getCommitMessage;
|
115 | function 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 | }
|
122 | exports.getConfig = getConfig;
|
123 | function 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 | }
|
134 | exports.getCurrentBranch = getCurrentBranch;
|
135 | function getLastCommitMessage(branch) {
|
136 | return getCommitMessage(branch, 1);
|
137 | }
|
138 | exports.getLastCommitMessage = getLastCommitMessage;
|
139 | function 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 | }
|
146 | exports.getLastCommitSHA = getLastCommitSHA;
|
147 | function 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 | }
|
156 | exports.getRemoteUrl = getRemoteUrl;
|
157 | function getRepoFromRemoteURL(url) {
|
158 | var parsed = parseRemoteUrl(url);
|
159 | return parsed && parsed[1];
|
160 | }
|
161 | exports.getRepoFromRemoteURL = getRepoFromRemoteURL;
|
162 | function getUserFromRemoteUrl(url) {
|
163 | var parsed = parseRemoteUrl(url);
|
164 | return parsed && parsed[0];
|
165 | }
|
166 | exports.getUserFromRemoteUrl = getUserFromRemoteUrl;
|
167 | function getRepo(remote) {
|
168 | return getRepoFromRemoteURL(getRemoteUrl(remote));
|
169 | }
|
170 | exports.getRepo = getRepo;
|
171 | function getUser(remote) {
|
172 | return getUserFromRemoteUrl(getRemoteUrl(remote));
|
173 | }
|
174 | exports.getUser = getUser;
|
175 | function parseRemoteUrl(url) {
|
176 | var parsed = /[\/:]([\w-]+)\/(.*?)(?:\.git)?$/.exec(url);
|
177 | if (parsed) {
|
178 | parsed.shift();
|
179 | }
|
180 | return parsed;
|
181 | }
|
182 | exports.parseRemoteUrl = parseRemoteUrl;
|
183 |
|
\ | No newline at end of file |