| 1 |
# @HEADER |
| 2 |
# ***************************************************************************** |
| 3 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 4 |
# |
| 5 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 6 |
# SPDX-License-Identifier: BSD-3-Clause |
| 7 |
# ***************************************************************************** |
| 8 |
# @HEADER |
| 9 |
# |
| 10 |
|
| 11 |
|
| 12 |
# |
| 13 |
# Imports |
| 14 |
# |
| 15 |
|
| 16 |
import sys |
| 17 |
import time |
| 18 |
import pprint |
| 19 |
|
| 20 |
pp = pprint.PrettyPrinter(indent=4) |
| 21 |
|
| 22 |
from GeneralScriptSupport import * |
| 23 |
from Python2and3 import b, s |
| 24 |
|
| 25 |
|
| 26 |
# |
| 27 |
# Class for defining default options |
| 28 |
# |
| 29 |
|
| 30 |
class DefaultOptions: |
| 31 |
|
| 32 |
def __init__(self): |
| 33 |
self.origDir = "" |
| 34 |
self.destDir = "" |
| 35 |
|
| 36 |
def setDefaultDefaults(self): |
| 37 |
self.origDir = addTrailingSlashToPath(os.path.abspath(os.path.dirname(sys.argv[0]))) |
| 38 |
self.destDir = addTrailingSlashToPath(os.getcwd()) |
| 39 |
|
| 40 |
def setDefaultOrigDir(self, origDirIn): |
| 41 |
self.origDir = origDirIn |
| 42 |
|
| 43 |
def getDefaultOrigDir(self): |
| 44 |
return self.origDir |
| 45 |
|
| 46 |
def setDefaultDestDir(self, destDirIn): |
| 47 |
self.destDir = destDirIn |
| 48 |
|
| 49 |
def getDefaultDestDir(self): |
| 50 |
return self.destDir |
| 51 |
|
| 52 |
|
| 53 |
# |
| 54 |
# Main help string |
| 55 |
# |
| 56 |
|
| 57 |
usageHelp = r""" |
| 58 |
|
| 59 |
This tool uses rsync and some git commands to snapshot the contents of an |
| 60 |
origin directory ('orig-dir') in one Git repo to destination directory |
| 61 |
('dest-dir') in another Git repo and logs version information in a new Git |
| 62 |
commit message. |
| 63 |
|
| 64 |
WARNING: Because this tool uses 'rsync --delete', it can be quite destructive |
| 65 |
to the contents of dest-dir if one is not careful and selects the wrong |
| 66 |
orig-dir or wrong dest-dir! Therefore, please carefully read this entire help |
| 67 |
message and especially the warnings given below and always start by running |
| 68 |
with --no-op! |
| 69 |
|
| 70 |
To sync between any two arbitrary directories between two different git repos |
| 71 |
invoking this script from any directory location, one can do: |
| 72 |
|
| 73 |
$ <some-base-dir>/snapshot-dir.py \ |
| 74 |
--orig-dir=<dest-dir> \ |
| 75 |
--dest-dir=<dest-dir> \ |
| 76 |
[--no-op] |
| 77 |
|
| 78 |
(where --no-op should be used on the initial trial run to check that the |
| 79 |
correct rsync and other commands will be run, see below). |
| 80 |
|
| 81 |
To describe how this script is used, consider the desire to snapshot the |
| 82 |
directory tree from one git repo: |
| 83 |
|
| 84 |
<some-orig-base-dir>/orig-dir/ |
| 85 |
|
| 86 |
and exactly duplicate it in another git repo under: |
| 87 |
|
| 88 |
<some-dest-base-dir>/dest-dir/ |
| 89 |
|
| 90 |
Here, the directories can be any two directories from local git repos with any |
| 91 |
names. (Note if the paths don't end with '/', then '/' will be added. |
| 92 |
Otherwise, rsync will copy the contents from 'orig-dir' into a subdir of |
| 93 |
'dest-dir' which is usually not what you want.) |
| 94 |
|
| 95 |
A typical case is to have snapshot-dir.py soft linked into orig-dir/ to allow |
| 96 |
a simple sync process. The linked-in location of snapshot-dir.py gives the |
| 97 |
default 'orig-dir' directory automatically (but can be overridden with |
| 98 |
--orig-dir=<orig-dir> option). |
| 99 |
|
| 100 |
When snapshot-dir.py is soft-linked into the 'orig-dir' directory base, the |
| 101 |
way to run this script would be: |
| 102 |
|
| 103 |
$ cd <some-dest-base-dir>/dest-dir/ |
| 104 |
$ <some-orig-base-dir>/orig-dir/snapshot-dir.py |
| 105 |
|
| 106 |
By default, this assumes that git repos are used for both the 'orig-dir' and |
| 107 |
'dest-dir' locations. The info about the origin of the snapshot from |
| 108 |
'orig-dir' is recorded in the commit message of the 'dest-dir' git repo to |
| 109 |
provide tractability for the versions (see below). |
| 110 |
|
| 111 |
By default, this script does the following: |
| 112 |
|
| 113 |
1) Assert that 'orig-dir' in its git repo is clean (i.e. no uncommitted |
| 114 |
files). (Can be disabled by passing in --allow-dirty-orig-dir.) |
| 115 |
|
| 116 |
2) Assert that <dest-dir>/ in its git repo is clean (same checks as for |
| 117 |
'orig-dir' above). (Can be disabled by passing in --allow-dirty-dest-dir. |
| 118 |
Also, this must be skipped on the initial snapshot where <some-dist-dir>/ |
| 119 |
does not exist yet.) |
| 120 |
|
| 121 |
3) Clean out the ignored files from <some-source-dir>/orig-dir using 'git |
| 122 |
clean -xdf' run in that directory. (Only if --clean-ignored-files-orig-dir |
| 123 |
is passed.) |
| 124 |
|
| 125 |
4) Run 'rsync -cav --delete [other options] <orig-dir>/ <dest-dir>/' to copy |
| 126 |
the contents from 'orig-dir' to 'dest-dir', excluding the '.git/' directory |
| 127 |
if it exists in either git repo dir. After this runs, <dest-dir>/ should |
| 128 |
be an exact duplicate of <orig-dir>/ (except for otherwise noted excluded |
| 129 |
files). This rsync will delete any files in 'dest-dir' that are not in |
| 130 |
'orig-dir'. Note that if there are any ignored untracked files in |
| 131 |
'orig-dir' that get copied over, then the copied .gitignore files should |
| 132 |
avoid treating them as tracked files in the 'dest-dir' git repo. (The |
| 133 |
rsync command is skipped if the argument --no-op is passed.) |
| 134 |
|
| 135 |
5) Run 'git add .' in <dest-dir>/ to stage any files copied over. (Note that |
| 136 |
git will automatically stage deletes for any files removed by the 'rsync |
| 137 |
-cav --delete' command. Also note that any untracked, unknown or ignored |
| 138 |
files in 'orig-dir' that get copied over and are not ignored in 'dist-dir' |
| 139 |
in the copied-over '.gitignore' files, or other git ignore files in |
| 140 |
'dist-dir', then they will be added to the new commit in 'dist-dir'.) |
| 141 |
|
| 142 |
6) Get the git remote URL from the orig-dir git repo, and the git log for the |
| 143 |
last commit for the directory orig-dir from its git repo. (If the orig-dir |
| 144 |
repo is on a tracking branch, then this remote will be guaranteed to be |
| 145 |
correct. However, if the orig-dir repo not on a tracking branch, then the |
| 146 |
first remote returned from 'git remote -v' will be used. This information |
| 147 |
is used to provide tractability of the version info back to the originating |
| 148 |
git repo in the commit created in the dest-dir repo.) |
| 149 |
|
| 150 |
7) Commit the updated dest-dir directory using a commit message with the |
| 151 |
orig-dir snapshot version info. (This will only commit files in 'dest-dir' |
| 152 |
and not in other directories in the destination git repo!) (The 'git |
| 153 |
commit' will be skipped if the options --skip-commit or --no-op are |
| 154 |
passed.) |
| 155 |
|
| 156 |
NOTES: |
| 157 |
|
| 158 |
* When first running this tool, use the --no-op option to see what commands |
| 159 |
would be run without actually performing any mutable operations. Especially |
| 160 |
pay attention to the rsync command that would be run and make sure it is |
| 161 |
operating on the desired directories. |
| 162 |
|
| 163 |
* On the first creation of <dest-dir>/, one must pass in |
| 164 |
--allow-dirty-dest-dir to avoid checks of <dest-dir>/ (because it does not yet |
| 165 |
exist). |
| 166 |
|
| 167 |
* This script allows the syncing between base git repos or subdirs within git |
| 168 |
repos. This is allowed because the rsync command is told to ignore the |
| 169 |
.git/ directory when syncing. |
| 170 |
|
| 171 |
* The cleaning of orig-dir/ using 'git clean -xdf' may be somewhat dangerous |
| 172 |
but it is recommended by passing in --clean-ignored-files-orig-dir to avoid |
| 173 |
copying locally-ignored files in orig-dir/ (e.g. ignored in |
| 174 |
.git/info/excludes and not ignored in a committed .gitignore file in |
| 175 |
orig-dir/) that get copied to and then committed in the dest-dir/ repo. |
| 176 |
Therefore, be sure you don't have any of these type of ignored files in |
| 177 |
orig-dir/ that you want to keep before you run this tool with the option |
| 178 |
--clean-ignored-files-orig-dir! |
| 179 |
|
| 180 |
* Snapshotting with this script will create an exact duplicate of 'orig-dir' |
| 181 |
in 'dest-dir' and therefore if there are any local changes to the files or |
| 182 |
changes after the last snapshot, they will get wiped out. To avoid this, |
| 183 |
one can the snapshot on a branch in the 'dest-dir' git repo, then merge that |
| 184 |
branch into the main branch (e.g. 'master') in 'dest-dir' repo. As long as |
| 185 |
there are no merge conflicts, this will preserve local changes for the |
| 186 |
mirrored directories and files. This strategy can work well as a way to |
| 187 |
allow for local modifications but still do snapshotting. |
| 188 |
|
| 189 |
WARNINGS: |
| 190 |
|
| 191 |
* Make sure that orig-dir is a non-ignored subdir of the origin git repo and |
| 192 |
that it does not contain any ignored subdirs and files that are ignored by |
| 193 |
listing them in the .git/info/exclude file instead of .gitignore files that |
| 194 |
would get copied over to dist-dir. (If ignored files are ignored using |
| 195 |
.gitignore files within orig-dir, then those files will be copied by the |
| 196 |
rsync command along with the rest of the files from orig-dir and those files |
| 197 |
will also be ignored after being copied to dest-dir. However, if these |
| 198 |
files and dirs are ignored because of being listed in the .git/info/exclude |
| 199 |
file in orig-dir, then those files will not be ignored when copied over to |
| 200 |
dest-dir and will therefore be added to the git commit in the dist-dir git |
| 201 |
repo. That is why it is recommended to run with |
| 202 |
--clean-ignored-files-orig-dir to ensure that all ignore files are removed |
| 203 |
from orig-dir before doing the rsync.) |
| 204 |
|
| 205 |
* Make sure that the top commit in orig-dir has been pushed (or will be |
| 206 |
pushed) to the listed remote git repo showed in the output line 'origin |
| 207 |
remote name' or 'origin remote URL'. Otherwise, others will not be able to |
| 208 |
trace that exact version by cloning that repo and traceability is lost. |
| 209 |
|
| 210 |
* Make sure that dest-dir is a non-ignored subdir of the destination git repo |
| 211 |
and does not contain any ignored subdirs or files that you don't care if |
| 212 |
they are deleted. (Otherwise, the 'rsync --delete' command will delete any |
| 213 |
files in dest-dir that are not also in orig-dir and since these non-ignored |
| 214 |
files in dest-dir are not under version control, they will not be |
| 215 |
recoverable.) |
| 216 |
|
| 217 |
* As a corollary to the above warnings about ignored files and directories, do |
| 218 |
not snapshot from an orig-dir or to a dest-dir that contain build |
| 219 |
directories or other generated files that you want to keep! Even if those |
| 220 |
files and directories are ignored in copied-over .gitignore files, the copy |
| 221 |
of those ignored files will still occur. (Just another reason to keep your |
| 222 |
build directories completely outside of the source tree!) |
| 223 |
|
| 224 |
""" |
| 225 |
|
| 226 |
|
| 227 |
# Direct script driver (taking in command-line arguments) |
| 228 |
# |
| 229 |
# This runs given a set of command-line arguments and a stream to do |
| 230 |
# outputting to. This allows running an a unit testing environment very |
| 231 |
# efficiently. |
| 232 |
# |
| 233 |
def snapshotDirMainDriver(cmndLineArgs, defaultOptionsIn = None, stdout = None): |
| 234 |
|
| 235 |
oldstdout = sys.stdout |
| 236 |
|
| 237 |
try: |
| 238 |
|
| 239 |
if stdout: |
| 240 |
sys.stdout = stdout |
| 241 |
|
| 242 |
if defaultOptionsIn: |
| 243 |
defaultOptions = defaultOptionsIn |
| 244 |
else: |
| 245 |
defaultOptions = DefaultOptions() |
| 246 |
defaultOptions.setDefaultDefaults() |
| 247 |
|
| 248 |
#print("cmndLineArgs = " + str(cmndLineArgs)) |
| 249 |
|
| 250 |
# |
| 251 |
# A) Get the command-line options |
| 252 |
# |
| 253 |
|
| 254 |
from argparse import ArgumentParser, RawDescriptionHelpFormatter |
| 255 |
|
| 256 |
clp = ArgumentParser( |
| 257 |
description=usageHelp, |
| 258 |
formatter_class=RawDescriptionHelpFormatter) |
| 259 |
|
| 260 |
clp.add_argument( |
| 261 |
"--show-defaults", dest="showDefaults", action="store_true", |
| 262 |
help="Show the default option values and do nothing at all.", |
| 263 |
default=False ) |
| 264 |
|
| 265 |
clp.add_argument( |
| 266 |
"--orig-dir", dest="origDir", |
| 267 |
default=defaultOptions.getDefaultOrigDir(), |
| 268 |
help="Original directory that is the source for the snapshotted directory." \ |
| 269 |
+" If a trailing '/' is missing then it will be added." \ |
| 270 |
+" The default is the directory where this script lives (or is soft-linked)." \ |
| 271 |
+" [default: '"+defaultOptions.getDefaultOrigDir()+"']") |
| 272 |
|
| 273 |
clp.add_argument( |
| 274 |
"--dest-dir", dest="destDir", |
| 275 |
default=defaultOptions.getDefaultDestDir(), |
| 276 |
help="Destination directory that is the target for the snapshoted directory." \ |
| 277 |
+" If a trailing '/' is missing then it will be added." \ |
| 278 |
+" The default dest-dir is current working directory." \ |
| 279 |
+" [default: '"+defaultOptions.getDefaultDestDir()+"']" \ |
| 280 |
) |
| 281 |
|
| 282 |
clp.add_argument( |
| 283 |
"--exclude", dest="exclude", default=None, nargs="*", |
| 284 |
help="List of files/directories/globs to exclude from orig-dir when " \ |
| 285 |
+"snapshotting.") |
| 286 |
|
| 287 |
clp.add_argument( |
| 288 |
"--assert-clean-orig-dir", dest="assertCleanOrigDir", action="store_true", |
| 289 |
default=True, |
| 290 |
help="Check that orig-dir is committed and clean. [default]" ) |
| 291 |
clp.add_argument( |
| 292 |
"--allow-dirty-orig-dir", dest="assertCleanOrigDir", action="store_false", |
| 293 |
help="Skip clean check of orig-dir." ) |
| 294 |
|
| 295 |
clp.add_argument( |
| 296 |
"--assert-clean-dest-dir", dest="assertCleanDestDir", action="store_true", |
| 297 |
default=True, |
| 298 |
help="Check that dest-dir is committed and clean. [default]" ) |
| 299 |
clp.add_argument( |
| 300 |
"--allow-dirty-dest-dir", dest="assertCleanDestDir", action="store_false", |
| 301 |
help="Skip clean check of dest-dir." ) |
| 302 |
|
| 303 |
clp.add_argument( |
| 304 |
"--clean-ignored-files-orig-dir", dest="cleanIgnoredFilesOrigDir", action="store_true", |
| 305 |
default=False, |
| 306 |
help="Clean out the ignored files from orig-dir/ before snapshotting." ) |
| 307 |
clp.add_argument( |
| 308 |
"--no-clean-ignored-files-orig-dir", dest="cleanIgnoredFilesOrigDir", action="store_false", |
| 309 |
help="Do not clean out orig-dir/ ignored files before snapshotting. [default]" ) |
| 310 |
|
| 311 |
clp.add_argument( |
| 312 |
"--do-commit", dest="doCommit", action="store_true", |
| 313 |
default=True, |
| 314 |
help="Actually do the commit. [default]" ) |
| 315 |
clp.add_argument( |
| 316 |
"--skip-commit", dest="doCommit", action="store_false", |
| 317 |
help="Skip the commit." ) |
| 318 |
|
| 319 |
clp.add_argument( |
| 320 |
"--verify-commit", dest="noVerifyCommit", action="store_false", |
| 321 |
default=False, |
| 322 |
help="Do not pass --no-verify to git commit. [default]" ) |
| 323 |
clp.add_argument( |
| 324 |
"--no-verify-commit", dest="noVerifyCommit", action="store_true", |
| 325 |
help="Pass --no-verify to git commit." ) |
| 326 |
|
| 327 |
clp.add_argument( |
| 328 |
"--no-op", dest="noOp", action="store_true", |
| 329 |
default=False, |
| 330 |
help="Don't actually run any commands that would change the state other" |
| 331 |
+" (other than the natural side-effects of running git query commands)" ) |
| 332 |
|
| 333 |
options = clp.parse_args(cmndLineArgs) |
| 334 |
|
| 335 |
# |
| 336 |
# B) Echo the command-line |
| 337 |
# |
| 338 |
|
| 339 |
print("") |
| 340 |
print("**************************************************************************") |
| 341 |
print("Script: snapshot-dir.py \\") |
| 342 |
|
| 343 |
print(" --orig-dir='" + options.origDir + "' \\") |
| 344 |
print(" --dest-dir='" + options.destDir + "' \\") |
| 345 |
if options.exclude: |
| 346 |
print(" --exclude " + " ".join(options.exclude) + " \\") |
| 347 |
if options.assertCleanOrigDir: |
| 348 |
print(" --assert-clean-orig-dir \\") |
| 349 |
else: |
| 350 |
print(" --allow-dirty-orig-dir \\") |
| 351 |
if options.assertCleanDestDir: |
| 352 |
print(" --assert-clean-dest-dir \\") |
| 353 |
else: |
| 354 |
print(" --allow-dirty-dest-dir \\") |
| 355 |
if options.cleanIgnoredFilesOrigDir: |
| 356 |
print(" --clean-ignored-files-orig-dir \\") |
| 357 |
else: |
| 358 |
print(" --no-clean-ignored-files-orig-dir \\") |
| 359 |
if options.doCommit: |
| 360 |
print(" --do-commit \\") |
| 361 |
else: |
| 362 |
print(" --skip-commit \\") |
| 363 |
if options.noVerifyCommit: |
| 364 |
print(" --no-verify-commit \\") |
| 365 |
else: |
| 366 |
print(" --verify-commit \\") |
| 367 |
if options.noOp: |
| 368 |
print(" --no-op \\") |
| 369 |
|
| 370 |
if options.showDefaults: |
| 371 |
return # All done! |
| 372 |
|
| 373 |
# |
| 374 |
# C) Execute the |
| 375 |
# |
| 376 |
|
| 377 |
snapshotDir(options) |
| 378 |
|
| 379 |
finally: |
| 380 |
sys.stdout = oldstdout |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
# |
| 385 |
# Implement the guts of snapshoting after reading in options |
| 386 |
# |
| 387 |
|
| 388 |
def snapshotDir(inOptions): |
| 389 |
|
| 390 |
addTrailingSlashToPaths(inOptions) |
| 391 |
|
| 392 |
# |
| 393 |
print("\nA) Assert that orig-dir is 100% clean with all changes committed\n") |
| 394 |
# |
| 395 |
|
| 396 |
if inOptions.assertCleanOrigDir: |
| 397 |
assertCleanGitDir(inOptions.origDir, "origin", |
| 398 |
"The created snapshot commit would not have the correct origin commit info!" ) |
| 399 |
else: |
| 400 |
print("Skipping on request!") |
| 401 |
|
| 402 |
# |
| 403 |
print("\nB) Assert that dest-dir is 100% clean with all changes committed\n") |
| 404 |
# |
| 405 |
|
| 406 |
if inOptions.assertCleanDestDir: |
| 407 |
assertCleanGitDir(inOptions.destDir, "destination", |
| 408 |
"Location changes in the destination directory would be overritten and lost!") |
| 409 |
else: |
| 410 |
print("Skipping on request!") |
| 411 |
|
| 412 |
# |
| 413 |
print("\nC) Cleaning out ignored files in orig-dir\n") |
| 414 |
# |
| 415 |
|
| 416 |
if inOptions.cleanIgnoredFilesOrigDir: |
| 417 |
cleanIgnoredFilesFromGitDir(inOptions.origDir, inOptions.noOp, "origin") |
| 418 |
else: |
| 419 |
print("Skipping on request!") |
| 420 |
|
| 421 |
# |
| 422 |
print("\nD) Get info for git commit from orig-dir [optional]\n") |
| 423 |
# |
| 424 |
|
| 425 |
# Get the repo for origin |
| 426 |
(remoteRepoName, remoteBranch, remoteRepoUrl) = \ |
| 427 |
getGitRepoRemoteNameBranchAndUrl(inOptions.origDir) |
| 428 |
print("origin remote name = '" + remoteRepoName + "'") |
| 429 |
print("origin remote branch = '" + remoteBranch + "'") |
| 430 |
print("origin remote URL = '" + remoteRepoUrl + "'") |
| 431 |
gitDescribe = getGitDescribe(inOptions.origDir) |
| 432 |
print("Git describe = '" + gitDescribe + "'") |
| 433 |
|
| 434 |
# Get the last commit message |
| 435 |
originLastCommitMsg = getLastCommitMsg(inOptions.origDir) |
| 436 |
print("\norigin commit message:") |
| 437 |
print("---------------------------------------") |
| 438 |
print(originLastCommitMsg) |
| 439 |
print("---------------------------------------") |
| 440 |
|
| 441 |
# |
| 442 |
print("\nE) Run rsync to add and remove files and dirs between two directories\n") |
| 443 |
# |
| 444 |
|
| 445 |
excludes = r"""--exclude=\.git""" |
| 446 |
if inOptions.exclude: |
| 447 |
excludes += " " + " ".join(map(lambda ex: "--exclude="+ex, |
| 448 |
inOptions.exclude)) |
| 449 |
print("Excluding files/directories/globs: " + |
| 450 |
" ".join(inOptions.exclude)) |
| 451 |
# Note that when syncing one git repo to another, we want to sync the |
| 452 |
# .gitingore and other hidden files as well. |
| 453 |
|
| 454 |
# When we support syncing from hg repos, add these excludes as well: |
| 455 |
# --exclude=\.hg --exclude=.hgignore --exclude=.hgtags |
| 456 |
|
| 457 |
rsyncCmnd = \ |
| 458 |
r"rsync -cav --delete "+excludes+" "+inOptions.origDir+" "+inOptions.destDir |
| 459 |
|
| 460 |
if not inOptions.noOp: |
| 461 |
rtn = echoRunSysCmnd(rsyncCmnd, throwExcept=False, timeCmnd=True) |
| 462 |
if rtn != 0: |
| 463 |
print("Rsync failed, aborting!") |
| 464 |
return False |
| 465 |
else: |
| 466 |
print("Would be running: "+rsyncCmnd) |
| 467 |
|
| 468 |
# |
| 469 |
print("\nE) Create a new commit in dest-dir [optional]") |
| 470 |
# |
| 471 |
|
| 472 |
origDirLast = inOptions.origDir.split("/")[-2] |
| 473 |
origSha1 = getCommitSha1(inOptions.origDir) |
| 474 |
|
| 475 |
commitMessage = \ |
| 476 |
"Automatic snapshot commit from "+origDirLast+" at "+origSha1+"\n"+\ |
| 477 |
"\n" |
| 478 |
|
| 479 |
if remoteBranch: |
| 480 |
commitMessage += \ |
| 481 |
"Origin repo remote tracking branch: '"+remoteRepoName+"/"+remoteBranch+"'\n"+\ |
| 482 |
"Origin repo remote repo URL: '"+remoteRepoName+" = "+remoteRepoUrl+"'\n" |
| 483 |
else: |
| 484 |
commitMessage += \ |
| 485 |
"Origin repo remote repo URL: '"+remoteRepoName+" = "+remoteRepoUrl+"'\n" |
| 486 |
|
| 487 |
commitMessage += \ |
| 488 |
"Git describe: "+gitDescribe+"\n" +\ |
| 489 |
"\n"+\ |
| 490 |
"At commit:\n"+\ |
| 491 |
"\n"+\ |
| 492 |
originLastCommitMsg |
| 493 |
|
| 494 |
print("\nGenerating commit in dest-dir with commit message:\n") |
| 495 |
print("---------------------------------------") |
| 496 |
print(commitMessage) |
| 497 |
print("---------------------------------------") |
| 498 |
|
| 499 |
if inOptions.doCommit: |
| 500 |
|
| 501 |
gitAddCmnd = "git add ." |
| 502 |
if not inOptions.noOp: |
| 503 |
echoRunSysCmnd(gitAddCmnd, workingDir=inOptions.destDir) |
| 504 |
else: |
| 505 |
print("\nWould be running: "+gitAddCmnd+"\n" \ |
| 506 |
+"\n in directory '"+inOptions.destDir+"'" ) |
| 507 |
|
| 508 |
if inOptions.noVerifyCommit: |
| 509 |
noVerifyCommitArgStr = " --no-verify" |
| 510 |
else: |
| 511 |
noVerifyCommitArgStr = "" |
| 512 |
|
| 513 |
gitCommitCmndBegin = "git commit"+noVerifyCommitArgStr+" -m " |
| 514 |
if not inOptions.noOp: |
| 515 |
echoRunSysCmnd(gitCommitCmndBegin+"\""+commitMessage+"\"", |
| 516 |
workingDir=inOptions.destDir) |
| 517 |
else: |
| 518 |
print("\nWould be running: "+gitCommitCmndBegin+"\"<commit-msg>\"\n" |
| 519 |
+"\n in directory '"+inOptions.destDir+"'" ) |
| 520 |
|
| 521 |
else: |
| 522 |
|
| 523 |
print("\nSkipping commit on request!\n") |
| 524 |
|
| 525 |
if inOptions.noOp: |
| 526 |
|
| 527 |
print( |
| 528 |
"\n***\n" |
| 529 |
"*** NOTE: No modifying operations were performed!\n" |
| 530 |
"***\n" |
| 531 |
"*** Run again removing the option --no-op to make modifying\n" |
| 532 |
"*** changes.\n" |
| 533 |
"***\n" |
| 534 |
"*** But first, carefully look at the orig-dir, dest-dir and the\n" |
| 535 |
"*** various operations performed above to make sure that\n" |
| 536 |
"*** everything is as it should be before removing the option --no-op.\n" |
| 537 |
"***\n" |
| 538 |
"*** In particular, look carefully at the 'git clean' and 'rsync' commands\n" |
| 539 |
"*** on the lines that begin with 'Would be running:'\n" |
| 540 |
"***\n" |
| 541 |
"***\n" |
| 542 |
"***\n" |
| 543 |
"***\n" |
| 544 |
) |
| 545 |
|
| 546 |
# |
| 547 |
# F) Success! (if you get this far) |
| 548 |
# |
| 549 |
|
| 550 |
return True |
| 551 |
|
| 552 |
|
| 553 |
# |
| 554 |
# Helper functions |
| 555 |
# |
| 556 |
|
| 557 |
|
| 558 |
def addTrailingSlashToPath(path): |
| 559 |
if path[-1] != "/": |
| 560 |
return path + "/" |
| 561 |
return path |
| 562 |
|
| 563 |
|
| 564 |
def addTrailingSlashToPaths(options): |
| 565 |
options.origDir = addTrailingSlashToPath(options.origDir) |
| 566 |
options.destDir = addTrailingSlashToPath(options.destDir) |
| 567 |
|
| 568 |
|
| 569 |
def assertCleanGitDir(dirPath, dirName, explanation): |
| 570 |
|
| 571 |
changedFiles = getCmndOutput( |
| 572 |
"git diff --name-status HEAD -- .", |
| 573 |
stripTrailingSpaces = True, |
| 574 |
workingDir = dirPath ) |
| 575 |
|
| 576 |
if changedFiles: |
| 577 |
raise Exception( |
| 578 |
"Error, aborting snapshot!\n" \ |
| 579 |
"The "+dirName+" git directory '"+dirPath+"' is not clean and" \ |
| 580 |
+" has the changed files:\n"+changedFiles+"\n" \ |
| 581 |
+explanation |
| 582 |
) |
| 583 |
else: |
| 584 |
print("The " + dirName + " git directory '" + dirPath + "' is clean!") |
| 585 |
|
| 586 |
# NOTE: The above git diff command will not catch unknown files but that is |
| 587 |
# not a huge risk for the use cases that I am concerned with. |
| 588 |
|
| 589 |
|
| 590 |
def cleanIgnoredFilesFromGitDir(dirPath, noOp, dirName): |
| 591 |
gitCleanCmnd = r"git clean -xdf" |
| 592 |
if not noOp: |
| 593 |
rtn = echoRunSysCmnd(gitCleanCmnd, workingDir=dirPath, |
| 594 |
throwExcept=False, timeCmnd=True) |
| 595 |
if rtn != 0: |
| 596 |
raise Exception( |
| 597 |
"Error, cleaning of origin `"+dirPath+"` failed!") |
| 598 |
else: |
| 599 |
print("Would be running: "+gitCleanCmnd+"\n" \ |
| 600 |
+"\n in directory '"+dirPath+"'" ) |
| 601 |
|
| 602 |
|
| 603 |
def getCommitSha1(gitDir): |
| 604 |
return getCmndOutput("git log -1 --pretty=format:'%h' -- .", workingDir=gitDir).strip() |
| 605 |
|
| 606 |
|
| 607 |
def getGitRepoRemoteNameBranchAndUrl(gitDir): |
| 608 |
|
| 609 |
remoteRepoName = "" |
| 610 |
remoteBranch = "" |
| 611 |
remoteRepoUrl = "" |
| 612 |
|
| 613 |
# Get the remote tracking branch |
| 614 |
(trackingBranchStr, trackingBranchErrCode) = getCmndOutput( |
| 615 |
"git rev-parse --abbrev-ref --symbolic-full-name @{u}", workingDir=gitDir, |
| 616 |
throwOnError=False, rtnCode=True) |
| 617 |
|
| 618 |
if trackingBranchErrCode == 0: |
| 619 |
(remoteRepoName, remoteBranch) = trackingBranchStr.strip().split("/") |
| 620 |
else: |
| 621 |
remoteRepoName = "" |
| 622 |
remoteBranch = "" |
| 623 |
|
| 624 |
# Get the list of remote repos |
| 625 |
remoteReposListStr = getCmndOutput("git remote -v", workingDir=gitDir) |
| 626 |
#print("remoteReposListStr = " + remoteReposListStr) |
| 627 |
|
| 628 |
# Loop through looking for remoteRepoName |
| 629 |
for remoteRepo in remoteReposListStr.splitlines(): |
| 630 |
|
| 631 |
#print("remoteRepo = '" + remoteRepo + "'") |
| 632 |
if remoteRepo == "": |
| 633 |
continue |
| 634 |
|
| 635 |
remoteRepoList = remoteRepo.split(" ") |
| 636 |
#print("remoteRepoList = " + str(remoteRepoList)) |
| 637 |
|
| 638 |
# Remove empty items |
| 639 |
k = 0 |
| 640 |
while k < len(remoteRepoList): |
| 641 |
if remoteRepoList[k] == "": |
| 642 |
del remoteRepoList[k] |
| 643 |
k += 1 |
| 644 |
#print("remoteRepoList = " + str(remoteRepoList)) |
| 645 |
|
| 646 |
# Get the remote name and URL |
| 647 |
(repoName, repoUrl) = remoteRepoList[0].split("\t") |
| 648 |
#print("repoName = '" + repoName + "'") |
| 649 |
#print("repoUrl = '" + repoUrl + "'") |
| 650 |
|
| 651 |
if remoteRepoName: |
| 652 |
# Grab the URL if the remote name matches |
| 653 |
if repoName == remoteRepoName: |
| 654 |
remoteRepoUrl = repoUrl |
| 655 |
break |
| 656 |
else: |
| 657 |
# Just grab the first remote name you find if there is no tracking branch |
| 658 |
remoteRepoName = repoName |
| 659 |
remoteRepoUrl = repoUrl |
| 660 |
break |
| 661 |
|
| 662 |
# end for |
| 663 |
|
| 664 |
return (remoteRepoName, remoteBranch, remoteRepoUrl) |
| 665 |
|
| 666 |
|
| 667 |
def getGitDescribe(gitDir): |
| 668 |
|
| 669 |
gitDescribe = getCmndOutput( "git describe", workingDir=gitDir, stripTrailingSpaces=True) |
| 670 |
|
| 671 |
return gitDescribe |
| 672 |
|
| 673 |
|
| 674 |
def getLastCommitMsg(gitDir): |
| 675 |
return getCmndOutput( |
| 676 |
"git log " \ |
| 677 |
+" --pretty=format:'commit %H%nAuthor: %an <%ae>%nDate: %ad%nSummary: %s%n'" \ |
| 678 |
+" -1 -- .", |
| 679 |
workingDir=gitDir |
| 680 |
) |
| 681 |
|
| 682 |
# LocalWords: traceability TriBITS Snapshotting snapshotting |