Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ xgit download https://github.com/your-org/your-repo main path/to/your-folder/or-
### Upload folders to Git repositories

```shell
# Upload a folder to a Git repository on a specific branch (force push)
# Upload a folder to a Git repository on a specific branch
# (default: preserve history, only overwrite files with same path/name)
xgit upload path/to/source-folder https://github.com/your-org/your-repo target-branch

# Upload to a specific directory in the repository (non-force push)
# Upload to a specific directory in the repository
xgit upload path/to/source-folder https://github.com/your-org/your-repo target-branch target/directory

# Customize commit message
xgit upload path/to/source-folder https://github.com/your-org/your-repo target-branch --message "chore: sync assets"

# Discard history and force-push source folder
xgit upload path/to/source-folder https://github.com/your-org/your-repo target-branch --force
```

### Manage Git submodules
Expand All @@ -60,7 +67,7 @@ xgit submodule remove path/to/submodule
## Commands

- `xgit download <GitURL> [branchName] [folderOrFilePath] [targetFolder]` - Download folders or files from a Git repository
- `xgit upload <sourceFolder> <GitURL> <targetBranch> [targetFolder]` - Upload a folder to a Git repository
- `xgit upload <sourceFolder> <GitURL> <targetBranch> [targetFolder] [--message|-m <message>] [--force|-f]` - Upload a folder to a Git repository
- `xgit submodule remove [path]` - Remove a Git submodule

[1]: https://git-scm.com/
Expand Down
77 changes: 53 additions & 24 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,40 +74,52 @@ async function uploadFolder(
sourceFolder: string,
GitURL: string,
targetBranch: string,
targetFolder?: string
targetFolder?: string,
{ message = 'upload by Git-utility CLI', force = false } = {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用普通参数形式

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为普通参数形式,uploadFolder 现在使用 messageforce 常规参数,不再用对象解构。已在提交 dfb9cd4 中处理。CLI 变更无 UI,截图不适用。

) {
sourceFolder = path.resolve(sourceFolder);

if (targetFolder) {
const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname);

await fs.remove(tempFolder);
await fs.mkdirp(tempFolder);
cd(tempFolder);

await $`git clone -b ${targetBranch} ${GitURL} .`;

targetFolder = path.join(tempFolder, targetFolder);

await fs.remove(targetFolder);
await fs.mkdirp(targetFolder);
await fs.copy(sourceFolder, targetFolder);
await fs.remove(path.join(targetFolder, '.git'));

await $`git add .`;
await $`git commit -m "upload by Git-utility CLI"`;
await $`git push origin ${targetBranch}`;
} else {
if (force) {
cd(sourceFolder);

await $`git init`;
await $`git remote add origin ${GitURL}`;
await $`git checkout -b ${targetBranch}`;
await $`git add .`;
await $`git commit -m "upload by Git-utility CLI"`;
await $`git commit -m ${message}`;
await $`git push --set-upstream origin ${targetBranch} -f`;
await fs.remove('.git');

return;
}

const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname);

await fs.remove(tempFolder);
await fs.mkdirp(tempFolder);
cd(tempFolder);

await $`git clone -b ${targetBranch} ${GitURL} .`;

const uploadTarget = targetFolder
? path.join(tempFolder, targetFolder)
: tempFolder;

await fs.mkdirp(uploadTarget);

for (const entry of await fs.readdir(sourceFolder))
if (entry !== '.git')
await fs.copy(
path.join(sourceFolder, entry),
path.join(uploadTarget, entry),
{
overwrite: true
}
);

await $`git add .`;
await $`git commit -m ${message}`;
await $`git push origin ${targetBranch}`;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

保留原有的分支顺序,以便减少 Git 差异。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已按原有顶层分支顺序调整为 if (targetFolder) ... else ...,同时保留默认保留历史的新行为。已在提交 dfb9cd4 中处理。CLI 变更无 UI,截图不适用。

}

Command.execute(
Expand Down Expand Up @@ -135,13 +147,30 @@ Command.execute(
name="upload"
parameters="<sourceFolder> <GitURL> <targetBranch> [targetFolder]"
description="Upload a folder to a Git repository"
options={{
message: {
shortcut: 'm',
parameters: '<message>',
description: 'Custom commit message'
},
force: {
shortcut: 'f',
description:
'Discard Git history and force-push source folder'
}
}}
executor={(
_,
{ message, force },
sourceFolder: string,
GitURL: string,
targetBranch: string,
targetFolder?: string
) => uploadFolder(sourceFolder, GitURL, targetBranch, targetFolder)}
) =>
uploadFolder(sourceFolder, GitURL, targetBranch, targetFolder, {
message: typeof message === 'string' ? message : undefined,
force: force === true
})
}
/>
<Command name="submodule" description="Manage Git submodules">
<Command
Expand Down