Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to show untracked files on the diff output #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Git.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@
// Watch for gitignore changes?
// When found, import them. This will hide the ignored files from the sidebar.
,"gitignore_sync": false

// Allows diff to show untracked (new) files as well, instead of omitting
// them until staged
,"diff_untracked_files": false
}
36 changes: 35 additions & 1 deletion git/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@

class GitDiff (object):
def run(self, edit=None, ignore_whitespace=False):
self.ignore_whitespace = ignore_whitespace
self.edit = edit
s = sublime.load_settings("Git.sublime-settings")

# stage untracked files as empty files so they can be seen on the diff
if s.get('diff_untracked_files'):
self.stage_untracked_files()
else:
self.continue_diff(None)

def continue_diff(self, result):
command = ['git', 'diff', '--no-color']
if ignore_whitespace:
if self.ignore_whitespace:
command.extend(('--ignore-all-space', '--ignore-blank-lines'))
command.extend(('--', self.get_file_name()))
self.run_command(command, self.diff_done)
Expand All @@ -26,6 +37,29 @@ def diff_done(self, result):
else:
self.scratch(result, title="Git Diff", syntax=syntax)

self.reset_untracked_files()

# obtain the modified files list
def stage_untracked_files(self):
untracked_files_command = ['git', 'status', '-uall', '--porcelain']
self.run_command(untracked_files_command, self.stage_untracked_files_result)

# obtain the untracked files list and stage them
def stage_untracked_files_result(self, result):
# filter for untracked files
lines = result.rstrip().split('\n')
self.empty_stages = [x[3:] for x in lines if x.startswith('??')]

if len(self.empty_stages) > 0:
command = ['git', 'add', '.', '-N']
self.run_command(command, self.continue_diff, show_status=False)

# reset the empty staged files to their original untracked status
def reset_untracked_files(self):
if len(self.empty_stages) > 0:
reset_empty_stages_command = ['git', 'reset', '--quiet', '--'] + self.empty_stages
self.run_command(reset_empty_stages_command, show_status=False)


class GitDiffCommit (object):
def run(self, edit=None, ignore_whitespace=False):
Expand Down