@kaynooo/cli-git 0.17.3 → 0.18.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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/dist/gp.js +35 -12
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -18,6 +18,7 @@ Prints one line with the branch, its upstream and how far ahead and behind it is
18
18
 
19
19
  `gp [dir]...` pushes the current branch of the repository, or of each dir's, when it is ahead of its upstream as of the last fetch, and with `-u` to origin (the only remote when there is no origin) when it has no upstream yet. `--repos` (`-r`) pushes every repository `gst --repos` lists, one after another.
20
20
 
21
+ - `--branch <name>` (`-b`, repeatable) pushes that local branch instead of the current one: to the upstream it tracks, under its name there, else with `-u` to origin; a repository without it is skipped.
21
22
  - A detached HEAD, a branch with nothing ahead and a repository with no remote are skipped, saying so; a refused push shows git's reason and the next repository is still pushed.
22
23
  - What follows `--` goes to each `git push` (`gp -- --tags`, `gp -- --force-with-lease`), and a branch with nothing ahead is then pushed too.
23
24
  - `--dry-run` says what would be pushed; `--json` prints `{path, branch, remote, commits, outcome, reason?}` per repository, `outcome` being `pushed`, `would-push`, `skipped` or `refused`.
package/dist/gp.js CHANGED
@@ -2631,10 +2631,12 @@ async function dirsOf(positionals, all) {
2631
2631
  // src/gp.ts
2632
2632
  var USAGE = `gp [dir]...: pushes the current branch of the repository, or of each dir, when it is ahead of its upstream or has none yet
2633
2633
 
2634
+ --branch pushes that local branch instead, to its upstream or with -u to origin, whichever branch is checked out; given several times, each is pushed, and a repository without it is skipped.
2634
2635
  --repos pushes every repository at and below the current one's toplevel, or below each dir given, the way gst --repos lists them. A detached HEAD, a branch with nothing to push and a repository with no remote are skipped, saying so; the pushes run one after another.
2635
2636
  What follows -- goes to every git push as is (gp -- --tags, gp -- --force-with-lease), and a branch with nothing ahead is then pushed too.`;
2636
2637
  var EXITS = [[0, "every push went through, or there was nothing to push"], [1, "a push was refused or a repository could not be read"], [2, "outside a git repository or a usage error"]];
2637
2638
  var OPTIONS = {
2639
+ branch: { type: "string", short: "b", multiple: true, description: "push this local branch instead of the current one" },
2638
2640
  repos: { type: "boolean", short: "r", description: "every repository nested below too" },
2639
2641
  "dry-run": { type: "boolean", description: "say what would be pushed, pushing nothing" },
2640
2642
  json: jsonOption("one {path, branch, remote, commits, outcome, reason?} per repository, outcome being pushed, would-push, skipped or refused")
@@ -2656,19 +2658,38 @@ async function unpushed(dir, branch) {
2656
2658
  const count = await git2(["-C", dir, "rev-list", "--count", branch, "--not", "--remotes"]);
2657
2659
  return count.code === 0 ? Number(count.stdout.trim()) : null;
2658
2660
  }
2659
- async function push(repo, { dryRun, extra, env }) {
2661
+ var AHEAD = /ahead (\d+)/;
2662
+ async function namedTarget(dir, branch) {
2663
+ const read = await git2(["-C", dir, "for-each-ref", "--format=%(upstream:short)%00%(upstream:remotename)%00%(upstream:remoteref)%00%(upstream:track,nobracket)", `refs/heads/${branch}`]);
2664
+ const line = read.stdout.trim();
2665
+ if (read.code !== 0 || line === "")
2666
+ return;
2667
+ const [upstream = "", remote = "", ref = "", track = ""] = line.split("\x00");
2668
+ if (upstream === "")
2669
+ return { branch, upstream: null, ahead: null, refspec: [] };
2670
+ return { branch, upstream, ahead: track === "gone" ? null : Number(AHEAD.exec(track)?.[1] ?? 0), refspec: [remote, `${branch}:${ref}`] };
2671
+ }
2672
+ async function push(repo, asked, { dryRun, extra, env }) {
2660
2673
  const { path, dir, status } = repo;
2661
2674
  const skipped = (reason, branch = null) => ({ path, branch, remote: null, commits: null, outcome: "skipped", reason });
2662
2675
  if (status === null)
2663
2676
  return { path, branch: null, remote: null, commits: null, outcome: "refused", reason: "git could not read it" };
2664
- const { branch, upstream, aheadBehind } = status;
2665
- if (branch === "(detached)")
2666
- return skipped("detached HEAD");
2677
+ let target;
2678
+ if (asked !== undefined) {
2679
+ target = await namedTarget(dir, asked);
2680
+ if (target === undefined)
2681
+ return skipped(`no branch ${asked}`, asked);
2682
+ } else {
2683
+ if (status.branch === "(detached)")
2684
+ return skipped("detached HEAD");
2685
+ target = { branch: status.branch, upstream: status.upstream, ahead: status.aheadBehind?.ahead ?? null, refspec: [] };
2686
+ }
2687
+ const { branch, upstream, ahead, refspec } = target;
2667
2688
  let args;
2668
2689
  let remote;
2669
2690
  let commits;
2670
2691
  if (upstream === null) {
2671
- if ((await git2(["-C", dir, "rev-parse", "--verify", "--quiet", "HEAD"])).code !== 0)
2692
+ if ((await git2(["-C", dir, "rev-parse", "--verify", "--quiet", `refs/heads/${branch}`])).code !== 0)
2672
2693
  return skipped("no commit yet, nothing to push", branch);
2673
2694
  const name = await remoteFor(dir);
2674
2695
  if (name === undefined)
@@ -2677,11 +2698,11 @@ async function push(repo, { dryRun, extra, env }) {
2677
2698
  remote = `${name}/${branch}`;
2678
2699
  commits = await unpushed(dir, branch);
2679
2700
  } else {
2680
- if (aheadBehind !== null && aheadBehind.ahead === 0 && extra.length === 0)
2701
+ if (ahead === 0 && extra.length === 0)
2681
2702
  return skipped("nothing to push", branch);
2682
- args = ["push", ...extra];
2703
+ args = ["push", ...extra, ...refspec];
2683
2704
  remote = upstream;
2684
- commits = aheadBehind?.ahead ?? await unpushed(dir, branch);
2705
+ commits = ahead ?? await unpushed(dir, branch);
2685
2706
  }
2686
2707
  if (dryRun)
2687
2708
  return { path, branch, remote, commits, outcome: "would-push" };
@@ -2701,17 +2722,19 @@ function line(result) {
2701
2722
  return [role("path", process24.stdout)(result.path), said()];
2702
2723
  }
2703
2724
  async function main(argv) {
2704
- const { values, positionals, rest = [] } = parse(argv, USAGE, OPTIONS, { positionals: ["[dir]..."], rest: true, exits: EXITS, replaces: "git push, git push -u origin <branch> and a git -C <repo> push per nested repository", examples: ["gp", ["gp --repos", "every repository nested in this one"], ["gp --repos --dry-run", "what would go out"], ["gp . ../other", "this repository and a sibling"], ["gp -- --force-with-lease", "flags for git push"]] });
2725
+ const { values, positionals, rest = [] } = parse(argv, USAGE, OPTIONS, { positionals: ["[dir]..."], rest: true, exits: EXITS, replaces: "git push, git push -u origin <branch> and a git -C <repo> push per nested repository", examples: ["gp", ["gp --repos", "every repository nested in this one"], ["gp --repos --dry-run", "what would go out"], ["gp . ../other", "this repository and a sibling"], ["gp -b feature", "a branch other than the current one"], ["gp -- --force-with-lease", "flags for git push"]] });
2705
2726
  const dirs = await dirsOf(positionals, values.repos === true);
2706
2727
  const repos = await Promise.all(dirs.map(repoState));
2728
+ const branches = values.branch ?? [undefined];
2729
+ const pushes = repos.flatMap((repo) => branches.map((branch) => ({ repo, branch })));
2707
2730
  const results = [];
2708
2731
  const progress = progressLine();
2709
2732
  const prompts = askpass(progress.pause);
2710
2733
  const env = prompts === undefined ? undefined : { ...process24.env, ...prompts.env };
2711
2734
  try {
2712
- for (const [at, repo] of repos.entries()) {
2713
- progress.update(repos.length > 1 ? { step: `pushing ${repo.path}`, done: at, total: repos.length, unit: "repositories" } : { step: `pushing ${repo.path}` });
2714
- results.push(await push(repo, { dryRun: values["dry-run"] === true, extra: rest, env }));
2735
+ for (const [at, { repo, branch }] of pushes.entries()) {
2736
+ progress.update(pushes.length > 1 ? { step: `pushing ${repo.path}`, done: at, total: pushes.length, unit: "pushes" } : { step: `pushing ${repo.path}` });
2737
+ results.push(await push(repo, branch, { dryRun: values["dry-run"] === true, extra: rest, env }));
2715
2738
  }
2716
2739
  } finally {
2717
2740
  prompts?.close();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-git",
3
3
  "type": "module",
4
- "version": "0.17.3",
4
+ "version": "0.18.0",
5
5
  "description": "gst, gc, gb, gp, gf, gm, gcl, gd and gu: a repository's state in one call, a commit of exactly the paths given, its message checked before anything is staged, a branch picked, nested repositories pushed or fetched, a branch merged with its conflicts listed, a clone into the workspace, the changes per file and an undo that keeps what it takes away.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -56,6 +56,6 @@
56
56
  "gu": "$npm_execpath src/gu.ts"
57
57
  },
58
58
  "devDependencies": {
59
- "@kaynooo/cli-kit": "0.46.0"
59
+ "@kaynooo/cli-kit": "0.46.1"
60
60
  }
61
61
  }