safe-push 0.1.1 → 0.2.1

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.
package/dist/index.js CHANGED
@@ -6859,15 +6859,21 @@ async function getDiffFiles(remote = "origin") {
6859
6859
  `).filter(Boolean);
6860
6860
  }
6861
6861
  async function execPush(args = [], remote = "origin") {
6862
- const branch = await getCurrentBranch();
6863
- const isNew = await isNewBranch(remote);
6864
- const pushArgs = ["push"];
6865
- if (isNew) {
6866
- pushArgs.push("-u");
6867
- }
6868
- pushArgs.push(remote, branch);
6869
- if (args.length > 0) {
6870
- pushArgs.push(...args);
6862
+ let pushArgs;
6863
+ const hasUserRefspec = args.some((arg) => !arg.startsWith("-"));
6864
+ if (hasUserRefspec) {
6865
+ pushArgs = ["push", ...args];
6866
+ } else {
6867
+ const branch = await getCurrentBranch();
6868
+ const isNew = await isNewBranch(remote);
6869
+ pushArgs = ["push"];
6870
+ const hasSetUpstream = args.some((a) => a === "-u" || a === "--set-upstream");
6871
+ if (isNew || hasSetUpstream) {
6872
+ pushArgs.push("-u");
6873
+ }
6874
+ pushArgs.push(remote, branch);
6875
+ const remainingFlags = args.filter((a) => a !== "-u" && a !== "--set-upstream");
6876
+ pushArgs.push(...remainingFlags);
6871
6877
  }
6872
6878
  try {
6873
6879
  const output = await execGit(pushArgs);
@@ -7046,7 +7052,8 @@ function createCheckCommand() {
7046
7052
 
7047
7053
  // src/commands/push.ts
7048
7054
  function createPushCommand() {
7049
- return new Command("push").description("Check and push if allowed").option("-f, --force", "Bypass safety checks").option("--dry-run", "Show what would be pushed without actually pushing").action(async (options) => {
7055
+ return new Command("push").description("Check and push if allowed").option("-f, --force", "Bypass safety checks").option("--dry-run", "Show what would be pushed without actually pushing").allowUnknownOption().action(async (options, command) => {
7056
+ const gitArgs = command.args;
7050
7057
  try {
7051
7058
  if (!await isGitRepository()) {
7052
7059
  printError("Not a git repository");
@@ -7063,7 +7070,7 @@ function createPushCommand() {
7063
7070
  printSuccess("Dry run: would push (checks bypassed)");
7064
7071
  process.exit(0);
7065
7072
  }
7066
- const result2 = await execPush();
7073
+ const result2 = await execPush(gitArgs);
7067
7074
  if (result2.success) {
7068
7075
  printSuccess("Push successful");
7069
7076
  process.exit(0);
@@ -7082,7 +7089,7 @@ function createPushCommand() {
7082
7089
  printSuccess("Dry run: would push (user confirmed)");
7083
7090
  process.exit(0);
7084
7091
  }
7085
- const result2 = await execPush();
7092
+ const result2 = await execPush(gitArgs);
7086
7093
  if (result2.success) {
7087
7094
  printSuccess("Push successful");
7088
7095
  process.exit(0);
@@ -7101,7 +7108,7 @@ function createPushCommand() {
7101
7108
  printSuccess("Dry run: would push");
7102
7109
  process.exit(0);
7103
7110
  }
7104
- const result = await execPush();
7111
+ const result = await execPush(gitArgs);
7105
7112
  if (result.success) {
7106
7113
  printSuccess("Push successful");
7107
7114
  process.exit(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "safe-push",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Git push safety checker - blocks pushes to forbidden areas",
5
5
  "type": "module",
6
6
  "bin": {
@@ -18,7 +18,9 @@ export function createPushCommand(): Command {
18
18
  .description("Check and push if allowed")
19
19
  .option("-f, --force", "Bypass safety checks")
20
20
  .option("--dry-run", "Show what would be pushed without actually pushing")
21
- .action(async (options: { force?: boolean; dryRun?: boolean }) => {
21
+ .allowUnknownOption()
22
+ .action(async (options: { force?: boolean; dryRun?: boolean }, command: Command) => {
23
+ const gitArgs = command.args;
22
24
  try {
23
25
  // Gitリポジトリ内か確認
24
26
  if (!(await isGitRepository())) {
@@ -43,7 +45,7 @@ export function createPushCommand(): Command {
43
45
  process.exit(0);
44
46
  }
45
47
 
46
- const result = await execPush();
48
+ const result = await execPush(gitArgs);
47
49
  if (result.success) {
48
50
  printSuccess("Push successful");
49
51
  process.exit(0);
@@ -73,7 +75,7 @@ export function createPushCommand(): Command {
73
75
  process.exit(0);
74
76
  }
75
77
 
76
- const result = await execPush();
78
+ const result = await execPush(gitArgs);
77
79
  if (result.success) {
78
80
  printSuccess("Push successful");
79
81
  process.exit(0);
@@ -96,7 +98,7 @@ export function createPushCommand(): Command {
96
98
  process.exit(0);
97
99
  }
98
100
 
99
- const result = await execPush();
101
+ const result = await execPush(gitArgs);
100
102
  if (result.success) {
101
103
  printSuccess("Push successful");
102
104
  process.exit(0);
package/src/git.ts CHANGED
@@ -106,21 +106,36 @@ export async function execPush(
106
106
  args: string[] = [],
107
107
  remote = "origin"
108
108
  ): Promise<{ success: boolean; output: string }> {
109
- const branch = await getCurrentBranch();
110
- const isNew = await isNewBranch(remote);
109
+ let pushArgs: string[];
111
110
 
112
- const pushArgs = ["push"];
111
+ // ユーザーがremote/refspecを明示的に指定したか判定
112
+ const hasUserRefspec = args.some((arg) => !arg.startsWith("-"));
113
113
 
114
- // 新規ブランチの場合は-uオプションを追加
115
- if (isNew) {
116
- pushArgs.push("-u");
117
- }
114
+ if (hasUserRefspec) {
115
+ // ユーザー指定のremote/refspecをそのまま使用
116
+ pushArgs = ["push", ...args];
117
+ } else {
118
+ // 自動でremote/branchを決定
119
+ const branch = await getCurrentBranch();
120
+ const isNew = await isNewBranch(remote);
121
+
122
+ pushArgs = ["push"];
123
+
124
+ // 新規ブランチ、またはユーザーが-uを指定した場合に追加
125
+ const hasSetUpstream = args.some(
126
+ (a) => a === "-u" || a === "--set-upstream"
127
+ );
128
+ if (isNew || hasSetUpstream) {
129
+ pushArgs.push("-u");
130
+ }
118
131
 
119
- pushArgs.push(remote, branch);
132
+ pushArgs.push(remote, branch);
120
133
 
121
- // 追加の引数がある場合
122
- if (args.length > 0) {
123
- pushArgs.push(...args);
134
+ // -u/--set-upstream以外のフラグを追加
135
+ const remainingFlags = args.filter(
136
+ (a) => a !== "-u" && a !== "--set-upstream"
137
+ );
138
+ pushArgs.push(...remainingFlags);
124
139
  }
125
140
 
126
141
  try {