# Path: sgit/util.py
# def noop(*args, **kwargs):
#     pass
# 
# Path: sgit/cmd.py
# class GitCmd(GitRepoHelper, Cmd):
#     executable = 'git'
#     bin = ['git']
#     opts = [
#         '--no-pager',
#         '-c', 'color.diff=false',
#         '-c', 'color.status=false',
#         '-c', 'color.branch=false',
#         '-c', 'status.displayCommentPrefix=true',
#         '-c', 'core.commentchar=#',
#     ]
# 
#     def git(self, cmd, *args, **kwargs):
#         return self.cmd(cmd, *args, **kwargs)
# 
#     def git_string(self, cmd, *args, **kwargs):
#         return self._string(cmd, *args, **kwargs)
# 
#     def git_lines(self, cmd, *args, **kwargs):
#         return self._lines(cmd, *args, **kwargs)
# 
#     def git_exit_code(self, cmd, *args, **kwargs):
#         return self._exit_code(cmd, *args, **kwargs)
# 
#     def git_async(self, cmd, *args, **kwargs):
#         return self.cmd_async(cmd, *args, **kwargs)
# 
# Path: sgit/helpers.py
# class GitStashHelper(object):
# 
#     STASH_RE = re.compile(r'^stash@\{(.*)\}:\s*(.*)')
# 
#     def get_stashes(self, repo):
#         stashes = []
#         output = self.git_lines(['stash', 'list'], cwd=repo)
#         for line in output:
#             match = self.STASH_RE.match(line)
#             if match:
#                 stashes.append((match.group(1), match.group(2)))
#         return stashes
# 
# class GitStatusHelper(object):
# 
#     def file_in_git(self, repo, filename):
#         return self.git_exit_code(['ls-files', filename, '--error-unmatch'], cwd=repo) == 0
# 
#     def has_changes(self, repo):
#         return self.has_staged_changes(repo) or self.has_unstaged_changes(repo)
# 
#     def has_staged_changes(self, repo):
#         return self.git_exit_code(['diff', '--exit-code', '--quiet', '--cached'], cwd=repo) != 0
# 
#     def has_unstaged_changes(self, repo):
#         return self.git_exit_code(['diff', '--exit-code', '--quiet'], cwd=repo) != 0
# 
#     # def get_porcelain_status(self, repo):
#     #     mode = self.get_untracked_mode()
#     #     cmd = ['status', '--porcelain', ('--untracked-files=%s' % mode) if mode else None]
#     #     return self.git_lines(cmd, cwd=repo)
# 
#     def get_porcelain_status(self, repo):
#         mode = self.get_untracked_mode()
#         cmd = ['status', '-z', ('--untracked-files=%s' % mode) if mode else None]
# 
#         output = self.git_string(cmd, cwd=repo, strip=False)
#         rows = output.split('\x00')
#         lines = []
#         idx = 0
#         while idx < len(rows):
#             row = rows[idx]
#             if row and not row.startswith('#'):
#                 status, filename = row[:2], row[3:]
#                 if status[0] == 'R':
#                     lines.append("%s %s -> %s" % (status, rows[idx + 1], filename))
#                     idx += 1
#                 else:
#                     lines.append("%s %s" % (status, filename))
#             idx += 1
#         return lines
# 
#     def get_files_status(self, repo):
#         untracked, unstaged, staged = [], [], []
#         status = self.get_porcelain_status(repo)
#         for l in status:
#             state, filename = l[:2], l[3:]
#             index, worktree = state
#             if state in ('DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU'):
#                 logger.warning("unmerged WTF: %s, %s", state, filename)
#             elif state == '??':
#                 untracked.append(('?', filename))
#             elif state == '!!':
#                 continue
#             else:
#                 if worktree != ' ':
#                     unstaged.append((worktree, filename))
#                 if index != ' ':
#                     staged.append((index, filename))
#         return untracked, unstaged, staged
# 
#     def get_untracked_mode(self):
#         # get untracked files mode
#         setting = get_setting('git_status_untracked_files', 'all')
# 
#         mode = 'all'
#         if setting == 'none':
#             mode = 'no'
#         elif setting == 'auto':
#             mode = None
#         return mode
# 
# class GitErrorHelper(object):
# 
#     def format_error_message(self, msg):
#         if msg.startswith('error: '):
#             msg = msg[7:]
#         elif msg.lower().startswith('Note: '):
#             msg = msg[6:]
#         if msg.endswith('Aborting\n'):
#             msg = msg.rstrip()[:-8]
#         return msg


import time
import sublime
from sublime_plugin import WindowCommand
from .util import noop
from .cmd import GitCmd
from .helpers import GitStashHelper, GitStatusHelper, GitErrorHelper
    def pop_or_apply_from_panel(self, action):
        repo = self.get_repo()
        if not repo:
            return

        stashes = self.get_stashes(repo)

        if not stashes:
            return sublime.error_message('No stashes. Use the Git: Stash command to stash changes')

        callback = self.pop_or_apply_callback(repo, action, stashes)
        panel = []
        for name, title in stashes:
            panel.append([title, "stash@{%s}" % name])

        self.window.show_quick_panel(panel, callback)

    def pop_or_apply_callback(self, repo, action, stashes):
        def inner(choice):
            if choice != -1:
                name, _ = stashes[choice]
                exit_code, stdout, stderr = self.git(['stash', action, '-q', 'stash@{%s}' % name], cwd=repo)
                if exit_code != 0:
                    sublime.error_message(self.format_error_message(stderr))
                window = sublime.active_window()
                if window:
                    window.run_command('git_status', {'refresh_only': True})
        return inner



# Path: sgit/util.py
# def noop(*args, **kwargs):
#     pass
# 
# Path: sgit/cmd.py
# class GitCmd(GitRepoHelper, Cmd):
#     executable = 'git'
#     bin = ['git']
#     opts = [
#         '--no-pager',
#         '-c', 'color.diff=false',
#         '-c', 'color.status=false',
#         '-c', 'color.branch=false',
#         '-c', 'status.displayCommentPrefix=true',
#         '-c', 'core.commentchar=#',
#     ]
# 
#     def git(self, cmd, *args, **kwargs):
#         return self.cmd(cmd, *args, **kwargs)
# 
#     def git_string(self, cmd, *args, **kwargs):
#         return self._string(cmd, *args, **kwargs)
# 
#     def git_lines(self, cmd, *args, **kwargs):
#         return self._lines(cmd, *args, **kwargs)
# 
#     def git_exit_code(self, cmd, *args, **kwargs):
#         return self._exit_code(cmd, *args, **kwargs)
# 
#     def git_async(self, cmd, *args, **kwargs):
#         return self.cmd_async(cmd, *args, **kwargs)
# 
# Path: sgit/helpers.py
# class GitStashHelper(object):
# 
#     STASH_RE = re.compile(r'^stash@\{(.*)\}:\s*(.*)')
# 
#     def get_stashes(self, repo):
#         stashes = []
#         output = self.git_lines(['stash', 'list'], cwd=repo)
#         for line in output:
#             match = self.STASH_RE.match(line)
#             if match:
#                 stashes.append((match.group(1), match.group(2)))
#         return stashes
# 
# class GitStatusHelper(object):
# 
#     def file_in_git(self, repo, filename):
#         return self.git_exit_code(['ls-files', filename, '--error-unmatch'], cwd=repo) == 0
# 
#     def has_changes(self, repo):
#         return self.has_staged_changes(repo) or self.has_unstaged_changes(repo)
# 
#     def has_staged_changes(self, repo):
#         return self.git_exit_code(['diff', '--exit-code', '--quiet', '--cached'], cwd=repo) != 0
# 
#     def has_unstaged_changes(self, repo):
#         return self.git_exit_code(['diff', '--exit-code', '--quiet'], cwd=repo) != 0
# 
#     # def get_porcelain_status(self, repo):
#     #     mode = self.get_untracked_mode()
#     #     cmd = ['status', '--porcelain', ('--untracked-files=%s' % mode) if mode else None]
#     #     return self.git_lines(cmd, cwd=repo)
# 
#     def get_porcelain_status(self, repo):
#         mode = self.get_untracked_mode()
#         cmd = ['status', '-z', ('--untracked-files=%s' % mode) if mode else None]
# 
#         output = self.git_string(cmd, cwd=repo, strip=False)
#         rows = output.split('\x00')
#         lines = []
#         idx = 0
#         while idx < len(rows):
#             row = rows[idx]
#             if row and not row.startswith('#'):
#                 status, filename = row[:2], row[3:]
#                 if status[0] == 'R':
#                     lines.append("%s %s -> %s" % (status, rows[idx + 1], filename))
#                     idx += 1
#                 else:
#                     lines.append("%s %s" % (status, filename))
#             idx += 1
#         return lines
# 
#     def get_files_status(self, repo):
#         untracked, unstaged, staged = [], [], []
#         status = self.get_porcelain_status(repo)
#         for l in status:
#             state, filename = l[:2], l[3:]
#             index, worktree = state
#             if state in ('DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU'):
#                 logger.warning("unmerged WTF: %s, %s", state, filename)
#             elif state == '??':
#                 untracked.append(('?', filename))
#             elif state == '!!':
#                 continue
#             else:
#                 if worktree != ' ':
#                     unstaged.append((worktree, filename))
#                 if index != ' ':
#                     staged.append((index, filename))
#         return untracked, unstaged, staged
# 
#     def get_untracked_mode(self):
#         # get untracked files mode
#         setting = get_setting('git_status_untracked_files', 'all')
# 
#         mode = 'all'
#         if setting == 'none':
#             mode = 'no'
#         elif setting == 'auto':
#             mode = None
#         return mode
# 
# class GitErrorHelper(object):
# 
#     def format_error_message(self, msg):
#         if msg.startswith('error: '):
#             msg = msg[7:]
#         elif msg.lower().startswith('Note: '):
#             msg = msg[6:]
#         if msg.endswith('Aborting\n'):
#             msg = msg.rstrip()[:-8]
#         return msg


# Path: sgit/stash.py
import time
import sublime
from sublime_plugin import WindowCommand
from .util import noop
from .cmd import GitCmd
from .helpers import GitStashHelper, GitStatusHelper, GitErrorHelper

    def pop_or_apply_from_panel(self, action):
        repo = self.get_repo()
        if not repo:
            return

        stashes = self.get_stashes(repo)

        if not stashes:
            return sublime.error_message('No stashes. Use the Git: Stash command to stash changes')

        callback = self.pop_or_apply_callback(repo, action, stashes)
        panel = []
        for name, title in stashes:
            panel.append([title, "stash@{%s}" % name])

        self.window.show_quick_panel(panel, callback)

    def pop_or_apply_callback(self, repo, action, stashes):
        def inner(choice):
            if choice != -1:
                name, _ = stashes[choice]
                exit_code, stdout, stderr = self.git(['stash', action, '-q', 'stash@{%s}' % name], cwd=repo)
                if exit_code != 0:
                    sublime.error_message(self.format_error_message(stderr))
                window = sublime.active_window()
                if window:
                    window.run_command('git_status', {'refresh_only': True})
        return inner



class GitStashCommand(WindowCommand, GitCmd, GitStatusHelper):