def _get_git_url(path): """ Try to auto-detect the git origin url in the path dir. If found, return it. """ ret_code, output, _ = exec_cmd('git config --get remote.origin.url', path) return output if ret_code == 0 else None
def run(self): self.logger.debug('A new git init task has been started.') # If the project directory already exists, delete it. if os.path.exists(self.user_cwd): shutil.rmtree(self.user_cwd) create_missing_dirs(self.project_cwd, isfile=False) ret_code, output, _ = exec_cmd( 'git clone %s %s' % (self.url, self.jid), self.project_cwd) if not ret_code: self.logger.debug('Git init task finished.') eventbus.fire('git-init-success', self.bid) else: eventbus.fire('git-init-failure', self.bid, "Cannot initialize the git repository.")
def run(self): self.logger.debug('A new git init task has been started.') # If the project directory already exists, delete it. if os.path.exists(self.user_cwd): shutil.rmtree(self.user_cwd) create_missing_dirs(self.project_cwd, isfile=False) ret_code, output, _ = exec_cmd('git clone %s %s' % ( self.url, self.jid), self.project_cwd) if not ret_code: self.logger.debug('Git init task finished.') eventbus.fire('git-init-success', self.bid) else: eventbus.fire('git-init-failure', self.bid, "Cannot initialize the git repository.")
def _user_side(self, user): user_cwd = os.path.join(self.project_cwd, user) # If the user cwd is locked, stop the process. if os.path.exists(os.path.join(user_cwd, '.baboon.lock')): # The path exists -> the user_cwd is locke. self.logger.error('The %s is locked. Ignore it' % user_cwd) return # Add the master_cwd remote. exec_cmd('git remote add %s %s' % (self.username, self.master_cwd), user_cwd) # Fetch the master_cwd repository. exec_cmd('git fetch %s' % self.username, user_cwd) # Get the current user branch _, out_branch, _ = exec_cmd('git symbolic-ref -q HEAD', self.master_cwd) # out_branch looks like something like : # refs/head/master\n. Parse it to only retrieve the branch # name. cur_branch = os.path.basename(out_branch).rstrip('\r\n') # Get the merge-base hash commit between the master user and the # current user. mergebase_hash = exec_cmd( 'git merge-base -a HEAD %s/%s' % (self.username, cur_branch), user_cwd)[1].rstrip('\r\n') # Get the diff between the master_cwd and the mergebase_hash. _, diff, _ = exec_cmd( 'git diff --binary --full-index %s' % mergebase_hash, self.master_cwd) # Set the return code of the merge task to 0 by default. It means # there's no conflict. ret = 0 # The future result string of the `git apply --check` command. patch_output = "" # If the diff is not empty, check if it can be applied in the user_cwd. # Otherwise, it means that there's no change, so there's no possible # conflict. if diff: # Create a temp file. tmpfile = tempfile.NamedTemporaryFile() try: # Write the diff into a temp file. tmpfile.write(diff) # After writing, rewind the file handle using seek() in order # to read the data back from it. tmpfile.seek(0) # Check if the diff can be applied in the master user. ret, patch_output, _ = exec_cmd( 'git apply --check %s' % tmpfile.name, user_cwd) finally: # Automatically delete the temp file. tmpfile.close() # Build the *args for the _alert method. alert_args = (self.project_name, self.username, user) # Build the **kwargs for the _alert method if there's no # conflict. alert_kwargs = { 'merge_conflict': False, } if ret: # Build the **kwargs for the _alert method if there's a # merge conflict.x alert_kwargs = { 'merge_conflict': True, 'conflict_files': self._get_conflict_files(patch_output) } # Call the _alert method with alert_args tuple as *args # argument and alert_kwargs dict as **kwargs. self._alert(*alert_args, **alert_kwargs)
def _user_side(self, user): user_cwd = os.path.join(self.project_cwd, user) # If the user cwd is locked, stop the process. if os.path.exists(os.path.join(user_cwd, '.baboon.lock')): # The path exists -> the user_cwd is locke. self.logger.error('The %s is locked. Ignore it' % user_cwd) return # Add the master_cwd remote. exec_cmd('git remote add %s %s' % (self.username, self.master_cwd), user_cwd) # Fetch the master_cwd repository. exec_cmd('git fetch %s' % self.username, user_cwd) # Get the current user branch _, out_branch, _ = exec_cmd('git symbolic-ref -q HEAD', self.master_cwd) # out_branch looks like something like : # refs/head/master\n. Parse it to only retrieve the branch # name. cur_branch = os.path.basename(out_branch).rstrip('\r\n') # Get the merge-base hash commit between the master user and the # current user. mergebase_hash = exec_cmd('git merge-base -a HEAD %s/%s' % (self.username, cur_branch), user_cwd)[1].rstrip('\r\n') # Get the diff between the master_cwd and the mergebase_hash. _, diff, _ = exec_cmd('git diff --binary --full-index %s' % mergebase_hash, self.master_cwd) # Set the return code of the merge task to 0 by default. It means # there's no conflict. ret = 0 # The future result string of the `git apply --check` command. patch_output = "" # If the diff is not empty, check if it can be applied in the user_cwd. # Otherwise, it means that there's no change, so there's no possible # conflict. if diff: # Create a temp file. tmpfile = tempfile.NamedTemporaryFile() try: # Write the diff into a temp file. tmpfile.write(diff) # After writing, rewind the file handle using seek() in order # to read the data back from it. tmpfile.seek(0) # Check if the diff can be applied in the master user. ret, patch_output, _ = exec_cmd('git apply --check %s' % tmpfile.name, user_cwd) finally: # Automatically delete the temp file. tmpfile.close() # Build the *args for the _alert method. alert_args = (self.project_name, self.username, user) # Build the **kwargs for the _alert method if there's no # conflict. alert_kwargs = {'merge_conflict': False, } if ret: # Build the **kwargs for the _alert method if there's a # merge conflict.x alert_kwargs = { 'merge_conflict': True, 'conflict_files': self._get_conflict_files(patch_output) } # Call the _alert method with alert_args tuple as *args # argument and alert_kwargs dict as **kwargs. self._alert(*alert_args, **alert_kwargs)
def _on_message(self, message): exec_cmd(self.notif_cmd %(message))