Skip to content

Commit bcc4ba5

Browse files
authored
Merge pull request #8 from TheTechTrap/v0.1.0-final-fixes
Include numCommits in update-ref tx
2 parents e04a5f7 + d33b53d commit bcc4ba5

File tree

7 files changed

+156
-161
lines changed

7 files changed

+156
-161
lines changed

.github/workflows/dgit-mirror.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Dgit Mirror
1+
name: gitopia mirror
22

33
on:
44
push:
@@ -14,10 +14,10 @@ jobs:
1414
with:
1515
node-version: 12
1616
registry-url: https://registry.npmjs.org/
17-
- name: Dgit mirror action
18-
uses: thetechtrap/dgit-action@master
17+
- name: gitopia mirror action
18+
uses: thetechtrap/gitopia-mirror-action@master
1919
# The action should not publish any real changes, but should succeed.
2020
with:
21-
dgitWallet: "${{ secrets.DGIT_WALLET }}"
21+
gitopiaWallet: "${{ secrets.GITOPIA_WALLET }}"
2222
branch: "master"
23-
remoteUrl: "dgit://7yFP2o906A3nDl5eCMtN4Ycs9m5otfRrJDAGoGKDoHk/git-remote-dgit"
23+
remoteUrl: "gitopia://7yFP2o906A3nDl5eCMtN4Ycs9m5otfRrJDAGoGKDoHk/git-remote-gitopia"

bin/helper.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import DGitHelper from "./lib/dgit.js";
1111
import LineHelper from "./lib/line.js";
1212
import Arweave from "arweave";
1313
import {
14-
pushGitObject,
1514
makeDataItem,
1615
makeUpdateRefDataItem,
1716
parseArgitRemoteURI,
@@ -21,10 +20,11 @@ import { getAllRefs } from "./lib/graphql.js";
2120

2221
import * as deepHash from "arweave/node/lib/deepHash.js";
2322
import ArweaveBundles from "arweave-bundles";
24-
2523
import pkg from "cli-progress";
2624
const { SingleBar, Presets } = pkg;
2725

26+
export const VERSION = "0.1.4"
27+
2828
const _timeout = async (duration) => {
2929
return new Promise((resolve, reject) => {
3030
setTimeout(() => {
@@ -61,11 +61,11 @@ export default class Helper {
6161
// address and path shortcuts
6262
this.address = this.url.split("://")[1];
6363
this.path = path.resolve(process.env.GIT_DIR || "");
64-
this.arweaveWalletPath = process.env.ARWEAVE_WALLET_PATH;
64+
this.gitopiaWalletPath = process.env.GITOPIA_WALLET_PATH;
6565
// config
6666
this.config = this._config();
6767
// lib
68-
this.debug = debug("dgit");
68+
this.debug = debug("gitopia");
6969
this.line = new LineHelper();
7070
this.git = new GitHelper(this);
7171
this.dgit = new DGitHelper(this);
@@ -134,7 +134,6 @@ export default class Helper {
134134
forPush ? this.debug("cmd", "list", "for-push") : this.debug("cmd, list");
135135

136136
const refs = await this._fetchRefs();
137-
console.error("refs", refs);
138137
// tslint:disable-next-line:forin
139138
for (const ref in refs) {
140139
this._send(refs[ref] + " " + ref);
@@ -164,18 +163,18 @@ export default class Helper {
164163
async _handlePush(line) {
165164
this.debug("cmd", line);
166165

167-
if (!this.arweaveWalletPath) {
168-
if (process.env.DGIT_WALLET) {
169-
this.wallet = JSON.parse(process.env.DGIT_WALLET);
166+
if (!this.gitopiaWalletPath) {
167+
if (process.env.GITOPIA_WALLET) {
168+
this.wallet = JSON.parse(process.env.GITOPIA_WALLET);
170169
} else {
171170
console.error(
172-
"Missing ARWEAVE_WALLET_PATH or DGIT_WALLET env variable"
171+
"Missing GITOPIA_WALLET_PATH or GITOPIA_WALLET env variable"
173172
);
174173

175174
this._die();
176175
}
177176
} else {
178-
const rawdata = fs.readFileSync(this.arweaveWalletPath);
177+
const rawdata = fs.readFileSync(this.gitopiaWalletPath);
179178
this.wallet = JSON.parse(rawdata);
180179
}
181180

@@ -200,21 +199,21 @@ export default class Helper {
200199

201200
// OK
202201
async _fetchRefs() {
203-
this.debug("fetching remote refs from arweave");
202+
this.debug("fetching remote refs from Gitopia");
204203

205204
let start;
206205
let block;
207206
let events;
208207
const ops = [];
209-
const spinner = ora("Fetching remote refs from arweave").start();
208+
const spinner = ora("Fetching remote refs from Gitopia").start();
210209

211210
try {
212211
const refs = await getAllRefs(this._arweave, this.url);
213212

214-
spinner.succeed("Remote refs fetched from arweave");
213+
spinner.succeed("Remote refs fetched from Gitopia");
215214
return refs;
216215
} catch (err) {
217-
spinner.fail("Failed to fetch remote refs from arweave");
216+
spinner.fail("Failed to fetch remote refs from Gitopia");
218217
throw err;
219218
}
220219
}
@@ -348,7 +347,7 @@ export default class Helper {
348347

349348
// upload git objects
350349
try {
351-
spinner = ora("Uploading git objects to arweave").start();
350+
spinner = ora("Uploading git objects to Gitopia").start();
352351
const dataItems = await Promise.all(puts);
353352
bar1.stop();
354353
await postBundledTransaction(
@@ -358,21 +357,21 @@ export default class Helper {
358357
this.url,
359358
dataItems
360359
);
361-
spinner.succeed("Git objects uploaded to arweave");
360+
spinner.succeed("Git objects uploaded to Gitopia");
362361
} catch (err) {
363362
spinner.fail(
364-
"Failed to upload git objects to arweave: " + err.message
363+
"Failed to upload git objects to Gitopia: " + err.message
365364
);
366365
this._die();
367366
}
368367

369368
// register on chain
370369
try {
371-
spinner = ora(`Updating ref ${dst} ${objects[0]} on arweave`).start();
370+
spinner = ora(`Updating ref ${dst} ${objects[0]} on Gitopia`).start();
372371
spinner.succeed(`Updated ref ${dst} ${objects[0]} successfully`);
373372
} catch (err) {
374373
spinner.fail(
375-
`Failed to update ref ${dst} ${objects[0]} on arweave: ` +
374+
`Failed to update ref ${dst} ${objects[0]} on Gitopia: ` +
376375
err.message
377376
);
378377
this._die();

bin/index.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
2-
3-
import Helper from "./helper.js";
2+
import axios from "axios";
3+
import Helper, { VERSION } from "./helper.js";
44

55
const main = async () => {
66
const args = process.argv.slice(2);
@@ -11,6 +11,15 @@ const main = async () => {
1111
process.exit(1);
1212
}
1313

14+
// Show warning when newer version is available
15+
try {
16+
const npmRegistryApi = "https://registry.npmjs.org/-/package/@thetechtrap/git-remote-dgit/dist-tags"
17+
const { data } = await axios.get(npmRegistryApi)
18+
if (VERSION !== data.latest) {
19+
console.error(`Warning: New version ${data.latest} of git-remote-gitopia is available. Please upgrade.`)
20+
}
21+
} catch (error) {}
22+
1423
const name = args[0] === args[1] ? "_" : args[0];
1524
const url = args[1];
1625
const helper = new Helper(name, url);

bin/lib/arweave.js

Lines changed: 35 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
import * as smartweave from "smartweave";
2+
import shell from "shelljs"
3+
import { VERSION } from "../helper.js"
24

35
// prettier-ignore
4-
const argitRemoteURIRegex = '^dgit:\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
6+
const argitRemoteURIRegex = '^gitopia:\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
57
const contractId = "UvjBvJUy8pOMR_lf85tBDaJD0jF85G5Ayj1p2h7yols";
68

7-
const repoQuery = (remoteURI) => {
8-
const { repoOwnerAddress, repoName } = parseArgitRemoteURI(remoteURI);
9-
return {
10-
op: "and",
11-
expr1: {
12-
op: "and",
13-
expr1: {
14-
op: "equals",
15-
expr1: "App-Name",
16-
expr2: "dgit",
17-
},
18-
expr2: {
19-
op: "equals",
20-
expr1: "from",
21-
expr2: repoOwnerAddress,
22-
},
23-
},
24-
expr2: { op: "equals", expr1: "Repo", expr2: repoName },
25-
};
26-
};
27-
289
export function parseArgitRemoteURI(remoteURI) {
2910
const matchGroups = remoteURI.match(argitRemoteURIRegex);
3011
const repoOwnerAddress = matchGroups[1];
@@ -33,57 +14,36 @@ export function parseArgitRemoteURI(remoteURI) {
3314
return { repoOwnerAddress, repoName };
3415
}
3516

36-
function addTransactionTags(tx, repo, txType) {
37-
tx.addTag("Repo", repo);
38-
tx.addTag("Type", txType);
39-
tx.addTag("App-Name", "dgit");
40-
tx.addTag("version", "0.0.1");
41-
tx.addTag("Unix-Time", Math.round(new Date().getTime() / 1000)); // Add Unix timestamp
42-
return tx;
43-
}
44-
4517
export async function makeUpdateRefDataItem(
4618
arData,
4719
wallet,
4820
remoteURI,
49-
name,
50-
ref
21+
ref,
22+
oid
5123
) {
5224
const { repoName } = parseArgitRemoteURI(remoteURI);
5325
const tags = [
54-
{ name: "App-Name", value: "dgit" },
55-
{ name: "version", value: "0.0.1" },
5626
{ name: "Repo", value: repoName },
27+
{ name: "Version", value: "0.0.2" },
28+
{ name: "Ref", value: ref },
5729
{ name: "Type", value: "update-ref" },
30+
{ name: "App-Name", value: "gitopia" },
5831
{
5932
name: "Unix-Time",
6033
value: Math.round(new Date().getTime() / 1000).toString(),
6134
},
62-
{ name: "ref", value: name },
6335
{ name: "Content-Type", value: "text/plain" },
6436
];
6537

66-
const item = await arData.createData({ data: ref, tags }, wallet);
67-
return await arData.sign(item, wallet);
68-
}
69-
70-
export async function pushGitObject(arweave, wallet, remoteURI, oid, object) {
71-
const { repoName } = parseArgitRemoteURI(remoteURI);
72-
73-
let tx = await arweave.createTransaction({ data: object }, wallet);
74-
tx = addTransactionTags(tx, repoName, "push-git-object");
75-
tx.addTag("oid", oid);
76-
tx.addTag("Content-Type", "application/octet-stream");
77-
78-
await arweave.transactions.sign(tx, wallet);
79-
let uploader = await arweave.transactions.getUploader(tx);
80-
81-
while (!uploader.isComplete) {
82-
await uploader.uploadChunk();
83-
// console.error(
84-
// `${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`
85-
// );
38+
const numCommits = shell.exec(`git rev-list --count ${ref}`, { silent: true }).stdout.trim();
39+
const obj = {
40+
oid,
41+
numCommits
8642
}
43+
const data = JSON.stringify(obj);
44+
45+
const item = await arData.createData({ data, tags }, wallet);
46+
return await arData.sign(item, wallet);
8747
}
8848

8949
export const makeDataItem = async (
@@ -95,15 +55,15 @@ export const makeDataItem = async (
9555
) => {
9656
const { repoName } = parseArgitRemoteURI(remoteURI);
9757
const tags = [
98-
{ name: "App-Name", value: "dgit" },
99-
{ name: "version", value: "0.0.1" },
58+
{ name: "Oid", value: oid },
59+
{ name: "Version", value: "0.0.2" },
10060
{ name: "Repo", value: repoName },
101-
{ name: "Type", value: "push-git-object" },
61+
{ name: "Type", value: "git-object" },
62+
{ name: "App-Name", value: "gitopia" },
10263
{
10364
name: "Unix-Time",
10465
value: Math.round(new Date().getTime() / 1000).toString(),
10566
},
106-
{ name: "oid", value: oid },
10767
{ name: "Content-Type", value: "application/octet-stream" },
10868
];
10969

@@ -122,12 +82,21 @@ export const postBundledTransaction = async (
12282
const bundle = await arData.bundleData(dataItems);
12383
const data = JSON.stringify(bundle);
12484
const tx = await arweave.createTransaction({ data }, wallet);
85+
tx.addTag("Repo", repoName);
86+
tx.addTag("Version", "0.0.2")
87+
tx.addTag("Type", "git-objects-bundle");
88+
tx.addTag("App-Name", "gitopia");
12589
tx.addTag("Bundle-Format", "json");
12690
tx.addTag("Bundle-Version", "1.0.0");
12791
tx.addTag("Content-Type", "application/json");
128-
tx.addTag("App-Name", "dgit");
129-
tx.addTag("Type", "git-objects-bundle");
130-
tx.addTag("Repo", repoName);
92+
tx.addTag("Helper", VERSION);
93+
94+
// Push triggered from gitopia mirror action
95+
if (process.env.GITHUB_SHA) {
96+
tx.addTag("Origin", "gitopia-mirror-action")
97+
} else {
98+
tx.addTag("Origin", "git-remote-gitopia")
99+
}
131100

132101
await arweave.transactions.sign(tx, wallet);
133102
const uploader = await arweave.transactions.getUploader(tx);
@@ -147,33 +116,11 @@ export const postBundledTransaction = async (
147116
{ target: holder, quantity: arweave.ar.arToWinston("0.01") },
148117
wallet
149118
);
150-
pstTx.addTag("App-Name", "dgit");
151-
pstTx.addTag("version", "0.0.1");
119+
pstTx.addTag("Bundle-TxID", tx.id)
152120
pstTx.addTag("Repo", repoName);
121+
pstTx.addTag("Version", "0.0.2");
122+
pstTx.addTag("App-Name", "gitopia");
153123

154124
await arweave.transactions.sign(pstTx, wallet);
155125
await arweave.transactions.post(pstTx);
156126
};
157-
158-
export async function fetchGitObjects(arweave, arData, remoteURI) {
159-
const query = {
160-
op: "and",
161-
expr1: repoQuery(remoteURI),
162-
expr2: { op: "equals", expr1: "Type", expr2: "push-git-object" },
163-
};
164-
const txids = await arweave.arql(query);
165-
const objects = await Promise.all(
166-
txids.map(async (txid) => {
167-
const tx = await arweave.transactions.get(txid);
168-
let oid = "";
169-
tx.get("tags").forEach((tag) => {
170-
const key = tag.get("name", { decode: true, string: true });
171-
const value = tag.get("value", { decode: true, string: true });
172-
if (key === "oid") oid = value;
173-
});
174-
const data = await arweave.transactions.getData(txid, { decode: true });
175-
return { data, oid };
176-
})
177-
);
178-
return objects;
179-
}

0 commit comments

Comments
 (0)