Skip to content

Commit cbe58d3

Browse files
CopilotTechQuery
andauthored
[add] upload subcommand and support optional Target Folder for download subcommand (#5)
Co-authored-by: TechQuery <[email protected]>
1 parent 2863ab3 commit cbe58d3

File tree

4 files changed

+111
-41
lines changed

4 files changed

+111
-41
lines changed

ReadMe.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@ npx git-utility # your arguments here
2424
### Download folders or files from Git repositories
2525

2626
```shell
27-
# Download entire repository
27+
# Download entire repository to current directory
2828
xgit download https://github.com/your-org/your-repo
2929

3030
# Download from specific branch
3131
xgit download https://github.com/your-org/your-repo main
3232

3333
# Download specific folder or file
3434
xgit download https://github.com/your-org/your-repo main path/to/your-folder/or-file
35+
36+
# Download to a specific local path
37+
xgit download https://github.com/your-org/your-repo main path/to/your-folder/or-file ./local-destination
38+
```
39+
40+
### Upload folders to Git repositories
41+
42+
```shell
43+
# Upload a folder to a Git repository on a specific branch (force push)
44+
xgit upload path/to/source-folder https://github.com/your-org/your-repo target-branch
45+
46+
# Upload to a specific directory in the repository (non-force push)
47+
xgit upload path/to/source-folder https://github.com/your-org/your-repo target-branch target/directory
3548
```
3649

3750
### Manage Git submodules
@@ -46,7 +59,8 @@ xgit submodule remove path/to/submodule
4659

4760
## Commands
4861

49-
- `xgit download <GitURL> [branchName] [folderOrFilePath]` - Download folders or files from a Git repository
62+
- `xgit download <GitURL> [branchName] [folderOrFilePath] [targetFolder]` - Download folders or files from a Git repository
63+
- `xgit upload <sourceFolder> <GitURL> <targetBranch> [targetFolder]` - Upload a folder to a Git repository
5064
- `xgit submodule remove [path]` - Remove a Git submodule
5165

5266
[1]: https://git-scm.com/

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-utility",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"license": "LGPL-3.0",
55
"author": "[email protected]",
66
"description": "A Git utility CLI tool with some missing sub commands",
@@ -10,6 +10,7 @@
1010
"command",
1111
"cli",
1212
"download",
13+
"upload",
1314
"submodule"
1415
],
1516
"homepage": "https://github.com/idea2app/Git-utility#readme",
@@ -25,14 +26,14 @@
2526
"xgit": "dist/index.js"
2627
},
2728
"dependencies": {
28-
"commander-jsx": "^0.7.1",
29+
"commander-jsx": "^0.7.2",
2930
"zx": "^8.8.5"
3031
},
3132
"devDependencies": {
3233
"@types/fs-extra": "^11.0.4",
33-
"@types/node": "^22.18.13",
34+
"@types/node": "^22.19.1",
3435
"husky": "^9.1.7",
35-
"lint-staged": "^16.2.6",
36+
"lint-staged": "^16.2.7",
3637
"prettier": "^3.6.2",
3738
"typescript": "~5.9.3"
3839
},

pnpm-lock.yaml

Lines changed: 26 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.tsx

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ $.verbose = true;
88
async function downloadGitFolder(
99
GitURL: string,
1010
branchName?: string,
11-
folderOrFilePath?: string
11+
folderOrFilePath?: string,
12+
targetFolder = '.'
1213
) {
13-
const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname),
14-
targetFolder = process.cwd();
14+
targetFolder = path.resolve(targetFolder);
15+
16+
const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname);
1517

1618
await fs.remove(tempFolder);
1719
await fs.mkdirp(tempFolder);
@@ -39,10 +41,8 @@ async function downloadGitFolder(
3941
if (sourceStat.isFile()) {
4042
const fileName = path.basename(sourcePath);
4143

42-
await fs.copy(sourcePath, path.join(targetFolder, fileName), {
43-
overwrite: true
44-
});
45-
} else await fs.copy(sourcePath, targetFolder, { overwrite: true });
44+
await fs.copy(sourcePath, path.join(targetFolder, fileName));
45+
} else await fs.copy(sourcePath, targetFolder);
4646
}
4747

4848
async function listSubmodules() {
@@ -70,25 +70,79 @@ Note: You may want to commit these changes with:
7070
git commit -m "Remove submodule ${submodulePath}"`);
7171
}
7272

73+
async function uploadFolder(
74+
sourceFolder: string,
75+
GitURL: string,
76+
targetBranch: string,
77+
targetFolder?: string
78+
) {
79+
sourceFolder = path.resolve(sourceFolder);
80+
81+
if (targetFolder) {
82+
const tempFolder = path.join(os.tmpdir(), new URL(GitURL).pathname);
83+
84+
await fs.remove(tempFolder);
85+
await fs.mkdirp(tempFolder);
86+
cd(tempFolder);
87+
88+
await $`git clone -b ${targetBranch} ${GitURL} .`;
89+
90+
targetFolder = path.join(tempFolder, targetFolder);
91+
92+
await fs.remove(targetFolder);
93+
await fs.mkdirp(targetFolder);
94+
await fs.copy(sourceFolder, targetFolder);
95+
await fs.remove(path.join(targetFolder, '.git'));
96+
97+
await $`git add .`;
98+
await $`git commit -m "upload by Git-utility CLI"`;
99+
await $`git push origin ${targetBranch}`;
100+
} else {
101+
cd(sourceFolder);
102+
103+
await $`git init`;
104+
await $`git remote add origin ${GitURL}`;
105+
await $`git checkout -b ${targetBranch}`;
106+
await $`git add .`;
107+
await $`git commit -m "upload by Git-utility CLI"`;
108+
await $`git push --set-upstream origin ${targetBranch} -f`;
109+
await fs.remove('.git');
110+
}
111+
}
112+
73113
Command.execute(
74114
<Command name="xgit">
75115
<Command
76116
name="download"
77-
parameters="<GitURL> [branchName] [folderOrFilePath]"
117+
parameters="<GitURL> [branchName] [folderOrFilePath] [targetFolder]"
78118
description="Download folders or files from a Git repository"
79119
executor={(
80120
_,
81121
GitURL: string,
82122
branchName = 'main',
83-
folderOrFilePath?: string
123+
folderOrFilePath?: string,
124+
targetFolder?: string
84125
) =>
85126
downloadGitFolder(
86127
GitURL,
87128
branchName as string,
88-
folderOrFilePath
129+
folderOrFilePath,
130+
targetFolder
89131
)
90132
}
91133
/>
134+
<Command
135+
name="upload"
136+
parameters="<sourceFolder> <GitURL> <targetBranch> [targetFolder]"
137+
description="Upload a folder to a Git repository"
138+
executor={(
139+
_,
140+
sourceFolder: string,
141+
GitURL: string,
142+
targetBranch: string,
143+
targetFolder?: string
144+
) => uploadFolder(sourceFolder, GitURL, targetBranch, targetFolder)}
145+
/>
92146
<Command name="submodule" description="Manage Git submodules">
93147
<Command
94148
name="remove"

0 commit comments

Comments
 (0)