Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
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
75 changes: 66 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ async function uploadFolder(
sourceFolder: string,
GitURL: string,
targetBranch: string,
targetFolder?: string
targetFolder?: string,
message = 'upload by Git-utility CLI',
force = false
) {
sourceFolder = path.resolve(sourceFolder);

Expand All @@ -89,24 +91,53 @@ async function uploadFolder(

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'));

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

await $`git add .`;
await $`git commit -m "upload by Git-utility CLI"`;
await $`git commit -m ${message}`;
await $`git push origin ${targetBranch}`;
} else {
} 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');
} else {
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} .`;

for (const entry of await fs.readdir(sourceFolder))
if (entry !== '.git')
await fs.copy(
path.join(sourceFolder, entry),
path.join(tempFolder, 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.

targetFolder = '.' 的默认值,这个分支是不是就和最上面的分支能合并成一种?

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.

已处理,统一了非 force 的上传分支:通过 targetFolder ||= '.' 让根目录上传与目标子目录上传走同一套 clone/overlay/push 逻辑;仅在“未传 targetFolder--force”时走强推分支。对应提交:2a7229b。CLI 逻辑调整无 UI 变更,截图不适用。

}

Expand Down Expand Up @@ -135,13 +166,39 @@ 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={(
_,
options,
sourceFolder: string,
GitURL: string,
targetBranch: string,
targetFolder?: string
) => uploadFolder(sourceFolder, GitURL, targetBranch, targetFolder)}
) => {
const message =
typeof options.message === 'string'
? options.message
: undefined;

return uploadFolder(
sourceFolder,
GitURL,
targetBranch,
targetFolder,
message,
options.force === true
);
}}
/>
<Command name="submodule" description="Manage Git submodules">
<Command
Expand Down