add repositories to workspace from source (#804)

* make addworkspace clone multiple repositories into ws from list of URLs in a file

* extend usage

* check if source is file or URL

* update Commands.md

* checked allwedargcount function

* resolved merge conflicts and change requests

* update manuals

* change the order of error and usage in allwedargcount()

* improve URL matcher. include also SSH protocol
This commit is contained in:
Roshak Zarhoun 2019-12-10 14:57:32 +01:00 committed by 罗泽轩
parent bb7d389084
commit 16bb0df9f9
5 changed files with 86 additions and 18 deletions

View file

@ -253,7 +253,7 @@ $ git effort bin/* lib/*
```bash
usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>
git bulk --addworkspace <ws-name> <ws-root-directory>
git bulk --addworkspace <ws-name> <ws-root-directory> (--from <URL or file>)
git bulk --removeworkspace <ws-name>
git bulk --addcurrent <ws-name>
git bulk --purge
@ -264,6 +264,20 @@ usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>
```bash
$ git bulk --addworkspace personal ~/workspaces/personal
```
With option `--from` the URL to a single repository or a file containing multiple URLs can be added and they will be cloned diretly into the workspace. Suitable for the initial setup of a multi-repo project.
```bash
$ git bulk --addworkspace projectX ~/workspaces/projectx --from https://github.com/x/project-x.git
# OR with a file containing many repositories on each line:
$ git bulk --addworkspace projectX ~/workspaces/projectx --from ~/workspaces/repositories.txt
```
with `repositories.txt` be like:
```
https://github.com/x/project-x-1.git
https://github.com/x/project-x-2.git
https://github.com/x/project-x-3.git
```
Register the current directory as a workspace to `git bulk`
@ -277,6 +291,7 @@ $ git bulk --addcurrent personal
```bash
$ git bulk --listall
bulkworkspaces.personal /Users/niklasschlimm/workspaces/personal
```
Run a git command on the repositories of the current workspace:

View file

@ -14,7 +14,7 @@ allwsmode=false
#
usage() {
echo 1>&2 "usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>"
echo 1>&2 " git bulk --addworkspace <ws-name> <ws-root-directory>"
echo 1>&2 " git bulk --addworkspace <ws-name> <ws-root-directory> (--from <URL or file>)"
echo 1>&2 " git bulk --removeworkspace <ws-name>"
echo 1>&2 " git bulk --addcurrent <ws-name>"
echo 1>&2 " git bulk --purge"
@ -22,7 +22,17 @@ usage() {
}
# add another workspace to global git config
function addworkspace { git config --global bulkworkspaces."$wsname" "$wsdir"; }
function addworkspace {
git config --global bulkworkspaces."$wsname" "$wsdir";
if [ ! -z "$source" ]; then
if ! cd "$wsdir" 2>/dev/null; then echo "Path of workspace doesn't exist, make it first."; exit 1; fi
regex='http(s)?://|ssh://|(git@)?.*:.*/.*'
if [[ "$source" =~ $regex ]]; then git clone "$source";
elif [ -f "$source" ]; then while read line; do git clone "$line"; done < "$source";
else echo 1>&2 "format of URL or file unknown";
fi
fi
}
# add current directory
function addcurrent { git config --global bulkworkspaces."$wsname" "$PWD"; }
@ -94,12 +104,12 @@ function wsnameToCurrent () {
wslist="$(echo "$(listall)")" && echo "${wslist:-'<no workspaces defined yet>'}" && exit 1
}
# helper to check number of arguments
function allowedargcount {
if test "$paramcount" -ne $1; then
echo 1>&2 "error: wrong number of arguments" && usage
exit 1;
fi
# helper to check number of arguments.
function allowedargcount () {
if [ $paramcount -ne $1 ] && [ $paramcount -ne $2 ]; then
echo 1>&2 "error: wrong number of arguments" && usage;
exit 1;
fi
}
# execute the bulk operation
@ -135,7 +145,7 @@ while [ "${#}" -ge 1 ] ; do
--listall|--purge)
butilcommand="${1:2}" && break ;;
--removeworkspace|--addcurrent|--addworkspace)
butilcommand="${1:2}" && wsname="$2" && wsdir="$3" && break ;;
butilcommand="${1:2}" && wsname="$2" && wsdir="$3" && if [ "$4"=="--from" ]; then source="$5"; fi && break ;;
-a)
allwsmode=true ;;
-g)
@ -161,7 +171,7 @@ if $singlemode; then echo "Selected single workspace mode in workspace: $wsname"
case $butilcommand in
listall|purge) allowedargcount 1;;
addcurrent|removeworkspace) allowedargcount 2;;
addworkspace) allowedargcount 3;;
addworkspace) allowedargcount 3 5;;
esac
$butilcommand # run user command

View file

@ -10,7 +10,7 @@
git bulk [\-g] ([\-a]|[\-w <ws\-name>]) <git command>
.
.br
git bulk \-\-addworkspace <ws\-name> <ws\-root\-directory>
git bulk \-\-addworkspace <ws\-name> <ws\-root\-directory> (\-\-from <URL or file>)
.
.br
git bulk \-\-removeworkspace <ws\-name>
@ -63,12 +63,15 @@ Run the git command on the specified workspace\. The workspace must be registere
Any git Command you wish to execute on the repositories\.
.
.P
\-\-addworkspace <ws\-name> <ws\-root\-directory>
\-\-addworkspace <ws\-name> <ws\-root\-directory> (\-\-from <URL or file&rt;gt;)
.
.P
Register a workspace for bulk operations\. All repositories in the directories below <ws\-root\-directory> get registered under this workspace with the name <ws\-name>\. <ws\-root\-directory> must be absolute path\.
.
.P
With option \'\-\-from\' the URL to a single repository or a file containing multiple URLs can be added and they will be cloned diretly into the workspace\. Suitable for the initial setup of a multi\-repo project\.
.
.P
\-\-removeworkspace <ws\-name>
.
.P
@ -100,6 +103,18 @@ Register a workspace so that git bulk knows about it:
$ git bulk \-\-addworkspace personal ~/workspaces/personal
Use option \-\-from in order to directly clone a repository or multiple repositories
$ git bulk \-\-addworkspace personal ~/workspaces/personal \-\-from https://github\.com/tj/git\-extras\.git
$ git bulk \-\-addworkspace personal ~/workspaces/personal \-\-from ~/repositories\.txt
repositories\.txt
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
https://host\-of\-git/repo\-1\.git
https://host\-of\-git/repo\-2\.git
https://host\-of\-git/repo\-3\.git
Register the current directory as a workspace to git bulk:
$ git bulk \-\-addcurrent personal

View file

@ -77,7 +77,7 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p>git bulk [-g] ([-a]|[-w &lt;ws-name&gt;]) &lt;git command&gt; <br />
git bulk --addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt; <br />
git bulk --addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt; (--from &lt;URL or file&gt;) <br />
git bulk --removeworkspace &lt;ws-name&gt; <br />
git bulk --addcurrent &lt;ws-name&gt; <br />
git bulk --purge <br />
@ -112,10 +112,12 @@ git bulk --listall</p>
<p> Any git Command you wish to execute on the repositories.</p>
<p> --addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt;</p>
<p> --addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt; (--from &lt;URL or file&rt;gt;)</p>
<p> Register a workspace for bulk operations. All repositories in the directories below &lt;ws-root-directory&gt; get registered under this workspace with the name &lt;ws-name&gt;. &lt;ws-root-directory&gt; must be absolute path.</p>
<p> With option '--from' the URL to a single repository or a file containing multiple URLs can be added and they will be cloned diretly into the workspace. Suitable for the initial setup of a multi-repo project.</p>
<p> --removeworkspace &lt;ws-name&gt;</p>
<p> Remove the workspace with the logical name &lt;ws-name&gt;.</p>
@ -138,6 +140,18 @@ git bulk --listall</p>
$ git bulk --addworkspace personal ~/workspaces/personal
Use option --from in order to directly clone a repository or multiple repositories
$ git bulk --addworkspace personal ~/workspaces/personal --from https://github.com/tj/git-extras.git
$ git bulk --addworkspace personal ~/workspaces/personal --from ~/repositories.txt
repositories.txt
----------------------------------
https://host-of-git/repo-1.git
https://host-of-git/repo-2.git
https://host-of-git/repo-3.git
Register the current directory as a workspace to git bulk:
$ git bulk --addcurrent personal
@ -169,7 +183,7 @@ $ git bulk --purge
<h2 id="AUTHOR">AUTHOR</h2>
<p>Written by Niklas Schlimm &lt;<a href="&#x6d;&#x61;&#105;&#108;&#116;&#111;&#x3a;&#x6e;&#115;&#49;&#48;&#51;&#64;&#x68;&#111;&#x74;&#x6d;&#97;&#105;&#x6c;&#46;&#100;&#x65;" data-bare-link="true">&#110;&#115;&#49;&#48;&#51;&#64;&#104;&#x6f;&#x74;&#x6d;&#x61;&#x69;&#x6c;&#46;&#100;&#x65;</a>&gt;</p>
<p>Written by Niklas Schlimm &lt;<a href="&#109;&#x61;&#x69;&#x6c;&#x74;&#x6f;&#x3a;&#x6e;&#115;&#49;&#48;&#51;&#x40;&#104;&#x6f;&#116;&#109;&#97;&#x69;&#108;&#x2e;&#100;&#x65;" data-bare-link="true">&#110;&#x73;&#x31;&#48;&#51;&#x40;&#104;&#x6f;&#x74;&#109;&#x61;&#x69;&#x6c;&#46;&#100;&#x65;</a>&gt;</p>
<h2 id="REPORTING-BUGS">REPORTING BUGS</h2>

View file

@ -4,7 +4,7 @@ git-bulk(1) -- Run git commands on multiple repositories
## SYNOPSIS
git bulk [-g] ([-a]|[-w &lt;ws-name&gt;]) &lt;git command&gt; <br/>
git bulk --addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt; <br/>
git bulk --addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt; (--from &lt;URL or file&gt;) <br/>
git bulk --removeworkspace &lt;ws-name&gt; <br/>
git bulk --addcurrent &lt;ws-name&gt; <br/>
git bulk --purge <br/>
@ -36,10 +36,12 @@ git bulk adds convenient support for operations that you want to execute on mult
Any git Command you wish to execute on the repositories.
--addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt;
--addworkspace &lt;ws-name&gt; &lt;ws-root-directory&gt; (--from &lt;URL or file&rt;gt;)
Register a workspace for bulk operations. All repositories in the directories below &lt;ws-root-directory&gt; get registered under this workspace with the name &lt;ws-name&gt;. &lt;ws-root-directory&gt; must be absolute path.
With option '--from' the URL to a single repository or a file containing multiple URLs can be added and they will be cloned diretly into the workspace. Suitable for the initial setup of a multi-repo project.
--removeworkspace &lt;ws-name&gt;
Remove the workspace with the logical name &lt;ws-name&gt;.
@ -62,6 +64,18 @@ git bulk adds convenient support for operations that you want to execute on mult
$ git bulk --addworkspace personal ~/workspaces/personal
Use option --from in order to directly clone a repository or multiple repositories
$ git bulk --addworkspace personal ~/workspaces/personal --from https://github.com/tj/git-extras.git
$ git bulk --addworkspace personal ~/workspaces/personal --from ~/repositories.txt
repositories.txt
----------------------------------
https://host-of-git/repo-1.git
https://host-of-git/repo-2.git
https://host-of-git/repo-3.git
Register the current directory as a workspace to git bulk:
$ git bulk --addcurrent personal