Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 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
91 changes: 75 additions & 16 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,55 @@ 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 {
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 push --set-upstream origin ${targetBranch} -f`;
await fs.remove('.git');
if (force) {

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.

else 和 if 合并,可以减少一层缩进,才能看得清差异。

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.

已按建议把 else { if (...) ... } 合并为 else if (...),减少一层缩进,便于看差异。对应提交:b957b11。CLI 逻辑调整无 UI 变更,截图不适用。

cd(sourceFolder);

await $`git init`;
await $`git remote add origin ${GitURL}`;
await $`git checkout -b ${targetBranch}`;
await $`git add .`;
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}`;
}
}
}

Expand Down Expand Up @@ -135,13 +168,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