def commit(self, author_name, author_email, msg, amend=False):
        if amend:
            # Get details of current HEAD
            is_merge_resolve = False

            output = self.run_cmd(['log', '-1', '--pretty=format:%P%n%an%n%ae%n%aD'])
            if not output.strip():
                raise GitError, "Cannot amend in an empty repository!"

            parents, author_name, author_email, author_date = output.split('\n')
            parents = parents.split(' ')
        else:
            author_date = None # Use current date

            # Get HEAD sha1 id
            if self.head == 'HEAD':
                parents = []
            else:
                head = self.run_cmd(['rev-parse', 'HEAD']).strip()
                parents = [head]

            # Get merge head if exists
            is_merge_resolve = False
            try:
                merge_head_filename = os.path.join(self.dir, '.git', 'MERGE_HEAD')
                if os.path.isfile(merge_head_filename):
                    f = open(merge_head_filename)
                    p = f.read().strip()
                    f.close()
                    parents.append(p)
                    is_merge_resolve = True
            except OSError:
                raise GitError, "Cannot open MERGE_HEAD file"

        # Write tree
        tree = self.run_cmd(['write-tree'], raise_error=True).strip()

        # Write commit
        parent_args = []
        for parent in parents:
            parent_args += ['-p', parent]

        env = {}
        if author_name: env['GIT_AUTHOR_NAME'] = author_name
        if author_email: env['GIT_AUTHOR_EMAIL'] = author_email
        if author_date: env['GIT_AUTHOR_DATE'] = author_date

        commit = self.run_cmd(
            ['commit-tree', tree] + parent_args,
            raise_error=True,
            input=msg,
            env=env
        ).strip()

        # Update reference
        self.run_cmd(['update-ref', 'HEAD', commit], raise_error=True)

        # Remove MERGE_HEAD
        if is_merge_resolve:
            try:
                os.unlink(os.path.join(self.dir, '.git', 'MERGE_HEAD'))
                os.unlink(os.path.join(self.dir, '.git', 'MERGE_MODE'))
                os.unlink(os.path.join(self.dir, '.git', 'MERGE_MSG'))
                os.unlink(os.path.join(self.dir, '.git', 'ORIG_HEAD'))
            except __HOLE__:
                pass
dataset/ETHPy150Open gyim/stupidgit/stupidgit_gui/git.py/Repository.commit