bstack 1.2.0 → 1.3.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 (2) hide show
  1. package/dist/bstack.mjs +33 -5
  2. package/package.json +1 -1
package/dist/bstack.mjs CHANGED
@@ -6,7 +6,7 @@ import * as v from "valibot";
6
6
  import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
7
7
  import { dirname } from "node:path";
8
8
  //#region package.json
9
- var version = "1.2.0";
9
+ var version = "1.3.0";
10
10
  //#endregion
11
11
  //#region src/command.ts
12
12
  var CommandError = class extends Error {
@@ -279,6 +279,7 @@ var GitCliRepository = class {
279
279
  remote,
280
280
  ...refspecs
281
281
  ]);
282
+ return branches.filter((branch) => existing.get(branch.name) !== branch.oid).map((branch) => branch.name);
282
283
  }
283
284
  statePath() {
284
285
  return this.git([
@@ -457,6 +458,13 @@ var GitHubCliPlatform = class {
457
458
  String(stackNumber)
458
459
  ]);
459
460
  }
461
+ closePullRequest(pr) {
462
+ this.gh([
463
+ "pr",
464
+ "close",
465
+ String(pr.number)
466
+ ]);
467
+ }
460
468
  editPullRequestBase(pr, base) {
461
469
  this.gh([
462
470
  "pr",
@@ -719,11 +727,13 @@ function syncStack(dependencies, options) {
719
727
  }
720
728
  }
721
729
  reporter.progress(`Pushing ${changes.length} remote branch${changes.length === 1 ? "" : "es"}`);
730
+ let pushedBranches = /* @__PURE__ */ new Set();
722
731
  try {
723
- repository.pushBranches(remote, changes.map((change) => ({
732
+ const updatedBranches = repository.pushBranches(remote, changes.map((change) => ({
724
733
  name: change.remoteBranch,
725
734
  oid: change.oid
726
735
  })));
736
+ pushedBranches = new Set(updatedBranches);
727
737
  } catch (error) {
728
738
  if (isReorder) restorePreviousStack(github, previous, base, remote, reporter, error);
729
739
  throw error;
@@ -737,6 +747,7 @@ function syncStack(dependencies, options) {
737
747
  reporter.progress(`Creating pull request: ${change.subject}`);
738
748
  return github.createPullRequest(change, pullRequestBase, options.draft);
739
749
  });
750
+ const createdBranches = new Set(changes.filter((_change, index) => existing[index] === void 0).map((change) => change.remoteBranch));
740
751
  if (changes.length === 1) {
741
752
  if (transition.kind === "partial") reporter.progress("Updating this down-stack prefix while preserving higher pull requests");
742
753
  const pullRequest = pullRequests[0];
@@ -770,6 +781,12 @@ function syncStack(dependencies, options) {
770
781
  }), remote, options.draft);
771
782
  } else if (transition.kind === "partial") reporter.progress("Updating this down-stack prefix while preserving higher pull requests");
772
783
  else reporter.progress("The native GitHub stack already has the correct members");
784
+ const currentIds = new Set(changes.map((change) => change.id));
785
+ const omittedPullRequests = transition.kind === "partial" ? [] : (previous?.changes ?? []).filter((change) => !currentIds.has(change.id)).map((change) => github.pullRequest(change.pullRequest)).filter((pullRequest) => pullRequest.state === "OPEN");
786
+ if (omittedPullRequests.length > 0) {
787
+ reporter.progress(`Closing ${omittedPullRequests.length} omitted pull request${omittedPullRequests.length === 1 ? "" : "s"}`);
788
+ for (const pullRequest of omittedPullRequests) github.closePullRequest(pullRequest);
789
+ }
773
790
  reporter.progress("Synchronizing pull request titles and descriptions");
774
791
  for (const [index, pr] of pullRequests.entries()) {
775
792
  github.editPullRequest(pr, changes[index]);
@@ -796,10 +813,19 @@ function syncStack(dependencies, options) {
796
813
  rewritten,
797
814
  changes: changes.map((change, index) => ({
798
815
  ...change,
799
- pullRequest: pullRequests[index]
816
+ pullRequest: pullRequests[index],
817
+ outcome: changeOutcome(change, index, pullRequests[index], previous, changes, base, createdBranches, pushedBranches)
800
818
  }))
801
819
  };
802
820
  }
821
+ function changeOutcome(change, index, pullRequest, previous, changes, base, createdBranches, pushedBranches) {
822
+ if (createdBranches.has(change.remoteBranch)) return "created";
823
+ const previousIndex = previous?.changes.findIndex((candidate) => candidate.id === change.id);
824
+ const previousBase = previousIndex === void 0 || previousIndex < 0 ? void 0 : previousIndex === 0 ? previous.base : previous.changes[previousIndex - 1].remoteBranch;
825
+ const currentBase = index === 0 ? base : changes[index - 1].remoteBranch;
826
+ const metadataChanged = pullRequest.title !== change.subject || pullRequest.body !== change.body;
827
+ return pushedBranches.has(change.remoteBranch) || previousBase !== currentBase || metadataChanged ? "updated" : "unchanged";
828
+ }
803
829
  function restorePreviousStack(github, previous, base, remote, reporter, rebuildError) {
804
830
  const rebuildMessage = rebuildError instanceof Error ? rebuildError.message : String(rebuildError);
805
831
  reporter.progress("Rebuild failed; restoring the previous native GitHub stack");
@@ -913,10 +939,12 @@ function main() {
913
939
  draft: values.draft,
914
940
  dryRun: values["dry-run"]
915
941
  });
916
- console.log(`${values["dry-run"] ? "Would sync" : "Synced"} ${result.changes.length} change${result.changes.length === 1 ? "" : "s"} against ${result.base}:`);
942
+ const changeCount = `${result.changes.length} change${result.changes.length === 1 ? "" : "s"}`;
943
+ console.log(values["dry-run"] ? `Would sync ${changeCount} against ${result.base}:` : `Synced stack against ${result.base} (${changeCount}):`);
917
944
  for (const change of result.changes) {
945
+ const outcome = change.outcome ? `${change.outcome.padEnd(9)} ` : "";
918
946
  const destination = change.pullRequest ? ` ${change.pullRequest.url}` : "";
919
- console.log(` ${change.oid.slice(0, 8)} ${change.subject}${destination}`);
947
+ console.log(` ${outcome}${change.oid.slice(0, 8)} ${change.subject}${destination}`);
920
948
  }
921
949
  }
922
950
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bstack",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Create native GitHub stacked pull requests from a linear series of commits",
5
5
  "license": "MIT",
6
6
  "repository": {