|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Puter Technologies Inc. |
| 3 | + * |
| 4 | + * This file is part of Puter's Git client. |
| 5 | + * |
| 6 | + * Puter's Git client is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +import git from 'isomorphic-git'; |
| 20 | +import http from 'isomorphic-git/http/web'; |
| 21 | +import { determine_fetch_remote, find_repo_root, PROXY_URL } from '../git-helpers.js'; |
| 22 | +import { SHOW_USAGE } from '../help.js'; |
| 23 | + |
| 24 | +export default { |
| 25 | + name: 'fetch', |
| 26 | + usage: [ |
| 27 | + 'git fetch <repository>', |
| 28 | + 'git fetch --all', |
| 29 | + ], |
| 30 | + description: `Download objects and refs from another repository.`, |
| 31 | + args: { |
| 32 | + allowPositionals: true, |
| 33 | + options: { |
| 34 | + all: { |
| 35 | + description: 'Fetch all remotes.', |
| 36 | + type: 'boolean', |
| 37 | + default: false, |
| 38 | + } |
| 39 | + }, |
| 40 | + }, |
| 41 | + execute: async (ctx) => { |
| 42 | + const { io, fs, env, args } = ctx; |
| 43 | + const { stdout, stderr } = io; |
| 44 | + const { options, positionals } = args; |
| 45 | + const cache = {}; |
| 46 | + |
| 47 | + const { repository_dir, git_dir } = await find_repo_root(fs, env.PWD); |
| 48 | + |
| 49 | + // TODO: Support <refspec> syntax. |
| 50 | + |
| 51 | + const remotes = await git.listRemotes({ |
| 52 | + fs, |
| 53 | + dir: repository_dir, |
| 54 | + gitdir: git_dir, |
| 55 | + }); |
| 56 | + |
| 57 | + if (options.all) { |
| 58 | + for (const { remote, url } of remotes) { |
| 59 | + stdout(`Fetching ${remote}\nFrom ${url}`); |
| 60 | + await git.fetch({ |
| 61 | + fs, |
| 62 | + http, |
| 63 | + cache, |
| 64 | + corsProxy: PROXY_URL, |
| 65 | + dir: repository_dir, |
| 66 | + gitdir: git_dir, |
| 67 | + remote, |
| 68 | + onMessage: (message) => { stdout(message); }, |
| 69 | + }); |
| 70 | + } |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const remote = positionals.shift(); |
| 75 | + const remote_data = determine_fetch_remote(remote, remotes); |
| 76 | + |
| 77 | + await git.fetch({ |
| 78 | + fs, |
| 79 | + http, |
| 80 | + cache, |
| 81 | + corsProxy: PROXY_URL, |
| 82 | + dir: repository_dir, |
| 83 | + gitdir: git_dir, |
| 84 | + ...remote_data, |
| 85 | + onMessage: (message) => { stdout(message); }, |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments