Skip to content

Commit bde0089

Browse files
committed
cli: clean up help message & add to README; also inline build commit
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 2d15e34 commit bde0089

File tree

3 files changed

+87
-38
lines changed

3 files changed

+87
-38
lines changed

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ there must be a better way. and that's exactly how git-stacked-rebase came to be
1717

1818
a branch is just a reference to some commit (literally, it's a single-line file that contains a 40-character commit SHA -- check your `.git/refs/` folder). why not just work on your latest feature branch, rebase comfortably, and then have your tool automatically update the partial branches to make them point to the correct new commits?
1919

20-
from those partial branches, you can create pull requests. with this workflow, you get to comfortably iterate in a single branch; your teammates get the benefits of reviewing smaller PRs (when they're ready). win win. that's _literally_ all.
20+
from those partial branches, you can create pull requests. with this workflow, you get to comfortably iterate in a single branch; your teammates get the benefits of reviewing smaller PRs (when they're ready). win win. that's it.
2121

2222
---
2323

@@ -77,5 +77,43 @@ cd git-stacked-rebase
7777
## Usage
7878

7979
```sh
80-
git-stacked-rebase --help
80+
$ git-stacked-rebase --help
81+
82+
git-stacked-rebase <branch>
83+
84+
0. usually <branch> should be a remote one, e.g. 'origin/master'.
85+
1. will perform the interactive stacked rebase from HEAD to <branch>,
86+
2. but will not apply the changes to partial branches until --apply is used.
87+
88+
89+
git-stacked-rebase <branch> [-a|--apply]
90+
91+
1. will apply the changes to partial branches,
92+
2. but will not push any partial branches to a remote until --push is used.
93+
94+
95+
git-stacked-rebase <branch> [-p|--push -f|--force]
96+
97+
1. will push partial branches with --force (and extra safety).
98+
99+
100+
101+
non-positional args:
102+
103+
--autosquash, --no-autosquash
104+
105+
handles "fixup!", "squash!" -prefixed commits
106+
just like --autosquash for a regular rebase does.
107+
108+
can be enabled by default with the 'rebase.autosquash' option.
109+
110+
111+
--git-dir <path/to/git/dir/>
112+
113+
makes git-stacked-rebase begin operating inside the specified directory.
114+
115+
116+
-V|--version
117+
-h|--help
118+
81119
```

git-stacked-rebase.ts

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { bullets } from "nice-comment";
1616
import { setupPostRewriteHookFor } from "./git-reconcile-rewritten-list/postRewriteHook";
1717

1818
import { filenames } from "./filenames";
19-
import { configKeys, loadGitConfig } from "./config";
19+
import { loadGitConfig } from "./config";
2020
import {
2121
getDefaultOptions, //
2222
ParsedOptionsForGitStackedRebase,
@@ -1376,45 +1376,24 @@ async function getCommitOfBranch(repo: Git.Repository, branchReference: Git.Refe
13761376

13771377
//
13781378

1379-
/**
1380-
* the CLI
1381-
*/
1382-
export async function git_stacked_rebase(): Promise<void> {
1383-
const pkgFromSrc = path.join(__dirname, "package.json");
1384-
const pkgFromDist = path.join(__dirname, "../", "package.json");
1385-
let pkg;
1386-
1387-
// eslint-disable-next-line import/no-dynamic-require
1388-
if (fs.existsSync(pkgFromSrc)) pkg = require(pkgFromSrc);
1389-
// eslint-disable-next-line import/no-dynamic-require
1390-
else if (fs.existsSync(pkgFromDist)) pkg = require(pkgFromDist);
1391-
else pkg = {};
1392-
1393-
const gitStackedRebaseVersion = pkg.version;
1394-
1395-
const gitStackedRebaseVersionStr: string = !gitStackedRebaseVersion ? "" : "v" + gitStackedRebaseVersion;
1396-
1397-
const helpMsg = `\
1379+
export const helpMsg = `\
13981380
13991381
git-stacked-rebase <branch>
14001382
1401-
1. will edit/create the todo & will execute the interactive rebase (in the latest branch),
1383+
0. usually <branch> should be a remote one, e.g. 'origin/master'.
1384+
1. will perform the interactive stacked rebase from HEAD to <branch>,
14021385
2. but will not apply the changes to partial branches until --apply is used.
1403-
2.1 unless autoApply is enabled via \`git config [--global] ${configKeys.autoApplyIfNeeded} true'.
14041386
14051387
14061388
git-stacked-rebase <branch> [-a|--apply]
14071389
1408-
1. will apply the changes from the latest branch
1409-
to all partial branches (currently, using 'git reset --hard'),
1410-
2. but wil not push the partial branches to a remote until --push --force is used.
1390+
1. will apply the changes to partial branches,
1391+
2. but will not push any partial branches to a remote until --push is used.
14111392
14121393
1413-
git-stacked-rebase <branch> [--push|-p --force|-f]
1394+
git-stacked-rebase <branch> [-p|--push -f|--force]
14141395
1415-
1. will checkout each branch and will push --force,
1416-
2. but will NOT have any effect if --apply was not used yet.
1417-
2.1 unless autoApply is enabled.
1396+
1. will push partial branches with --force (and extra safety).
14181397
14191398
14201399
@@ -1423,22 +1402,27 @@ non-positional args:
14231402
--autosquash, --no-autosquash
14241403
14251404
handles "fixup!", "squash!" -prefixed commits
1426-
just like git-rebase's --autosquash does.
1405+
just like --autosquash for a regular rebase does.
14271406
1428-
can be enabled by default with the rebase.autosquash option.
1407+
can be enabled by default with the 'rebase.autosquash' option.
14291408
14301409
1431-
git-stacked-rebase [...] --git-dir <path/to/git/dir/> [...]
1410+
--git-dir <path/to/git/dir/>
14321411
14331412
makes git-stacked-rebase begin operating inside the specified directory.
14341413
14351414
1436-
git-stacked-rebase [...] -V|--version [...]
1437-
git-stacked-rebase [...] -h|--help [...]
1415+
-V|--version
1416+
-h|--help
14381417
14391418
1440-
git-stacked-rebase ${gitStackedRebaseVersionStr} __BUILD_DATE_REPLACEMENT_STR__
1419+
git-stacked-rebase __VERSION_REPLACEMENT_STR__ __BUILD_DATE_REPLACEMENT_STR__
14411420
`.replace(/\t/g, " ".repeat(4));
1421+
1422+
/**
1423+
* the CLI
1424+
*/
1425+
export async function git_stacked_rebase(): Promise<void> {
14421426
process.argv.splice(0, 2);
14431427

14441428
if (process.argv.some((arg) => ["-h", "--help"].includes(arg))) {
@@ -1447,7 +1431,7 @@ git-stacked-rebase ${gitStackedRebaseVersionStr} __BUILD_DATE_REPLACEMENT_STR__
14471431
}
14481432

14491433
if (process.argv.some((arg) => ["-V", "--version"].includes(arg))) {
1450-
process.stdout.write(`\ngit-stacked-rebase ${gitStackedRebaseVersionStr}\n\n`);
1434+
process.stdout.write(`git-stacked-rebase __VERSION_REPLACEMENT_STR__\n`);
14511435
return;
14521436
}
14531437

script/postbuild.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#!/usr/bin/env node
2+
13
const fs = require("fs");
4+
const { execSync } = require("child_process");
25
const { execSyncP } = require("pipestdio");
36

47
const executablePath = "./dist/git-stacked-rebase.js";
@@ -14,12 +17,14 @@ function modifyLines(path) {
1417

1518
const afterUpdateShebang = updateShebang(lines);
1619
const afterInjectRelativeBuildDatePrinter = injectRelativeBuildDatePrinter(lines);
20+
const afterInjectVersionStr = injectVersionStr(lines);
1721

1822
const newFile = lines.join("\n");
1923
fs.writeFileSync(path, newFile);
2024

2125
afterUpdateShebang();
2226
afterInjectRelativeBuildDatePrinter();
27+
afterInjectVersionStr();
2328
}
2429

2530
function updateShebang(lines) {
@@ -38,6 +43,7 @@ function updateShebang(lines) {
3843
function injectRelativeBuildDatePrinter(lines) {
3944
const BUILD_DATE_REPLACEMENT_STR = "__BUILD_DATE_REPLACEMENT_STR__";
4045
const targetLineIdx = lines.findIndex((line) => line.includes(BUILD_DATE_REPLACEMENT_STR));
46+
4147
const buildDate = new Date().getTime();
4248
const printRelativeDate =
4349
"(" + //
@@ -52,3 +58,24 @@ function injectRelativeBuildDatePrinter(lines) {
5258

5359
return () => execSyncP(`cat ${executablePath} | grep " mins ago"`);
5460
}
61+
62+
function injectVersionStr(lines) {
63+
const NEEDLE = "__VERSION_REPLACEMENT_STR__";
64+
const targetLines = lines.map((line, idx) => [line, idx]).filter(([line]) => line.includes(NEEDLE));
65+
66+
if (!targetLines.length) {
67+
throw new Error("0 target lines found.");
68+
}
69+
70+
const commitSha = execSync("git rev-parse @").toString().trim();
71+
const hasUntrackedChanges = execSync("git status -s", { encoding: "utf-8" }).toString().length > 0;
72+
73+
const REPLACEMENT = commitSha + (hasUntrackedChanges ? "-dirty" : "");
74+
75+
for (const [_line, idx] of targetLines) {
76+
lines[idx] = lines[idx].replace(NEEDLE, REPLACEMENT);
77+
}
78+
79+
// return () => execSyncP(`cat ${executablePath} | grep -v ${NEEDLE}`);
80+
return () => void 0;
81+
}

0 commit comments

Comments
 (0)