Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
55 changes: 46 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ 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);
const uploadToRoot = !targetFolder;
targetFolder ||= '.';

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

await fs.remove(tempFolder);
Expand All @@ -89,13 +93,20 @@ 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);
Expand All @@ -104,7 +115,7 @@ async function uploadFolder(
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');
}
Expand Down Expand Up @@ -135,13 +146,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