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

Use git environment variables if set #42

Merged
merged 2 commits into from
Feb 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions autoload/committia/git.vim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function! s:extract_first_line(str) abort
endfunction

function! s:search_git_dir_and_work_tree() abort
" Use environment variables if set
if !empty($GIT_DIR) && !empty($GIT_WORK_TREE)
return [$GIT_DIR, $GIT_WORK_TREE]
endif
Copy link
Owner

@rhysd rhysd Feb 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$GIT_WORK_TREE is set by user and the path must be existing. So $GIT_WORK_TREE should be verified as follows:

if !empty($GIT_DIR) && !empty($GIT_WORK_TREE) && isdirectory($GIT_WORK_TREE)
    return [$GIT_DIR, $GIT_WORK_TREE]
endif

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should we explicitly raise an error rather than falling back into automatic worktree detection silently?

if !empty($GIT_DIR) && !empty($GIT_WORK_TREE)
    if !isdirectory($GIT_WORK_TREE)
        throw 'Directory specified with $GIT_WORK_TREE is not found: ' . $GIT_WORK_TREE
    endif
    return [$GIT_DIR, $GIT_WORK_TREE]
endif

Either look ok for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I set GIT_WORK_TREE manually, that's correct. I like the variant throwing an error because git cannot operate if the value of GIT_WORK_TREE is invalid:

test master@HEAD$ GIT_WORK_TREE="/tmp/invalid" git status
fatal: This operation must be run in a work tree


" '/.git' is unnecessary under submodule directory.
let matched = matchlist(expand('%:p'), '[\\/]\.git[\\/]\%(\(modules\|worktrees\)[\\/].\+[\\/]\)\?\%(COMMIT_EDITMSG\|MERGE_MSG\)$')
if len(matched) > 1
Expand Down