HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythongitMinor

Python script to synchronise your locally cloned fork to its parent github repository

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptforkyoursynchronisegithublocallyparentitspythoncloned

Problem

I recently wrote a python script that syncs your locally clone repository to its parent repository, so I would like you to review it and help me in improving it.

```
#!/usr/bin/python3
# coding=utf-8

__author__ = "Ratan Kulshreshtha"
__github__ = "RatanShreshtha"

import os
import sys
from subprocess import call, check_output

import requests

"""
This script runs a bunch of boilerplate code to synchronise
your locally cloned fork to its parent github repository.
"""

CURRENT_REPO_ORIGIN = ['git', 'config', '--get', 'remote.origin.url']
CURRENT_REPO_UPSTREAM = ['git', 'config', '--get', 'remote.upstream.url']
ADD_REMOTE_CMD = ['git', 'remote', 'add', 'upstream']
CHECK_REMOTES_CMD = ['git', 'remote', '-v']
FETCH_UPSTREAM_CMD = ['git', 'fetch', 'upstream']
CHECKOUT_MASTER_CMD = ['git', 'checkout', 'master']
MERGE_UPSTREAM_CMD = ['git', 'merge', 'upstream/master']
PUSH_TO_UPSTREAM_CMD = ['git', 'push', 'origin', 'master']

def checkGitRepository():
"""
Returns True if the repository is a git repository.
"""
return os.path.isdir('.git')

def getRepoOriginUrl():
"""
Return origin url of the git repository.
"""

try:
repo_origin_url = str(check_output(CURRENT_REPO_ORIGIN))
repo_origin_url = repo_origin_url.replace("b'", "").replace("\\n'", "")

print("origin url for this repository:- ", repo_origin_url)
return repo_origin_url
except Exception as e:
print("Unable to get origin url for the repository")
print(e)
raise

def getRepoUpstreamUrl():
"""
Return upstream url of the git repository.
"""

try:
repo_upstream_url = str(check_output(CURRENT_REPO_UPSTREAM))
repo_upstream_url = repo_upstream_url.replace("b'", "")
repo_upstream_url = repo_upstream_url.replace("\\n'", "")

print("upstream url for this repository:- ", repo_upstream_url)
return repo_upstream_url
except Exception as e:
print("Unable t

Solution

Overall, the code is organized in a modular fashion and it's clear what is going on. There are some things that need improvement though.

Code Style Improvements

  • follow Python naming conventions - you are using camel case style a lot, but, in Python, it is only used for class names



  • there are also some rules violated regarding the docstrings style. For instance, if it fits one line, don't add newlines; always start it with an upper case character and end with a dot



Other ideas

-
you can replace if repo_origin_url[0] == "h" (same for the g) with a more readable str.startswith() call:

if repo_origin_url.startswith("h"):


Or, you may even take it a step further and check for http and git substrings - it may be clearer from readability perspective.

Other high-level ideas

  • check out some git Python modules - you may replace manually constructing the git commands and subprocess calls with nice Python function calls. For example, there is that mature gitpython package



  • notice that most of your functions have the same layout - you have the main "meat" of the functions put into the try/except blocks. See if you can avoid that and just wrap the function calls with a single try/except instead



  • I think you are overloading the code with comments more than you should. The downside of putting a lot of comments is it they should always be up-to-date with the code they are attached to, it's easy to forget to update a comment after updating the relevant block of code. This may lead to confusing situations

Code Snippets

if repo_origin_url.startswith("h"):

Context

StackExchange Code Review Q#159451, answer score: 3

Revisions (0)

No revisions yet.