Skip to content

Commit ad7e0f1

Browse files
committed
Fixup + Log: use 0-index relative number
1 parent 088050b commit ad7e0f1

3 files changed

Lines changed: 45 additions & 25 deletions

File tree

src/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const DefaultOptions = {
180180
const FixupOptions = {
181181
commit: {
182182
type: "number",
183-
default: 1,
183+
default: 0,
184184
description: [
185185
"Relative number of commit to amend staged changes.",
186186
"Most recent is 1, next is 2, etc.",

src/commands/Fixup.tsx

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import * as React from "react";
33
import * as Ink from "ink-cjs";
44

55
import { Await } from "~/app/Await";
6+
import { Command } from "~/app/Command";
67
import { FormatText } from "~/app/FormatText";
78
import { Parens } from "~/app/Parens";
89
import { Store } from "~/app/Store";
910
import { cli } from "~/core/cli";
1011
import { colors } from "~/core/colors";
12+
import { is_finite_value } from "~/core/is_finite_value";
1113

1214
export function Fixup() {
1315
return (
@@ -25,27 +27,48 @@ async function run() {
2527

2628
const relative_number = argv.commit;
2729

28-
if (!relative_number) {
30+
if (!is_finite_value(relative_number)) {
2931
actions.output(
30-
<Ink.Text color={colors.red}>❗️ Usage: git fixup {"<relative-commit-number>"}</Ink.Text>,
32+
<Ink.Box flexDirection="column">
33+
<Ink.Text color={colors.red}>❗️ Usage: git fixup {"<relative-commit-number>"}</Ink.Text>
34+
<Ink.Text color={colors.gray}>
35+
Automates the process of adding staged changes to a previous commit.
36+
</Ink.Text>
37+
<FormatText
38+
wrapper={<Ink.Text color={colors.gray} />}
39+
message="You can use {git_stack_log} to get the relative commit number."
40+
values={{ git_stack_log: <Command>git stack log</Command> }}
41+
/>
42+
<Ink.Box height={1} />
43+
<FormatText
44+
message=" {prompt} git stack log"
45+
values={{ prompt: <Ink.Text color={colors.green}></Ink.Text> }}
46+
/>
47+
<FormatText
48+
message=" 0 * {sha} 18 hours ago noah homebrew-git-stack 2.9.9"
49+
values={{ sha: <Ink.Text color={colors.green}>e329794</Ink.Text> }}
50+
/>
51+
<FormatText
52+
message=" 1 * {sha} 18 hours ago noah 2.9.9"
53+
values={{ sha: <Ink.Text color={colors.green}>c7e4065</Ink.Text> }}
54+
/>
55+
<FormatText
56+
message=" 2 * {sha} 18 hours ago noah command: --label + github add labels"
57+
values={{ sha: <Ink.Text color={colors.green}>f82ac73</Ink.Text> }}
58+
/>
59+
<Ink.Box height={1} />
60+
<FormatText
61+
wrapper={<Ink.Text color={colors.gray} />}
62+
message="To target {sha} above, use {command}"
63+
values={{
64+
sha: <Ink.Text color={colors.green}>838e878</Ink.Text>,
65+
command: <Command>git stack fixup 2</Command>,
66+
}}
67+
/>
68+
</Ink.Box>,
3169
);
70+
3271
actions.output("");
33-
actions.output("This script automates the process of adding staged changes as a fixup commit");
34-
actions.output(
35-
"and the subsequent git rebase to flatten the commits based on relative commit number",
36-
);
37-
actions.output("You can use a `git log` like below to get the relative commit number");
38-
actions.output("");
39-
actions.output(" ❯ git stack log");
40-
actions.output(
41-
" 1\te329794d5f881cbf0fc3f26d2108cf6f3fdebabe enable drop_error_subtask test param",
42-
);
43-
actions.output(
44-
" 2\t57f43b596e5c6b97bc47e2a591f82ccc81651156 test drop_error_subtask baseline",
45-
);
46-
actions.output(" 3\t838e878d483c6a2d5393063fc59baf2407225c6d ErrorSubtask test baseline");
47-
actions.output("");
48-
actions.output("To target `838e87` above, you would call `fixup 3`");
4972

5073
actions.exit(0);
5174
}
@@ -64,11 +87,8 @@ async function run() {
6487
// );
6588
}
6689

67-
// Calculate commit SHA based on the relative commit number
68-
const adjusted_number = Number(relative_number) - 1;
69-
7090
// get the commit SHA of the target commit
71-
const commit_sha = (await cli(`git rev-parse HEAD~${adjusted_number}`)).stdout;
91+
const commit_sha = (await cli(`git rev-parse HEAD~${relative_number}`)).stdout;
7292

7393
actions.output(
7494
<FormatText
@@ -104,7 +124,7 @@ async function run() {
104124

105125
try {
106126
// rebase target needs to account for new commit created above
107-
const rebase_target = Number(relative_number) + 1;
127+
const rebase_target = Number(relative_number) + 2;
108128

109129
await cli(`git rebase -i --autosquash HEAD~${rebase_target}`, {
110130
env: {

src/commands/Log.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function run(args: Args) {
6565
const command = [
6666
`git log --pretty=format:"${format}" -n20 --graph --color ${rest_args}`,
6767
`cut -c 1-"${truncation_width}"`,
68-
`nl -w3 -s' '`,
68+
`nl -v0 -w3 -s' '`,
6969
].join(" | ");
7070

7171
const result = await cli(command);

0 commit comments

Comments
 (0)