def test_status_performance(self): def assert_status_performance(): # The test fails if `gl status` takes more than 100 times # the time `git status` took. MAX_TOLERANCE = 100 t = time.time() gl.status() gl_t = time.time() - t t = time.time() git.status() git_t = time.time() - t self.assertTrue( gl_t < git_t*MAX_TOLERANCE, msg='gl_t {0}, git_t {1}'.format(gl_t, git_t)) # All files are untracked assert_status_performance() # Track all files, repeat logging.info('Doing a massive git add, this might take a while') git.add('.') logging.info('Done') assert_status_performance()
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 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 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 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 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 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 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 commit(self, page, message='', author=None, parent=None, extra_path=None): path = page.path paths_to_commit = [path] if extra_path: paths_to_commit.append(extra_path) kwargs = {} User = get_user_model() if isinstance(author, User) and author.is_authenticated(): kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email) elif isinstance(author, six.string_types): kwargs['author'] = author try: there_were_changes = parent and parent != self.last_version(page) status = git.status('--porcelain', path).stdout.decode('utf8')[:2] if parent and status != "UU": git.stash() git.checkout('--detach', parent) try: git.stash('pop') except: git.checkout('--theirs', path) if status == 'UU': # See http://stackoverflow.com/a/8062976/811740 kwargs['i'] = True git.add(path) git_commit_cmd = git.commit.bake(allow_empty=True, allow_empty_message=True, m=message, **kwargs) git_commit_cmd('--', *paths_to_commit) last = self.last_version(page) if parent and status != "UU": git.checkout('master') git.merge(last) except ErrorReturnCode as e: # TODO: make this more robust! error = e.stdout.decode('utf8') if 'CONFLICT' in error: # For '-i' attribute see http://stackoverflow.com/q/5827944/811740 git_commit_cmd = git.commit.bake(allow_empty=True, allow_empty_message=True, m=_('Merged with conflict'), i=True, **kwargs) git_commit_cmd('--', *paths_to_commit) raise Page.EditionConflict( _('Automatic merge failed. Please, fix the conflict and save the page.' )) else: raise return there_were_changes
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 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 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 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_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 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 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 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 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 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 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 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 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 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 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 _update_config(self, config, log=None): branch = self._current_branch() try: git.checkout('tut') yaml.dump( config, open('tut.cfg', 'w'), default_flow_style=False, ) git.add('tut.cfg') git.commit(m=log or 'Update configuration.', ) finally: git.checkout(branch)
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'))
def commit(self, page, message="", author=None, parent=None, extra_path=None): path = page.path paths_to_commit = [path] if extra_path: paths_to_commit.append(extra_path) kwargs = {} if isinstance(author, User) and author.is_authenticated(): kwargs["author"] = u"%s <%s>" % (author.get_full_name() or author.username, author.email) elif isinstance(author, six.string_types): kwargs["author"] = author try: there_were_changes = parent and parent != self.last_version(page) status = git.status("--porcelain", path).stdout.decode("utf8")[:2] if parent and status != "UU": git.stash() git.checkout("--detach", parent) try: git.stash("pop") except: git.checkout("--theirs", path) if status == "UU": # See http://stackoverflow.com/a/8062976/811740 kwargs["i"] = True git.add(path) git_commit_cmd = git.commit.bake(allow_empty=True, allow_empty_message=True, m=message, **kwargs) git_commit_cmd("--", *paths_to_commit) last = self.last_version(page) if parent and status != "UU": git.checkout("master") git.merge(last) except ErrorReturnCode as e: # TODO: make this more robust! error = e.stdout.decode("utf8") if "CONFLICT" in error: # For '-i' attribute see http://stackoverflow.com/q/5827944/811740 git_commit_cmd = git.commit.bake( allow_empty=True, allow_empty_message=True, m=_("Merged with conflict"), i=True, **kwargs ) git_commit_cmd("--", *paths_to_commit) raise Page.EditionConflict(_("Automatic merge failed. Please, fix the conflict and save the page.")) else: raise return there_were_changes
def test_whatchanged_multiples_files_in_one_commit(self): git_dir = os.path.join(WALIKI_DATA_DIR, '.git') shutil.rmtree(git_dir) Git() self.page.raw = 'line\n' another_page = PageFactory(path='another-page.rst') another_page.raw = "hello!" git.add('.') git.commit('-am', 'commit all') response = self.client.get(reverse('waliki_whatchanged')) changes = response.context[0]['changes'] self.assertEqual(len(changes), 2) self.assertEqual(changes[0]['page'], another_page) self.assertEqual(changes[1]['page'], self.page) self.assertEqual(changes[0]['message'], 'commit all') self.assertEqual(changes[1]['message'], 'commit all')
def add(self,path): o = git.add(path) if o.exit_code != 0: raise RepositoryException("Failed to add file {} to git repo: {}".format(path, o)) return True
def test_whatchanged_multiples_files_in_one_commit(self): git_dir = os.path.join(WALIKI_DATA_DIR, ".git") shutil.rmtree(git_dir) Git() self.page.raw = "line\n" another_page = PageFactory(path="another-page.rst") another_page.raw = "hello!" git.add(".") git.commit("-am", "commit all") response = self.client.get(reverse("waliki_whatchanged")) changes = response.context[0]["changes"] self.assertEqual(len(changes), 2) self.assertEqual(changes[0]["page"], another_page) self.assertEqual(changes[1]["page"], self.page) self.assertEqual(changes[0]["message"], "commit all") self.assertEqual(changes[1]["message"], "commit all")
def log(msg): global f print('[logbot]', 'log:', msg) today = strftime('%Y-%m-%d.log') if f.name != today: f.close() try: git.add(f.name) git.commit('-m', 'add ' + f.name) git.push() except ErrorReturnCode: print('[logbot]', 'err:', 'fail to commit', f.name) print('[logbot]', 'commit:', f.name) print('[logbot]', 'new:', today) f = open(today, 'w+') f.write(msg)
def commit(self, page, message='', author=None, parent=None, extra_path=None): path = page.path paths_to_commit = [path] if extra_path: paths_to_commit.append(extra_path) kwargs = {} User = get_user_model() if isinstance(author, User) and is_authenticated(author): kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email) elif isinstance(author, six.string_types): kwargs['author'] = author try: there_were_changes = parent and parent != self.last_version(page) status = git.status('--porcelain', path).stdout.decode('utf8')[:2] if parent and status != "UU": git.stash() git.checkout('--detach', parent) try: git.stash('pop') except: git.checkout('--theirs', path) if status == 'UU': # See http://stackoverflow.com/a/8062976/811740 kwargs['i'] = True git.add(path) git_commit_cmd = git.commit.bake(allow_empty=True, allow_empty_message=True, m=message, **kwargs) git_commit_cmd('--', *paths_to_commit) last = self.last_version(page) if parent and status != "UU": git.checkout('master') git.merge(last) except ErrorReturnCode as e: # TODO: make this more robust! error = e.stdout.decode('utf8') if 'CONFLICT' in error: # For '-i' attribute see http://stackoverflow.com/q/5827944/811740 git_commit_cmd = git.commit.bake(allow_empty=True, allow_empty_message=True, m=_('Merged with conflict'), i=True, **kwargs) git_commit_cmd('--', *paths_to_commit) raise Page.EditionConflict(_('Automatic merge failed. Please, fix the conflict and save the page.')) else: raise return there_were_changes
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', 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) self.curr_b = self.repo.current_branch
def add(self, path): o = git.add(path) if o.exit_code != 0: raise RepositoryException( "Failed to add file {} to git repo: {}".format(path, o)) return True
def update_docs(self, args: argparse.Namespace) -> None: l10n_content = ".. GENERATED BY i18n_tool.py DO NOT EDIT:\n\n" for (code, info) in sorted(self.supported_languages.items()): l10n_content += "* " + info["name"] + " (``" + code + "``)\n" includes = abspath(join(args.docs_repo_dir, "docs/includes")) l10n_txt = join(includes, "l10n.txt") io.open(l10n_txt, mode="w").write(l10n_content) self.require_git_email_name(includes) if self.file_is_modified(l10n_txt): k = {"_cwd": includes} git.add("l10n.txt", **k) msg = "docs: update the list of supported languages" git.commit("-m", msg, "l10n.txt", **k) log.warning(l10n_txt + " updated") git_show_out = git.show(**k) log.warning(git_show_out) else: log.warning(l10n_txt + " already up to date")
def update_docs(self, args: argparse.Namespace) -> None: l10n_content = u'.. GENERATED BY i18n_tool.py DO NOT EDIT:\n\n' for (code, info) in sorted(self.supported_languages.items()): l10n_content += '* ' + info['name'] + ' (``' + code + '``)\n' includes = abspath(join(args.docs_repo_dir, 'docs/includes')) l10n_txt = join(includes, 'l10n.txt') io.open(l10n_txt, mode='w').write(l10n_content) self.require_git_email_name(includes) if self.file_is_modified(l10n_txt): k = {'_cwd': includes} git.add('l10n.txt', **k) msg = 'docs: update the list of supported languages' git.commit('-m', msg, 'l10n.txt', **k) log.warning(l10n_txt + " updated") git_show_out = git.show(**k) log.warning(git_show_out) else: log.warning(l10n_txt + " already up to date")