def stash(): # Getting jet directory and stash path to avoid re-fetching later jet_directory = hf.get_jet_directory() stash_path = os.path.join(jet_directory + '/.jet/stash/') # If there is a stash being currently stored. if os.path.exists(stash_path): # Ask if they're ok overwriting it response = raw_input("Are you sure you wish to overwrite your" " previous stash? (yes/no) ") # Accept y as shorthand for yes if not response == "y" or response == "yes": # If they said anything else, cancel the operation. print ("Cancelling....") return # completely remove all trace of it shutil.rmtree(stash_path) # Make the directory that will store the stash os.mkdir(stash_path) f = [] # Full filenames filenames_list = [] # Names of the files # Goes through all files in the current directory and below. for (dirpath, dirnames, filenames) in os.walk(jet_directory): for filename in filenames: # Checks they're not in the jet ignore file if hf.filter_one_file_by_ignore(filename): if not dirpath\ .startswith(os.path.join(jet_directory + '/.jet')): filenames_list.append(filename) f.append(os.path.join(dirpath, filename)) # Counter to loop through folder names with count = 0 for file_to_add in f: # A folder for each file, to store filename and contents separately. folder = os.path.join(stash_path + '/%s' % count) os.mkdir(folder) # Storing filename.. filename = os.path.join(stash_path + '/%s/filename.txt' % count) with open(filename, 'w') as myFile: myFile.write(file_to_add) filename = filenames_list[count] # Copying the contents over, to enable diffs to work new_filename = os.path.join(stash_path + '/%s/%s' % (count, filename)) shutil.copyfile(file_to_add, new_filename) # Increment the counter count += 1 # Alert user all went well! print ("Successfully stashed the code")
def display(): # This method shows all the branches and their parents # and prints to the console if not hf.already_initialized(): print ("Please init a jet repo before calling other commands") return print ("Branch name (parent)") print ("Master(root)") branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') if os.path.exists(branches_path): branches = hf.get_immediate_subdirectories(branches_path) # Directories are named after the branch, so grab names from here. for b in branches: print ("%s (%s)" % (b, hf.get_parent(b))) # Friendly reminder what branch is the current one. print ("You are currently on branch %s" % hf.get_branch())
def display(): # This method shows all the branches and their parents # and prints to the console if not hf.already_initialized(): print("Please init a jet repo before calling other commands") return print("Branch name (parent)") print("Master(root)") branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') if os.path.exists(branches_path): branches = hf.get_immediate_subdirectories(branches_path) # Directories are named after the branch, so grab names from here. for b in branches: print("%s (%s)" % (b, hf.get_parent(b))) # Friendly reminder what branch is the current one. print("You are currently on branch %s" % hf.get_branch())
def delete_branch(): if len(sys.argv) != 3: print ("Please form your delete commands $jet delete <branch_name>") return branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') # Check that the branches directory is there if os.path.exists(branches_path): if not os.path.exists(os.path.join(branches_path + sys.argv[2])): print ("Invalid branch name, please try again.") return else: print ("Invalid branch name, please try again") return if sys.argv[2] == 'master': # Master cannot be deleted because every other branch is a child of it. print ("Cannot delete master") return delete(sys.argv[2])
def delete_branch(): if len(sys.argv) != 3: print("Please form your delete commands $jet delete <branch_name>") return branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') # Check that the branches directory is there if os.path.exists(branches_path): if not os.path.exists(os.path.join(branches_path + sys.argv[2])): print("Invalid branch name, please try again.") return else: print("Invalid branch name, please try again") return if sys.argv[2] == 'master': # Master cannot be deleted because every other branch is a child of it. print("Cannot delete master") return delete(sys.argv[2])
def login(): if not hf.already_initialized(): print ("Please init a jet repo before calling other commands") return # Making sure the username is included in the command if len(sys.argv) != 3: print ("To login, type: \n $jet login <username> \nThis could" " be the username you use on www.jetvc.co.uk or one you" " wish to put with your commits") else: username = sys.argv[2] # Getting the filename to store the username in filename = os.path.join(hf.get_jet_directory() + '/.jet/username') with open(filename, 'w') as file_: # Writing the file file_.write(username) # All done!! print (hf.BColors.GREEN + "Welcome %s" % username + hf.BColors.ENDC)
def branch(branch_name): # Checking that there is no un-committed changed files filename = os.path.join(hf.get_branch_location() + 'changeset.txt') # Assume nothing has changed... changed_files = False # If there is a uncommitted changeset... if os.path.isfile(filename): changed_files = True # If any hashes don't match up if len(hf.get_changed_files(None, None)) > 0: changed_files = True # Error if there are uncommitted changed files... if changed_files: print ("You can't branch until you commit....") return # END changed files check # Checking that the given branch name is ok branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') if os.path.exists(branches_path): if os.path.exists(os.path.join(branches_path + branch_name))\ or branch_name == 'master': # Name exists, error print ("Already a branch with that name... please try another!") return else: # Make directory for the branch os.mkdir(os.path.join(branches_path + branch_name)) else: # Make the required directories os.mkdir(os.path.join(hf.get_jet_directory() + '/.jet/branches/')) os.mkdir(os.path.join(branches_path + branch_name)) # All ok, starting branching. f = [] # Contains the full path and filenames of all files to be branched filenames_list = [] # Contains all the file names (not full path) # Trawl all files in the users repo'd directory for (dirpath, dirnames, filenames) in os.walk(hf.get_jet_directory()): for filename in filenames: # Make sure no ignored files are present if hf.filter_one_file_by_ignore(filename): # No jet files to be included if not '.jet' in dirpath: # Add files name to filename list filenames_list.append(filename) # Add full path and filename to f f.append(os.path.join(dirpath, filename)) # Create a new file listing all the current files and hashes at this point file_name = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/latest_saved_files' % branch_name) with open(file_name, 'w') as file_: for file_to_add in f: file_.write(file_to_add + "\n") file_.write(hf.checksum_md5(file_to_add) + "\n") # Make a folder for the init commit of the branch os.mkdir('.jet/branches/%s/0/' % branch_name) # Store the files that are in the branch at the first commit file_name = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/0/file_log.txt' % branch_name) with open(file_name, 'w') as file_: for file_to_add in f: file_.write(file_to_add + "\n") count = 0 for file_to_add in f: # Make a folder to store details of that file folder = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/%s/%s' % (branch_name, 0, count)) os.mkdir(folder) # Storing the full filename filename = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/%s/%s/filename.txt' % (branch_name, 0, count)) with open(filename, 'w') as myFile: myFile.write(file_to_add) filename = filenames_list[count] # Store the contents of the file copyfile(file_to_add, os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/0/%s/%s' % (branch_name, count, filename))) count += 1 # Branch making complete, store details of branch print ("Branch %s made" % branch_name) # Storing parent branch filename = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/parent' % branch_name) old_branch = hf.get_branch() old_commit = hf.get_commit() with open(filename, 'w') as file_: file_.write(old_branch) # Old branch name file_.write('\n') file_.write(old_commit) # Old branch commit number # from where branched off # Now storing which is the current branch filename = os.path.join(hf.get_jet_directory() + '/.jet/branch') with open(filename, 'w') as file_: file_.write(branch_name) # Now storing the commit number (0) filename = os.path.join(hf.get_jet_directory() + '/.jet/current_commit') with open(filename, 'w') as file_: file_.write("0") # All done print ("You are now working in branch %s" % branch_name)
def delete(branch_name): # This method deletes the branch and all records of it from the repository. directory = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/' % branch_name) rmtree(directory) print ("Deleted")
def switch(): if not hf.already_initialized(): print ("Please init a jet repo before calling other commands") return if len(sys.argv) != 3: print ("Please form your switch commands $jet switch <branch_name>") return # Checks for any changed files, as you must have committed first!!! filename = os.path.join(hf.get_branch_location() + 'changeset.txt') # Assumes nothing has changed changed_files = False # If there is a changeset uncommitted if os.path.isfile(filename): changed_files = True # If any hashes don't match up... if len(hf.get_changed_files(None, None)) > 0: changed_files = True # Error if anything changed!! if changed_files: print ("You can't switch branch until you commit....") return # Master info stored separately, hence the if statement if sys.argv[2] == 'master': # Record master as the branch currently on filename = os.path.join(hf.get_jet_directory() + '/.jet/branch') with open(filename, 'w') as file_: file_.write(sys.argv[2]) # Do the revert process hf.revert(sys.argv[2], hf.get_highest_commit(sys.argv[2])) # Update the latest saved files with ones from new branch filename = os.path.join(hf.get_branch_location() + 'latest_saved_files') # Delete previous save os.remove(filename) with open(filename, 'w') as file_: for file_to_add in hf.get_current_files(None): file_.write(file_to_add + "\n") file_.write(hf.checksum_md5(file_to_add) + "\n") # All done! print ("Successfully switched to branch %s" % sys.argv[2]) return # Only here if the branch to switch to isn't master branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') if os.path.exists(branches_path): if os.path.exists(os.path.join(branches_path + sys.argv[2])): filename = os.path.join(hf.get_jet_directory() + '/.jet/branch') # Setting the new branch name as current branch with open(filename, 'w') as file_: file_.write(sys.argv[2]) hf.revert(sys.argv[2], hf.get_highest_commit(sys.argv[2])) filename = os.path.join(hf.get_branch_location() + 'latest_saved_files') os.remove(filename) with open(filename, 'w') as file_: for file_to_add in hf.get_current_files(None): file_.write(file_to_add + "\n") file_.write(hf.checksum_md5(file_to_add) + "\n") print ("Successfully switched to branch %s" % sys.argv[2]) else: print ("Invalid branch name") else: print ("Invalid branch name")
def unstash(): jet_directory = hf.get_jet_directory() if not hf.already_initialized(): print ("Please init a jet repo before calling other commands") return # Ensures there is actually some content stashed... stash_path = os.path.join(jet_directory + '/.jet/stash/') if not os.path.exists(stash_path): print ("Cannot unstash as there is no stashed content") return # Double check they wish to do this, as it's irreversible print ("Are you sure you wish to restore the code that is stashed?" " Any un-committed changes will be lost.") # Get response from user response = raw_input("This action is irreversible. (yes/no) ") # Allow y as well as yes if not response == "y" or response == "yes": print ("Cancelling...") return # Get the current files in the repo current_files = hf.get_current_files(None) # Get the stored files folders in the stash files = os.listdir(stash_path) for _file in files: # Get the actual filename stored_filename_filename = os.path.join(stash_path + '/%s/filename.txt' % _file) # Read the file with open(stored_filename_filename, 'r') as myFile: filename = myFile.read() try: # Check to see if the file is new by trying to remove current_files.remove(filename) new = False except ValueError: # New if error removing new = True try: # Attempt to hash the files contents current_hash = hf.checksum_md5(filename) except IOError: # Give fake value if there's a problem current_hash = "Not a file" # Get the actual filename (no path) head, name = os.path.split(filename) stored_contents_filename = os.path.join(stash_path + '/%s/%s' % (_file, name)) # Get the contents from the stored version with open(stored_contents_filename, 'r') as myFile: stored_contents = myFile.read() # Hash the stored version stored_contents_hash = hf.checksum_md5(stored_contents_filename) if current_hash == stored_contents_hash: # File is unchanged continue # File is either new or edited if new: # Make the directories to place the file in hf.make_directories(filename, clone=False) # Alert user the files being changed print ("Updating %s" % hf.relative(filename, os.getcwd())) # Change the file with open(filename, 'w') as new_file: new_file.write(stored_contents) # If the file needs deleting, delete it for file_to_delete in current_files: os.remove(file_to_delete) # Alert user it all worked! print ("Loaded in the code from the stash")
def commit(message, verbose): filename = os.path.join(hf.get_branch_location() + 'changeset.txt') # Checks to see if there are any files in the changeset at all... if os.path.isfile(filename): files_in_changeset = hf.get_files_in_changeset(None) new_files_in_changeset\ = hf.get_new_files_in_changeset(files_in_changeset) deleted_files_in_changeset\ = hf.get_deleted_files_in_changeset(files_in_changeset) changed_files_in_changeset\ = hf.get_changed_files_in_changeset(files_in_changeset) new_commit_number = hf.get_new_commit_number() folder = os.path.join(hf.get_branch_location() + '/%s/' % new_commit_number) # Making a folder to put all the details of the commit in os.mkdir(folder) filename = os.path.join(hf.get_branch_location() + '/%s/file_log.txt' % new_commit_number) # Record which files changed with open(filename, 'w')\ as file_: for file_to_add in changed_files_in_changeset: file_.write("~" + file_to_add + "\n") for file_to_add in new_files_in_changeset: file_.write("+" + file_to_add + "\n") for file_to_add in deleted_files_in_changeset: file_.write("-" + file_to_add + "\n") # Recording commit message, and who committed it. filename = os.path.join(hf.get_branch_location() + '/%s/info' % new_commit_number) with open(filename, 'w') as file_: file_.write(hf.get_username() + '\n') file_.write(message) # Recording which commit the HEAD is pointing to filename = os.path.join(hf.get_jet_directory() + '/.jet/current_commit') with open(filename, 'w') as file_: file_.write(str(new_commit_number)) # Counter to make unique folder name for each file change counter = 0 for file_ in changed_files_in_changeset: # Making the folder folder = os.path.join(hf.get_branch_location() + '/%s/%s' % (new_commit_number, counter)) os.mkdir(folder) # Recording the full filename filename = os.path.join(hf.get_branch_location() + '/%s/%s/filename.txt' % (new_commit_number, counter)) with open(filename, 'w') as myFile: myFile.write(file_) # Recording the diff from last commit filename = os.path.join(hf.get_branch_location() + '/%s/%s/changes.txt' % (new_commit_number, counter)) description = hf.get_change_description(file_) if description: with open(filename, 'w') as myFile: myFile.write(description) else: # If no difference, put the contents of the file in there copyfile(file_, filename) counter += 1 # Remove files from changeset as they have been committed. filename = os.path.join(hf.get_branch_location() + 'changeset.txt') os.remove(filename) # Section that updates the latest stored files # Gets all previous files lines = hf.get_stored_files(None) # Adds newly saved files lines.extend(new_files_in_changeset) to_keep = [] for line in lines: # Only adds files not deleted if line not in deleted_files_in_changeset: to_keep.append(line) filename = os.path.join(hf.get_branch_location() + 'latest_saved_files') # Delete old record os.remove(filename) with open(filename, 'w') as file_: for file_to_add in to_keep: file_.write(file_to_add + "\n") # Including new checksum of file, for status/diff checks file_.write(hf.checksum_md5(file_to_add) + "\n") if verbose: # Done! print("Commiting") else: if verbose: print("Please add files to commit using " "jet add before commiting!")
def branch(branch_name): # Checking that there is no un-committed changed files filename = os.path.join(hf.get_branch_location() + 'changeset.txt') # Assume nothing has changed... changed_files = False # If there is a uncommitted changeset... if os.path.isfile(filename): changed_files = True # If any hashes don't match up if len(hf.get_changed_files(None, None)) > 0: changed_files = True # Error if there are uncommitted changed files... if changed_files: print("You can't branch until you commit....") return # END changed files check # Checking that the given branch name is ok branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') if os.path.exists(branches_path): if os.path.exists(os.path.join(branches_path + branch_name))\ or branch_name == 'master': # Name exists, error print("Already a branch with that name... please try another!") return else: # Make directory for the branch os.mkdir(os.path.join(branches_path + branch_name)) else: # Make the required directories os.mkdir(os.path.join(hf.get_jet_directory() + '/.jet/branches/')) os.mkdir(os.path.join(branches_path + branch_name)) # All ok, starting branching. f = [] # Contains the full path and filenames of all files to be branched filenames_list = [] # Contains all the file names (not full path) # Trawl all files in the users repo'd directory for (dirpath, dirnames, filenames) in os.walk(hf.get_jet_directory()): for filename in filenames: # Make sure no ignored files are present if hf.filter_one_file_by_ignore(filename): # No jet files to be included if not '.jet' in dirpath: # Add files name to filename list filenames_list.append(filename) # Add full path and filename to f f.append(os.path.join(dirpath, filename)) # Create a new file listing all the current files and hashes at this point file_name = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/latest_saved_files' % branch_name) with open(file_name, 'w') as file_: for file_to_add in f: file_.write(file_to_add + "\n") file_.write(hf.checksum_md5(file_to_add) + "\n") # Make a folder for the init commit of the branch os.mkdir('.jet/branches/%s/0/' % branch_name) # Store the files that are in the branch at the first commit file_name = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/0/file_log.txt' % branch_name) with open(file_name, 'w') as file_: for file_to_add in f: file_.write(file_to_add + "\n") count = 0 for file_to_add in f: # Make a folder to store details of that file folder = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/%s/%s' % (branch_name, 0, count)) os.mkdir(folder) # Storing the full filename filename = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/%s/%s/filename.txt' % (branch_name, 0, count)) with open(filename, 'w') as myFile: myFile.write(file_to_add) filename = filenames_list[count] # Store the contents of the file copyfile( file_to_add, os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/0/%s/%s' % (branch_name, count, filename))) count += 1 # Branch making complete, store details of branch print("Branch %s made" % branch_name) # Storing parent branch filename = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/parent' % branch_name) old_branch = hf.get_branch() old_commit = hf.get_commit() with open(filename, 'w') as file_: file_.write(old_branch) # Old branch name file_.write('\n') file_.write(old_commit) # Old branch commit number # from where branched off # Now storing which is the current branch filename = os.path.join(hf.get_jet_directory() + '/.jet/branch') with open(filename, 'w') as file_: file_.write(branch_name) # Now storing the commit number (0) filename = os.path.join(hf.get_jet_directory() + '/.jet/current_commit') with open(filename, 'w') as file_: file_.write("0") # All done print("You are now working in branch %s" % branch_name)
def delete(branch_name): # This method deletes the branch and all records of it from the repository. directory = os.path.join(hf.get_jet_directory() + '/.jet/branches/%s/' % branch_name) rmtree(directory) print("Deleted")
def switch(): if not hf.already_initialized(): print("Please init a jet repo before calling other commands") return if len(sys.argv) != 3: print("Please form your switch commands $jet switch <branch_name>") return # Checks for any changed files, as you must have committed first!!! filename = os.path.join(hf.get_branch_location() + 'changeset.txt') # Assumes nothing has changed changed_files = False # If there is a changeset uncommitted if os.path.isfile(filename): changed_files = True # If any hashes don't match up... if len(hf.get_changed_files(None, None)) > 0: changed_files = True # Error if anything changed!! if changed_files: print("You can't switch branch until you commit....") return # Master info stored separately, hence the if statement if sys.argv[2] == 'master': # Record master as the branch currently on filename = os.path.join(hf.get_jet_directory() + '/.jet/branch') with open(filename, 'w') as file_: file_.write(sys.argv[2]) # Do the revert process hf.revert(sys.argv[2], hf.get_highest_commit(sys.argv[2])) # Update the latest saved files with ones from new branch filename = os.path.join(hf.get_branch_location() + 'latest_saved_files') # Delete previous save os.remove(filename) with open(filename, 'w') as file_: for file_to_add in hf.get_current_files(None): file_.write(file_to_add + "\n") file_.write(hf.checksum_md5(file_to_add) + "\n") # All done! print("Successfully switched to branch %s" % sys.argv[2]) return # Only here if the branch to switch to isn't master branches_path = os.path.join(hf.get_jet_directory() + '/.jet/branches/') if os.path.exists(branches_path): if os.path.exists(os.path.join(branches_path + sys.argv[2])): filename = os.path.join(hf.get_jet_directory() + '/.jet/branch') # Setting the new branch name as current branch with open(filename, 'w') as file_: file_.write(sys.argv[2]) hf.revert(sys.argv[2], hf.get_highest_commit(sys.argv[2])) filename = os.path.join(hf.get_branch_location() + 'latest_saved_files') os.remove(filename) with open(filename, 'w') as file_: for file_to_add in hf.get_current_files(None): file_.write(file_to_add + "\n") file_.write(hf.checksum_md5(file_to_add) + "\n") print("Successfully switched to branch %s" % sys.argv[2]) else: print("Invalid branch name") else: print("Invalid branch name")