Exemple #1
0
    def execute(self, options, quiet=False, **kwargs):
        if not options.branch:
            self.message = 'ERROR: Could not determine branch.'
            return False

        staged_entries = self.get_staged_entries(options)
        if len(staged_entries):
            for stage in staged_entries:
                print(
                    'The following entries are pending in the "%s" staging area:'
                    % stage)
                s = Info.Status()
                s.process_lines(staged_entries[stage], options)
        else:
            if self.message is None:
                self.message = 'No currently staged entries were found.'
            return False

        return True
Exemple #2
0
    def __init__(self, options):
        super(Unstage, self).__init__()

        if not options.branch:
            print('ERROR: Could not determine branch.', file=sys.stderr)
            sys.exit(1)

        root = find_hg_root()
        if root:
            os.chdir(root)
            os.chdir("..")

        if not os.path.exists('.hg'):
            os.chdir(working_dir)
            print('ERROR: Must be in root of working copy to stage.',
                  file=sys.stderr)
            sys.exit(1)

        stage_name = "default" if options.stage_name is None else options.stage_name

        stage_path = super(Unstage, self).get_staging_root(root, options)
        if not os.path.exists(stage_path):
            os.mkdir(stage_path)
        stage_db_path = os.path.join(stage_path, stage_name)
        stage_db_file = os.path.join(stage_db_path, 'stage.db')
        if not os.path.exists(stage_db_path):
            if options.erase_cache:
                return  # nothing more to do
            print(
                'ERROR: No modifications are currently staged in "%s" for committing.'
                % stage_name,
                file=sys.stderr)
            sys.exit(1)

        if options.erase_cache:
            print('All entries in the "%s" staging area were cleared.' %
                  stage_name)
            shutil.rmtree(stage_db_path)
            return

        if len(options.args) == 0:
            print('ERROR: No filter(s) specified for unstaging.',
                  file=sys.stderr)
            sys.exit(1)

        stage_db = {}
        if os.path.exists(stage_db_file):
            stage_db = super(Unstage, self).load_stage_db(stage_db_file)

        command = ['hg', 'status', '-q', '-C', '.']
        output = subprocess.Popen(
            command, stdout=subprocess.PIPE).communicate()[0].decode("utf-8")
        output_lines = fixup_renames(output.split('\n'))

        bad_keys = []
        for key in stage_db:
            for item in options.args:
                if item in key:
                    bad_keys.append(key)

        if len(bad_keys) == 0:
            print(
                'ERROR: Your specified filter(s) did not match any entries in the "%s" staging area.'
                % stage_name,
                file=sys.stderr)
            sys.exit(1)

        unstaged_entries = []
        for key in bad_keys:
            for line in output_lines:
                if key in line:
                    unstaged_entries.append(line)

        for key in bad_keys:
            if stage_db[key].snapshot is not None:
                # it's a snapshot, delete it as well
                snapshot_file = os.path.join(stage_db_path,
                                             stage_db[key].snapshot)
                os.remove(snapshot_file)
            del stage_db[key]

        if len(stage_db) == 0:
            if os.path.exists(stage_db_path):
                shutil.rmtree(stage_db_path)
        else:
            # save the new database
            super(Unstage, self).save_stage_db(stage_db, stage_db_file)

            if len(unstaged_entries):
                print(
                    'The following existing entries were removed from the "%s" staging area:'
                    % stage_name)
                s = Info.Status()
                s.process_lines(unstaged_entries, options)
            else:
                print(
                    'No unique entries were removed from the "%s" staging area.'
                    % stage_name)
Exemple #3
0
    def __init__(self, options):
        super(Stage, self).__init__()

        def generate_snapshot(options, stage_db_path, file_path, entry):
            if (entry is None) or (entry.snapshot is None):
                if (not options.snapshot) or (not os.path.exists(file_path)):
                    return StageEntry(None, entry.state)
            if entry.state != 'M':
                print(
                    "ERROR: Only (M)odified files can be captured by snapshot.",
                    file=sys.stderr)
                sys.exit(1)
            ss = entry.snapshot
            if ss is None:
                ss = str(uuid.uuid4()).replace('-', '')
            snapshot_file_name = os.path.join(stage_db_path, ss)
            if os.path.exists(snapshot_file_name):
                os.remove(snapshot_file_name)  # we are refreshing the snapshot
            if not os.path.exists(stage_db_path):
                os.mkdir(stage_db_path)
            # use copy2() to make sure the snapshot shares the timestamp of
            # the source file at the time of creation
            shutil.copy2(file_path, snapshot_file_name)
            return StageEntry(ss, entry.state)

        if not options.branch:
            print('ERROR: Could not determine branch.', file=sys.stderr)
            sys.exit(1)

        root = find_hg_root()
        if root:
            os.chdir(root)
            os.chdir("..")

        if not os.path.exists('.hg'):
            os.chdir(working_dir)
            print('ERROR: Must be in root of working copy to stage.',
                  file=sys.stderr)
            sys.exit(1)

        stage_name = "default" if options.stage_name is None else options.stage_name

        stage_db = {}
        stage_path = super(Stage, self).get_staging_root(root, options)
        if not os.path.exists(stage_path):
            os.mkdir(stage_path)
        stage_db_path = os.path.join(stage_path, stage_name)
        stage_db_file = os.path.join(stage_db_path, 'stage.db')
        if os.path.exists(stage_db_file):
            if options.erase_cache:
                print('All staged entries in "%s" cleared.' % stage_name)
                shutil.rmtree(stage_db_path)
            else:
                stage_db = super(Stage, self).load_stage_db(stage_db_file)

        if options.erase_cache:
            return  # nothing else to do

        if (len(options.args) == 0):
            capture_count = 0
            for key in stage_db:
                if stage_db[key].snapshot is not None:
                    capture_count += 1
            if capture_count:
                try:
                    print(
                        'You are about to refresh snapshot entries in the "%s" staging area.'
                        % stage_name)
                    input(
                        'Press ENTER if this is the intent (or press Ctrl-C to abort):'
                    )
                except SyntaxError:
                    pass

        command = ['hg', 'status', '-q', '-C', '.']
        output = subprocess.Popen(
            command, stdout=subprocess.PIPE).communicate()[0].decode("utf-8")
        output_lines = fixup_renames(output.split('\n'))

        lines = []
        if len(options.args):
            newlines = []
            # they are specifying files to be staged...filter 'lines'
            # based on them
            for item in options.args:
                found = -1
                for i in range(len(output_lines)):
                    if item in output_lines[i]:
                        newlines.append(output_lines[i][2:])
                        break

            if len(newlines) == 0:
                print(
                    "ERROR: Your specified filter(s) did not match any pending changes in the working copy.",
                    file=sys.stderr)
                sys.exit(1)

            lines = newlines
        else:
            # strip off the status bit
            for i in range(len(output_lines)):
                lines.append(output_lines[i][2:])

        if len(lines) == 0:
            print("ERROR: No files have been selected for staging.",
                  file=sys.stderr)
            sys.exit(1)

        # filter out duplicate entries
        status_db = {}
        for path in output_lines:
            key = path[2:].strip()
            status_db[key] = StageEntry(None, path[:1])

        added_files = []
        refreshed_files = []

        # all the files in lines[] are to be added to the staging database
        for path in lines:
            if path in stage_db:
                stage_db[path] = generate_snapshot(options, stage_db_path,
                                                   path, stage_db[path])
                refreshed_files.append('%s %s' % (stage_db[path].state, path))
            else:
                for l in output_lines:
                    if path in l:
                        added_files.append(l)
                        break
                entry = StageEntry()
                if path in status_db:
                    entry = status_db[path]
                stage_db[path] = generate_snapshot(options, stage_db_path,
                                                   path, entry)

        bad_keys = []
        for key in stage_db:
            if key not in status_db:
                # this file had been staged, but is now no longer modified
                bad_keys.append(key)

        for key in bad_keys:
            if stage_db[key].snapshot is not None:
                # it's a snapshot, delete it as well
                snapshot_file = os.path.join(stage_db_path,
                                             stage_db[key].snapshot)
                os.remove(snapshot_file)
            del status_db[key]

        # save the new database
        super(Stage, self).save_stage_db(stage_db, stage_db_file)

        if len(added_files) or len(refreshed_files):
            s = Info.Status()
            if len(added_files):
                print(
                    'The following new %s entries were added to the "%s" staging area:'
                    % ('snapshot' if options.snapshot else 'reference',
                       stage_name))
                s.process_lines(added_files, options)
            if len(refreshed_files):
                print(
                    'The following snapshot entries were refreshed in the "%s" staging area:'
                    % stage_name)
                s.process_lines(refreshed_files, options)
        else:
            print('No unique entries were added to the "%s" staging area.' %
                  stage_name)