Skip to content

Commit

Permalink
TagsReader uses git rev-parse --git-dir to determine .git path
Browse files Browse the repository at this point in the history
What?
=====

Rather than assume that reading tags will occur within the current
directory's `.git` directory, this uses `git` itself to determine the
application root.
  • Loading branch information
joshuaclayton committed May 19, 2020
1 parent 147f80f commit 1f0ea1d
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions crates/read_ctags/src/tags_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use super::{CtagItem, CtagsParseError};
use std::collections::HashSet;
use std::convert::From;
use std::default::Default;
use std::env::current_dir;
use std::fmt::{Display, Formatter};
use std::fs;
use std::io;
use std::io::Error;
use std::path::PathBuf;
use std::process::Command;

/// TagsReader provides a mechanism for attempting to read multiple ctags files until the first is
/// found
Expand Down Expand Up @@ -48,15 +50,48 @@ impl Display for ReadCtagsError {
}
}

fn git_path() -> Option<PathBuf> {
match Command::new("git")
.arg("rev-parse")
.arg("--git-dir")
.output()
{
Ok(o) => {
if o.status.success() {
std::str::from_utf8(&o.stdout)
.ok()
.map(|k| PathBuf::from(k.trim_end()))
} else {
None
}
}
Err(_) => None,
}
}

fn cwd_tags_paths() -> Vec<PathBuf> {
vec![PathBuf::from("tags"), PathBuf::from("tmp/tags")]
}

impl Default for TagsReader {
fn default() -> Self {
TagsReader {
filenames: vec![
PathBuf::from(".git/tags"),
PathBuf::from("tags"),
PathBuf::from("tmp/tags"),
],
let mut filenames = vec![];

if let Ok(current_dir) = current_dir() {
if let Some(app_git_path) = git_path() {
if current_dir == app_git_path {
filenames.push(app_git_path.join("tags"));
filenames.extend(cwd_tags_paths());
} else {
filenames.extend(cwd_tags_paths());
filenames.push(app_git_path.join("tags"));
filenames.push(app_git_path.join("../tags"));
filenames.push(app_git_path.join("../tmp/tags"));
}
}
}

TagsReader { filenames }
}
}

Expand Down

0 comments on commit 1f0ea1d

Please sign in to comment.