Skip to content

Commit 1bf8e31

Browse files
committed
Add functionality for CLI options and existing env variables. Make github the default host.
1 parent fd4459c commit 1bf8e31

File tree

6 files changed

+248
-59
lines changed

6 files changed

+248
-59
lines changed

README.md

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,98 @@
33

44
This script clones a subdirectory of a github/gitlab repository.
55

6-
# Usage
7-
## Quick test
8-
Test the script with the example config file.
9-
By the end of the execution, you will see a `tmp` directory containing the subfolder of the example repository.
6+
- [Install](#install)
7+
- [Using the comand line options](#using-the-comand-line-options)
8+
- [Clone from public repositories](#clone-from-public-repositories)
9+
- [Clone from private repositories](#clone-from-private-repositories)
10+
- [Using a configuration file](#using-a-configuration-file)
11+
- [Configuration variables](#configuration-variables)
12+
- [Mandatory variables](#mandatory-variables)
13+
- [Variables for **private repositories**](#variables-for-private-repositories)
14+
- [Optional variables](#optional-variables)
15+
16+
# Install
17+
Clone this repository
1018
```zsh
11-
./git-partial-clone.sh --file example.conf
19+
sudo apt install -y git
20+
git clone https://github.com/lu0/git-partial-clone.git
1221
```
13-
## Install
1422
Install the script and autocompletion rules.
1523
```zsh
16-
./install.sh
24+
cd git-partial-clone/ && ./install.sh
1725
```
18-
Then you can execute the script from any directory with your custom config file and use `TAB` to autocomplete the CLI options.
26+
Then you can call the command `git-partial-clone` from any directory and use `TAB` to autocomplete the CLI options.
27+
28+
# Using the comand line options
29+
Run with the `--help` flag to see the complete list of options (recommended). Or read the following sections to clone using the most common options.
30+
```
31+
$ git-partial-clone -h
32+
33+
Clone a subdirectory of a github/gitlab repository.
34+
35+
USAGE:
36+
git-partial-clone [OPTIONS] ARGUMENTS
37+
git-partial-clone # Or assume config variables in shell.
38+
39+
OPTIONS:
40+
--help Show this manual.
41+
42+
Using a config file:
43+
-f | --file Path to the configuration file.
44+
45+
CLI options (mandatory):
46+
-o | --owner Author (owner) of the repository.
47+
-r | --repo Name of the repository.
48+
49+
CLI options (optional):
50+
-h | --host github (default) or gitlab.
51+
-s | --subdir Subfolder to be cloned.
52+
53+
-t | --token Path to your access token (for private repos).
54+
-u | --user Your username (for private repos).
55+
56+
-b | --branch Branch to be fetched.
57+
-d | --depth Number of commits to be fetched.
58+
```
59+
60+
## Clone from public repositories
61+
Provide the mandatory options `--repo`, `--owner` and the subdirectory (`--subdir`) you want to clone.
62+
63+
The following example clones a subfolder of my [vscode-settings](https://github.com/lu0/vscode-settings/tree/master/json/snippets) repository.
1964
```zsh
20-
git-partial-clone --file path/to/your/config/file.conf
65+
git-partial-clone --owner=lu0 --repo=vscode-settings --subdir=json/snippets
2166
```
2267

23-
# Configuration
68+
You can also clone the entire repository, although this is not the intended use.
69+
```zsh
70+
git-partial-clone --owner=lu0 --repo=vscode-settings
71+
```
72+
73+
## Clone from private repositories
74+
You will need to generate an access token in order to clone private repositories, as password authentication is deprecated.
75+
76+
- Github: [github.com/settings/tokens](https://github.com/settings/tokens).
77+
- Gitlab: [gitlab.com/-/profile/personal_access_tokens](https://gitlab.com/-/profile/personal_access_tokens).
78+
79+
Save your token in a file and provide its path with the `--token` option, then provide your username with the `--user` option.
80+
81+
The following example would clone a subfolder of a private repository.
82+
```zsh
83+
git-partial-clone --owner=owner --repo=repo --subdir=path/to/subdir \
84+
--token=/path/to/your/token/file --user=username_with_access
85+
```
86+
87+
# Using a configuration file
88+
Using a configuration file will give you more control over the objects you're cloning. You can test this functionality with the provided configuration file:
89+
```zsh
90+
git-partial-clone --file=example.conf
91+
```
92+
By the end of the execution, you will see a `tmp` directory containing the subfolder of the example repository.
93+
94+
## Configuration variables
2495
Fill in the config file ([`template.conf`](./template.conf)) with the information of the repository you're cloning. You can see the example file [here](./example.conf).
2596

26-
## Mandatory variables
97+
### Mandatory variables
2798

2899
- `GIT_HOST`:
29100
- `github` if the repository is hosted on Github.
@@ -36,21 +107,13 @@ Fill in the config file ([`template.conf`](./template.conf)) with the informatio
36107
- **Subdirectory of the repository you want to clone**.
37108
- Omit it to clone the entire repository.
38109

39-
## Mandatory variables for **private repositories**
40-
You will need to generate an access token in order to clone private repositories, as password authentication is deprecated.
41-
42-
- Github: [github.com/settings/tokens](https://github.com/settings/tokens).
43-
- Gitlab: [gitlab.com/-/profile/personal_access_tokens](https://gitlab.com/-/profile/personal_access_tokens).
44-
45-
Once you have a token, store it in a file.
46-
110+
### Variables for **private repositories**
47111
- `TOKEN_PATH`:
48112
- Path to the file containing the access token.
49113
- `GIT_USER`:
50114
- Username with access to the repository.
51115

52-
## Optional variables
53-
The following variables give you more control over the objects you're cloning.
116+
### Optional variables
54117
- `BRANCH`:
55118
- The branch to be fetched.
56119
- Omit it to pull all of the branches and switch to the default one.

completion-rules.sh

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Bash completion file for the git-partial-clone script
2+
# Bash completion extension file for the git-partial-clone script
33
#
44
# Copyright (c) 2021 Lucero Alvarado
55
# https://github.com/lu0/git-partial-clone
@@ -10,25 +10,55 @@ _git-partial-clone()
1010
{
1111
local cur prev
1212
local words cword
13-
_init_completion || return
1413

14+
# Complete by using _split_longopt()
15+
_init_completion -s || return
16+
17+
# Try to stop option suggestion if -(-f)ile was provided
1518
local i use_config_file
1619
for ((i = cword - 1; i > 0; i--)); do
17-
[[ ${words[i]} == --file ]] \
20+
{ [[ ${words[i]} == --file* ]] || [[ ${words[i]} == -f ]] ;} \
1821
&& use_config_file=true && break
1922
done
2023

21-
case $prev in
24+
case "${prev}" in
2225
--file | -!(-*)f)
2326
# Suggest files with .conf extension
24-
_filedir '?()conf'
27+
_expand "$cur" && _filedir '?()conf'
28+
return
29+
;;
30+
--host | -!(-*)h)
31+
# Suggest tested hosts
32+
COMPREPLY=( $(compgen -W '
33+
github gitlab
34+
' -- "$cur" ) )
35+
return 0
36+
;;
37+
--token | -!(-*)t)
38+
# Autocomplete for any file/directory
39+
_expand "$cur" && _filedir
40+
return
41+
;;
42+
--owner | -o | --repo | -r | --subdir | -s | \
43+
--user | -u | --branch | -b | --depth | -d )
44+
# Suggest nothing :P
45+
return
46+
;;
47+
--help*)
2548
return
2649
;;
2750
esac
2851

29-
# Suggest options contained in the 'usage' section of the script
52+
# Suggest options contained in the 'usage'
53+
# section of the git-partial-clone script
54+
# If the option is long, suggest it with a leading '='
55+
# unless it is the --help option
3056
[[ ! $use_config_file ]] \
31-
&& COMPREPLY=($(compgen -W '$(_parse_help "$1")' -- "$cur"))
57+
&& COMPREPLY=($(compgen -W '
58+
$(_parse_help "$1" \
59+
| while read opt; do echo ${opt}\=; done \
60+
| sed "s/--help=/--help/g")
61+
' -- "$cur"))
3262
}
33-
complete -F _git-partial-clone git-partial-clone
63+
complete -o nosort -o nospace -F _git-partial-clone git-partial-clone
3464

example.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ REMOTE_PARTIAL_DIR=json/snippets
1515
TOKEN_PATH=
1616
GIT_USER=
1717

18-
BRANCH=
18+
BRANCH=master
1919
COMMIT_DEPTH=3
2020

2121
PARENT_DIR=tmp

0 commit comments

Comments
 (0)