Skip to content

Commit a01e44b

Browse files
committed
Breakdown Git.update() and Hg.update() into sequence of SCM methods, e.g. SCM.fetch(), SCM.checkout(), SCM.discard(), etc.
Git.update() would try to merge/fast-forward only if the working set is on a branch (merge/pull from detached state is meaningless). Added very verbose (-vv) flag which also prints query commands and output
1 parent 7e13407 commit a01e44b

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

mbed/mbed.py

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989

9090
# verbose logging
9191
verbose = False
92+
very_verbose = False
9293

9394

9495
# Logging and output
@@ -160,6 +161,9 @@ def pquery(command, stdin=None, **kwargs):
160161

161162
stdout, _ = proc.communicate(stdin)
162163

164+
if very_verbose:
165+
print str(stdout).strip()
166+
163167
if proc.returncode != 0:
164168
raise ProcessException(proc.returncode, command[0], ' '.join(command), os.getcwd())
165169

@@ -249,18 +253,21 @@ def remove(file):
249253
def commit():
250254
popen([hg_cmd, 'commit'] + (['-v'] if verbose else ['-q']))
251255

252-
def push(repo, all=None):
256+
def publish(repo, all=None):
253257
popen([hg_cmd, 'push'] + (['--new-branch'] if all else []) + (['-v'] if verbose else ['-q']))
254258

255-
def pull(repo):
259+
def fetch(repo):
260+
log("Pulling remote repository \"%s\" to local \"%s\"" % (repo.url, repo.name))
256261
popen([hg_cmd, 'pull'] + (['-v'] if verbose else ['-q']))
257262

263+
def checkout(repo, hash=None, clean=False):
264+
log("Checkout \"%s\" to %s" % (repo.name, repo.hashtype(hash, True)))
265+
popen([hg_cmd, 'update'] + (['-r', hash] if hash else []) + (['-C'] if clean else []) + (['-v'] if verbose else ['-q']))
266+
258267
def update(repo, hash=None, clean=False):
259268
if not repo.is_local:
260-
log("Pulling remote repository \"%s\" to local \"%s\"" % (repo.url, repo.name))
261-
popen([hg_cmd, 'pull'] + (['-v'] if verbose else ['-q']))
262-
log("Updating \"%s\" to %s" % (repo.name, repo.hashtype(hash, True)))
263-
popen([hg_cmd, 'update'] + (['-r', hash] if hash else []) + (['-C'] if clean else []) + (['-v'] if verbose else ['-q']))
269+
Hg.fetch(repo)
270+
Hg.checkout(repo, hash, clean)
264271

265272
def status():
266273
return pquery([hg_cmd, 'status'] + (['-v'] if verbose else ['-q']))
@@ -434,7 +441,7 @@ def remove(file):
434441
def commit():
435442
popen([git_cmd, 'commit', '-a'] + (['-v'] if verbose else ['-q']))
436443

437-
def push(repo, all=None):
444+
def publish(repo, all=None):
438445
if all:
439446
popen([git_cmd, 'push', '--all'] + (['-v'] if verbose else ['-q']))
440447
else:
@@ -443,32 +450,45 @@ def push(repo, all=None):
443450
if remote and branch:
444451
popen([git_cmd, 'push', remote, branch] + (['-v'] if verbose else ['-q']))
445452
else:
446-
err = "Unable to push outgoing changes for \"%s\" in \"%s\".\n" % (repo.name, repo.path)
453+
err = "Unable to publish outgoing changes for \"%s\" in \"%s\".\n" % (repo.name, repo.path)
447454
if not remote:
448-
error(err+"The local repository is not associated with a remote one.\n", 1)
455+
error(err+"The local repository is not associated with a remote one.", 1)
449456
if not branch:
450-
error(err+"Working set is not on a branch.\n", 1)
457+
error(err+"Working set is not on a branch.", 1)
451458

452-
453-
def pull(repo):
459+
def fetch(repo):
460+
log("Fetching remote repository \"%s\" to local \"%s\"" % (repo.url, repo.name))
454461
popen([git_cmd, 'fetch', '--all'] + (['-v'] if verbose else ['-q']))
455462

463+
def discard(repo):
464+
log("Discarding local changes in \"%s\"" % repo.name)
465+
popen([git_cmd, 'reset', 'HEAD'] + ([] if verbose else ['-q'])) # unmarks files for commit
466+
popen([git_cmd, 'checkout', '.'] + ([] if verbose else ['-q'])) # undo modified files
467+
popen([git_cmd, 'clean', '-fdq'] + ([] if verbose else ['-q'])) # cleans up untracked files and folders
468+
469+
def checkout(repo, hash=None, clean=False):
470+
log("Checkout \"%s\" to %s" % (repo.name, repo.hashtype(hash, True)))
471+
popen([git_cmd, 'checkout', hash] + ([] if verbose else ['-q']))
472+
456473
def update(repo, hash=None, clean=False):
457474
if clean:
458-
log("Discarding local changes in \"%s\"" % repo.name)
459-
popen([git_cmd, 'reset', 'HEAD'] + ([] if verbose else ['-q'])) # unmarks files for commit
460-
popen([git_cmd, 'checkout', '.'] + ([] if verbose else ['-q'])) # undo modified files
461-
popen([git_cmd, 'clean', '-fdq'] + ([] if verbose else ['-q'])) # cleans up untracked files and folders
475+
Git.discard(repo)
476+
if not repo.is_local:
477+
Git.fetch(repo)
462478
if hash:
463-
if not repo.is_local:
464-
log("Fetching remote repository \"%s\" to local \"%s\"" % (repo.url, repo.name))
465-
popen([git_cmd, 'fetch', '-v', '--all'] + (['-v'] if verbose else ['-q']))
466-
log("Updating \"%s\" to %s" % (repo.name, repo.hashtype(hash, True)))
467-
popen([git_cmd, 'checkout'] + [hash] + ([] if verbose else ['-q']))
479+
Git.checkout(repo, hash, clean)
468480
else:
469-
if not repo.is_local:
470-
log("Fetching remote repository \"%s\" to local \"%s\" and updating to latest revision in the current branch" % (repo.url, repo.name))
471-
popen([git_cmd, 'pull', '--all'] + (['-v'] if verbose else ['-q']))
481+
remote = Git.getremote()
482+
branch = Git.getbranch()
483+
if remote and branch:
484+
log("Merging \"%s\" with remote branch %s/%s from \"%s\"" % (repo.name, remote, branch, repo.url))
485+
popen([git_cmd, 'merge', "%s/%s" % (remote, branch)] + (['-v'] if verbose else ['-q']))
486+
else:
487+
err = "Unable to update \"%s\" in \"%s\".\n" % (repo.name, repo.path)
488+
if not remote:
489+
log(err+"The local repository is not associated with a remote one.")
490+
if not branch:
491+
log(err+"Working set is not on a branch.")
472492

473493
def status():
474494
return pquery([git_cmd, 'status', '-s'] + (['-v'] if verbose else []))
@@ -495,7 +515,7 @@ def outgoing():
495515
# Check if remote branch exists
496516
if not pquery([git_cmd, 'rev-parse', '%s/%s' % (remote, branch)]):
497517
return 1
498-
except ProcessException as e:
518+
except ProcessException:
499519
return 1
500520

501521
# Check for outgoing commits for the same remote branch
@@ -991,6 +1011,7 @@ def subcommand(command):
9911011
subparser.add_argument(*opt, **arg)
9921012

9931013
subparser.add_argument("-v", "--verbose", action="store_true", dest="verbose", help="Verbose diagnostic output")
1014+
subparser.add_argument("-vv", "--very_verbose", action="store_true", dest="very_verbose", help="Very verbose diagnostic output")
9941015

9951016
def thunk(parsed_args):
9961017
argv = [arg['dest'] if 'dest' in arg else arg['name'] for arg in args]
@@ -1182,14 +1203,14 @@ def publish(all=None, top=True):
11821203

11831204
if repo.scm.dirty():
11841205
action("Uncommitted changes in %s \"%s\" in \"%s\"" % (repo.pathtype(repo.path), repo.name, repo.path))
1185-
raw_input('Press enter to commit and push: ')
1206+
raw_input('Press enter to commit and publish: ')
11861207
repo.scm.commit()
11871208

11881209
try:
11891210
outgoing = repo.scm.outgoing()
11901211
if outgoing > 0:
11911212
action("Pushing local repository \"%s\" to remote \"%s\"" % (repo.name, repo.url))
1192-
repo.scm.push(repo, all)
1213+
repo.scm.publish(repo, all)
11931214
except ProcessException as e:
11941215
if e[0] != 1:
11951216
raise e
@@ -1552,7 +1573,8 @@ def toolchain_(name=None):
15521573
status = 1
15531574

15541575
try:
1555-
verbose = args.verbose
1576+
very_verbose = args.very_verbose
1577+
verbose = very_verbose or args.verbose
15561578
log('Working path \"%s\" (%s)' % (os.getcwd(), cwd_type))
15571579
status = args.command(args)
15581580
except ProcessException as e:

0 commit comments

Comments
 (0)