SPB Git

spb/spbgit Public MIT

SPB Git — the platform hosting itself

JavaScript 73.9% CSS 11.7% Nunjucks 11.6% Shell 2.7%
2.3 KB · 52 lines javascript
Raw Blame History
1/**2 * ─────────────────────────────────────────────3 *  SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 *  Author  : Simon-Pierre Boucher6 *  Contact : contact@spboucher.ai7 *  File    : cli/commands/push.mjs8 *  Purpose : `spbgit push [name]|--all` — push current branches9 *  License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { basename } from 'node:path';14import pc from 'picocolors';15import { requireCliConfig } from '../lib/config.mjs';16import { git, repoStatus } from '../lib/git.mjs';17import { mapLimit, SYM } from '../lib/ui.mjs';18import { EXIT } from '../lib/api.mjs';19import { resolveTargets } from './commit.mjs';2021export function registerPush(program) {22  program23    .command('push [name]')24    .description('push the current branch of target repo(s)')25    .option('--all', 'every workspace repo with commits ahead', false)26    .action(async (name, opts) => {27      const config = requireCliConfig();28      const targets = resolveTargets(config, name, opts.all);29      let failures = 0;30      let pushed = 0;31      await mapLimit(targets, 4, async (dir) => {32        const repo = basename(dir);33        const status = await repoStatus(dir);34        if (opts.all && status.hasUpstream && status.ahead === 0) {35          console.log(`${SYM.skip} ${repo} ${pc.dim('nothing to push')}`);36          return;37        }38        const args = status.hasUpstream ? ['push'] : ['push', '-u', 'origin', status.branch];39        const result = await git(dir, args, { token: config.token });40        if (result.ok) {41          pushed += 1;42          console.log(`${SYM.push} ${repo} ${pc.dim(`pushed ${status.branch}${status.ahead ? ` (+${status.ahead})` : ''}`)}`);43        } else {44          failures += 1;45          console.error(`${SYM.fail} ${repo}: ${result.stderr.trim().split('\n').pop()}`);46        }47      });48      if (failures > 0) process.exit(EXIT.PARTIAL);49      if (pushed === 0 && targets.length > 0) console.log(pc.dim('Everything up to date.'));50    });51}52