| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# |
| 5 |
# Byte array / string / unicode support across Python 2 & 3 |
| 6 |
# |
| 7 |
# Note that the str class in Python 2 is an ASCII string (byte) array and in |
| 8 |
# Python 3 it is a Unicode object. For Python 3 code that is backward compatible |
| 9 |
# with Python 2, we sometimes need version-specific conversion functions to give |
| 10 |
# us the data type we desire. These functions are: |
| 11 |
# |
| 12 |
# b(x) return a byte array of str x, much like b'<string const>' in |
| 13 |
# Python 3 |
| 14 |
# s(x) return a version-specific str object equivalent to x |
| 15 |
# |
| 16 |
import sys |
| 17 |
if sys.version_info < (3,): |
| 18 |
# Python 2 |
| 19 |
def b(x): return x |
| 20 |
def s(x): return x |
| 21 |
else: |
| 22 |
# Python 3 |
| 23 |
import codecs |
| 24 |
def b(x): return codecs.latin_1_encode(x)[0] |
| 25 |
def s(x): |
| 26 |
try: |
| 27 |
return x.decode("utf-8") |
| 28 |
except AttributeError: |
| 29 |
return x |
| 30 |
|
| 31 |
# |
| 32 |
# Pieces of the --help documentation |
| 33 |
# |
| 34 |
|
| 35 |
|
| 36 |
distRepoStatusLegend = r"""Legend: |
| 37 |
* ID: Repository ID, zero based (order git commands are run) |
| 38 |
* Repo Dir: Relative to base repo (base repo shown first with '(Base)') |
| 39 |
* Branch: Current branch, or (if detached HEAD) tag name or SHA1 |
| 40 |
* Tracking Branch: Tracking branch (or empty if no tracking branch exists) |
| 41 |
* C: Number local commits w.r.t. tracking branch (empty if zero or no TB) |
| 42 |
* M: Number of tracked modified (uncommitted) files (empty if zero) |
| 43 |
* ?: Number of untracked, non-ignored files (empty if zero) |
| 44 |
""" |
| 45 |
|
| 46 |
|
| 47 |
helpTopics = [ |
| 48 |
'overview', |
| 49 |
'repo-selection-and-setup', |
| 50 |
'dist-repo-status', |
| 51 |
'repo-versions', |
| 52 |
'dist-repo-versions-table', |
| 53 |
'aliases', |
| 54 |
'default-branch', |
| 55 |
'move-to-base-dir', |
| 56 |
'usage-tips', |
| 57 |
'script-dependencies', |
| 58 |
] |
| 59 |
|
| 60 |
|
| 61 |
def getHelpTopicsStr(): |
| 62 |
helpTopicStr = "" |
| 63 |
for helpTopic in helpTopics: |
| 64 |
helpTopicStr += "* '" + helpTopic + "'\n" |
| 65 |
return helpTopicStr |
| 66 |
|
| 67 |
|
| 68 |
# Look up help help string given keys from helpTopics array. |
| 69 |
helpTopicDefaultIdx = 0; |
| 70 |
|
| 71 |
|
| 72 |
helpTopicsDict = {} |
| 73 |
|
| 74 |
|
| 75 |
helpUsageHeader = r"""gitdist [gitdist arguments] <raw-git-command> [git arguments] |
| 76 |
gitdist [gitdist arguments] dist-repo-status |
| 77 |
gitdist [gitdist arguments] dist-repo-versions-table |
| 78 |
|
| 79 |
Run git over a set of git repos in a multi-repository git project (see |
| 80 |
--dist-help=overview --help). This script also includes other tools like |
| 81 |
printing a compact repo status table (see --dist-help=dist-repo-status) and |
| 82 |
tracking compatible versions through multi-repository SHA1 version files (see |
| 83 |
--dist-help=repo-versions). |
| 84 |
|
| 85 |
The options in [gitdist options] are prefixed with '--dist-' and are pulled |
| 86 |
out before running 'git <raw-git-command> [git arguments]' in each local git |
| 87 |
repo that is processed (see --dist-help=repo-selection-and-setup). |
| 88 |
""" |
| 89 |
|
| 90 |
|
| 91 |
overviewHelp = r""" |
| 92 |
OVERVIEW: |
| 93 |
|
| 94 |
Running: |
| 95 |
|
| 96 |
$ gitdist [gitdist options] <raw-git-command> [git arguments] |
| 97 |
|
| 98 |
will distribute git commands specified by '<raw-git-command> [git arguments]' |
| 99 |
across the current base git repo and the set of git repos listed in the file |
| 100 |
./.gitdist (or the file ./.gitdist.default, or the argument |
| 101 |
--dist-repos=<repo0>,<repo1>,..., see |
| 102 |
--dist-help=repo-selection-and-setup). |
| 103 |
|
| 104 |
For example, consider the following base git repo 'BaseRepo' with three other |
| 105 |
"extra" git repos cloned under it: |
| 106 |
|
| 107 |
BaseRepo/ |
| 108 |
.git/ |
| 109 |
.gitdist |
| 110 |
ExtraRepo1/ |
| 111 |
.git/ |
| 112 |
ExtraRepo2/ |
| 113 |
.git/ |
| 114 |
ExtraRepo3/ |
| 115 |
.git/ |
| 116 |
|
| 117 |
The file .gitdist shown above is created by the user and in this example |
| 118 |
should have the contents (note the base repo entry '.'): |
| 119 |
|
| 120 |
. |
| 121 |
ExtraRepo1 |
| 122 |
ExtraRepo1/ExtraRepo2 |
| 123 |
ExtraRepo3 |
| 124 |
|
| 125 |
For this example, running the command: |
| 126 |
|
| 127 |
$ cd BaseRepo/ |
| 128 |
$ gitdist status |
| 129 |
|
| 130 |
results in the following commands: |
| 131 |
|
| 132 |
$ git status |
| 133 |
$ cd ExtraRepo1/ ; git status ; .. |
| 134 |
$ cd ExtraRepo1/ExtraRepo2/ ; git status ; ../.. |
| 135 |
$ cd ExtraRepo3/ ; git status ; .. |
| 136 |
|
| 137 |
which produces output like: |
| 138 |
|
| 139 |
*** Base Git Repo: BaseRepo |
| 140 |
On branch master |
| 141 |
Your branch is up-to-date with 'origin/master'. |
| 142 |
nothing to commit, working directory clean |
| 143 |
|
| 144 |
*** Git Repo: ExtraRepo1 |
| 145 |
On branch master |
| 146 |
Your branch is up-to-date with 'origin/master'. |
| 147 |
nothing to commit, working directory clean |
| 148 |
|
| 149 |
*** Git Repo: ExtraRepo1/ExtraRepo2 |
| 150 |
On branch master |
| 151 |
Your branch is up-to-date with 'origin/master'. |
| 152 |
nothing to commit, working directory clean |
| 153 |
|
| 154 |
*** Git Repo: ExtraRepo3 |
| 155 |
On branch master |
| 156 |
Your branch is up-to-date with 'origin/master'. |
| 157 |
nothing to commit, working directory clean |
| 158 |
|
| 159 |
The gitdist tool allows managing a set of git repos like one big integrated |
| 160 |
git repo. For example, after cloning a set of git repos, one can perform |
| 161 |
basic operations like for single git repos such as creating a new release |
| 162 |
branch and pushing it with: |
| 163 |
|
| 164 |
$ gitdist checkout master |
| 165 |
$ gitdist pull |
| 166 |
$ gitdist tag -a -m "Start of the 2.3 release" release-2.3-start |
| 167 |
$ gitdist checkout -b release-2.3 release-2.3-start |
| 168 |
$ gitdist push origin release-2.3-start |
| 169 |
$ gitdist push origin -u release 2.3 |
| 170 |
$ gitdist checkout master |
| 171 |
|
| 172 |
The above gitdist commands create the same tag 'release-2.3-start' and the |
| 173 |
same branch 'release-2.3' in all of the local git repos and pushes these to |
| 174 |
the remote 'origin' for each git repo. |
| 175 |
|
| 176 |
For more information about a certain topic, use '--dist-help=<topic-name> |
| 177 |
[--help]' for <topic-name>: |
| 178 |
|
| 179 |
"""+getHelpTopicsStr()+r""" |
| 180 |
To see full help with all topics, use '--dist-help=all [--help]'. |
| 181 |
|
| 182 |
This script is self-contained and has no dependencies other than standard |
| 183 |
python 2.6+ packages so it can be copied to anywhere and used. |
| 184 |
""" |
| 185 |
helpTopicsDict.update( { 'overview' : overviewHelp } ) |
| 186 |
|
| 187 |
|
| 188 |
repoSelectionAndSetupHelp = r""" |
| 189 |
REPO SELECTION AND SETUP: |
| 190 |
|
| 191 |
Before using the gitdist tool, one must first add the gitdist script to one's |
| 192 |
default path. On bash, the simplest way to do this is to source the |
| 193 |
gitdist-setup.py script: |
| 194 |
|
| 195 |
$ source <some-base-dir>/TriBITS/tribits/python_utils/gitdist-setup.sh |
| 196 |
|
| 197 |
This will set an alias to the gitdist script in that same directory by |
| 198 |
default, will set up useful alias 'gitdist-status', 'gitdist-mod', and |
| 199 |
'gitdist-mod-status', and 'gitdist-repo-versions', and will set up |
| 200 |
command-line completion just like for raw git (assuming that |
| 201 |
git-completion.bash has been sourced first). The files 'gitdist' and |
| 202 |
'gitdist-setup.sh' can also be copied to another directory (e.g. ~/bin) and |
| 203 |
then 'gitdist-setup.sh' can be sourced from there (as a simple "install"): |
| 204 |
|
| 205 |
$ cp <some-base-dir>/TriBITS/tribits/python_utils/gitdist \ |
| 206 |
<some-base-dir>/TriBITS/tribits/python_utils/gitdist-setup.sh \ |
| 207 |
~/bin/ |
| 208 |
$ source ~/bin/gitdist-setup.sh |
| 209 |
$ export PATH=$HOME/bin:$PATH |
| 210 |
|
| 211 |
This script can also be set up manually, for example, by copying the gitdist |
| 212 |
script to one's ~/bin/ directory: |
| 213 |
|
| 214 |
$ cp <some-base-dir>/TriBITS/tribits/python_utils/gitdist ~/bin/ |
| 215 |
$ chmod a+x ~/bin/gitdist |
| 216 |
|
| 217 |
and then adding $HOME/bin to one's 'PATH' env var with: |
| 218 |
|
| 219 |
$ export PATH=$HOME/bin:$PATH |
| 220 |
|
| 221 |
(i.e. in one's ~/.bash_profile file). Then, one will want to set up some |
| 222 |
useful shell aliases like 'gitdist-status', 'gitdist-mod', and |
| 223 |
'gitdist-mod-status' and 'gitdist-repo-versions' (see --dist-help=aliases). |
| 224 |
|
| 225 |
The set of git repos processed by gitdist is determined by the argument: |
| 226 |
|
| 227 |
--dist-repos=<repo0>,<repo1>,... |
| 228 |
|
| 229 |
or the files .gitdist or .gitdist.default. If --dist-repos="", then the list |
| 230 |
of repos to process will be read from the file '.gitdist' in the current |
| 231 |
directory. If the file '.gitdist' does not exist, then the list of repos to |
| 232 |
process will be read from the file '.gitdist.default' in the current |
| 233 |
directory. The format of this files '.gitdist' and '.gitdist.default' is to |
| 234 |
have one repo relative directory per line, for example: |
| 235 |
|
| 236 |
$ cat .gitdist |
| 237 |
. |
| 238 |
ExtraRepo1 |
| 239 |
ExtraRepo1/ExtraRepo2 |
| 240 |
ExtraRepo3 |
| 241 |
|
| 242 |
where each line is the relative path under the base git repo (i.e. under |
| 243 |
'BaseRepo/'). The file .gitdist.default is meant to be committed to the base |
| 244 |
git repo (i.e. 'BaseRepo') so that gitdist is ready to use right away after |
| 245 |
the base repo and the extra repos are cloned. |
| 246 |
|
| 247 |
If an extra repository directory (i.e. listed in |
| 248 |
--dist-repos=<repo0>,<repo1>,..., .gitdist, or .gitdist.default) does |
| 249 |
not exist, then it will be ignored by the script. Therefore, be careful to |
| 250 |
manually verify that the script recognizes the repositories that you list. |
| 251 |
The best way to do that is to run 'gitdist-status' and see which repos are |
| 252 |
listed. |
| 253 |
|
| 254 |
Certain git repos can also be selectively excluded using the option |
| 255 |
'--dist-not-repos=<repox>,<repoy>,...'. |
| 256 |
|
| 257 |
Setting up to use gitdist on a specific set of local git repos first requires |
| 258 |
cloning and organizing the local git repo. For the example listed here, one |
| 259 |
would clone the base repo 'BaseRepo' and the three extra git repos, set up a |
| 260 |
.gitdist file, and then add ignores for the extra cloned repos like: |
| 261 |
|
| 262 |
# A) Clone and organize the git repos |
| 263 |
$ git clone [email protected]:BaseRepo.git |
| 264 |
$ cd BaseRepo/ |
| 265 |
$ git clone [email protected]:ExtraRepo1.git |
| 266 |
$ cd ExtraRepo1/ |
| 267 |
$ git clone [email protected]:ExtraRepo2.git |
| 268 |
$ cd .. |
| 269 |
$ git clone [email protected]:ExtraRepo3.git |
| 270 |
|
| 271 |
# B) Create .gitdist |
| 272 |
$ echo . > .gitdist |
| 273 |
$ echo ExtraRepo1 >> .gitdist |
| 274 |
$ echo ExtraRepo1/ExtraRepo2 >> .gitdist |
| 275 |
$ echo ExtraRepo3 >> .gitdist |
| 276 |
|
| 277 |
# C) Add ignores in base repo |
| 278 |
$ echo /ExtraRepo1/ >> .git/info/exclude |
| 279 |
$ echo /ExtraRepo3/ >> .git/info/exclude |
| 280 |
|
| 281 |
# D) Add ignore in nested extra repo |
| 282 |
$ echo /ExtraRepo2/ >> ExtraRepo1/.git/info/exclude |
| 283 |
|
| 284 |
(Note that one may instead add the above ignores to the version-controlled |
| 285 |
files BaseRepo/.gitignore and ExtraRepo1/.gitignore.) |
| 286 |
|
| 287 |
This produces the local repo structure: |
| 288 |
|
| 289 |
BaseRepo/ |
| 290 |
.git/ |
| 291 |
.gitdist |
| 292 |
ExtraRepo1/ |
| 293 |
.git/ |
| 294 |
ExtraRepo2/ |
| 295 |
.git/ |
| 296 |
ExtraRepo3/ |
| 297 |
.git/ |
| 298 |
|
| 299 |
After this setup, running: |
| 300 |
|
| 301 |
$ gitdist <raw-git-command> [git arguments] |
| 302 |
|
| 303 |
in the 'BaseRepo/ 'directory will automatically distribute a given command |
| 304 |
across the base repo 'BaseRepo/ and the extra repos ExtraRepo1/, |
| 305 |
ExtraRepo1/ExtraRepo2/, and ExtraRepo3/, in that order. |
| 306 |
|
| 307 |
To simplify the setup for the usage of gitdist for a given set of local git |
| 308 |
repos, one may choose to instead create the file .gitdist.default in the base |
| 309 |
repo (i.e. `BaseRepo/`') and add the ignores for the extra repos to the |
| 310 |
.gitignore files and commit the files to the repo(s). That way, one does not |
| 311 |
have to manually do any extra setup for every new set of local clones of the |
| 312 |
repos. But if the file .gitdist is present, then it will override the file |
| 313 |
.gitdist.default as described above (which allows customization of what git |
| 314 |
repos are processed at any time). |
| 315 |
""" |
| 316 |
helpTopicsDict.update( { 'repo-selection-and-setup' : repoSelectionAndSetupHelp } ) |
| 317 |
|
| 318 |
|
| 319 |
distRepoStatusHelp = r""" |
| 320 |
SUMMARY OF REPO STATUS: |
| 321 |
|
| 322 |
The script gitdist also supports the special command 'dist-repo-status' which |
| 323 |
prints a compact table showing the current status of all the repos (see alias |
| 324 |
'gitdist-status' in --dist-help=aliases). For the example set of repos shown |
| 325 |
in OVERVIEW (see --dist-help=overview), running: |
| 326 |
|
| 327 |
$ gitdist dist-repo-status # alias 'gitdist-status' |
| 328 |
|
| 329 |
outputs a table like: |
| 330 |
|
| 331 |
---------------------------------------------------------------------- |
| 332 |
| ID | Repo Dir | Branch | Tracking Branch | C | M | ? | |
| 333 |
|----|-----------------------|--------|-----------------|---|----|---| |
| 334 |
| 0 | BaseRepo (Base) | dummy | | | | | |
| 335 |
| 1 | ExtraRepo1 | master | origin/master | 1 | 2 | | |
| 336 |
| 2 | ExtraRepo1/ExtraRepo2 | abc123 | | | 25 | 4 | |
| 337 |
| 3 | ExtraRepo3 | master | origin/master | | | | |
| 338 |
---------------------------------------------------------------------- |
| 339 |
|
| 340 |
If the option --dist-legend is also passed in, the output will include: |
| 341 |
|
| 342 |
"""+distRepoStatusLegend+\ |
| 343 |
r""" |
| 344 |
|
| 345 |
In the case of a detached head state, as shown above with the repo |
| 346 |
'ExtraRepo3', the SHA1 (e.g. 'abc123') was printed instead of 'HEAD'. |
| 347 |
However, if the repo is in the detached head state but a tag happens to point |
| 348 |
to the current commit (e.g. 'git tag --points-at' returns non-empy), then the |
| 349 |
tag name (e.g. 'v1.2.3') is printed instead of the SHA1 of the commit. |
| 350 |
|
| 351 |
One can also show the status of only changed repos with the command: |
| 352 |
|
| 353 |
$ gitdist dist-repo-status --dist-mod-only # alias 'gitdist-mod-status' |
| 354 |
|
| 355 |
which produces a table like: |
| 356 |
|
| 357 |
---------------------------------------------------------------------- |
| 358 |
| ID | Repo Dir | Branch | Tracking Branch | C | M | ? | |
| 359 |
|----|-----------------------|--------|-----------------|---|----|---| |
| 360 |
| 1 | ExtraRepo1 | master | origin/master | 1 | 2 | | |
| 361 |
| 2 | ExtraRepo1/ExtraRepo2 | abc123 | | | 25 | 4 | |
| 362 |
---------------------------------------------------------------------- |
| 363 |
|
| 364 |
(see the alias 'gitdist-mod-status' in --dist-help=aliases). |
| 365 |
|
| 366 |
Note that rows for the repos BaseRepo and ExtraRepo2 were left out but the |
| 367 |
repo indexes for the remaining repos are preserved. This allows one to |
| 368 |
compactly show the status of the changed local repos even when there are many |
| 369 |
local git repos by filtering out rows for repos that have no changes |
| 370 |
w.r.t. their tracking branches. This allows one to get the status on a few |
| 371 |
repos with changes out of a large number of local repos (i.e. 10s and even |
| 372 |
100s of local git repos). |
| 373 |
""" |
| 374 |
helpTopicsDict.update( { 'dist-repo-status' : distRepoStatusHelp } ) |
| 375 |
|
| 376 |
|
| 377 |
repoVersionFilesHelp = r""" |
| 378 |
REPO VERSION FILES: |
| 379 |
|
| 380 |
The script gitdist also supports the options --dist-version-file=<versionFile> |
| 381 |
and --dist-version-file2=<versionFile2> which are used to provide different |
| 382 |
SHA1 versions for each local git repo. Each of these version files is |
| 383 |
expected to represent a compatible set of versions of the repos (e.g. in the |
| 384 |
same style as .gitmodule files used by the 'git submodule' command). |
| 385 |
|
| 386 |
The format of these repo version files is shown in the following example: |
| 387 |
|
| 388 |
*** Base Git Repo: BaseRepo |
| 389 |
e102e27 [Mon Sep 23 11:34:59 2013 -0400] <[email protected]> |
| 390 |
First summary message |
| 391 |
*** Git Repo: ExtraRepo1 |
| 392 |
b894b9c [Fri Aug 30 09:55:07 2013 -0400] <[email protected]> |
| 393 |
Second summary message |
| 394 |
*** Git Repo: ExtraRepo1/ExtraRepo2 |
| 395 |
97cf1ac [Thu Dec 1 23:34:06 2011 -0500] <[email protected]> |
| 396 |
Third summary message |
| 397 |
*** Git Repo: ExtraRepo3 |
| 398 |
6facf33 [Fri May 6 15:28:35 2013 -0400] <[email protected]> |
| 399 |
Fourth summary message |
| 400 |
|
| 401 |
Each repository entry can have a summary message or not (i.e. use two or three |
| 402 |
lines per repo in the file). A compatible repo version file can be generated |
| 403 |
with this script listing three lines per repo (e.g. as shown above) using (for |
| 404 |
example): |
| 405 |
|
| 406 |
$ gitdist --dist-no-color log --color=never -1 --pretty=format:"%h [%ad] <%ae>%n%s" \ |
| 407 |
| grep -v "^$" &> RepoVersion.txt |
| 408 |
|
| 409 |
(which is defined as the alias 'gitdist-repo-versions' in the file |
| 410 |
'gitdist-setup.sh') or two lines per repo using (for example): |
| 411 |
|
| 412 |
$ gitdist --dist-no-color log --color=never -1 --pretty=format:"%h [%ad] <%ae>" \ |
| 413 |
| grep -v "^$" &> RepoVersion.txt |
| 414 |
|
| 415 |
This allows checking out consistent versions of the set git repos, diffing two |
| 416 |
consistent versions of the set of git repos, etc. |
| 417 |
|
| 418 |
To checkout an older set of consistent versions of the set of repos |
| 419 |
represented by the set of versions given in a file RepoVersion.<date>.txt, |
| 420 |
use: |
| 421 |
|
| 422 |
$ gitdist fetch origin |
| 423 |
$ gitdist --dist-version-file=RepoVersion.<date>.txt checkout _VERSION_ |
| 424 |
|
| 425 |
The string '_VERSION_' is replaced with the SHA1 for each of the repos listed |
| 426 |
in the file 'RepoVersion.<date>.txt'. (NOTE: this puts the repos into a |
| 427 |
detached head state so one has to know what that means.) |
| 428 |
|
| 429 |
To tag a set of repos using a consistent set of versions, use (for example): |
| 430 |
|
| 431 |
$ gitdist --dist-version-file=RepoVersion.<date>.txt \ |
| 432 |
tag -a -m "<message>" <some_tag> _VERSION_ |
| 433 |
|
| 434 |
To create a branch off of a consistent set of versions, use (for example): |
| 435 |
|
| 436 |
$ gitdist --dist-version-file=RepoVersion.<date>.txt \ |
| 437 |
checkout -b some-branch _VERSION_ |
| 438 |
|
| 439 |
To diff two sets of versions of the repos, use (for example): |
| 440 |
|
| 441 |
$ gitdist \ |
| 442 |
--dist-version-file=RepoVersion.<new-date>.txt \ |
| 443 |
--dist-version-file2=RepoVersion.<old-date>.txt \ |
| 444 |
diff _VERSION_ ^_VERSION2_ |
| 445 |
|
| 446 |
Here, _VERSION_ is replaced by the SHA1s listed in the file |
| 447 |
'RepoVersion.<new-date>.txt' and _VERSION2_ is replaced by the SHA1s listed in |
| 448 |
'RepoVersion.<old-date>.txt'. |
| 449 |
|
| 450 |
One can construct any git command taking one or two different repo version |
| 451 |
arguments (SHA1s) using this approach (which covers a huge number of different |
| 452 |
git operations). |
| 453 |
|
| 454 |
Note that the set of git repos listed in the 'RepoVersion.txt' file must be a |
| 455 |
super-set of those processed by this script or an error will occur and the |
| 456 |
script will abort (before running any git commands). If there are additional |
| 457 |
repos RepoX, RepoY, etc. not listed in the 'RepVersion'.txt file, then one can |
| 458 |
exclude them with: |
| 459 |
|
| 460 |
$ gitdist --dist-not-repos=RepoX,RepoY,... \ |
| 461 |
--dist-version-file=RepoVersion.txt \ |
| 462 |
<raw-git-comand> [git arguments] |
| 463 |
""" |
| 464 |
helpTopicsDict.update( { 'repo-versions' : repoVersionFilesHelp } ) |
| 465 |
|
| 466 |
|
| 467 |
distRepoVersionsTableHelp = r""" |
| 468 |
REPO VERSION TABLE: |
| 469 |
|
| 470 |
The script gitdist also supports the special command |
| 471 |
'dist-repo-versions-table', which prints a Markdown-formatted table of |
| 472 |
repositories and corresponding commit information for easy inclusion in an |
| 473 |
issue tracking system. For instance, running: |
| 474 |
|
| 475 |
$ gitdist dist-repo-versions-table |
| 476 |
|
| 477 |
outputs a table like: |
| 478 |
|
| 479 |
| Repository | SHA1 | Commit Date | Author | Summary | |
| 480 |
|:-------------- |:-------:|:------------------- |:---------------------- |:---------------------------------------------- | |
| 481 |
| MockProjectDir | e2dc488 | 2019-10-23 10:16:07 | [email protected] | Merge Pull Request #1234 from user/repo/branch | |
| 482 |
| ExtraRepo1 | f671414 | 2019-10-22 11:18:47 | [email protected] | Fixed a Bug | |
| 483 |
| ExtraRepo2 | 50bbf3e | 2019-10-17 16:32:15 | [email protected] | Did Some Work | |
| 484 |
|
| 485 |
If the option --dist-short is also passed in, the output will be limited to: |
| 486 |
|
| 487 |
| Repository | SHA1 | |
| 488 |
|:-------------- |:-------:| |
| 489 |
| MockProjectDir | e2dc488 | |
| 490 |
| ExtraRepo1 | f671414 | |
| 491 |
| ExtraRepo2 | 50bbf3e | |
| 492 |
""" |
| 493 |
helpTopicsDict.update( { 'dist-repo-versions-table' : distRepoVersionsTableHelp } ) |
| 494 |
|
| 495 |
|
| 496 |
usefulAliasesHelp =r""" |
| 497 |
USEFUL ALIASES: |
| 498 |
|
| 499 |
A few very useful (bash) shell aliases and setup commands to use with gitdist |
| 500 |
include: |
| 501 |
|
| 502 |
$ alias gitdist-status="gitdist dist-repo-status" |
| 503 |
$ alias gitdist-mod="gitdist --dist-mod-only" |
| 504 |
$ alias gitdist-mod-status="gitdist dist-repo-status --dist-mod-only" |
| 505 |
$ alias gitdist-repo-versions="gitdist --dist-no-color log --color=never -1 \ |
| 506 |
--pretty=format:\"%h [%ad] <%ae>%n%s\" | grep -v \"^$\"" |
| 507 |
|
| 508 |
These are added by sourcing the provided file 'gitdist-setup.sh' (which should |
| 509 |
be sourced in your ~/.bash_profile file.) which also adds some useful |
| 510 |
commandline tab completions. |
| 511 |
|
| 512 |
This avoids lots of extra typing as these gitdist arguments are used a lot. |
| 513 |
For example, to see the compact status table of all your local git repos, do: |
| 514 |
|
| 515 |
$ gitdist-status |
| 516 |
|
| 517 |
To just see a compact status table of only changed repos, do: |
| 518 |
|
| 519 |
$ gitdist-mod-status |
| 520 |
|
| 521 |
To process only repos that have changes and see commits in these repos |
| 522 |
w.r.t. their tracking branches, do (for example): |
| 523 |
|
| 524 |
$ gitdist-mod log --name-status HEAD ^@{u} |
| 525 |
|
| 526 |
or |
| 527 |
|
| 528 |
$ gitdist-mod local-stat |
| 529 |
|
| 530 |
(where 'local-stat' is a useful git alias defined in the script |
| 531 |
'git-config-alias.sh' which adds these to your ~/.gitconf file). |
| 532 |
""" |
| 533 |
helpTopicsDict.update( { 'aliases' : usefulAliasesHelp } ) |
| 534 |
|
| 535 |
defaultBranchHelp = r""" |
| 536 |
DEFAULT BRANCH SPECIFICATION: |
| 537 |
|
| 538 |
When using any git command that accepts a reference (a SHA1, or branch or tag |
| 539 |
name), it is possible to use _DEFAULT_BRANCH_ instead. For instance, |
| 540 |
|
| 541 |
gitdist checkout _DEFAULT_BRANCH_ |
| 542 |
|
| 543 |
will check out the default development branch in each repository being managed |
| 544 |
by gitdist. You can specify the default branch for each repository in your |
| 545 |
.gitdist[.default] file. For instance, if your .gitdist file contains |
| 546 |
|
| 547 |
. master |
| 548 |
extraRepo1 develop |
| 549 |
extraRepo2 app-devel |
| 550 |
|
| 551 |
then the command above would check out 'master' in the base repo, 'develop' in |
| 552 |
extraRepo1, and 'app-devel' in extraRepo2. This makes it convenient when |
| 553 |
working with multiple repositories that have different names for their main |
| 554 |
development branches. For instance, you can do a topic branch workflow like: |
| 555 |
|
| 556 |
gitdist checkout _DEFAULT_BRANCH_ |
| 557 |
gitdist pull |
| 558 |
gitdist checkout -b newFeatureBranch |
| 559 |
<create some commits> |
| 560 |
gitdist fetch |
| 561 |
gitdist merge origin/_DEFAULT_BRANCH_ |
| 562 |
<create some commits> |
| 563 |
gitdist checkout _DEFAULT_BRANCH_ |
| 564 |
gitdist pull |
| 565 |
gitdist merge newFeatureBranch |
| 566 |
|
| 567 |
and not worry about this 'newFeatureBranch' being off of 'master' in the root |
| 568 |
repo, off of 'develop' in extraRepo1, and off of 'app-devel' in extraRepo2. |
| 569 |
|
| 570 |
If no branch name is specified for any given repository in the |
| 571 |
.gitdist[.default] file, then 'master' is assumed. |
| 572 |
""" |
| 573 |
helpTopicsDict.update( { 'default-branch' : defaultBranchHelp } ) |
| 574 |
|
| 575 |
|
| 576 |
moveToBaseDirHelp = r""" |
| 577 |
MOVE TO BASE DIRECTORY: |
| 578 |
|
| 579 |
By default, when you run gitdist, it will look in your current working |
| 580 |
directory for a .gitdist[.default] file. If it fails to find one, it will |
| 581 |
treat the current directory as the base git repository (as if there was a |
| 582 |
.gitdist file in it, having a single line with only "." in it) and then run as |
| 583 |
usual. You have the ability to change this behavior by setting the |
| 584 |
GITDIST_MOVE_TO_BASE_DIR environment variable. |
| 585 |
|
| 586 |
To describe the behavior for the differ net options, consider the following set |
| 587 |
of nested git repositories and directories: |
| 588 |
|
| 589 |
BaseRepo/ |
| 590 |
.git |
| 591 |
.gitdist |
| 592 |
... |
| 593 |
ExtraRepo/ |
| 594 |
.git |
| 595 |
.gitdist |
| 596 |
... |
| 597 |
path/ |
| 598 |
... |
| 599 |
to/ |
| 600 |
... |
| 601 |
some/ |
| 602 |
... |
| 603 |
directory/ |
| 604 |
... |
| 605 |
|
| 606 |
|
| 607 |
The valid settings for GITDIST_MOVE_TO_BASE_DIR include: |
| 608 |
|
| 609 |
"" (Empty) |
| 610 |
|
| 611 |
This gives the default behavior where gitdist runs in the current working |
| 612 |
directory. |
| 613 |
|
| 614 |
IMMEDIATE_BASE |
| 615 |
|
| 616 |
In this case, gitdist will start moving up the directory tree until it |
| 617 |
finds a .gitdist[.default] file, and then run in the directory where it |
| 618 |
finds it. In the above example, if you are in |
| 619 |
BaseRepo/ExtraRepo/path/to/some/directory/ when you run gitdist, it will |
| 620 |
move up to ExtraRepo to execute the command you give it from there. |
| 621 |
|
| 622 |
EXTREME_BASE: |
| 623 |
|
| 624 |
In this case, gitdist will continue moving up the directory tree until it |
| 625 |
finds the outer-most repository containing a .gitdist[.default] file, and |
| 626 |
then run in that directory. Given the directory tree above, if you were |
| 627 |
in BaseRepo/ExtraRepo/path/to/some/directory, it will move up to BaseRepo |
| 628 |
to execute the command you give it. |
| 629 |
|
| 630 |
With either of the settings above, when gitdist is finished running, it will |
| 631 |
leave you in the same directory you were in when you executed command in the |
| 632 |
first place. Additionally, if no .gitdist[.default] file can be found, gitdist |
| 633 |
will execute the command you give it in your current working directory, as if |
| 634 |
GITDIST_MOVE_TO_BASE_DIR hadn't been set. |
| 635 |
""" |
| 636 |
helpTopicsDict.update( { 'move-to-base-dir' : moveToBaseDirHelp } ) |
| 637 |
|
| 638 |
usageTipsHelp = r""" |
| 639 |
USAGE TIPS: |
| 640 |
|
| 641 |
Since gitdist allows treating a set of git repos as one big git repo, almost |
| 642 |
any git workflow that is used for a single git repo can be used for a set of |
| 643 |
repos using gitdist. The main difference is that one will typically need to |
| 644 |
create commits individually for each repo. Also, pulls and pushes are no |
| 645 |
longer atomic like is guaranteed for a single git repo. |
| 646 |
|
| 647 |
In general, the mapping between the commands for a single-repo git workflow |
| 648 |
using raw git vs. a multi-repo git workflow using gitdist (using the shell |
| 649 |
aliases 'gitdist-status', 'gitdist-mod-status', and 'gitdist-mod'; see |
| 650 |
--dist-help=aliases) is given by: |
| 651 |
|
| 652 |
git pull => gitdist pull |
| 653 |
git checkout -b <branch> [<ref>] => gitdist checkout -b <branch> [<ref>] |
| 654 |
git checkout <branch> => gitdist checkout <branch> |
| 655 |
git tag -a -m "<message>" <tag> => gitdist tag -a -m "<message>" <tag> |
| 656 |
git status => gitdist-mod status # status details |
| 657 |
=> gitdist-status # table for all |
| 658 |
=> gitdist-mod-status # table for mod. |
| 659 |
git commit => gitdist-mod commit |
| 660 |
git log HEAD ^@{u} => gitdist-mod log HEAD ^@{u} |
| 661 |
git push => gitdist-mod push |
| 662 |
git push [-u] <remote> <branch> => gitdist push [-u] <remote> <branch> |
| 663 |
git push <remote> <tag> => gitdist push <remote> <tag> |
| 664 |
|
| 665 |
NOTE: The usage of 'gitdist-mod' can be replaced with just 'gitdist' in all of |
| 666 |
the above commands. It is just that in these cases gitdist-mod produces more |
| 667 |
compact output and avoids do-nothing commands for repos that have no changes |
| 668 |
with respect to their tracking branch. But when it doubt, just use raw |
| 669 |
'gitdist' if you are not sure. |
| 670 |
|
| 671 |
A typical development iteration of the centralized workflow using using |
| 672 |
multiple git repos looks like the following: |
| 673 |
|
| 674 |
1) Update the local branches from the remote tracking branches: |
| 675 |
|
| 676 |
$ cd BaseRepo/ |
| 677 |
$ gitdist pull |
| 678 |
|
| 679 |
2) Make local modifications for each repo: |
| 680 |
|
| 681 |
$ emacs <base-files> |
| 682 |
$ cd ExtraRepo1/ |
| 683 |
$ emacs <files-in-extra-repo1> |
| 684 |
$ cd .. |
| 685 |
$ cd ExtraRepo1/ExtraRepo2/ |
| 686 |
$ emacs <files-in-extra-repo2> |
| 687 |
$ cd ../.. |
| 688 |
$ cd ExtraRepo3/ |
| 689 |
$ emacs <files-in-extra-repo3> |
| 690 |
$ cd .. |
| 691 |
|
| 692 |
3) Build and test local modifications: |
| 693 |
|
| 694 |
$ cd BUILD/ |
| 695 |
$ make -j16 |
| 696 |
$ make test # hopefully all pass! |
| 697 |
$ cd .. |
| 698 |
|
| 699 |
4) View the modifications before committing: |
| 700 |
|
| 701 |
$ gitdist-mod-status # Produces a summary table |
| 702 |
$ gitdist-mod status # See status details |
| 703 |
|
| 704 |
5) Make commits to each repo: |
| 705 |
|
| 706 |
$ gitdist-mod commit -a # Opens editor for each repo in order |
| 707 |
|
| 708 |
or use the same commit message for all repos: |
| 709 |
|
| 710 |
$ emacs commitmsg.txt |
| 711 |
$ echo /commitmsg.txt >> .git/info/exclude |
| 712 |
$ gitdist-mod commit -a -F $PWD/commitmsg.txt |
| 713 |
|
| 714 |
or manually create the commits in each repo separately with raw git: |
| 715 |
|
| 716 |
$ cd BaseRepo/ |
| 717 |
$ git commit -a |
| 718 |
$ cd ExtraRepo1/ |
| 719 |
$ git commit -a |
| 720 |
$ cd .. |
| 721 |
$ cd ExtraRepo1/ExtraRepo2/ |
| 722 |
$ git commit -a |
| 723 |
$ cd ../.. |
| 724 |
$ cd ExtraRepo3/ |
| 725 |
$ git commit -a |
| 726 |
$ cd .. |
| 727 |
|
| 728 |
6) Examine the local commits that are about to be pushed: |
| 729 |
|
| 730 |
$ gitdist-mod-status # Should be no unmodified or untracked files! |
| 731 |
$ gitdist-mod log --name-status HEAD ^@{u} # or ... |
| 732 |
$ gitdist-mod local-stat # alias defined in 'git-config-alias.sh' |
| 733 |
|
| 734 |
7) Rebase and push local commits to remote tracking branch: |
| 735 |
|
| 736 |
$ gitdist pull --rebase |
| 737 |
$ gitdist-mod push |
| 738 |
$ gitdist-mod-status # Make sure all the pushes occurred! |
| 739 |
|
| 740 |
Another example workflow is creating a new release branch as shown in the |
| 741 |
OVERVIEW section (--dist-help=overview). |
| 742 |
|
| 743 |
Other usage tips: |
| 744 |
|
| 745 |
- 'gitdist --help' will run gitdist help, not git help. If you want raw git |
| 746 |
help, then run 'git --help'. |
| 747 |
|
| 748 |
- Be sure to run 'gitdist-status' to make sure that each repo is on the |
| 749 |
correct local branch and is tracking the correct remote branch. |
| 750 |
|
| 751 |
- In general, for most workflows, one should use the same local branch name, |
| 752 |
remote repo name, and remote tracking branch name in each local git repo. |
| 753 |
That allows commands like 'gitdist checkout --track <remote>/<branch>' and |
| 754 |
'gitdist checkout <branch>' to work correctly. |
| 755 |
|
| 756 |
- For many git commands, it is better to process only repos that are changed |
| 757 |
w.r.t. their tracking branch with 'gitdist-mod <raw-git-command> [git |
| 758 |
arguments]'. For example, to see the status of only changed repos use |
| 759 |
'gitdist-mod status'. This allows the usage of gitdist to scale well when |
| 760 |
there are even 100s of git repos. |
| 761 |
|
| 762 |
- As an exception to the last item, a few different types of git commands |
| 763 |
tend to be run on all the git repos like 'gitdist pull', 'gitdist |
| 764 |
checkout', and 'gitdist tag'. |
| 765 |
|
| 766 |
- If one is not sure whether to run 'gitdist' or 'gitdist-mod', then just |
| 767 |
run 'gitdist' to be safe. |
| 768 |
""" |
| 769 |
helpTopicsDict.update( { 'usage-tips' : usageTipsHelp } ) |
| 770 |
|
| 771 |
|
| 772 |
scriptDependenciesHelp = r""" |
| 773 |
SCRIPT DEPENDENCIES: |
| 774 |
|
| 775 |
The Python script gitdist only depends on the Python 2.6+ standard modules |
| 776 |
'sys', 'os', 'subprocess', and 're'. Also, of course, it requires some |
| 777 |
compatible version of 'git' in your path (but gitdist works with several |
| 778 |
versions of git starting as far back as git 1.6+). |
| 779 |
""" |
| 780 |
helpTopicsDict.update( { 'script-dependencies' : scriptDependenciesHelp } ) |
| 781 |
|
| 782 |
|
| 783 |
# |
| 784 |
# Functions to help Format a table |
| 785 |
# |
| 786 |
|
| 787 |
|
| 788 |
# Shrink a string to a given width by inserting an ellipsis (...) in the |
| 789 |
# middle. |
| 790 |
def shrinkString(string, width): |
| 791 |
if len(string) > width: |
| 792 |
start = int(width//2) - 1 |
| 793 |
stop = width - start - 3 |
| 794 |
return string[:start] + "..." + string[-stop:] |
| 795 |
else: |
| 796 |
return string |
| 797 |
|
| 798 |
|
| 799 |
# Fill in a field |
| 800 |
def getTableField(field, width, just): |
| 801 |
if just == "R": |
| 802 |
return field.rjust(width) |
| 803 |
return field.ljust(width) |
| 804 |
|
| 805 |
|
| 806 |
# Format an ASCII/UTF-8 table from a set of fields. |
| 807 |
# |
| 808 |
# The format is of tableData input is: |
| 809 |
# |
| 810 |
# [ { "label":"<label0>:, "align":"<align0>, "fields":[<fld00>, ... ]}, |
| 811 |
# { "label":"<label1>:, "align":"<align1>, "fields":[<fld10>, ... ]}, |
| 812 |
# ... |
| 813 |
# ] |
| 814 |
# |
| 815 |
# The "align" field is either "R" for right, or "L" for left. |
| 816 |
# |
| 817 |
def createTable(tableData, utf8=False): |
| 818 |
|
| 819 |
# Table size |
| 820 |
numFields = len(tableData) |
| 821 |
numRows = len(tableData[0]["fields"]) |
| 822 |
|
| 823 |
# a) Get the max field width for each column. |
| 824 |
tableFieldWidth = [] |
| 825 |
for fieldDict in tableData: |
| 826 |
label = fieldDict["label"] |
| 827 |
maxFieldWidth = len(label) |
| 828 |
if len(fieldDict["fields"]) != numRows: |
| 829 |
raise Exception("Error: column '"+label+"' numfields = " + \ |
| 830 |
str(len(fieldDict["fields"])) + " != numRows = "+str(numRows)+"\n" ) |
| 831 |
for field in fieldDict["fields"]: |
| 832 |
fieldWidth = len(field) |
| 833 |
if fieldWidth > maxFieldWidth: maxFieldWidth = fieldWidth |
| 834 |
tableFieldWidth.append(maxFieldWidth) |
| 835 |
|
| 836 |
# b) Shrink the dist-repo-status table to fit in the terminal if needed. |
| 837 |
shrink = True |
| 838 |
for fieldDict in tableData: |
| 839 |
label = fieldDict["label"] |
| 840 |
if (label != "ID" and |
| 841 |
label != "Repo Dir" and |
| 842 |
label != "Branch" and |
| 843 |
label != "Tracking Branch" and |
| 844 |
label != "C" and |
| 845 |
label != "M" and |
| 846 |
label != "?"): |
| 847 |
shrink = False |
| 848 |
if shrink: |
| 849 |
try: |
| 850 |
mockSttySize = os.environ.get("GITDIST_UNIT_TEST_STTY_SIZE") |
| 851 |
if mockSttySize: |
| 852 |
sttySize = mockSttySize |
| 853 |
else: |
| 854 |
with os.popen("stty size", "r") as subprocess: |
| 855 |
sttySize = subprocess.read() |
| 856 |
rows, columns = sttySize.split() |
| 857 |
except: |
| 858 |
shrink = False |
| 859 |
if shrink: |
| 860 |
terminalWidth = int(columns) |
| 861 |
numDividers = len(tableData) + 1 |
| 862 |
numSpaces = 2 * len(tableData) |
| 863 |
fullTableWidth = sum(tableFieldWidth) + numDividers + numSpaces |
| 864 |
if fullTableWidth > terminalWidth: |
| 865 |
widthToShrink = sum(tableFieldWidth[1:4]) |
| 866 |
availableWidth = (terminalWidth |
| 867 |
- tableFieldWidth[0] |
| 868 |
- sum(tableFieldWidth[4:]) |
| 869 |
- numDividers |
| 870 |
- numSpaces) |
| 871 |
newWidth = {} |
| 872 |
remainingWidth = availableWidth |
| 873 |
for i in range(1, 3): |
| 874 |
ratio = float(tableFieldWidth[i]) / widthToShrink |
| 875 |
newWidth[i] = int((ratio*availableWidth) // 1) |
| 876 |
remainingWidth = remainingWidth - newWidth[i] |
| 877 |
newWidth[3] = remainingWidth |
| 878 |
for i in range(1, 4): |
| 879 |
if newWidth[i] < len(tableData[i]["label"]): |
| 880 |
shrink = False |
| 881 |
break |
| 882 |
if shrink: |
| 883 |
for i in range(1, 4): |
| 884 |
tableFieldWidth[i] = newWidth[i] |
| 885 |
for j, field in enumerate(tableData[i]["fields"]): |
| 886 |
tableData[i]["fields"][j] = shrinkString(field, tableFieldWidth[i]) |
| 887 |
fullTableWidth = terminalWidth |
| 888 |
|
| 889 |
# c) Write the header of the table (always left-align the column labels). |
| 890 |
table = "┌" if utf8 else "-" |
| 891 |
for index, width in enumerate(tableFieldWidth): |
| 892 |
table += (("─" if utf8 else "-")*(width+2)) |
| 893 |
if index != len(tableFieldWidth)-1: |
| 894 |
table += "┬" if utf8 else "-" |
| 895 |
else: |
| 896 |
table += "┐" if utf8 else "-" |
| 897 |
table += "\n"+("│" if utf8 else "|") |
| 898 |
fieldIdx = 0 |
| 899 |
for fieldDict in tableData: |
| 900 |
table += " " |
| 901 |
table += getTableField(fieldDict["label"], tableFieldWidth[fieldIdx], "L") |
| 902 |
table += " "+("│" if utf8 else "|") |
| 903 |
fieldIdx += 1 |
| 904 |
table += "\n"+("┝" if utf8 else "|") |
| 905 |
for field_i in range(numFields): |
| 906 |
table += (("━" if utf8 else "-")*(tableFieldWidth[field_i]+2)) |
| 907 |
if field_i != numFields-1: |
| 908 |
table += "┿" if utf8 else "|" |
| 909 |
else: |
| 910 |
table += "┥" if utf8 else "|" |
| 911 |
table += "\n" |
| 912 |
|
| 913 |
# d) Write each row of the table |
| 914 |
for row_i in range(numRows): |
| 915 |
table += "│" if utf8 else "|" |
| 916 |
field_i = 0 |
| 917 |
for fieldDict in tableData: |
| 918 |
table += " "+getTableField(fieldDict["fields"][row_i], |
| 919 |
tableFieldWidth[field_i], fieldDict["align"] )+" " |
| 920 |
table += "│" if utf8 else "|" |
| 921 |
field_i += 1 |
| 922 |
table += "\n" |
| 923 |
table += "└" if utf8 else "-" |
| 924 |
for index, width in enumerate(tableFieldWidth): |
| 925 |
table += (("─" if utf8 else "-")*(width+2)) |
| 926 |
if index != len(tableFieldWidth)-1: |
| 927 |
table += "┴" if utf8 else "-" |
| 928 |
else: |
| 929 |
table += "┘" if utf8 else "-" |
| 930 |
table += "\n" |
| 931 |
|
| 932 |
return table |
| 933 |
|
| 934 |
|
| 935 |
# Format a Markdown table from a set of fields. |
| 936 |
# |
| 937 |
# The format of the tableData input is: |
| 938 |
# |
| 939 |
# [ { "label":"<label0>:, "align":"<align0>, "fields":[<fld00>, ... ]}, |
| 940 |
# { "label":"<label1>:, "align":"<align1>, "fields":[<fld10>, ... ]}, |
| 941 |
# ... |
| 942 |
# ] |
| 943 |
# |
| 944 |
# The "align" field is either "R" for right, "C" for center, or "L" for left. |
| 945 |
# |
| 946 |
def createMarkdownTable(tableData): |
| 947 |
|
| 948 |
# Table size |
| 949 |
numFields = len(tableData) |
| 950 |
numRows = len(tableData[0]["fields"]) |
| 951 |
|
| 952 |
# a) Get the max field width for each column. |
| 953 |
tableFieldWidth = [] |
| 954 |
for fieldDict in tableData: |
| 955 |
label = fieldDict["label"] |
| 956 |
maxFieldWidth = len(label) |
| 957 |
if len(fieldDict["fields"]) != numRows: |
| 958 |
raise Exception("Error: column '"+label+"' numfields = " + \ |
| 959 |
str(len(fieldDict["fields"])) + " != numRows = "+str(numRows)+"\n" ) |
| 960 |
for field in fieldDict["fields"]: |
| 961 |
fieldWidth = len(field) |
| 962 |
if fieldWidth > maxFieldWidth: |
| 963 |
maxFieldWidth = fieldWidth |
| 964 |
tableFieldWidth.append(maxFieldWidth) |
| 965 |
|
| 966 |
# b) Write the header of the table. |
| 967 |
table = "|" |
| 968 |
fieldIdx = 0 |
| 969 |
for fieldDict in tableData: |
| 970 |
table += " " |
| 971 |
table += getTableField(fieldDict["label"], tableFieldWidth[fieldIdx], |
| 972 |
fieldDict["align"]) |
| 973 |
table += " |" |
| 974 |
fieldIdx += 1 |
| 975 |
table += "\n|" |
| 976 |
for i, fieldDict in enumerate(tableData): |
| 977 |
if ((fieldDict["align"] == "L") or (fieldDict["align"] == "C")): |
| 978 |
table += ":" |
| 979 |
else: |
| 980 |
table += " " |
| 981 |
table += "-"*tableFieldWidth[i] |
| 982 |
if ((fieldDict["align"] == "C") or (fieldDict["align"] == "R")): |
| 983 |
table += ":" |
| 984 |
else: |
| 985 |
table += " " |
| 986 |
table += "|" |
| 987 |
|
| 988 |
# c) Write each row of the table |
| 989 |
for row_i in range(numRows): |
| 990 |
table += "\n|" |
| 991 |
field_i = 0 |
| 992 |
for fieldDict in tableData: |
| 993 |
table += " "+getTableField(fieldDict["fields"][row_i], |
| 994 |
tableFieldWidth[field_i], fieldDict["align"] )+" |" |
| 995 |
field_i += 1 |
| 996 |
|
| 997 |
return table |
| 998 |
|
| 999 |
|
| 1000 |
# |
| 1001 |
# Helper functions for gitdist |
| 1002 |
# |
| 1003 |
|
| 1004 |
import sys |
| 1005 |
import os |
| 1006 |
import subprocess |
| 1007 |
import re |
| 1008 |
|
| 1009 |
from optparse import OptionParser |
| 1010 |
|
| 1011 |
def addOptionParserChoiceOption( |
| 1012 |
optionName, |
| 1013 |
optionDest, |
| 1014 |
choiceOptions, |
| 1015 |
defaultChoiceIndex, |
| 1016 |
helpStr, |
| 1017 |
optionParser |
| 1018 |
): |
| 1019 |
""" Add a general choice option to a optparse.OptionParser object""" |
| 1020 |
defaultOptionValue = choiceOptions[defaultChoiceIndex] |
| 1021 |
optionParser.add_option( |
| 1022 |
optionName, |
| 1023 |
dest=optionDest, |
| 1024 |
type="choice", |
| 1025 |
choices=choiceOptions, |
| 1026 |
default=defaultOptionValue, |
| 1027 |
help='%s Choices = (\'%s\'). [default = \'%s\']' |
| 1028 |
% (helpStr, '\', \''.join(choiceOptions), defaultOptionValue) |
| 1029 |
) |
| 1030 |
|
| 1031 |
|
| 1032 |
def getDistHelpTopicStr(helpTopicVal): |
| 1033 |
helpTopicStr = "" |
| 1034 |
if helpTopicVal == "": |
| 1035 |
return "" # Don't add any text |
| 1036 |
elif helpTopicVal == "all": |
| 1037 |
for helpTopic in helpTopics: |
| 1038 |
helpTopicStr += helpTopicsDict.get(helpTopic) |
| 1039 |
else: |
| 1040 |
helpTopicHelpStr = helpTopicsDict.get(helpTopicVal, None) |
| 1041 |
if helpTopicHelpStr: |
| 1042 |
helpTopicStr += helpTopicHelpStr |
| 1043 |
else: |
| 1044 |
# Invalid help topic so return nothing and help error handler deal! |
| 1045 |
return "" |
| 1046 |
return helpTopicStr |
| 1047 |
|
| 1048 |
|
| 1049 |
def getUsageHelpStr(helpTopicArg): |
| 1050 |
usageHelpStr = helpUsageHeader |
| 1051 |
if helpTopicArg == "": |
| 1052 |
# No help topic option so just use the standard help header |
| 1053 |
None |
| 1054 |
else: |
| 1055 |
helpTopicArgArray = helpTopicArg.split("=") |
| 1056 |
if len(helpTopicArgArray) == 1: |
| 1057 |
# Option not formatted correctly, set let error handler get it." |
| 1058 |
return "" |
| 1059 |
(helpTopicArgName, helpTopicVal) = helpTopicArg.split("=") |
| 1060 |
usageHelpStr += getDistHelpTopicStr(helpTopicVal) |
| 1061 |
return usageHelpStr |
| 1062 |
|
| 1063 |
|
| 1064 |
def filterWarningsGen(lines): |
| 1065 |
for line in lines: |
| 1066 |
if not line.startswith(s('warning')) and not line.startswith(s('error')): yield line |
| 1067 |
|
| 1068 |
|
| 1069 |
# Filter warning and error lines from output |
| 1070 |
def filterWarnings(lines): |
| 1071 |
g = filterWarningsGen(lines) |
| 1072 |
if g is not None: |
| 1073 |
return list(g) |
| 1074 |
return g |
| 1075 |
|
| 1076 |
|
| 1077 |
# Get output from command |
| 1078 |
def getCmndOutput(cmnd, rtnCode=False): |
| 1079 |
child = subprocess.Popen(cmnd, shell=True, stdout=subprocess.PIPE, |
| 1080 |
stderr = subprocess.STDOUT) |
| 1081 |
output = child.stdout.read() |
| 1082 |
child.communicate() |
| 1083 |
if rtnCode: |
| 1084 |
return (s(output), child.returncode) |
| 1085 |
return s(output) |
| 1086 |
|
| 1087 |
|
| 1088 |
# Run a command and synchronize the output |
| 1089 |
def runCmnd(options, cmnd): |
| 1090 |
if options.debug: |
| 1091 |
print("*** Running command: %s" % cmnd) |
| 1092 |
if options.noOpt: |
| 1093 |
print(cmnd) |
| 1094 |
else: |
| 1095 |
subprocess.Popen(cmnd, stdout=sys.stdout, stderr=sys.stderr).communicate() |
| 1096 |
print("") |
| 1097 |
|
| 1098 |
|
| 1099 |
# Determine if a command exists: |
| 1100 |
def commandExists(cmnd): |
| 1101 |
whichCmnd = getCmndOutput("which "+cmnd).strip() |
| 1102 |
if os.path.exists(whichCmnd): |
| 1103 |
return True |
| 1104 |
return False |
| 1105 |
|
| 1106 |
|
| 1107 |
# Get the terminal colors |
| 1108 |
txtbld=getCmndOutput(r"tput bold") # Bold |
| 1109 |
txtblu=getCmndOutput(r"tput setaf 4") # Blue |
| 1110 |
txtred=getCmndOutput(r"tput setaf 1") # Red |
| 1111 |
txtrst=getCmndOutput(r"tput sgr0") # Text reset |
| 1112 |
|
| 1113 |
|
| 1114 |
# Add color to the repo dirs printed out |
| 1115 |
def addColorToRepoDir(useColor, strIn): |
| 1116 |
if useColor: |
| 1117 |
return txtbld+txtblu+strIn+txtrst |
| 1118 |
return strIn |
| 1119 |
|
| 1120 |
|
| 1121 |
# Add color to the error messages printed out |
| 1122 |
def addColorToErrorMsg(useColor, strIn): |
| 1123 |
if useColor: |
| 1124 |
return txtred+strIn+txtrst |
| 1125 |
return strIn |
| 1126 |
|
| 1127 |
|
| 1128 |
# Get the paths to all the repos gitdist will work on, along with any optional |
| 1129 |
# default branches. |
| 1130 |
def parseGitdistFile(gitdistfile): |
| 1131 |
reposFullList = [] |
| 1132 |
defaultBranchDict = {} |
| 1133 |
with open(gitdistfile, 'r') as file: |
| 1134 |
for line in file: |
| 1135 |
line = line.strip() |
| 1136 |
if line == "": continue # ignore blank lines! |
| 1137 |
entries = line.split() |
| 1138 |
reposFullList.append(entries[0]) |
| 1139 |
if len(entries) > 1: |
| 1140 |
defaultBranchDict[entries[0]] = entries[1] |
| 1141 |
else: |
| 1142 |
defaultBranchDict[entries[0]] = "master" |
| 1143 |
return (reposFullList, defaultBranchDict) |
| 1144 |
|
| 1145 |
|
| 1146 |
# Get the commandline options |
| 1147 |
def getCommandlineOps(): |
| 1148 |
|
| 1149 |
# |
| 1150 |
# A) Define the native gitdist command-line arguments |
| 1151 |
# |
| 1152 |
|
| 1153 |
distHelpArgName = "--dist-help" # Must match --dist-help before --help! |
| 1154 |
helpArgName = "--help" |
| 1155 |
withGitArgName = "--dist-use-git" |
| 1156 |
reposArgName = "--dist-repos" |
| 1157 |
notReposArgName = "--dist-not-repos" |
| 1158 |
versionFileName = "--dist-version-file" |
| 1159 |
versionFile2Name = "--dist-version-file2" |
| 1160 |
noColorArgName = "--dist-no-color" |
| 1161 |
debugArgName = "--dist-debug" |
| 1162 |
noOptName = "--dist-no-opt" |
| 1163 |
modifiedOnlyName = "--dist-mod-only" |
| 1164 |
legendName = "--dist-legend" |
| 1165 |
shortName = "--dist-short" |
| 1166 |
|
| 1167 |
nativeArgNames = [ distHelpArgName, helpArgName, withGitArgName, \ |
| 1168 |
reposArgName, notReposArgName, \ |
| 1169 |
versionFileName, versionFile2Name, noColorArgName, debugArgName, noOptName, \ |
| 1170 |
modifiedOnlyName, legendName, shortName ] |
| 1171 |
if sys.version_info > (3,): |
| 1172 |
utf8Name = "--dist-utf8-output" |
| 1173 |
nativeArgNames.append(utf8Name) |
| 1174 |
|
| 1175 |
distRepoStatus = "dist-repo-status" |
| 1176 |
distRepoVersionTable = "dist-repo-versions-table" |
| 1177 |
nativeCmndNames = [ distRepoStatus, distRepoVersionTable ] |
| 1178 |
|
| 1179 |
# Select a version of git (see above help documentation) |
| 1180 |
defaultGit = "git" # Try system git |
| 1181 |
if not commandExists(defaultGit): |
| 1182 |
defaultGit = "" # Give up and make the user specify |
| 1183 |
|
| 1184 |
# |
| 1185 |
# B) Pull the native commandline arguments out of the commandline |
| 1186 |
# |
| 1187 |
|
| 1188 |
argv = sys.argv[1:] |
| 1189 |
nativeArgs = [] |
| 1190 |
nativeCmnds = [] |
| 1191 |
otherArgs = [] |
| 1192 |
helpTopicArg = "" |
| 1193 |
|
| 1194 |
for arg in argv: |
| 1195 |
matchedNativeArg = False |
| 1196 |
for nativeArgName in nativeArgNames: |
| 1197 |
currentArgName = arg[0:len(nativeArgName)] |
| 1198 |
if currentArgName == nativeArgName: |
| 1199 |
nativeArgs.append(arg) |
| 1200 |
matchedNativeArg = True |
| 1201 |
if currentArgName == distHelpArgName: |
| 1202 |
helpTopicArg = arg |
| 1203 |
break |
| 1204 |
matchedNativeCmnd = False |
| 1205 |
for nativeCmndName in nativeCmndNames: |
| 1206 |
if arg == nativeCmndName: |
| 1207 |
nativeCmnds.append(nativeCmndName) |
| 1208 |
matchedNativeCmnd = True |
| 1209 |
break |
| 1210 |
if not (matchedNativeArg or matchedNativeCmnd): |
| 1211 |
otherArgs.append(arg) |
| 1212 |
|
| 1213 |
if len(nativeCmnds) == 0: |
| 1214 |
nativeCmnd = None |
| 1215 |
elif len(nativeCmnds) == 1: |
| 1216 |
nativeCmnd = nativeCmnds[0] |
| 1217 |
elif len(nativeCmnds) > 1: |
| 1218 |
raise Exception("Error: Can't have more than one dist-xxx command "+\ |
| 1219 |
" but was passed in "+str(nativeCmnds)) |
| 1220 |
|
| 1221 |
# |
| 1222 |
# C) Set up the commandline parser and parse the native args |
| 1223 |
# |
| 1224 |
|
| 1225 |
usageHelp = getUsageHelpStr(helpTopicArg) |
| 1226 |
|
| 1227 |
clp = OptionParser(usage=usageHelp) |
| 1228 |
|
| 1229 |
addOptionParserChoiceOption( |
| 1230 |
distHelpArgName, "helpTopic", [""]+helpTopics+["all"], 0, |
| 1231 |
"Print a gitdist help topic. Using" \ |
| 1232 |
+" --dist-help=all prints all help topics. If" \ |
| 1233 |
+" --help is also specified, then the help usage header and" \ |
| 1234 |
+" command-line 'options' are also printed." , |
| 1235 |
clp ) |
| 1236 |
|
| 1237 |
clp.add_option( |
| 1238 |
withGitArgName, dest="useGit", type="string", |
| 1239 |
default=defaultGit, |
| 1240 |
help="Path to the git executable to use for each git repo command." |
| 1241 |
+" By default, gitdist will use 'git' in the environment. If it can't find" |
| 1242 |
+" 'git' in the environment, then it will require setting" |
| 1243 |
+" --dist-use-git=<path-to-git>. (Typically only used in automated" |
| 1244 |
+" testing.) (default='"+defaultGit+"')" |
| 1245 |
) |
| 1246 |
|
| 1247 |
clp.add_option( |
| 1248 |
reposArgName, dest="repos", type="string", default="", |
| 1249 |
help="Comma-separated list of repo relative paths '<repo0>,<repo1>,...'." |
| 1250 |
+" The base repo is specified with '.' and should usually be listed first." |
| 1251 |
+" If left empty '', then the list of repos to process is taken from" |
| 1252 |
+" the file ./.gitdist (which lists the relative path of each git repo" |
| 1253 |
+" separated by newlines). If the file" |
| 1254 |
+" ./.gitdist does not exist, then the repos listed in the file" |
| 1255 |
+" ./.gitdist.default are processed. If the file" |
| 1256 |
+" the file ./.gitdist.default is missing, then no extra repos are" |
| 1257 |
+" processed and it is assumed that the base repo will be processed." |
| 1258 |
+" Also, any git repos listed that don't exist are ignored." |
| 1259 |
+" See --dist-help=repo-selection-and-setup." |
| 1260 |
+" (default='')" |
| 1261 |
) |
| 1262 |
|
| 1263 |
clp.add_option( |
| 1264 |
notReposArgName, dest="notRepos", type="string", default="", |
| 1265 |
help="Comma-separated list of extra repo relative paths" \ |
| 1266 |
+" '<repoX>,<repoY>,...' to *not* process. (default='')" |
| 1267 |
) |
| 1268 |
|
| 1269 |
clp.add_option( |
| 1270 |
modifiedOnlyName, dest="modifiedOnly", action="store_true", |
| 1271 |
help="If set, then only git repos that have changes w.r.t." \ |
| 1272 |
" their tracking branches will be processed. That is, only repos" \ |
| 1273 |
" that have modified or untracked files or where" \ |
| 1274 |
" 'git diff --name-only ^<tracking-branch>' returns non-empty output" \ |
| 1275 |
" will be processed (where <tracking-branch> is returned" \ |
| 1276 |
" from 'rev-parse --abbrev-ref --symbolic-full-name @{u})'." \ |
| 1277 |
" If a local repo does not have a tracking branch, then the repo will" \ |
| 1278 |
" be skipped as well. Therefore, be careful to first run 'gitdist-status'" \ |
| 1279 |
" (see --dist-help=dist-repo-status) to see the" \ |
| 1280 |
" status of each local git repo to know which repos don't have tracking branches.", |
| 1281 |
default=False ) |
| 1282 |
|
| 1283 |
clp.add_option( |
| 1284 |
legendName, dest="printLegend", action="store_true", |
| 1285 |
help="If set, then a legend will be printed below the repo summary table"\ |
| 1286 |
" for the special dist-repo-status command. Only applicable with" \ |
| 1287 |
" dist-repo-status (see --dist-help=dist-repo-status).", |
| 1288 |
default=False ) |
| 1289 |
|
| 1290 |
if sys.version_info > (3,): |
| 1291 |
clp.add_option( |
| 1292 |
utf8Name, dest="utf8", action="store_true", |
| 1293 |
help="If set, use UTF-8 box drawing characters instead of ASCII ones" \ |
| 1294 |
" when creating the repo summary table.", |
| 1295 |
default=False ) |
| 1296 |
|
| 1297 |
clp.add_option( |
| 1298 |
versionFileName, dest="versionFile", type="string", |
| 1299 |
default="", |
| 1300 |
help="Path to a file which contains a list of extra repo relative directories" |
| 1301 |
+" and git versions (replaces _VERSION_)." \ |
| 1302 |
+" (See --dist-help=repo-versions.) (default='')" |
| 1303 |
) |
| 1304 |
|
| 1305 |
clp.add_option( |
| 1306 |
versionFile2Name, dest="versionFile2", type="string", |
| 1307 |
default="", |
| 1308 |
help="Path to a second file contains a list of extra repo relative" |
| 1309 |
+" directories and git versions (replaces _VERSION2_)." |
| 1310 |
+" (See --dist-help=repo-versions.) (default='')" |
| 1311 |
) |
| 1312 |
|
| 1313 |
clp.add_option( |
| 1314 |
noColorArgName, dest="useColor", action="store_false", |
| 1315 |
help="If set, don't use color in the output for gitdist and set" |
| 1316 |
+" '-c color.status=never' before the git command (like 'status')." |
| 1317 |
+" NOTE: user should also pass in --color=never for git commands " |
| 1318 |
+" accept that argument. (Better for output to a file).", |
| 1319 |
default=True ) |
| 1320 |
|
| 1321 |
clp.add_option( |
| 1322 |
debugArgName, dest="debug", action="store_true", |
| 1323 |
help="If set, then debugging info is printed.", |
| 1324 |
default=False ) |
| 1325 |
|
| 1326 |
clp.add_option( |
| 1327 |
noOptName, dest="noOpt", action="store_true", |
| 1328 |
help="If set, then no git commands will be run but instead will just be printed.", |
| 1329 |
default=False ) |
| 1330 |
|
| 1331 |
clp.add_option( |
| 1332 |
shortName, dest="short", action="store_true", |
| 1333 |
help="If set, then the repo versions table will only include the Repo " \ |
| 1334 |
"Dir and SHA1 columns; Commit Date, Author, and Summary will be " \ |
| 1335 |
"omitted.", |
| 1336 |
default=False ) |
| 1337 |
|
| 1338 |
(options, args) = clp.parse_args(nativeArgs) |
| 1339 |
|
| 1340 |
debugFromEnv = os.environ.get("GITDIST_DEBUG_OVERRIDE") |
| 1341 |
if debugFromEnv: |
| 1342 |
options.debug = True |
| 1343 |
|
| 1344 |
# |
| 1345 |
# D) Print --dist-topic=<topic-name>, check for valid usage |
| 1346 |
# |
| 1347 |
|
| 1348 |
if options.helpTopic: |
| 1349 |
print(getDistHelpTopicStr(options.helpTopic)) |
| 1350 |
sys.exit(0) |
| 1351 |
|
| 1352 |
if not nativeCmnd and len(otherArgs) == 0: |
| 1353 |
print(addColorToErrorMsg(options.useColor, |
| 1354 |
"Must specify git command. See 'git --help' for " |
| 1355 |
"options.")) |
| 1356 |
sys.exit(1) |
| 1357 |
|
| 1358 |
if not options.useGit: |
| 1359 |
print(addColorToErrorMsg(options.useColor, |
| 1360 |
"Can't find git, please set --dist-use-git")) |
| 1361 |
sys.exit(1) |
| 1362 |
|
| 1363 |
# |
| 1364 |
# E) Change to top-level git directory (in case of nested git repos) |
| 1365 |
# |
| 1366 |
|
| 1367 |
moveToBaseDir = os.environ.get("GITDIST_MOVE_TO_BASE_DIR") |
| 1368 |
if (moveToBaseDir == None) or (moveToBaseDir == ""): |
| 1369 |
# Run gitdist in the current directory |
| 1370 |
None |
| 1371 |
elif moveToBaseDir == "EXTREME_BASE": |
| 1372 |
# Run gitdist in the most base dir where .gitdist[.default] exists |
| 1373 |
drive, currentPath = os.path.splitdrive(os.getcwd()) |
| 1374 |
pathList = [] |
| 1375 |
while 1: |
| 1376 |
currentPath, currentDir = os.path.split(currentPath) |
| 1377 |
if currentDir != "": |
| 1378 |
pathList.append(currentDir) |
| 1379 |
else: |
| 1380 |
if currentPath != "": |
| 1381 |
pathList.append(currentPath) |
| 1382 |
break |
| 1383 |
pathList.reverse() |
| 1384 |
newPath = drive+pathList[0] |
| 1385 |
for directory in pathList: |
| 1386 |
newPath = os.path.join(newPath, directory) |
| 1387 |
if ((os.path.isfile(os.path.join(newPath, ".gitdist"))) or |
| 1388 |
(os.path.isfile(os.path.join(newPath, ".gitdist.default")))): |
| 1389 |
break |
| 1390 |
os.chdir(newPath) |
| 1391 |
elif moveToBaseDir == "IMMEDIATE_BASE": |
| 1392 |
# Run gitdist in the immediate base dir where .gitdist[.default] exists |
| 1393 |
currentPath = os.getcwd() |
| 1394 |
foundIt = False |
| 1395 |
while 1: |
| 1396 |
if ((os.path.isfile(os.path.join(currentPath, ".gitdist"))) or |
| 1397 |
(os.path.isfile(os.path.join(currentPath, ".gitdist.default")))): |
| 1398 |
foundIt = True |
| 1399 |
break |
| 1400 |
currentPath, currentDir = os.path.split(currentPath) |
| 1401 |
if currentDir == "": |
| 1402 |
break |
| 1403 |
if foundIt: |
| 1404 |
os.chdir(currentPath) |
| 1405 |
else: |
| 1406 |
print( |
| 1407 |
"Error, env var GITDIST_MOVE_TO_BASE_DIR='"+moveToBaseDir+"' is invalid!" |
| 1408 |
+ " Valid choices include empty '', IMMEDIATE_BASE, and EXTREME_BASE.") |
| 1409 |
sys.exit(1) |
| 1410 |
|
| 1411 |
# |
| 1412 |
# F) Get the list of extra repos |
| 1413 |
# |
| 1414 |
|
| 1415 |
if options.repos: |
| 1416 |
reposFullList = options.repos.split(",") |
| 1417 |
defaultBranchDict = {} |
| 1418 |
for repo in reposFullList: |
| 1419 |
defaultBranchDict[repo] = "master" |
| 1420 |
else: |
| 1421 |
if os.path.exists(".gitdist"): |
| 1422 |
gitdistfile = ".gitdist" |
| 1423 |
elif os.path.exists(".gitdist.default"): |
| 1424 |
gitdistfile = ".gitdist.default" |
| 1425 |
else: |
| 1426 |
gitdistfile = None |
| 1427 |
if gitdistfile: |
| 1428 |
(reposFullList, defaultBranchDict) = parseGitdistFile(gitdistfile) |
| 1429 |
else: |
| 1430 |
reposFullList = ["."] # The default is the base repo |
| 1431 |
defaultBranchDict = {".": "master"} |
| 1432 |
|
| 1433 |
# Get list of not extra repos |
| 1434 |
|
| 1435 |
if options.notRepos: |
| 1436 |
notReposFullList = options.notRepos.split(",") |
| 1437 |
else: |
| 1438 |
notReposFullList = [] |
| 1439 |
|
| 1440 |
# |
| 1441 |
# G) Return |
| 1442 |
# |
| 1443 |
|
| 1444 |
return (options, nativeCmnd, otherArgs, reposFullList, defaultBranchDict, |
| 1445 |
notReposFullList) |
| 1446 |
|
| 1447 |
|
| 1448 |
# Requote commandline arguments into an array |
| 1449 |
def requoteCmndLineArgsIntoArray(inArgs): |
| 1450 |
argsArray = [] |
| 1451 |
for arg in inArgs: |
| 1452 |
splitArg = arg.split("=") |
| 1453 |
newArg = None |
| 1454 |
if len(splitArg) == 1: |
| 1455 |
newArg = arg |
| 1456 |
else: |
| 1457 |
newArg = splitArg[0]+"="+'='.join(splitArg[1:]) |
| 1458 |
argsArray.append(newArg) |
| 1459 |
return argsArray |
| 1460 |
|
| 1461 |
|
| 1462 |
# Get a data-structure for a set of repos from a string |
| 1463 |
def getRepoVersionDictFromRepoVersionFileString(repoVersionFileStr): |
| 1464 |
repoVersionFileStrList = repoVersionFileStr.splitlines() |
| 1465 |
repoVersionDict = {} |
| 1466 |
len_repoVersionFileStrList = len(repoVersionFileStrList) |
| 1467 |
i = 0 |
| 1468 |
while i < len_repoVersionFileStrList: |
| 1469 |
repoDirLine = repoVersionFileStrList[i] |
| 1470 |
if repoDirLine[0:3] == "***": |
| 1471 |
repoDir = repoDirLine.split(":")[1].strip() |
| 1472 |
repoVersionLine = repoVersionFileStrList[i+1] |
| 1473 |
repoSha1 = repoVersionLine.split(" ")[0].strip() |
| 1474 |
repoDirToEnter = ("." if repoDir == baseRepoName else repoDir) |
| 1475 |
repoVersionDict.update({repoDirToEnter : repoSha1}) |
| 1476 |
else: |
| 1477 |
break |
| 1478 |
nextRepoNoSummary_i = i+2 |
| 1479 |
if nextRepoNoSummary_i >= len_repoVersionFileStrList: |
| 1480 |
break |
| 1481 |
if repoVersionFileStrList[nextRepoNoSummary_i][0:3] == "***": |
| 1482 |
# Has no summary line |
| 1483 |
i = i + 2 |
| 1484 |
else: |
| 1485 |
# Has a summary line |
| 1486 |
i = i + 3 |
| 1487 |
return repoVersionDict |
| 1488 |
|
| 1489 |
|
| 1490 |
# Get a data-structure for a set of repos from a file |
| 1491 |
def getRepoVersionDictFromRepoVersionFile(repoVersionFileName): |
| 1492 |
if repoVersionFileName: |
| 1493 |
repoVersionFileStr = open(repoVersionFileName, 'r').read() |
| 1494 |
return getRepoVersionDictFromRepoVersionFileString(repoVersionFileStr) |
| 1495 |
else: |
| 1496 |
None |
| 1497 |
|
| 1498 |
|
| 1499 |
def assertAndGetRepoVersionFromDict(repoDirName, repoVersionDict): |
| 1500 |
if repoVersionDict: |
| 1501 |
|
| 1502 |
repoSha1 = repoVersionDict.get(repoDirName, "") |
| 1503 |
if not repoSha1: |
| 1504 |
print(addColorToErrorMsg(options.useColor, |
| 1505 |
"Repo '" + repoDirName + "' is not in the " |
| 1506 |
+ "list of repos " + |
| 1507 |
str(sorted(repoVersionDict.keys())) + " read in from" |
| 1508 |
+ " the version file.")) |
| 1509 |
sys.exit(3) |
| 1510 |
return repoSha1 |
| 1511 |
else: |
| 1512 |
return "" |
| 1513 |
|
| 1514 |
|
| 1515 |
def replaceRepoVersionInCmndLineArg(cmndLineArg, verToken, repoDirName, repoSha1): |
| 1516 |
if repoSha1: |
| 1517 |
newCmndLineArg = re.sub(verToken, repoSha1, cmndLineArg) |
| 1518 |
return newCmndLineArg |
| 1519 |
return cmndLineArg |
| 1520 |
|
| 1521 |
|
| 1522 |
def replaceRepoVersionInCmndLineArgs(cmndLineArgsArray, repoDirName, \ |
| 1523 |
repoVersionDict, repoVersionDict2 \ |
| 1524 |
): |
| 1525 |
repoSha1 = assertAndGetRepoVersionFromDict(repoDirName, repoVersionDict) |
| 1526 |
repoSha1_2 = assertAndGetRepoVersionFromDict(repoDirName, repoVersionDict2) |
| 1527 |
cmndLineArgsArrayRepo = [] |
| 1528 |
for cmndLineArg in cmndLineArgsArray: |
| 1529 |
newCmndLineArg = replaceRepoVersionInCmndLineArg(cmndLineArg, \ |
| 1530 |
"_VERSION_", repoDirName, repoSha1) |
| 1531 |
newCmndLineArg = replaceRepoVersionInCmndLineArg(newCmndLineArg, \ |
| 1532 |
"_VERSION2_", repoDirName, repoSha1_2) |
| 1533 |
cmndLineArgsArrayRepo.append(newCmndLineArg) |
| 1534 |
return cmndLineArgsArrayRepo |
| 1535 |
|
| 1536 |
|
| 1537 |
# Replace _DEFAULT_BRANCH_ in the command line arguments with the appropriate |
| 1538 |
# default branch name. |
| 1539 |
def replaceDefaultBranchInCmndLineArgs(cmndLineArgsArray, repoDirName, \ |
| 1540 |
defaultBranchDict \ |
| 1541 |
): |
| 1542 |
cmndLineArgsArrayDefaultBranch = [] |
| 1543 |
for cmndLineArg in cmndLineArgsArray: |
| 1544 |
newCmndLineArg = re.sub("_DEFAULT_BRANCH_", \ |
| 1545 |
defaultBranchDict[repoDirName], cmndLineArg) |
| 1546 |
cmndLineArgsArrayDefaultBranch.append(newCmndLineArg) |
| 1547 |
return cmndLineArgsArrayDefaultBranch |
| 1548 |
|
| 1549 |
|
| 1550 |
# Generate the command line arguments |
| 1551 |
def runRepoCmnd(options, cmndLineArgsArray, repoDirName, baseDir, \ |
| 1552 |
repoVersionDict, repoVersionDict2, defaultBranchDict \ |
| 1553 |
): |
| 1554 |
cmndLineArgsArrayRepo = replaceRepoVersionInCmndLineArgs(cmndLineArgsArray, \ |
| 1555 |
repoDirName, repoVersionDict, repoVersionDict2) |
| 1556 |
cmndLineArgsArrayDefaultBranch = replaceDefaultBranchInCmndLineArgs( \ |
| 1557 |
cmndLineArgsArrayRepo, repoDirName, defaultBranchDict) |
| 1558 |
egCmndArray = [ options.useGit ] |
| 1559 |
if options.useColor: |
| 1560 |
egCmndArray.extend(['-c', 'color.status=always']) |
| 1561 |
else: |
| 1562 |
egCmndArray.extend(['-c', 'color.status=never']) |
| 1563 |
egCmndArray.extend(cmndLineArgsArrayDefaultBranch) |
| 1564 |
runCmnd(options, egCmndArray) |
| 1565 |
|
| 1566 |
|
| 1567 |
# Get the name of the base directory |
| 1568 |
def getBaseDirNameFromPath(dirPath): |
| 1569 |
dirPathArray = dirPath.split("/") |
| 1570 |
return dirPathArray[-1] |
| 1571 |
|
| 1572 |
|
| 1573 |
# Get the name of the base repo to insert into the table |
| 1574 |
def getBaseRepoTblName(baseRepoName): |
| 1575 |
return baseRepoName+" (Base)" |
| 1576 |
|
| 1577 |
|
| 1578 |
# Determine if the extra repo should be processed or not |
| 1579 |
def repoExistsAndNotExcluded(options, extraRepo, notReposList): |
| 1580 |
if not os.path.isdir(extraRepo): return False |
| 1581 |
if extraRepo in notReposList: return False |
| 1582 |
return True |
| 1583 |
|
| 1584 |
|
| 1585 |
# Get the identifier for the current commit in the repo |
| 1586 |
def getRepoVersionIdentifier(options, getCmndOutputFunc, showMoreHeadDetails): |
| 1587 |
branch = getLocalBranch(options, getCmndOutputFunc) |
| 1588 |
if showMoreHeadDetails != "SHOW_MORE_HEAD_DETAILS": |
| 1589 |
return branch |
| 1590 |
if branch != "HEAD": |
| 1591 |
return branch |
| 1592 |
tagName = getCmndOutputFunc(options.useGit + " tag --points-at").strip() |
| 1593 |
if tagName != "": |
| 1594 |
return tagName |
| 1595 |
sha1 = getCmndOutputFunc(options.useGit + " log --pretty=%h -1").strip() |
| 1596 |
return sha1 |
| 1597 |
|
| 1598 |
|
| 1599 |
# Get the tracking branch for a repo |
| 1600 |
def getLocalBranch(options, getCmndOutputFunc): |
| 1601 |
(resp, rtnCode) = getCmndOutputFunc( |
| 1602 |
options.useGit + " rev-parse --abbrev-ref HEAD", |
| 1603 |
rtnCode=True ) |
| 1604 |
if rtnCode == 0: |
| 1605 |
filteredLines = filterWarnings(resp.strip().splitlines()) |
| 1606 |
if filteredLines and len(filteredLines) > 0: |
| 1607 |
localBranch = filteredLines[0].strip() |
| 1608 |
else: |
| 1609 |
localBranch = "<AMBIGUOUS-HEAD>" |
| 1610 |
return s(localBranch) |
| 1611 |
return "" |
| 1612 |
|
| 1613 |
|
| 1614 |
# Get the tracking branch for a repo |
| 1615 |
def getTrackingBranch(options, getCmndOutputFunc): |
| 1616 |
(trackingBranch, rtnCode) = getCmndOutputFunc( |
| 1617 |
options.useGit + " rev-parse --abbrev-ref --symbolic-full-name @{u}", |
| 1618 |
rtnCode=True ) |
| 1619 |
if rtnCode == 0: |
| 1620 |
return s(trackingBranch.strip()) |
| 1621 |
return "" |
| 1622 |
# Above, if the command failed, there is likely no tracking branch. |
| 1623 |
# However, this could fail for other reasons so it is a little dangerous to |
| 1624 |
# just fail and return "" but I don't know of another way to do this. |
| 1625 |
|
| 1626 |
|
| 1627 |
# Get number of commits as a str wr.t.t tracking branch |
| 1628 |
def getNumCommitsWrtTrackingBranch(options, trackingBranch, getCmndOutputFunc): |
| 1629 |
if trackingBranch == "": |
| 1630 |
return "" |
| 1631 |
(summaryLines, rtnCode) = \ |
| 1632 |
getCmndOutputFunc(options.useGit + " shortlog -s HEAD ^" + |
| 1633 |
trackingBranch, rtnCode=True) |
| 1634 |
if rtnCode != 0: |
| 1635 |
raise Exception(summaryLines) |
| 1636 |
numCommits = 0 |
| 1637 |
summaryLines = summaryLines.strip() |
| 1638 |
if summaryLines: |
| 1639 |
for summaryLine in filterWarnings(summaryLines.splitlines()): |
| 1640 |
numAuthorCommits = int(summaryLine.strip().split()[0].strip()) |
| 1641 |
numCommits += numAuthorCommits |
| 1642 |
return str(numCommits) |
| 1643 |
# NOTE: Above, we would like to use 'git ref-list --count' but that is not |
| 1644 |
# supported in older versions of git (at least not in 1.7.0.4). Using 'git |
| 1645 |
# shortlog -s' will return just one line per author so this is not likely to |
| 1646 |
# return a lot of data and the cost of the python code to process this |
| 1647 |
# should be insignificant compared to the process execution command. |
| 1648 |
|
| 1649 |
|
| 1650 |
def matchFieldOneOrTwo(findIdx): |
| 1651 |
if findIdx == 0 or findIdx == 1: |
| 1652 |
return True |
| 1653 |
return False |
| 1654 |
|
| 1655 |
|
| 1656 |
# Get the number of modified |
| 1657 |
def getNumModifiedAndUntracked(options, getCmndOutputFunc): |
| 1658 |
(rawStatusOutput, rtnCode) = getCmndOutputFunc( |
| 1659 |
options.useGit + " status --porcelain", rtnCode=True ) |
| 1660 |
if rtnCode == 0: |
| 1661 |
numModified = 0 |
| 1662 |
numUntracked = 0 |
| 1663 |
for line in rawStatusOutput.splitlines(): |
| 1664 |
if matchFieldOneOrTwo(line.find(s("M"))): |
| 1665 |
numModified += 1 |
| 1666 |
elif matchFieldOneOrTwo(line.find(s("A"))): |
| 1667 |
numModified += 1 |
| 1668 |
elif matchFieldOneOrTwo(line.find(s("D"))): |
| 1669 |
numModified += 1 |
| 1670 |
elif matchFieldOneOrTwo(line.find(s("T"))): |
| 1671 |
numModified += 1 |
| 1672 |
elif matchFieldOneOrTwo(line.find(s("U"))): |
| 1673 |
numModified += 1 |
| 1674 |
elif matchFieldOneOrTwo(line.find(s("R"))): |
| 1675 |
numModified += 1 |
| 1676 |
elif line.find(s("??")) == 0: |
| 1677 |
numUntracked += 1 |
| 1678 |
return (str(numModified), str(numUntracked)) |
| 1679 |
return ("", "") |
| 1680 |
|
| 1681 |
|
| 1682 |
# |
| 1683 |
# Get the repo statistics |
| 1684 |
# |
| 1685 |
|
| 1686 |
class RepoStatsStruct: |
| 1687 |
|
| 1688 |
def __init__(self, branch, trackingBranch, numCommits, numModified, |
| 1689 |
numUntracked \ |
| 1690 |
): |
| 1691 |
self.branch = branch |
| 1692 |
self.trackingBranch = trackingBranch |
| 1693 |
self.numCommits = numCommits |
| 1694 |
self.numModified = numModified |
| 1695 |
self.numUntracked = numUntracked |
| 1696 |
|
| 1697 |
def __str__(self): |
| 1698 |
return "{" \ |
| 1699 |
"branch='" + self.branch + "'," \ |
| 1700 |
" trackingBranch='" + self.trackingBranch + "'," \ |
| 1701 |
" numCommits='" + self.numCommits + "'," \ |
| 1702 |
" numModified='" + self.numModified + "'," \ |
| 1703 |
" numUntracked='" + self.numUntracked + "'" \ |
| 1704 |
"}" |
| 1705 |
|
| 1706 |
def numCommitsInt(self): |
| 1707 |
if self.numCommits == '': return 0 |
| 1708 |
return int(self.numCommits) |
| 1709 |
|
| 1710 |
def numModifiedInt(self): |
| 1711 |
if self.numModified == '': return 0 |
| 1712 |
return int(self.numModified) |
| 1713 |
|
| 1714 |
def numUntrackedInt(self): |
| 1715 |
if self.numUntracked == '': return 0 |
| 1716 |
return int(self.numUntracked) |
| 1717 |
|
| 1718 |
def hasLocalChanges(self): |
| 1719 |
if self.numCommitsInt() + self.numModifiedInt() + self.numUntrackedInt() > 0: |
| 1720 |
return True |
| 1721 |
return False |
| 1722 |
|
| 1723 |
|
| 1724 |
def getRepoStats(options, getCmndOutputFunc=None, showMoreHeadDetails=""): |
| 1725 |
if not getCmndOutputFunc: |
| 1726 |
getCmndOutputFunc = getCmndOutput |
| 1727 |
branch = getRepoVersionIdentifier(options, getCmndOutputFunc, showMoreHeadDetails) |
| 1728 |
trackingBranch = getTrackingBranch(options, getCmndOutputFunc) |
| 1729 |
numCommits = getNumCommitsWrtTrackingBranch(options, trackingBranch, getCmndOutputFunc) |
| 1730 |
(numModified, numUntracked) = getNumModifiedAndUntracked(options, |
| 1731 |
getCmndOutputFunc) |
| 1732 |
return RepoStatsStruct(branch, trackingBranch, numCommits, |
| 1733 |
numModified, numUntracked) |
| 1734 |
|
| 1735 |
|
| 1736 |
class RepoVersionStruct: |
| 1737 |
|
| 1738 |
def __init__(self, sha1, commitDate, author, summary): |
| 1739 |
self.sha1 = sha1 |
| 1740 |
self.commitDate = commitDate |
| 1741 |
self.author = author |
| 1742 |
self.summary = summary |
| 1743 |
|
| 1744 |
def __str__(self): |
| 1745 |
return "{" \ |
| 1746 |
"sha1='" + self.sha1 + "'," \ |
| 1747 |
" commitDate='" + self.commitDate + "'," \ |
| 1748 |
" author='" + self.author + "'," \ |
| 1749 |
" summary='" + self.summary + "'" \ |
| 1750 |
"}" |
| 1751 |
|
| 1752 |
|
| 1753 |
# Get the SHA1 for the current commit. |
| 1754 |
def getCommitSha1(options, getCmndOutputFunc): |
| 1755 |
(resp, rtnCode) = getCmndOutputFunc( |
| 1756 |
options.useGit + " rev-parse --short HEAD", |
| 1757 |
rtnCode=True ) |
| 1758 |
if rtnCode == 0: |
| 1759 |
return s(resp.strip()) |
| 1760 |
return "" |
| 1761 |
|
| 1762 |
# Get the commit date for the current commit. |
| 1763 |
def getCommitDate(options, getCmndOutputFunc): |
| 1764 |
(resp, rtnCode) = getCmndOutputFunc( |
| 1765 |
options.useGit + " log -1 --pretty=format:\"%cd\" --date=format:\"%G-%m-%d %H:%M:%S\"", |
| 1766 |
rtnCode=True ) |
| 1767 |
if rtnCode == 0: |
| 1768 |
return s(resp.strip()) |
| 1769 |
return "" |
| 1770 |
|
| 1771 |
# Get the author of the current commit. |
| 1772 |
def getCommitAuthor(options, getCmndOutputFunc): |
| 1773 |
(resp, rtnCode) = getCmndOutputFunc( |
| 1774 |
options.useGit + " log -1 --pretty=format:\"%ae\"", |
| 1775 |
rtnCode=True ) |
| 1776 |
if rtnCode == 0: |
| 1777 |
return s(resp.strip()) |
| 1778 |
return "" |
| 1779 |
|
| 1780 |
# Get the first line of the current commit message. |
| 1781 |
def getCommitSummary(options, getCmndOutputFunc): |
| 1782 |
(resp, rtnCode) = getCmndOutputFunc( |
| 1783 |
options.useGit + " log -1 --pretty=format:\"%s\"", |
| 1784 |
rtnCode=True ) |
| 1785 |
if rtnCode == 0: |
| 1786 |
return s(resp.strip()) |
| 1787 |
return "" |
| 1788 |
|
| 1789 |
|
| 1790 |
def getRepoVersions(options, getCmndOutputFunc=None): |
| 1791 |
if not getCmndOutputFunc: |
| 1792 |
getCmndOutputFunc = getCmndOutput |
| 1793 |
sha1 = getCommitSha1(options, getCmndOutputFunc) |
| 1794 |
commitDate = getCommitDate(options, getCmndOutputFunc) |
| 1795 |
author = getCommitAuthor(options, getCmndOutputFunc) |
| 1796 |
summary = getCommitSummary(options, getCmndOutputFunc) |
| 1797 |
return RepoVersionStruct(sha1, commitDate, author, summary) |
| 1798 |
|
| 1799 |
|
| 1800 |
def convertZeroStrToEmpty(strIn): |
| 1801 |
if strIn == "0": |
| 1802 |
return "" |
| 1803 |
return strIn |
| 1804 |
|
| 1805 |
|
| 1806 |
class RepoStatTable: |
| 1807 |
|
| 1808 |
def __init__(self): |
| 1809 |
self.tableData = [ |
| 1810 |
{ "label" : "ID", "align" : "R", "fields" : [] }, |
| 1811 |
{ "label" : "Repo Dir", "align" : "L", "fields" : [] }, |
| 1812 |
{ "label" : "Branch", "align":"L", "fields" : [] }, |
| 1813 |
{ "label" : "Tracking Branch", "align":"L", "fields" : [] }, |
| 1814 |
{ "label" : "C", "align":"R", "fields" : [] }, |
| 1815 |
{ "label" : "M", "align":"R", "fields" : [] }, |
| 1816 |
{ "label" : "?", "align":"R", "fields" : [] }, |
| 1817 |
] |
| 1818 |
|
| 1819 |
def insertRepoStat(self, repoDir, repoStat, repoID): |
| 1820 |
self.tableData[0]["fields"].append(str(repoID)) |
| 1821 |
self.tableData[1]["fields"].append(repoDir) |
| 1822 |
self.tableData[2]["fields"].append(repoStat.branch) |
| 1823 |
self.tableData[3]["fields"].append(repoStat.trackingBranch) |
| 1824 |
self.tableData[4]["fields"].append(convertZeroStrToEmpty(repoStat.numCommits)) |
| 1825 |
self.tableData[5]["fields"].append(convertZeroStrToEmpty(repoStat.numModified)) |
| 1826 |
self.tableData[6]["fields"].append(convertZeroStrToEmpty(repoStat.numUntracked)) |
| 1827 |
|
| 1828 |
def getTableData(self): |
| 1829 |
return self.tableData |
| 1830 |
|
| 1831 |
|
| 1832 |
class RepoVersionTable: |
| 1833 |
|
| 1834 |
def __init__(self): |
| 1835 |
self.tableData = [ |
| 1836 |
{ "label" : "Repository", "align" : "L", "fields" : [] }, |
| 1837 |
{ "label" : "SHA1", "align" : "C", "fields" : [] }, |
| 1838 |
{ "label" : "Commit Date", "align" : "L", "fields" : [] }, |
| 1839 |
{ "label" : "Author", "align" : "L", "fields" : [] }, |
| 1840 |
{ "label" : "Summary", "align" : "L", "fields" : [] }, |
| 1841 |
] |
| 1842 |
|
| 1843 |
def insertRepoVersion(self, repoDir, repoVersion): |
| 1844 |
self.tableData[0]["fields"].append(repoDir) |
| 1845 |
self.tableData[1]["fields"].append(repoVersion.sha1) |
| 1846 |
self.tableData[2]["fields"].append(repoVersion.commitDate) |
| 1847 |
self.tableData[3]["fields"].append(repoVersion.author) |
| 1848 |
self.tableData[4]["fields"].append(repoVersion.summary) |
| 1849 |
|
| 1850 |
def getTableData(self): |
| 1851 |
return self.tableData |
| 1852 |
|
| 1853 |
|
| 1854 |
def getRepoName(repoDir, baseRepoName): |
| 1855 |
if repoDir == ".": |
| 1856 |
return baseRepoName |
| 1857 |
return repoDir |
| 1858 |
|
| 1859 |
# |
| 1860 |
# Run the script |
| 1861 |
# |
| 1862 |
|
| 1863 |
global baseRepoName |
| 1864 |
baseRepoName = None |
| 1865 |
|
| 1866 |
if __name__ == '__main__': |
| 1867 |
|
| 1868 |
(options, nativeCmnd, otherArgs, reposFullList, defaultBranchDict, \ |
| 1869 |
notReposList) = getCommandlineOps() |
| 1870 |
|
| 1871 |
if nativeCmnd == "dist-repo-status": |
| 1872 |
distRepoStatus = True |
| 1873 |
if len(otherArgs) > 0: |
| 1874 |
print("Error, passing in extra git commands/args ='" + " ".join(otherArgs) |
| 1875 |
+ "' with special command 'dist-repo-status' is not allowed!") |
| 1876 |
sys.exit(1) |
| 1877 |
else: |
| 1878 |
distRepoStatus = False |
| 1879 |
if nativeCmnd == "dist-repo-versions-table": |
| 1880 |
distRepoVersionTable = True |
| 1881 |
if len(otherArgs) > 0: |
| 1882 |
print("Error, passing in extra git commands/args ='" + " ".join(otherArgs) |
| 1883 |
+ "' with special command 'dist-repo-versions-table' is not allowed!") |
| 1884 |
sys.exit(1) |
| 1885 |
else: |
| 1886 |
distRepoVersionTable = False |
| 1887 |
|
| 1888 |
# Get the reference base directory |
| 1889 |
baseDir = os.getcwd() |
| 1890 |
|
| 1891 |
# Get the name of the base repo |
| 1892 |
baseRepoName = getBaseDirNameFromPath(baseDir) |
| 1893 |
|
| 1894 |
# Get the repo version files |
| 1895 |
repoVersionDict = getRepoVersionDictFromRepoVersionFile(options.versionFile) |
| 1896 |
repoVersionDict2 = getRepoVersionDictFromRepoVersionFile(options.versionFile2) |
| 1897 |
|
| 1898 |
# Reform the commandline arguments correctly |
| 1899 |
cmndLineArgsArray = requoteCmndLineArgsIntoArray(otherArgs) |
| 1900 |
|
| 1901 |
if options.debug: |
| 1902 |
print("*** Using git: " + str(options.useGit)) |
| 1903 |
|
| 1904 |
repoStatTable = RepoStatTable() |
| 1905 |
repoVersionTable = RepoVersionTable() |
| 1906 |
|
| 1907 |
repoID = 0 |
| 1908 |
|
| 1909 |
for repo in reposFullList: |
| 1910 |
|
| 1911 |
# Determine if we should process this repo |
| 1912 |
processThisExtraRepo = True |
| 1913 |
if not repoExistsAndNotExcluded(options, repo, notReposList): |
| 1914 |
processThisExtraRepo = False |
| 1915 |
if processThisExtraRepo: |
| 1916 |
repoDoesExistsAndNotExcluded = True |
| 1917 |
# cd into extrarepo dir |
| 1918 |
if options.debug: |
| 1919 |
print("\n*** Changing to directory " + repo) |
| 1920 |
os.chdir(repo) |
| 1921 |
# Get repo stats |
| 1922 |
repoStats = None |
| 1923 |
if options.modifiedOnly or distRepoStatus: |
| 1924 |
repoStats = getRepoStats(options, showMoreHeadDetails="SHOW_MORE_HEAD_DETAILS") |
| 1925 |
repoVersions = None |
| 1926 |
if distRepoVersionTable: |
| 1927 |
repoVersions = getRepoVersions(options) |
| 1928 |
# See if we should process based on --dist-mod-only |
| 1929 |
if options.modifiedOnly and not repoStats.hasLocalChanges(): |
| 1930 |
processThisExtraRepo = False |
| 1931 |
else: |
| 1932 |
repoDoesExistsAndNotExcluded = False |
| 1933 |
|
| 1934 |
# Process this repo |
| 1935 |
if processThisExtraRepo: |
| 1936 |
repoName = getRepoName(repo, baseRepoName) |
| 1937 |
repoNameInTpl = repoName + (" (Base)" if repo=="." else "") |
| 1938 |
if distRepoStatus: |
| 1939 |
repoStatTable.insertRepoStat(repoNameInTpl, repoStats, repoID) |
| 1940 |
processThisExtraRepo = False |
| 1941 |
elif distRepoVersionTable: |
| 1942 |
repoVersionTable.insertRepoVersion(repoName.split("/")[-1], |
| 1943 |
repoVersions) |
| 1944 |
processThisExtraRepo = False |
| 1945 |
else: |
| 1946 |
print("") |
| 1947 |
print( |
| 1948 |
"*** " + ("Base " if repo=="." else "") + "Git Repo: " |
| 1949 |
+ addColorToRepoDir(options.useColor,repoName) ) |
| 1950 |
sys.stdout.flush() |
| 1951 |
if options.debug: |
| 1952 |
print("*** Tracking branch for git repo '" + repoName + "' = '" + |
| 1953 |
repoStats.trackingBranch + "'") |
| 1954 |
runRepoCmnd(options, cmndLineArgsArray, repo, baseDir, \ |
| 1955 |
repoVersionDict, repoVersionDict2, defaultBranchDict) |
| 1956 |
if options.debug: |
| 1957 |
print("*** Changing to directory " + baseDir) |
| 1958 |
|
| 1959 |
if repoDoesExistsAndNotExcluded: |
| 1960 |
repoID += 1 |
| 1961 |
|
| 1962 |
os.chdir(baseDir) |
| 1963 |
|
| 1964 |
if distRepoStatus: |
| 1965 |
if sys.version_info < (3,): |
| 1966 |
print(createTable(repoStatTable.getTableData())) |
| 1967 |
else: |
| 1968 |
print(createTable(repoStatTable.getTableData(), options.utf8)) |
| 1969 |
if options.printLegend: |
| 1970 |
print(distRepoStatusLegend) |
| 1971 |
else: |
| 1972 |
print("(tip: to see a legend, pass in --dist-legend.)") |
| 1973 |
elif distRepoVersionTable: |
| 1974 |
if (options.short): |
| 1975 |
print(createMarkdownTable(repoVersionTable.getTableData()[0:2])) |
| 1976 |
else: |
| 1977 |
print(createMarkdownTable(repoVersionTable.getTableData())) |
| 1978 |
else: |
| 1979 |
print("") |
| 1980 |
|
| 1981 |
sys.stdout.flush() |