git-tm 0.3.0 → 0.4.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/src/git.js CHANGED
@@ -1,213 +1,213 @@
1
- import simpleGit from 'simple-git';
2
-
3
- async function getRepoData() {
4
- const git = simpleGit();
5
- let isOffline = false;
6
-
7
- const localData = await Promise.all([
8
- git.branch(),
9
- git.log(["--all"]),
10
- git.tags(),
11
- git.raw(["remote"]),
12
- ]).catch((err) => {
13
- console.error("Failed to get local repo data:", err);
14
- return [null, null, null, ""];
15
- });
16
-
17
- try {
18
- await git.fetch(["--all"]);
19
- isOffline = false;
20
- } catch (error) {
21
- console.warn("Working in offline mode - using local data only");
22
- isOffline = true;
23
- }
24
-
25
- const [localBranches, log, tags, remotes] = localData;
26
-
27
- if (!localBranches || !log) {
28
- throw new Error("Failed to read local repository data");
29
- }
30
-
31
- const localBranchList = localBranches.all.filter(
32
- (branch) => !branch.includes("remotes/")
33
- );
34
-
35
- let remoteBranchList = [];
36
- if (!isOffline && remotes) {
37
- try {
38
- const remoteBranches = await git.raw([
39
- "for-each-ref",
40
- "refs/remotes",
41
- "--format=%(refname:short)",
42
- ]);
43
-
44
- remoteBranchList = remoteBranches
45
- .split("\n")
46
- .filter(
47
- (branch) =>
48
- branch &&
49
- !branch.endsWith("/HEAD") &&
50
- branch.split("/").length > 1 &&
51
- !localBranchList.some((localBranch) =>
52
- branch.endsWith("/" + localBranch)
53
- )
54
- )
55
- .map((branch) => ({
56
- name: branch.trim(),
57
- fullName: "remotes/" + branch.trim(),
58
- remote: branch.split("/")[0],
59
- shortName: branch.split("/").slice(1).join("/"),
60
- }));
61
- } catch (error) {
62
- console.error("Failed to get remote branches:", error);
63
- }
64
- }
65
-
66
- const commits = log.all.map((commit) => ({
67
- ...commit,
68
- fullRefs: commit.refs
69
- .split(",")
70
- .map((ref) => ref.trim())
71
- .filter(Boolean),
72
- refs: commit.refs
73
- .split(",")
74
- .map((ref) => ref.trim())
75
- .filter((ref) => ref && !ref.includes("tag:")),
76
- }));
77
-
78
- return {
79
- commits,
80
- branches: localBranchList,
81
- remoteBranches: remoteBranchList,
82
- current: localBranches.current,
83
- tags: tags?.all || [],
84
- isOffline,
85
- };
86
- }
87
-
88
- async function getBranchCommits(branchName) {
89
- const git = simpleGit();
90
- try {
91
-
92
- const { current: currentBranch } = await git.branch();
93
- let baseBranch = currentBranch;
94
-
95
- if (currentBranch !== 'main' && currentBranch !== 'master') {
96
- try {
97
- await git.show(["main"]);
98
- baseBranch = "main";
99
- } catch {
100
- try {
101
- await git.show(["master"]);
102
- baseBranch = "master";
103
- } catch {
104
- baseBranch = currentBranch;
105
- }
106
- }
107
- }
108
-
109
- let targetBranch = branchName;
110
- if (branchName.startsWith("remotes/")) {
111
- if (branchName === "remotes/origin" || !branchName.includes("/")) {
112
- return [];
113
- }
114
- targetBranch = branchName.replace("remotes/", "refs/remotes/");
115
- }
116
-
117
- if (targetBranch === baseBranch) {
118
- const log = await git.log(["--first-parent", targetBranch]);
119
- return log.all;
120
- }
121
-
122
- const revList = await git.raw([
123
- "rev-list",
124
- "--first-parent",
125
- targetBranch,
126
- "^" + baseBranch,
127
- "--not",
128
- "--all",
129
- ]);
130
-
131
- if (!revList.trim()) {
132
- const log = await git.log(["--first-parent", targetBranch]);
133
- return log.all;
134
- }
135
-
136
- const commits = [];
137
- const commitHashes = revList.split("\n").filter((hash) => hash.trim());
138
-
139
- for (const hash of commitHashes) {
140
- if (!hash) continue;
141
-
142
- const commitInfo = await git.show([
143
- "--format=%H%n%an%n%ae%n%at%n%s%n%D",
144
- "--no-patch",
145
- hash,
146
- ]);
147
-
148
- const [
149
- commitHash,
150
- authorName,
151
- authorEmail,
152
- timestamp,
153
- subject,
154
- refs = "",
155
- ] = commitInfo.split("\n");
156
-
157
- commits.push({
158
- hash: commitHash,
159
- author_name: authorName,
160
- author_email: authorEmail,
161
- date: new Date(parseInt(timestamp) * 1000).toISOString(),
162
- message: subject,
163
- refs: refs,
164
- });
165
- }
166
-
167
- return commits;
168
- } catch (error) {
169
- console.error("Failed to get branch commits:", error);
170
- throw error;
171
- }
172
- }
173
-
174
- async function compareCommits(commit1, commit2) {
175
- const git = simpleGit();
176
- try {
177
- const diff = await git.raw([
178
- "diff",
179
- "--binary",
180
- "-p",
181
- "--no-color",
182
- commit1,
183
- commit2,
184
- ]);
185
- return diff;
186
- } catch (error) {
187
- console.error("Failed to compare commits:", error);
188
- throw new Error(`Failed to compare commits: ${error.message}`);
189
- }
190
- }
191
-
192
- async function checkoutRemoteBranch(branchName) {
193
- const git = simpleGit();
194
- try {
195
- await git.checkout([
196
- "-b",
197
- branchName.split("/").slice(2).join("/"),
198
- "--track",
199
- branchName,
200
- ]);
201
- return true;
202
- } catch (error) {
203
- console.error("Failed to checkout branch:", error.message);
204
- return false;
205
- }
206
- }
207
-
208
- export {
209
- getRepoData,
210
- getBranchCommits,
211
- compareCommits,
212
- checkoutRemoteBranch,
1
+ import simpleGit from 'simple-git';
2
+
3
+ async function getRepoData() {
4
+ const git = simpleGit();
5
+ let isOffline = false;
6
+
7
+ const localData = await Promise.all([
8
+ git.branch(),
9
+ git.log(["--all"]),
10
+ git.tags(),
11
+ git.raw(["remote"]),
12
+ ]).catch((err) => {
13
+ console.error("Failed to get local repo data:", err);
14
+ return [null, null, null, ""];
15
+ });
16
+
17
+ try {
18
+ await git.fetch(["--all"]);
19
+ isOffline = false;
20
+ } catch (error) {
21
+ console.warn("Working in offline mode - using local data only");
22
+ isOffline = true;
23
+ }
24
+
25
+ const [localBranches, log, tags, remotes] = localData;
26
+
27
+ if (!localBranches || !log) {
28
+ throw new Error("Failed to read local repository data");
29
+ }
30
+
31
+ const localBranchList = localBranches.all.filter(
32
+ (branch) => !branch.includes("remotes/")
33
+ );
34
+
35
+ let remoteBranchList = [];
36
+ if (!isOffline && remotes) {
37
+ try {
38
+ const remoteBranches = await git.raw([
39
+ "for-each-ref",
40
+ "refs/remotes",
41
+ "--format=%(refname:short)",
42
+ ]);
43
+
44
+ remoteBranchList = remoteBranches
45
+ .split("\n")
46
+ .filter(
47
+ (branch) =>
48
+ branch &&
49
+ !branch.endsWith("/HEAD") &&
50
+ branch.split("/").length > 1 &&
51
+ !localBranchList.some((localBranch) =>
52
+ branch.endsWith("/" + localBranch)
53
+ )
54
+ )
55
+ .map((branch) => ({
56
+ name: branch.trim(),
57
+ fullName: "remotes/" + branch.trim(),
58
+ remote: branch.split("/")[0],
59
+ shortName: branch.split("/").slice(1).join("/"),
60
+ }));
61
+ } catch (error) {
62
+ console.error("Failed to get remote branches:", error);
63
+ }
64
+ }
65
+
66
+ const commits = log.all.map((commit) => ({
67
+ ...commit,
68
+ fullRefs: commit.refs
69
+ .split(",")
70
+ .map((ref) => ref.trim())
71
+ .filter(Boolean),
72
+ refs: commit.refs
73
+ .split(",")
74
+ .map((ref) => ref.trim())
75
+ .filter((ref) => ref && !ref.includes("tag:")),
76
+ }));
77
+
78
+ return {
79
+ commits,
80
+ branches: localBranchList,
81
+ remoteBranches: remoteBranchList,
82
+ current: localBranches.current,
83
+ tags: tags?.all || [],
84
+ isOffline,
85
+ };
86
+ }
87
+
88
+ async function getBranchCommits(branchName) {
89
+ const git = simpleGit();
90
+ try {
91
+
92
+ const { current: currentBranch } = await git.branch();
93
+ let baseBranch = currentBranch;
94
+
95
+ if (currentBranch !== 'main' && currentBranch !== 'master') {
96
+ try {
97
+ await git.show(["main"]);
98
+ baseBranch = "main";
99
+ } catch {
100
+ try {
101
+ await git.show(["master"]);
102
+ baseBranch = "master";
103
+ } catch {
104
+ baseBranch = currentBranch;
105
+ }
106
+ }
107
+ }
108
+
109
+ let targetBranch = branchName;
110
+ if (branchName.startsWith("remotes/")) {
111
+ if (branchName === "remotes/origin" || !branchName.includes("/")) {
112
+ return [];
113
+ }
114
+ targetBranch = branchName.replace("remotes/", "refs/remotes/");
115
+ }
116
+
117
+ if (targetBranch === baseBranch) {
118
+ const log = await git.log(["--first-parent", targetBranch]);
119
+ return log.all;
120
+ }
121
+
122
+ const revList = await git.raw([
123
+ "rev-list",
124
+ "--first-parent",
125
+ targetBranch,
126
+ "^" + baseBranch,
127
+ "--not",
128
+ "--all",
129
+ ]);
130
+
131
+ if (!revList.trim()) {
132
+ const log = await git.log(["--first-parent", targetBranch]);
133
+ return log.all;
134
+ }
135
+
136
+ const commits = [];
137
+ const commitHashes = revList.split("\n").filter((hash) => hash.trim());
138
+
139
+ for (const hash of commitHashes) {
140
+ if (!hash) continue;
141
+
142
+ const commitInfo = await git.show([
143
+ "--format=%H%n%an%n%ae%n%at%n%s%n%D",
144
+ "--no-patch",
145
+ hash,
146
+ ]);
147
+
148
+ const [
149
+ commitHash,
150
+ authorName,
151
+ authorEmail,
152
+ timestamp,
153
+ subject,
154
+ refs = "",
155
+ ] = commitInfo.split("\n");
156
+
157
+ commits.push({
158
+ hash: commitHash,
159
+ author_name: authorName,
160
+ author_email: authorEmail,
161
+ date: new Date(parseInt(timestamp) * 1000).toISOString(),
162
+ message: subject,
163
+ refs: refs,
164
+ });
165
+ }
166
+
167
+ return commits;
168
+ } catch (error) {
169
+ console.error("Failed to get branch commits:", error);
170
+ throw error;
171
+ }
172
+ }
173
+
174
+ async function compareCommits(commit1, commit2) {
175
+ const git = simpleGit();
176
+ try {
177
+ const diff = await git.raw([
178
+ "diff",
179
+ "--binary",
180
+ "-p",
181
+ "--no-color",
182
+ commit1,
183
+ commit2,
184
+ ]);
185
+ return diff;
186
+ } catch (error) {
187
+ console.error("Failed to compare commits:", error);
188
+ throw new Error(`Failed to compare commits: ${error.message}`);
189
+ }
190
+ }
191
+
192
+ async function checkoutRemoteBranch(branchName) {
193
+ const git = simpleGit();
194
+ try {
195
+ await git.checkout([
196
+ "-b",
197
+ branchName.split("/").slice(2).join("/"),
198
+ "--track",
199
+ branchName,
200
+ ]);
201
+ return true;
202
+ } catch (error) {
203
+ console.error("Failed to checkout branch:", error.message);
204
+ return false;
205
+ }
206
+ }
207
+
208
+ export {
209
+ getRepoData,
210
+ getBranchCommits,
211
+ compareCommits,
212
+ checkoutRemoteBranch,
213
213
  };