def migrate_to_git(): users = parse_users() git_repo = arguments['<git_repo>'] if not os.path.exists(git_repo): os.makedirs(git_repo) if not os.path.exists(os.path.join(git_repo, '.git')): git.init(git_repo) data_dir = os.path.abspath(arguments['<data_dir>']) root = os.path.join(data_dir, 'pages') pages = os.listdir(root) os.chdir(git_repo) for page in pages: versions = get_versions(page, users=users, data_dir=data_dir) if not versions: print("### ignoring %s (no revisions found)" % page) continue path = _unquote(page) + '.rst' print("### Creating %s\n" % path) dirname, basename = os.path.split(path) if dirname and not os.path.exists(dirname): os.makedirs(dirname) for version in versions: print("revision %s" % version.pop('revision')) with open(path, 'w') as f: f.write(version.pop('content')) try: git.add(path) git.commit(path, allow_empty_message=True, **version) except: pass
def main(): """ Requires youtube uploader script from https://github.com/tokland/youtube-upload """ from subprocess import Popen, PIPE import glob from sh import git yt_ids = [] for fname in glob.glob('*.webm'): title = fname.replace('.webm','').replace('_',' ') command = 'youtube-upload --title="'+title+'" '+fname p = Popen(command,stdout=PIPE,shell=True) out = p.communicate() yt_ids.append(str(out[0].rstrip()).replace("b'",'').replace("'",'')) readme_content = '# White dwarf nova\n' for idd in yt_ids: readme_content += '[![IMAGE ALT TEXT HERE](http://img.youtube.com/vi/'+idd+'/0.jpg)](http://www.youtube.com/watch?v='+idd+')\n' with open('README.md','w') as f: f.write(readme_content) git.add('README.md') git.commit(m='update videos') git.push()
def test_merge(self): def cleanup_merge(): git.checkout("master") # if something failed, the branch might still exist if self.gd.branch_exists("to_merge_1"): git.branch("-D", "to_merge_1") git.branch("-D", "base_branch") self.addCleanup(cleanup_merge) git.checkout("-b", "to_merge_1") git.checkout("-b", "base_branch") file = open("foo.txt", "w") file.write("ABC\n") file.close() git.add("foo.txt") git.commit("-m","Test commit") new_sha = self.gd.merge("to_merge_1", "base_branch") self.assertTrue( new_sha != "", "new_sha=%s is non-empty" % new_sha) self.assertEqual(len(new_sha), 40, "SHA is 40 chars") self.assertTrue(True, "Merge succeeded")
def initialize_git_repository() -> None: """Prepare the git repository.""" print(" * Initializing the project git repository") try: git.init() git.remote( "add", "origin", "[email protected]:{{ cookiecutter.github_user }}/" "{{ cookiecutter.project_slug }}.git", ) git.add(".") git.commit("-m", "feat: create initial project structure") git.checkout("-b", "gh-pages") git.checkout("-b", "feat/initial_iteration") print(" * Pushing initial changes to master") git.push("--force", "--set-upstream", "origin", "master") git.push("--force", "--set-upstream", "origin", "gh-pages") git.push("--force", "--set-upstream", "origin", "feat/initial_iteration") git.push("--force") except sh.ErrorReturnCode as error: print("There was an error creating the Git repository.") print(str(error.stderr, "utf8")) sys.exit(1)
def main(): """ Requires youtube uploader script from https://github.com/tokland/youtube-upload """ from subprocess import Popen, PIPE import glob from sh import git yt_ids = [] for fname in glob.glob("*.webm"): title = fname.replace(".webm", "").replace("_", " ") command = 'youtube-upload --title="' + title + '" ' + fname p = Popen(command, stdout=PIPE, shell=True) out = p.communicate() yt_ids.append(str(out[0].rstrip()).replace("b'", "").replace("'", "")) readme_content = "# White dwarf nova\n" for idd in yt_ids: readme_content += ( "[![IMAGE ALT TEXT HERE](http://img.youtube.com/vi/" + idd + "/0.jpg)](http://www.youtube.com/watch?v=" + idd + ")\n" ) with open("README.md", "w") as f: f.write(readme_content) git.add("README.md") git.commit(m="update videos") git.push()
def remove_study(self,study_id, branch, author="OpenTree API <*****@*****.**>"): """Remove a study Given a study_id, branch and optionally an author, remove a study on the given branch and attribute the commit to author. Returns the SHA of the commit on branch. """ os.chdir(self.repo) study_dir = "study/%s" % study_id study_filename = "%s/%s.json" % (study_dir, study_id) if self.branch_exists(branch): git.checkout(branch) if not os.path.isdir(study_dir): # branch already exists locally with study removed # so just return the commit SHA return git("rev-parse","HEAD").strip() else: # Create this new branch off of master, NOT the currently-checked out branch! git.checkout("master") git.checkout("-b",branch) git.rm("-rf", study_dir) git.commit(author=author, message="Delete Study #%s via OpenTree API" % study_id) new_sha = git("rev-parse","HEAD") return new_sha.strip()
def test_merge_conflict(self): def cleanup_merge_conflict(): git.checkout("master") # if something failed, the branch might still exist if self.gd.branch_exists("to_merge_1"): git.branch("-D", "to_merge_1") if self.gd.branch_exists("to_merge_2"): git.branch("-D", "to_merge_2") self.addCleanup(cleanup_merge_conflict) git.checkout("master") git.checkout("-b", "to_merge_1") file = open("foo.txt", "w") file.write("ABC\n") file.close() git.add("foo.txt") git.commit("-m","Test commit") git.checkout("master") git.checkout("-b", "to_merge_2") file = open("foo.txt", "w") file.write("XYZ\n") file.close() git.add("foo.txt") git.commit("-m","Test commit") self.assertRaises(MergeException, lambda: self.gd.merge("to_merge_1", "to_merge_2") )
def add(self, paths, msg="Intializating"): """ Initializes Directory as repository if not already git repo. and adds uncommited changes automatically """ for path in paths: global git git = git.bake("--git-dir={0}/.git".format(path), "--work-tree={0}".format(path)) if os.path.isdir(path): if not os.path.isdir(path+"/.git"): try: Log.debug(self, "EEGit: git init at {0}" .format(path)) git.init(path) except ErrorReturnCode as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to git init at {0}" .format(path)) status = git.status("-s") if len(status.splitlines()) > 0: try: Log.debug(self, "EEGit: git commit at {0}" .format(path)) git.add("--all") git.commit("-am {0}".format(msg)) except ErrorReturnCode as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to git commit at {0} " .format(path)) else: Log.debug(self, "EEGit: Path {0} not present".format(path))
def test_merge_conflict(self): def cleanup_merge_conflict(): git.checkout("master") # if something failed, the branch might still exist if self.gd.branch_exists("to_merge_1"): git.branch("-D", "to_merge_1") if self.gd.branch_exists("to_merge_2"): git.branch("-D", "to_merge_2") self.addCleanup(cleanup_merge_conflict) git.checkout("master") git.checkout("-b", "to_merge_1") file = open("foo.txt", "w") file.write("ABC\n") file.close() git.add("foo.txt") git.commit("-m", "Test commit") git.checkout("master") git.checkout("-b", "to_merge_2") file = open("foo.txt", "w") file.write("XYZ\n") file.close() git.add("foo.txt") git.commit("-m", "Test commit") self.assertRaises(MergeException, lambda: self.gd.merge("to_merge_1", "to_merge_2"))
def add(self, paths, msg="Intializating"): """ Initializes Directory as repository if not already git repo. and adds uncommited changes automatically """ for path in paths: global git git = git.bake("--git-dir={0}/.git".format(path), "--work-tree={0}".format(path)) if os.path.isdir(path): if not os.path.isdir(path + "/.git"): try: Log.debug(self, "EEGit: git init at {0}".format(path)) git.init(path) except ErrorReturnCode as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to git init at {0}".format(path)) status = git.status("-s") if len(status.splitlines()) > 0: try: Log.debug(self, "EEGit: git commit at {0}".format(path)) git.add("--all") git.commit("-am {0}".format(msg)) except ErrorReturnCode as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to git commit at {0} ".format(path)) else: Log.debug(self, "EEGit: Path {0} not present".format(path))
def add(name, m, **kwargs): """Add a new note. args: ---------- name: Name of the note m: Text, if None, editor will be opened to prompt user.""" if os.path.isfile(name): existing_path = name mode = "edit" else: existing_path = None mode = "add" if m is None: text = utils.get_text_from_editor(existing_path) else: text = m if not text or text.isspace(): raise ValueError("Aborting due to empty note") with open(name, "w") as outfile: outfile.write(text.lstrip().rstrip()) git.add(name) try: git.commit("-m", "{} {}".format(mode, name)) except ErrorReturnCode_1: raise ValueError("No changes made to note")
def test_merge(self): def cleanup_merge(): git.checkout("master") # if something failed, the branch might still exist if self.gd.branch_exists("to_merge_1"): git.branch("-D", "to_merge_1") git.branch("-D", "base_branch") self.addCleanup(cleanup_merge) git.checkout("-b", "to_merge_1") git.checkout("-b", "base_branch") file = open("foo.txt", "w") file.write("ABC\n") file.close() git.add("foo.txt") git.commit("-m", "Test commit") new_sha = self.gd.merge("to_merge_1", "base_branch") self.assertTrue(new_sha != "", "new_sha=%s is non-empty" % new_sha) self.assertEqual(len(new_sha), 40, "SHA is 40 chars") self.assertTrue(True, "Merge succeeded")
def rm(name, **kwargs): """Remove a note by name.""" try: git.rm(name) except ErrorReturnCode_128: raise FileNotFoundError git.commit("-m", "rm {}".format(name))
def commit(self, objects, message): # validate commit message if not message or not isinstance(message, basestring): raise ValueError( "Commit message should not be empty or not string") env = os.environ.copy() env.update({ 'GIT_WORK_TREE': self.repo, 'GIT_DIR': '%s/.git' % self.repo, }) git.gc("--prune", _env=env) git.checkout("HEAD", _env=env) # pull and push from and to the remote git.pull("origin", "master", _env=env) for obj in objects: git.add("-A", obj, _env=env) try: git.commit("-m", message, _env=env) except Exception: pass git.push(_env=env)
def commit(self, objects, message): # validate commit message if not message or not isinstance(message, basestring): raise ValueError("Commit message should not be empty or not string") env = os.environ.copy() env.update({ 'GIT_WORK_TREE': self.repo, 'GIT_DIR': '%s/.git' % self.repo, }) git.gc("--prune", _env=env) git.checkout("HEAD", _env=env) # pull and push from and to the remote git.pull("origin", "master", _env=env) for obj in objects: git.add("-A", obj, _env=env) try: git.commit("-m", message, _env=env) except Exception: pass git.push(_env=env)
def init_repository(url=None): """Creates a new Gitless's repository in the cwd. Args: url: if given the local repository will be a clone of the remote repository given by this url. """ cwd = os.getcwd() try: pygit2.discover_repository(cwd) raise GlError('You are already in a Gitless repository') except KeyError: # Expected if not url: repo = pygit2.init_repository(cwd) # We also create an initial root commit git.commit(allow_empty=True, m='Initialize repository') return repo try: git.clone(url, cwd) except ErrorReturnCode as e: raise GlError(stderr(e)) # We get all remote branches as well and create local equivalents repo = Repository() remote = repo.remotes['origin'] for rb in (remote.lookup_branch(bn) for bn in remote.listall_branches()): if rb.branch_name == 'master': continue new_b = repo.create_branch(rb.branch_name, rb.head) new_b.upstream = rb return repo
def test_switch_contents_still_there_tracked_commit(self): utils_lib.write_file(TRACKED_FP, contents='commit') git.commit(TRACKED_FP, m='comment') self.repo.switch_current_branch(self.repo.lookup_branch(BRANCH)) self.assertEqual(TRACKED_FP_CONTENTS_2, utils_lib.read_file(TRACKED_FP)) self.repo.switch_current_branch(self.repo.lookup_branch('master')) self.assertEqual('commit', utils_lib.read_file(TRACKED_FP))
def generate_csv(output_directory='./'): # Set up root data dir root_filename = ROOT + '/cfb-data/automated/wiki/' + ROOT + '/' # Clone data repository for updates - continue if already exists from sh import git try: shutil.rmtree(ROOT + '/cfb-data/') except: pass git.clone( 'https://' + os.getenv('MACHINE_AUTH') + '@github.com/coffenbacher/cfb-data.git', ROOT + '/cfb-data/') # Create our root if required if not os.path.exists(root_filename): os.makedirs(root_filename) # Set up logging logging.basicConfig( level='WARNING', #format='%(asctime)s %(levelname)-8s %(message)s', #datefmt='%a, %d %b %Y %H:%M:%S', filename=root_filename + ROOT + '.log', filemode='w') # Extract all current names from Wiki data = FN() # Write everything to csv with open(root_filename + ROOT + '.csv', 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=FIELDNAMES, extrasaction='ignore') for d in data: asci = dict([(unidecode(k), unidecode(unicode(v))) for k, v in d.items()]) writer.writerow(asci) # [unidecode(unicode(d.get(field))) for field in FIELDNAMES] # Write everything to json with open(root_filename + ROOT + '.json', 'w') as jsonfile: relevant = [{f: d.get(f, None) for f in FIELDNAMES} for d in data] jsonfile.write(json.dumps(relevant)) with open(root_filename + ROOT + '_meta.json', 'w') as metafile: d = { 'created': datetime.now().strftime('%x %X'), 'rows': len(data), 'headers': ','.join(FIELDNAMES), } metafile.write(json.dumps(d)) git = git.bake(**{ 'git-dir': ROOT + '/cfb-data/.git/', 'work-tree': ROOT + '/cfb-data' }) git.commit(m='Auto updating %s data' % ROOT, a=True) git.push('origin', 'master')
def add_to_blacklist(self, items_to_blacklist, username, code_permissions): # Check if we're on master if git("rev-parse", "--abbrev-ref", "HEAD").strip() != "master": return (False, "Not currently on master.") # Check that we're up-to-date with origin (GitHub) git.remote.update() if git("rev-parse", "refs/remotes/origin/master").strip() != git("rev-parse", "master").strip(): return (False, "HEAD isn't at tip of origin's master branch") # Check that blacklisted_websites.txt isn't modified locally. That could get ugly fast if "blacklisted_websites.txt" in git.status(): # Also ugly return (False, "blacklisted_websites.txt modified locally. This is probably bad.") # Store current commit hash current_commit = git("rev-parse", "HEAD").strip() # Add items to file with open("blacklisted_websites.txt", "a+") as blacklisted_websites: last_character = blacklisted_websites.read()[-1:] if last_character != "\n": blacklisted_websites.write("\n") blacklisted_websites.write("\n".join(items_to_blacklist) + "\n") # Checkout a new branch (mostly unnecessary, but may help if we create PRs in the future branch = "auto-blacklist-{0}".format(str(time.time())) git.checkout("-b", branch) # Clear HEAD just in case git.reset("HEAD") git.add("blacklisted_websites.txt") git.commit("-m", "Auto blacklist of {0} by {1} --autopull".format(", ".join(items_to_blacklist), username)) if code_permissions: git.checkout("master") git.merge(branch) git.push() else: git.push("origin", branch) git.checkout("master") if GlobalVars.github_username is None or GlobalVars.github_password is None: return (False, "tell someone to set a GH password") payload = {"title": "{0}: Blacklist {1}".format(username, ", ".join(items_to_blacklist)), "body": "{0} requests blacklist of domains: \n\n - {1}".format(username, "\n - ".join(items_to_blacklist)), "head": branch, "base": "master"} response = requests.post("https://api.github.com/repos/Charcoal-SE/SmokeDetector/pulls", auth=HTTPBasicAuth(GlobalVars.github_username, GlobalVars.github_password), data=json.dumps(payload)) print(response.json()) return (True, "You don't have code privileges, but I've [created a pull request for you]({0}).".format(response.json()["html_url"])) git.checkout(current_commit) # Return to old commit to await CI. This will make Smokey think it's in reverted mode if it restarts if not code_permissions: return (False, "Unable to perform action due to lack of code-level permissions. [Branch pushed](https://github.com/Charcoal-SE/SmokeDetector/tree/{0}), PR at your leisure.".format(branch)) return (True, "Blacklisted {0} - the entry will be applied via autopull if CI succeeds.".format(", ".join(items_to_blacklist)))
def commit(self, message="."): try: git.commit(a=True, m=message) except ErrorReturnCode_1: pass return True
def commit(self,message="."): try: git.commit(a=True, m=message) except ErrorReturnCode_1: pass return True
def gitCommitModel(m, filename, msg): envDict = { 'GIT_COMMITTER_DATE': m['created_at'], 'GIT_AUTHOR_DATE': m['created_at'] } git.add(filename) git.commit(m=msg, _ok_code=[0, 1], date=m['created_at']) git.commit('--no-edit', amend=True, _env=envDict) # force date
def script_write(script, dirname, uid, backup_dir=None): dirname = Path(dirname) if backup_dir is None: backup_dir = dirname / 'backup' else: backup_dir = Path(backup_dir) if uid: cmd_file = dirname / ('kea2.%s.sh' % uid) else: cmd_file = dirname / 'kea2.sh' if not dirname.exists(): os.makedirs(dirname) try: output = git('rev-parse') ingit = True lg.debug("In a git repository - add & commit the script") except ErrorReturnCode as e: lg.info("not git - backing up the cmd file") ingit = False if cmd_file.exists(): #check if in git: if ingit: for line in git.status('-s', cmd_file): _status, _filename = line.strip().split(None, 1) lg.warning('git status prewrite: %s %s', _status, _filename) if _filename != cmd_file: lg.warning("this is not the file we want: %s", _filename) continue if _status == '??': git.add(cmd_file) if _status in ['??', 'A', 'M']: lg.warning("git commit old version of %s", cmd_file) git.commit( cmd_file, m='autocommit by kea2 - prepare for new version') else: #not in a git repository - copy file to a temp file ocf_stat = cmd_file.stat() timestamp = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(ocf_stat.st_ctime)) if not backup_dir.exists(): os.makedirs(backup_dir) new_file_name = backup_dir / ('_kea2.%s.%s.sh' % (uid, timestamp)) lg.info("rename old %s to %s", cmd_file, new_file_name) cmd_file.move(new_file_name) script = script.rstrip() with open(cmd_file, 'w') as F: F.write(script) F.write('\n') cmd_file.chmod('a+x') return cmd_file
def setUp(self): super(TestRemoteSync, self).setUp() utils_lib.write_file('foo', contents='foo') git.add('foo') git.commit('foo', m='msg') self.repo.remotes.create('remote', self.remote_path) self.remote = self.repo.remotes['remote']
def script_write(script, dirname, uid, backup_dir=None): dirname = Path(dirname) if backup_dir is None: backup_dir = dirname / 'backup' else: backup_dir = Path(backup_dir) if uid: cmd_file = dirname / ('kea2.%s.sh' % uid) else: cmd_file = dirname / 'kea2.sh' if not dirname.exists(): os.makedirs(dirname) try: output = git('rev-parse') ingit = True lg.debug("In a git repository - add & commit the script") except ErrorReturnCode as e: lg.info("not git - backing up the cmd file") ingit = False if cmd_file.exists(): #check if in git: if ingit: for line in git.status('-s', cmd_file): _status, _filename = line.strip().split(None, 1) lg.warning('git status prewrite: %s %s', _status, _filename) if _filename != cmd_file: lg.warning("this is not the file we want: %s", _filename) continue if _status == '??': git.add(cmd_file) if _status in ['??', 'A', 'M']: lg.warning("git commit old version of %s", cmd_file) git.commit(cmd_file, m='autocommit by kea2 - prepare for new version') else: #not in a git repository - copy file to a temp file ocf_stat = cmd_file.stat() timestamp = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(ocf_stat.st_ctime)) if not backup_dir.exists(): os.makedirs(backup_dir) new_file_name = backup_dir / ('_kea2.%s.%s.sh' % (uid, timestamp)) lg.info("rename old %s to %s", cmd_file, new_file_name) cmd_file.move(new_file_name) script = script.rstrip() with open(cmd_file, 'w') as F: F.write(script) F.write('\n') cmd_file.chmod('a+x') return cmd_file
def test_create_page_if_remote_added_files(self): assert not Page.objects.filter(path="newpage.rst").exists() with open("newpage.rst", 'w') as p: p.write('the new content') git.add('.') git.commit('-m', 'add new page') response = self.client.post(self.url, {}) # now exists new_page = Page.objects.get(path="newpage.rst") self.assertEqual(new_page.raw, 'the new content')
def main(): git.checkout("-b", "task_1") f = open("file1.txt", "w") f.write("here is a file") f.close() git.commit("-am", "commited new text file1") git.push("--set-upstream", "origin", "task1") git.checkout("-")
def setUp(self): super(TestFile, self).setUp() # Build up an interesting mock repo utils_lib.write_file(TRACKED_FP, contents=TRACKED_FP_CONTENTS_1) utils_lib.write_file(TRACKED_FP_WITH_SPACE, contents=TRACKED_FP_CONTENTS_1) utils_lib.write_file(TRACKED_DIR_FP, contents=TRACKED_FP_CONTENTS_1) utils_lib.write_file(TRACKED_DIR_FP_WITH_SPACE, contents=TRACKED_FP_CONTENTS_1) utils_lib.write_file(TRACKED_DIR_DIR_FP, contents=TRACKED_FP_CONTENTS_1) utils_lib.write_file(TRACKED_DIR_DIR_FP_WITH_SPACE, contents=TRACKED_FP_CONTENTS_1) git.add(TRACKED_FP, TRACKED_FP_WITH_SPACE, TRACKED_DIR_FP, TRACKED_DIR_FP_WITH_SPACE, TRACKED_DIR_DIR_FP, TRACKED_DIR_DIR_FP_WITH_SPACE) git.commit(TRACKED_FP, TRACKED_FP_WITH_SPACE, TRACKED_DIR_FP, TRACKED_DIR_FP_WITH_SPACE, TRACKED_DIR_DIR_FP, TRACKED_DIR_DIR_FP_WITH_SPACE, m='1') utils_lib.write_file(TRACKED_FP, contents=TRACKED_FP_CONTENTS_2) utils_lib.write_file(TRACKED_FP_WITH_SPACE, contents=TRACKED_FP_CONTENTS_2) utils_lib.write_file(TRACKED_DIR_FP, contents=TRACKED_FP_CONTENTS_2) utils_lib.write_file(TRACKED_DIR_FP_WITH_SPACE, contents=TRACKED_FP_CONTENTS_2) utils_lib.write_file(TRACKED_DIR_DIR_FP, contents=TRACKED_FP_CONTENTS_2) utils_lib.write_file(TRACKED_DIR_DIR_FP_WITH_SPACE, contents=TRACKED_FP_CONTENTS_2) git.commit(TRACKED_FP, TRACKED_FP_WITH_SPACE, TRACKED_DIR_FP, TRACKED_DIR_FP_WITH_SPACE, TRACKED_DIR_DIR_FP, TRACKED_DIR_DIR_FP_WITH_SPACE, m='2') utils_lib.write_file(UNTRACKED_FP) utils_lib.write_file(UNTRACKED_FP_WITH_SPACE) utils_lib.write_file(UNTRACKED_DIR_FP) utils_lib.write_file(UNTRACKED_DIR_FP_WITH_SPACE) utils_lib.write_file(UNTRACKED_DIR_DIR_FP) utils_lib.write_file(UNTRACKED_DIR_DIR_FP_WITH_SPACE) utils_lib.write_file(GITIGNORE_FP, contents='{0}\n{1}'.format( IGNORED_FP, IGNORED_FP_WITH_SPACE)) utils_lib.write_file(IGNORED_FP) utils_lib.write_file(IGNORED_FP_WITH_SPACE) utils_lib.write_file(GITTEST_FP) self.curr_b = self.repo.current_branch
def update(conf, args): '''Apply updates from the upstream repository.''' print('Checking for updates...') # fetch changes from the canonical repo git.fetch(constants.GIT_REMOTE, no_tags=True, quiet=True) # get a list of the commit messages for the incoming changes updates = git('--no-pager', 'log', '..FETCH_HEAD', oneline=True) updates = [tuple(m.split(None, 1)) for m in updates.splitlines()] # print out a list of the incoming updates if len(updates) > 0: print('Available updates:') max_updates = 10 for commit, msg in updates[:max_updates]: print(color.yellow('*'), msg) # print a special message if too many updates are available if len(updates) > max_updates: print('...and', color.green(len(updates) - max_updates), 'more!') print('Run `git log ..FETCH_HEAD` to see the full list') # bail if we have uncommitted changes (git exits non-0 in this case) if git.diff(exit_code=True, quiet=True, _ok_code=(0, 1)).exit_code != 0: raise ValueError('The repository has uncommitted changes. Handle them, ' 'then try updating again.') print('Applying the update...') # stash _all_ changes to the repo git.stash(include_untracked=True, all=True, quiet=True) # squash all the fetched commits together and merge them into master git.merge('@{u}', squash=True, quiet=True) # add a nice update commit that includes the latest upstream commit hash commit_message = 'Update dotparty to %s' % updates[0][0] git.commit(m=commit_message, quiet=True) # TODO: if squash merge failed, roll back to the pre-update state and # complain with instructions for the user to do their own update. # un-stash all our old changes git.stash('pop', quiet=True) # push our changes back up to the remote git.push(quiet=True) print('Update successful!') else: print('Already up-to-date!')
def addgitcommit(rev): mtime = calendar.timegm(time.strptime(rev['authordate'], '%Y-%m-%dT%H:%M:%SZ')) if not os.path.exists(rev['ns']): os.mkdir(rev['ns']) with codecs.open(rev['pfpath'],'w',encoding='utf-8') as pf: print(rev['text'].decode('utf-8'),file=pf) os.utime(rev['pfpath'],(time.time(),mtime)) git.add(rev['pfpath']) # use --allow-empty so that commit works even if the page content didn't change # this is to preserve the messages git.commit(rev['pfpath'],author=rev['author'],date=rev['authordate'],m=rev['gitcomment'],allow_empty=True)
def git_commit(file_path, message, recursive=True): '''Change current branch''' args = [] if recursive: args.append('-a') file_path = dir_path(file_path) args.append('-m') args.append('"' + message + '"') args.append(file_path) git.commit(*args, _cwd=dir_path(file_path)) show_msg('Commit Done', 'Info')
def test_low_level_whatchanged(self): self.page.raw = "line\n" Git().commit(self.page, message=u'"//"') another_page = PageFactory(path="another-page.rst") another_page.raw = "hello!" self.page.raw = "hello 2!" git.add(another_page.path) git.add(self.page.path) git.commit("-m", "commit all") wc = Git().whatchanged() self.assertEqual(wc[0][3], "commit all") self.assertEqual(wc[0][5], [another_page.path, self.page.path])
def test_low_level_whatchanged(self): self.page.raw = 'line\n' Git().commit(self.page, message=u'"//"') another_page = PageFactory(path='another-page.rst') another_page.raw = "hello!" self.page.raw = "hello 2!" git.add(another_page.path) git.add(self.page.path) git.commit('-m', 'commit all') wc = Git().whatchanged() self.assertEqual(wc[0][3], 'commit all') self.assertEqual(wc[0][5], [another_page.path, self.page.path])
def _release(language, message, channel): print message, "...", if _is_dirty(): sys.exit("Repo must be in clean state before deploying. Please commit changes.") _generate_yaml(language, channel) if _is_dirty(): git.add('.travis.yml') git.commit(m=message, allow_empty=True) git.pull(rebase=True) git.push() print "done."
def update(): """Update all submodules to Github versions""" if _is_dirty(): sys.exit("Repo must be in clean state before updating. Please commit changes.") git.submodule.update(remote=True, rebase=True) if _is_dirty(): print "Updated repositories:" print git.status(porcelain=True).strip() git.add(all=True) git.commit(m="Update submodules to origin") else: sys.exit('Nothing to update.')
def test_edit_file_no_conflict(self): # commit remotely with open("page.rst", 'w') as p: p.write(self.content + '\nhey! edited remotely!\n') git.add('.') git.commit('-m', 'a remote log') # webhook post (this would be done externally) response = self.client.post(self.url, {}) self.assertEqual(self.page.raw, self.content + '\nhey! edited remotely!\n') pull_stdout = json.loads(response.content.decode('utf8'))['pull'] self.assertIn('1 file changed', pull_stdout)
def update_roles(self): os.chdir('%s/%s' % (self.repo_path, self.ROLES)) git.pull('-f', '-u', 'origin', 'master') os.chdir(self.repo_path) message = '%s %s %s %s' % (self.UPDATE, self.ROLES, 'to', self.get_submodule_hash(self.ROLES)) git.commit('-m', message, '.gitmodules', self.ROLES) sys.stdout.write('Committed %s [%s]\n' % (self.ROLES, message)) try: git.push('origin', 'master') except sh.ErrorReturnCode_1: pass
def commit(self, path, message='', author=None): kwargs = {} if isinstance(author, User) and author.is_authenticated(): kwargs['author'] = "% <%s>" % (author.get_full_name() or author.username) elif isinstance(author, six.string_types): kwargs['author'] = author try: git.add(path) git.commit(path, m=message or 'Update %s' % path, **kwargs) except: # TODO: make this more robust! # skip when stage is empty pass
def _release(language, message, channel): print message, "...", if _is_dirty(): sys.exit( "Repo must be in clean state before deploying. Please commit changes." ) _generate_yaml(language, channel) if _is_dirty(): git.add('.travis.yml') git.commit(m=message, allow_empty=True) git.pull(rebase=True) git.push() print "done."
def update(): """Update all submodules to Github versions""" if _is_dirty(): sys.exit( "Repo must be in clean state before updating. Please commit changes." ) git.submodule.update(remote=True, rebase=True) if _is_dirty(): print "Updated repositories:" print git.status(porcelain=True).strip() git.add(all=True) git.commit(m="Update submodules to origin") else: sys.exit('Nothing to update.')
def init(self): """Create a new repository with an initial commit.""" cwd = os.getcwd() git.init(self.__path) os.chdir(self.__path) git.commit( m='Initializing empty Tut project.', allow_empty=True, ) os.chdir(cwd)
def test_edit_file_with_a_conflict(self): # commit remotely with open("page.rst", 'w') as p: p.write(self.content + '\nremote line') git.add('.') git.commit('-m', 'a remote log') # meanwhile. commit a change self.page.raw = self.page.raw + '\nlocal line' Git().commit(self.page) response = self.client.post(self.url, {}) # local wins # Note: newer versions of git don't leave a blank line at the end self.assertRegexpMatches(self.content + "\nlocal line\n?$", self.page.raw)
def install_app(self, appid, pdpobject): print "Creating app : %s on heroku" % appid r = self._create_app(appid) if r.ok: print "Successfully created" else: print "Error creating application" print r.status_code, r.text return resp = r.json() git_url = resp["git_url"] web_url = resp["web_url"] print "Staging PDP archive.." print "PDP archive is at : %s" % pdpobject.pdpdir print "Configuring git.." cwd = os.getcwd() os.chdir(pdpobject.pdpdir) from sh import git git.init() print "Invoking the right artifact handler" plan = pdpobject.plan for a in plan.artifacts: h = self.handler_map.get(a.type) if h is None: raise NotImplementedError("No handler for artifact type : %s" % a.type) ho = h(pdpobject, a) ho.handle_artifact() print "Configuring git remote.." git.remote.add("heroku", git_url) def process_output(line): print(line) print "Adding files to repo" git.add(".") print "Committing to local repo" git.commit("-m", "Initial commit") print "Pushing to heroku" git.push("-u", "heroku", "master", _out=process_output, _tty_out=True) print "Uploaded app successfully.." print "App is available at : %s" % web_url return appid, git_url, web_url
def write_study(self, study_id, content, branch, author="OpenTree API <*****@*****.**>"): """Write a study Given a study_id, content, branch and optionally an author, write a study on the given branch and attribute the commit to author. If the branch does not yet exist, it will be created. If the study is being created, it's containing directory will be created as well. Returns the SHA of the new commit on branch. """ os.chdir(self.repo) # If there are uncommitted changes to our repo, stash them so this commit can proceed git.stash() if self.branch_exists(branch): git.checkout(branch) else: # Create this new branch off of master, NOT the currently-checked out branch! git.checkout("master") git.checkout("-b", branch) study_dir = "study/%s" % study_id study_filename = "%s/%s.json" % (study_dir, study_id) # create a study directory if this is a new study if not os.path.isdir(study_dir): os.mkdir(study_dir) file = open(study_filename, 'w') file.write(content) file.close() git.add(study_filename) git.commit(author=author, message="Update Study #%s via OpenTree API" % study_id) new_sha = git("rev-parse", "HEAD") return new_sha.strip()
def setUp(self): super(TestFileResolve, self).setUp() # Generate a conflict git.checkout(b='branch') utils_lib.write_file(FP_IN_CONFLICT, contents='branch') utils_lib.write_file(DIR_FP_IN_CONFLICT, contents='branch') git.add(FP_IN_CONFLICT, DIR_FP_IN_CONFLICT) git.commit(FP_IN_CONFLICT, DIR_FP_IN_CONFLICT, m='branch') git.checkout('master') utils_lib.write_file(FP_IN_CONFLICT, contents='master') utils_lib.write_file(DIR_FP_IN_CONFLICT, contents='master') git.add(FP_IN_CONFLICT, DIR_FP_IN_CONFLICT) git.commit(FP_IN_CONFLICT, DIR_FP_IN_CONFLICT, m='master') git.merge('branch', _ok_code=[1])
def setUp(self): super(TestBranch, self).setUp() # Build up an interesting mock repo. utils_lib.write_file(TRACKED_FP, contents=TRACKED_FP_CONTENTS_1) git.add(TRACKED_FP) git.commit(TRACKED_FP, m='1') utils_lib.write_file(TRACKED_FP, contents=TRACKED_FP_CONTENTS_2) git.commit(TRACKED_FP, m='2') utils_lib.write_file(UNTRACKED_FP, contents=UNTRACKED_FP_CONTENTS) utils_lib.write_file('.gitignore', contents='{0}'.format(IGNORED_FP)) utils_lib.write_file(IGNORED_FP) git.branch(BRANCH) self.curr_b = self.repo.current_branch
def test_git_repo(): # create repo repo_dir = tempdir.TempDir() gitsh.init(repo_dir.name) # put something in the description file: description = open('{}/.git/description'.format(repo_dir.name), 'w') overwrite(description, 'Depeche Mode') # create a single file repo_file = tempfile.NamedTemporaryFile(dir=repo_dir.name, delete=False) overwrite(repo_file, '123\n') # commit it gitsh.add(repo_file.name, _cwd=repo_dir.name) gitsh.commit(m='message', author='First Last <*****@*****.**>', _cwd=repo_dir.name) return repo_dir
def main(): """Script body""" app_dir = join(dirname(abspath(__file__)), '..') sys.path.append(app_dir) from carto_renderer import version # pylint: disable=import-error cur_version = version.SEMANTIC if not cur_version.endswith('-SNAPSHOT'): raise ValueError('Not a SNAPSHOT version!') default_release = cur_version.replace('-SNAPSHOT', '') pytest.main(app_dir) release_version = prompt( 'Release version [{ver}]: '.format(ver=default_release), r'^\d+[.]\d+[.]\d+$', 'Release version should be Major.Minor.Patch!', default_release) split_version = [int(i) for i in release_version.split('.')] split_version[2] += 1 default_next = '.'.join([str(s) for s in split_version]) + '-SNAPSHOT' next_version = prompt( 'Next version [' + default_next + ']: ', r'^\d+[.]\d+[.]\d+-SNAPSHOT$', 'Not a valid SNAPSHOT version!', default_next) ver_file = join(app_dir, 'carto_renderer', 'version.py') set_version(ver_file, cur_version, release_version) git.add(ver_file) git.commit('-m', 'Setting version to ' + release_version) git.tag('v' + release_version) set_version(ver_file, release_version, next_version) git.add(ver_file) git.commit('-m' 'Setting version to ' + next_version) do_push = prompt('Push changes to the remote repository (y/n)? [y]: ', '.*', None, 'y') if do_push.lower().startswith('y'): print(git.push()) print(git.push('--tags'))