@wzyjs/cli 0.3.23 → 0.3.24

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/index.js +25 -4
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -608,13 +608,17 @@ async function getLatestCommitMsg() {
608
608
  }
609
609
  function getContinuousCommitCount() {
610
610
  return new Promise((resolve, reject) => {
611
- exec(`git log --oneline --format=%s`, (err, stdout) => {
611
+ exec(`git log -n 10 --oneline --format=%s`, (err, stdout) => {
612
612
  if (err) {
613
613
  reject(err);
614
614
  return;
615
615
  }
616
616
  const messages = stdout.split(`
617
- `);
617
+ `).filter((l) => l.trim());
618
+ if (messages.length === 0) {
619
+ resolve(0);
620
+ return;
621
+ }
618
622
  const msg = messages[0];
619
623
  let count = 0;
620
624
  for (const item of messages) {
@@ -638,6 +642,14 @@ function execCommand(command) {
638
642
  });
639
643
  });
640
644
  }
645
+ async function getMergeCommitMsg(count) {
646
+ const stdout = await execCommand(`git log -n ${count} --pretty=%B`);
647
+ const lines = stdout.split(`
648
+ `).map((l) => l.trim()).filter((l) => l);
649
+ const uniqueLines = Array.from(new Set(lines));
650
+ return uniqueLines.join(`
651
+ `);
652
+ }
641
653
  var script_default = async (options) => {
642
654
  const hasChange = await execCommand("git status -s");
643
655
  if (hasChange) {
@@ -648,8 +660,17 @@ var script_default = async (options) => {
648
660
  }
649
661
  }
650
662
  const count = options?.count || await getContinuousCommitCount();
651
- const command = `GIT_EDITOR=true GIT_SEQUENCE_EDITOR="sed -i -e '2,\\$s/^pick/squash/'" git rebase -i HEAD~${count}`;
652
- console.log("rebase success", await execCommand(command));
663
+ if (count <= 0)
664
+ return;
665
+ const msg = await getMergeCommitMsg(count);
666
+ const currentMsg = await getLatestCommitMsg();
667
+ if (count > 1) {
668
+ const command = `GIT_EDITOR=true GIT_SEQUENCE_EDITOR="sed -i -e '2,\\$s/^pick/squash/'" git rebase -i HEAD~${count}`;
669
+ console.log("rebase success", await execCommand(command));
670
+ await execCommand(`git commit --amend -m "${msg}"`);
671
+ } else if (msg.trim() !== currentMsg.trim()) {
672
+ await execCommand(`git commit --amend -m "${msg}"`);
673
+ }
653
674
  if (options?.push) {
654
675
  console.log("push success", await execCommand("git push origin HEAD --force"));
655
676
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wzyjs/cli",
3
- "version": "0.3.23",
3
+ "version": "0.3.24",
4
4
  "description": "description",
5
5
  "author": "wzy",
6
6
  "main": "index.js",
@@ -28,5 +28,5 @@
28
28
  "publishConfig": {
29
29
  "access": "public"
30
30
  },
31
- "gitHead": "7fdb40c0c0b5fcccd0166722cd81e77df55885fb"
31
+ "gitHead": "82ce1f2a13a92943ae2618039c78148ff9637d52"
32
32
  }