|
@@ -16,7 +16,7 @@ __author__ = "Ben Kurtovic" |
|
|
__copyright__ = "Copyright (c) 2011-2012 Ben Kurtovic" |
|
|
__copyright__ = "Copyright (c) 2011-2012 Ben Kurtovic" |
|
|
__license__ = "MIT License" |
|
|
__license__ = "MIT License" |
|
|
__version__ = "0.1" |
|
|
__version__ = "0.1" |
|
|
__email__ = "ben.kurtovic@verizon.net" |
|
|
|
|
|
|
|
|
__email__ = "ben.kurtovic@gmail.com" |
|
|
|
|
|
|
|
|
config_filename = os.path.join(os.path.expanduser("~"), ".gitup") |
|
|
config_filename = os.path.join(os.path.expanduser("~"), ".gitup") |
|
|
|
|
|
|
|
@@ -37,7 +37,7 @@ def style_text(text, effect): |
|
|
"yellow": "\x1b[1m\x1b[33m", |
|
|
"yellow": "\x1b[1m\x1b[33m", |
|
|
"blue": "\x1b[1m\x1b[34m", |
|
|
"blue": "\x1b[1m\x1b[34m", |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: # pad text with effect, unless effect does not exist |
|
|
try: # pad text with effect, unless effect does not exist |
|
|
return "{}{}{}".format(ansi[effect], text, ansi['reset']) |
|
|
return "{}{}{}".format(ansi[effect], text, ansi['reset']) |
|
|
except KeyError: |
|
|
except KeyError: |
|
@@ -72,10 +72,10 @@ def directory_is_git_repo(directory_path): |
|
|
def update_repository(repo_path, repo_name): |
|
|
def update_repository(repo_path, repo_name): |
|
|
"""Update a single git repository by pulling from the remote.""" |
|
|
"""Update a single git repository by pulling from the remote.""" |
|
|
out(1, bold(repo_name) + ":") |
|
|
out(1, bold(repo_name) + ":") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
os.chdir(repo_path) # cd into our folder so git commands target the correct |
|
|
os.chdir(repo_path) # cd into our folder so git commands target the correct |
|
|
# repo |
|
|
# repo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
dry_fetch = exec_shell("git fetch --dry-run") # check if there is |
|
|
dry_fetch = exec_shell("git fetch --dry-run") # check if there is |
|
|
# anything to pull, but |
|
|
# anything to pull, but |
|
@@ -84,27 +84,27 @@ def update_repository(repo_path, repo_name): |
|
|
out(2, red("Error: ") + "cannot fetch; do you have a remote " + |
|
|
out(2, red("Error: ") + "cannot fetch; do you have a remote " + |
|
|
"repository configured correctly?") |
|
|
"repository configured correctly?") |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
last_commit = exec_shell("git log -n 1 --pretty=\"%ar\"") |
|
|
last_commit = exec_shell("git log -n 1 --pretty=\"%ar\"") |
|
|
except subprocess.CalledProcessError: |
|
|
except subprocess.CalledProcessError: |
|
|
last_commit = "never" # couldn't get a log, so no commits |
|
|
last_commit = "never" # couldn't get a log, so no commits |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not dry_fetch: # no new changes to pull |
|
|
if not dry_fetch: # no new changes to pull |
|
|
out(2, blue("No new changes.") + " Last commit was {}.".format( |
|
|
out(2, blue("No new changes.") + " Last commit was {}.".format( |
|
|
last_commit)) |
|
|
last_commit)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: # stuff has happened! |
|
|
else: # stuff has happened! |
|
|
out(2, "There are new changes upstream...") |
|
|
out(2, "There are new changes upstream...") |
|
|
status = exec_shell("git status") |
|
|
status = exec_shell("git status") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if status.endswith("nothing to commit (working directory clean)"): |
|
|
if status.endswith("nothing to commit (working directory clean)"): |
|
|
out(2, green("Pulling new changes...")) |
|
|
out(2, green("Pulling new changes...")) |
|
|
result = exec_shell("git pull") |
|
|
result = exec_shell("git pull") |
|
|
out(2, "The following changes have been made since {}:".format( |
|
|
out(2, "The following changes have been made since {}:".format( |
|
|
last_commit)) |
|
|
last_commit)) |
|
|
print result |
|
|
print result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
else: |
|
|
out(2, red("Warning: ") + "you have uncommitted changes in this " + |
|
|
out(2, red("Warning: ") + "you have uncommitted changes in this " + |
|
|
"repository!") |
|
|
"repository!") |
|
@@ -119,44 +119,44 @@ def update_directory(dir_path, dir_name, is_bookmark=False): |
|
|
dir_type = "bookmark" # where did we get this directory from? |
|
|
dir_type = "bookmark" # where did we get this directory from? |
|
|
else: |
|
|
else: |
|
|
dir_type = "directory" |
|
|
dir_type = "directory" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dir_long_name = "{} '{}'".format(dir_type, bold(dir_path)) |
|
|
dir_long_name = "{} '{}'".format(dir_type, bold(dir_path)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
os.listdir(dir_path) # test if we can access this directory |
|
|
os.listdir(dir_path) # test if we can access this directory |
|
|
except OSError: |
|
|
except OSError: |
|
|
out(0, red("Error: ") + "cannot enter {}; does it exist?".format( |
|
|
out(0, red("Error: ") + "cannot enter {}; does it exist?".format( |
|
|
dir_long_name)) |
|
|
dir_long_name)) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not os.path.isdir(dir_path): |
|
|
if not os.path.isdir(dir_path): |
|
|
if os.path.exists(dir_path): |
|
|
if os.path.exists(dir_path): |
|
|
out(0, red("Error: ") + dir_long_name + " is not a directory!") |
|
|
out(0, red("Error: ") + dir_long_name + " is not a directory!") |
|
|
else: |
|
|
else: |
|
|
out(0, red("Error: ") + dir_long_name + " does not exist!") |
|
|
out(0, red("Error: ") + dir_long_name + " does not exist!") |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if directory_is_git_repo(dir_path): |
|
|
if directory_is_git_repo(dir_path): |
|
|
out(0, dir_long_name.capitalize() + " is a git repository:") |
|
|
out(0, dir_long_name.capitalize() + " is a git repository:") |
|
|
update_repository(dir_path, dir_name) |
|
|
update_repository(dir_path, dir_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
else: |
|
|
repositories = [] |
|
|
repositories = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dir_contents = os.listdir(dir_path) # get potential repos in directory |
|
|
dir_contents = os.listdir(dir_path) # get potential repos in directory |
|
|
for item in dir_contents: |
|
|
for item in dir_contents: |
|
|
repo_path = os.path.join(dir_path, item) |
|
|
repo_path = os.path.join(dir_path, item) |
|
|
repo_name = os.path.join(dir_name, item) |
|
|
repo_name = os.path.join(dir_name, item) |
|
|
if directory_is_git_repo(repo_path): # filter out non-repositories |
|
|
if directory_is_git_repo(repo_path): # filter out non-repositories |
|
|
repositories.append((repo_path, repo_name)) |
|
|
repositories.append((repo_path, repo_name)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
num_of_repos = len(repositories) |
|
|
num_of_repos = len(repositories) |
|
|
if num_of_repos == 1: |
|
|
if num_of_repos == 1: |
|
|
out(0, dir_long_name.capitalize() + " contains 1 git repository:") |
|
|
out(0, dir_long_name.capitalize() + " contains 1 git repository:") |
|
|
else: |
|
|
else: |
|
|
out(0, dir_long_name.capitalize() + |
|
|
out(0, dir_long_name.capitalize() + |
|
|
" contains {} git repositories:".format(num_of_repos)) |
|
|
" contains {} git repositories:".format(num_of_repos)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
repositories.sort() # go alphabetically instead of randomly |
|
|
repositories.sort() # go alphabetically instead of randomly |
|
|
for repo_path, repo_name in repositories: |
|
|
for repo_path, repo_name in repositories: |
|
|
update_repository(repo_path, repo_name) |
|
|
update_repository(repo_path, repo_name) |
|
@@ -174,7 +174,7 @@ def update_bookmarks(): |
|
|
bookmarks = load_config_file().items("bookmarks") |
|
|
bookmarks = load_config_file().items("bookmarks") |
|
|
except configparser.NoSectionError: |
|
|
except configparser.NoSectionError: |
|
|
bookmarks = [] |
|
|
bookmarks = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if bookmarks: |
|
|
if bookmarks: |
|
|
for bookmark_path, bookmark_name in bookmarks: |
|
|
for bookmark_path, bookmark_name in bookmarks: |
|
|
update_directory(bookmark_path, bookmark_name, is_bookmark=True) |
|
|
update_directory(bookmark_path, bookmark_name, is_bookmark=True) |
|
@@ -202,9 +202,9 @@ def add_bookmarks(paths): |
|
|
config = load_config_file() |
|
|
config = load_config_file() |
|
|
if not config.has_section("bookmarks"): |
|
|
if not config.has_section("bookmarks"): |
|
|
config.add_section("bookmarks") |
|
|
config.add_section("bookmarks") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out(0, yellow("Added bookmarks:")) |
|
|
out(0, yellow("Added bookmarks:")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for path in paths: |
|
|
for path in paths: |
|
|
path = os.path.abspath(path) # convert relative to absolute path |
|
|
path = os.path.abspath(path) # convert relative to absolute path |
|
|
if config.has_option("bookmarks", path): |
|
|
if config.has_option("bookmarks", path): |
|
@@ -213,13 +213,13 @@ def add_bookmarks(paths): |
|
|
path_name = os.path.split(path)[1] |
|
|
path_name = os.path.split(path)[1] |
|
|
config.set("bookmarks", path, path_name) |
|
|
config.set("bookmarks", path, path_name) |
|
|
out(1, bold(path)) |
|
|
out(1, bold(path)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
save_config_file(config) |
|
|
save_config_file(config) |
|
|
|
|
|
|
|
|
def delete_bookmarks(paths): |
|
|
def delete_bookmarks(paths): |
|
|
"""Remove a list of paths from the bookmark config file.""" |
|
|
"""Remove a list of paths from the bookmark config file.""" |
|
|
config = load_config_file() |
|
|
config = load_config_file() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if config.has_section("bookmarks"): |
|
|
if config.has_section("bookmarks"): |
|
|
out(0, yellow("Deleted bookmarks:")) |
|
|
out(0, yellow("Deleted bookmarks:")) |
|
|
for path in paths: |
|
|
for path in paths: |
|
@@ -230,7 +230,7 @@ def delete_bookmarks(paths): |
|
|
else: |
|
|
else: |
|
|
out(1, "'{}' is not bookmarked.".format(path)) |
|
|
out(1, "'{}' is not bookmarked.".format(path)) |
|
|
save_config_file(config) |
|
|
save_config_file(config) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
else: |
|
|
out(0, "There are no bookmarks to delete!") |
|
|
out(0, "There are no bookmarks to delete!") |
|
|
|
|
|
|
|
@@ -241,7 +241,7 @@ def list_bookmarks(): |
|
|
bookmarks = config.items("bookmarks") |
|
|
bookmarks = config.items("bookmarks") |
|
|
except configparser.NoSectionError: |
|
|
except configparser.NoSectionError: |
|
|
bookmarks = [] |
|
|
bookmarks = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if bookmarks: |
|
|
if bookmarks: |
|
|
out(0, yellow("Current bookmarks:")) |
|
|
out(0, yellow("Current bookmarks:")) |
|
|
for bookmark_path, bookmark_name in bookmarks: |
|
|
for bookmark_path, bookmark_name in bookmarks: |
|
@@ -255,53 +255,53 @@ def main(): |
|
|
repositories at once.""", epilog="""Both relative and absolute |
|
|
repositories at once.""", epilog="""Both relative and absolute |
|
|
paths are accepted by all arguments. Questions? Comments? Email the |
|
|
paths are accepted by all arguments. Questions? Comments? Email the |
|
|
author at {}.""".format(__email__), add_help=False) |
|
|
author at {}.""".format(__email__), add_help=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_u = parser.add_argument_group("updating repositories") |
|
|
group_u = parser.add_argument_group("updating repositories") |
|
|
group_b = parser.add_argument_group("bookmarking") |
|
|
group_b = parser.add_argument_group("bookmarking") |
|
|
group_m = parser.add_argument_group("miscellaneous") |
|
|
group_m = parser.add_argument_group("miscellaneous") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_u.add_argument('directories_to_update', nargs="*", metavar="path", |
|
|
group_u.add_argument('directories_to_update', nargs="*", metavar="path", |
|
|
help="""update all repositories in this directory (or the directory |
|
|
help="""update all repositories in this directory (or the directory |
|
|
itself, if it is a repo)""") |
|
|
itself, if it is a repo)""") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_u.add_argument('-u', '--update', action="store_true", help="""update |
|
|
group_u.add_argument('-u', '--update', action="store_true", help="""update |
|
|
all bookmarks (default behavior when called without arguments)""") |
|
|
all bookmarks (default behavior when called without arguments)""") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_b.add_argument('-a', '--add', dest="bookmarks_to_add", nargs="+", |
|
|
group_b.add_argument('-a', '--add', dest="bookmarks_to_add", nargs="+", |
|
|
metavar="path", help="add directory(s) as bookmarks") |
|
|
metavar="path", help="add directory(s) as bookmarks") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_b.add_argument('-d', '--delete', dest="bookmarks_to_del", nargs="+", |
|
|
group_b.add_argument('-d', '--delete', dest="bookmarks_to_del", nargs="+", |
|
|
metavar="path", |
|
|
metavar="path", |
|
|
help="delete bookmark(s) (leaves actual directories alone)") |
|
|
help="delete bookmark(s) (leaves actual directories alone)") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_b.add_argument('-l', '--list', dest="list_bookmarks", |
|
|
group_b.add_argument('-l', '--list', dest="list_bookmarks", |
|
|
action="store_true", help="list current bookmarks") |
|
|
action="store_true", help="list current bookmarks") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_m.add_argument('-h', '--help', action="help", |
|
|
group_m.add_argument('-h', '--help', action="help", |
|
|
help="show this help message and exit") |
|
|
help="show this help message and exit") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group_m.add_argument('-v', '--version', action="version", |
|
|
group_m.add_argument('-v', '--version', action="version", |
|
|
version="gitup version "+__version__) |
|
|
version="gitup version "+__version__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args() |
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print bold("gitup") + ": the git-repo-updater" |
|
|
print bold("gitup") + ": the git-repo-updater" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if args.bookmarks_to_add: |
|
|
if args.bookmarks_to_add: |
|
|
add_bookmarks(args.bookmarks_to_add) |
|
|
add_bookmarks(args.bookmarks_to_add) |
|
|
|
|
|
|
|
|
if args.bookmarks_to_del: |
|
|
if args.bookmarks_to_del: |
|
|
delete_bookmarks(args.bookmarks_to_del) |
|
|
delete_bookmarks(args.bookmarks_to_del) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if args.list_bookmarks: |
|
|
if args.list_bookmarks: |
|
|
list_bookmarks() |
|
|
list_bookmarks() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if args.directories_to_update: |
|
|
if args.directories_to_update: |
|
|
update_directories(args.directories_to_update) |
|
|
update_directories(args.directories_to_update) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if args.update: |
|
|
if args.update: |
|
|
update_bookmarks() |
|
|
update_bookmarks() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not any(vars(args).values()): # if they did not tell us to do anything, |
|
|
if not any(vars(args).values()): # if they did not tell us to do anything, |
|
|
update_bookmarks() # automatically update bookmarks |
|
|
update_bookmarks() # automatically update bookmarks |
|
|
|
|
|
|
|
|