Jelajahi Sumber

Merge pull request #586 from MartinFillon/fix-git-repos-column-always-showing

fix(git)!: remove Repo column when using --git-repos when no git repo
Christina Sørensen 2 tahun lalu
induk
melakukan
f113353910

+ 55 - 0
src/main.rs

@@ -71,6 +71,7 @@ fn main() {
 
             let git = git_options(&options, &input_paths);
             let writer = io::stdout();
+            let git_repos = git_repos(&options, &input_paths);
 
             let console_width = options.view.width.actual_terminal_width();
             let theme = options.theme.to_theme(stdout_istty);
@@ -81,6 +82,7 @@ fn main() {
                 theme,
                 console_width,
                 git,
+                git_repos,
             };
 
             info!("matching on exa.run");
@@ -148,6 +150,8 @@ pub struct Exa<'args> {
     /// This has to last the lifetime of the program, because the user might
     /// want to list several directories in the same repository.
     pub git: Option<GitCache>,
+
+    pub git_repos: bool,
 }
 
 /// The “real” environment variables type.
@@ -170,6 +174,51 @@ fn git_options(options: &Options, args: &[&OsStr]) -> Option<GitCache> {
     }
 }
 
+fn get_files_in_dir(paths: &mut Vec<PathBuf>, path: PathBuf) {
+    let temp_paths = if path.is_dir() {
+        path.read_dir()
+            .unwrap()
+            .map(|entry| entry.unwrap().path())
+            .collect::<Vec<PathBuf>>()
+    } else {
+        vec![path]
+    };
+    paths.extend(temp_paths);
+}
+
+fn git_repos(options: &Options, args: &[&OsStr]) -> bool {
+    let option_enabled = match options.view.mode {
+        Mode::Details(details::Options {
+            table: Some(ref table),
+            ..
+        })
+        | Mode::GridDetails(grid_details::Options {
+            details:
+                details::Options {
+                    table: Some(ref table),
+                    ..
+                },
+            ..
+        }) => table.columns.subdir_git_repos || table.columns.subdir_git_repos_no_stat,
+        _ => false,
+    };
+    if option_enabled {
+        let paths: Vec<PathBuf> = args.iter().map(PathBuf::from).collect::<Vec<PathBuf>>();
+        let mut files: Vec<PathBuf> = Vec::new();
+        for path in paths {
+            get_files_in_dir(&mut files, path);
+        }
+        let repos: Vec<bool> = files
+            .iter()
+            .map(git2::Repository::open)
+            .map(|repo| repo.is_ok())
+            .collect();
+        repos.contains(&true)
+    } else {
+        false
+    }
+}
+
 impl<'args> Exa<'args> {
     /// # Errors
     ///
@@ -355,6 +404,7 @@ impl<'args> Exa<'args> {
 
                 let git_ignoring = self.options.filter.git_ignore == GitIgnore::CheckAndIgnore;
                 let git = self.git.as_ref();
+                let git_repos = self.git_repos;
                 let r = details::Render {
                     dir,
                     files,
@@ -365,6 +415,7 @@ impl<'args> Exa<'args> {
                     filter,
                     git_ignoring,
                     git,
+                    git_repos,
                 };
                 r.render(&mut self.writer)
             }
@@ -377,6 +428,7 @@ impl<'args> Exa<'args> {
                 let filter = &self.options.filter;
                 let git_ignoring = self.options.filter.git_ignore == GitIgnore::CheckAndIgnore;
                 let git = self.git.as_ref();
+                let git_repos = self.git_repos;
 
                 let r = grid_details::Render {
                     dir,
@@ -390,6 +442,7 @@ impl<'args> Exa<'args> {
                     git_ignoring,
                     git,
                     console_width,
+                    git_repos,
                 };
                 r.render(&mut self.writer)
             }
@@ -400,6 +453,7 @@ impl<'args> Exa<'args> {
                 let recurse = self.options.dir_action.recurse_options();
                 let git_ignoring = self.options.filter.git_ignore == GitIgnore::CheckAndIgnore;
                 let git = self.git.as_ref();
+                let git_repos = self.git_repos;
 
                 let r = details::Render {
                     dir,
@@ -411,6 +465,7 @@ impl<'args> Exa<'args> {
                     filter,
                     git_ignoring,
                     git,
+                    git_repos,
                 };
                 r.render(&mut self.writer)
             }

+ 3 - 1
src/output/details.rs

@@ -137,6 +137,8 @@ pub struct Render<'a> {
     pub git_ignoring: bool,
 
     pub git: Option<&'a GitCache>,
+
+    pub git_repos: bool,
 }
 
 #[rustfmt::skip]
@@ -187,7 +189,7 @@ impl<'a> Render<'a> {
                 (None, _) => { /* Keep Git how it is */ }
             }
 
-            let mut table = Table::new(table, self.git, self.theme);
+            let mut table = Table::new(table, self.git, self.theme, self.git_repos);
 
             if self.opts.header {
                 let header = table.header_row();

+ 5 - 1
src/output/grid_details.rs

@@ -88,6 +88,8 @@ pub struct Render<'a> {
     pub git: Option<&'a GitCache>,
 
     pub console_width: usize,
+
+    pub git_repos: bool,
 }
 
 impl<'a> Render<'a> {
@@ -109,6 +111,7 @@ impl<'a> Render<'a> {
             filter:        self.filter,
             git_ignoring:  self.git_ignoring,
             git:           self.git,
+            git_repos:     self.git_repos,
         };
     }
 
@@ -128,6 +131,7 @@ impl<'a> Render<'a> {
             filter:        self.filter,
             git_ignoring:  self.git_ignoring,
             git:           self.git,
+            git_repos:     self.git_repos,
         };
     }
 
@@ -271,7 +275,7 @@ impl<'a> Render<'a> {
             (None, _) => { /* Keep Git how it is */ }
         }
 
-        let mut table = Table::new(options, self.git, self.theme);
+        let mut table = Table::new(options, self.git, self.theme, self.git_repos);
         let mut rows = Vec::new();
 
         if self.details.header {

+ 12 - 5
src/output/table.rs

@@ -57,7 +57,7 @@ pub struct Columns {
 }
 
 impl Columns {
-    pub fn collect(&self, actually_enable_git: bool) -> Vec<Column> {
+    pub fn collect(&self, actually_enable_git: bool, git_repos: bool) -> Vec<Column> {
         let mut columns = Vec::with_capacity(4);
 
         if self.inode {
@@ -123,11 +123,11 @@ impl Columns {
             columns.push(Column::GitStatus);
         }
 
-        if self.subdir_git_repos {
+        if self.subdir_git_repos && git_repos {
             columns.push(Column::SubdirGitRepo(true));
         }
 
-        if self.subdir_git_repos_no_stat {
+        if self.subdir_git_repos_no_stat && git_repos {
             columns.push(Column::SubdirGitRepo(false));
         }
 
@@ -387,11 +387,18 @@ pub struct Row {
 }
 
 impl<'a> Table<'a> {
-    pub fn new(options: &'a Options, git: Option<&'a GitCache>, theme: &'a Theme) -> Table<'a> {
-        let columns = options.columns.collect(git.is_some());
+    pub fn new(
+        options: &'a Options,
+        git: Option<&'a GitCache>,
+        theme: &'a Theme,
+        git_repos: bool,
+    ) -> Table<'a> {
+        let columns = options.columns.collect(git.is_some(), git_repos);
         let widths = TableWidths::zero(columns.len());
         let env = &*ENVIRONMENT;
 
+        debug!("Creating table with columns: {:?}", columns);
+
         Table {
             theme,
             widths,

+ 21 - 21
tests/cmd/long_git_repos_nix.stdout

@@ -1,21 +1,21 @@
-.rw-r--r--  0 nixbld  1 Jan  1970 - - a
-.rw-r--r--  0 nixbld  1 Jan  1970 - - b
-.rw-r--r--  0 nixbld  1 Jan  1970 - - c
-.rw-r--r--  0 nixbld  1 Jan  1970 - - d
-.rw-r--r--  0 nixbld  1 Jan  1970 - - e
-drwxr-xr-x  - nixbld  1 Jan  1970 - - exa
-.rw-r--r--  0 nixbld  1 Jan  1970 - - f
-.rw-r--r--  0 nixbld  1 Jan  1970 - - g
-.rw-r--r--  0 nixbld  1 Jan  1970 - - h
-.rw-r--r--  0 nixbld  1 Jan  1970 - - i
-.rw-r--r--  0 nixbld  1 Jan  1970 - - image.jpg.img.c.rs.log.png
-.rw-r--r-- 19 nixbld  1 Jan  1970 - - index.svg
-.rw-r--r--  0 nixbld  1 Jan  1970 - - j
-.rw-r--r--  0 nixbld  1 Jan  1970 - - k
-.rw-r--r--  0 nixbld  1 Jan  1970 - - l
-.rw-r--r--  0 nixbld  1 Jan  1970 - - m
-.rw-r--r--  0 nixbld  1 Jan  1970 - - n
-.rw-r--r--  0 nixbld  1 Jan  1970 - - o
-.rw-r--r--  0 nixbld  1 Jan  1970 - - p
-.rw-r--r--  0 nixbld  1 Jan  1970 - - q
-drwxr-xr-x  - nixbld  1 Jan  1970 - - vagrant
+.rw-r--r--  0 nixbld  1 Jan  1970 a
+.rw-r--r--  0 nixbld  1 Jan  1970 b
+.rw-r--r--  0 nixbld  1 Jan  1970 c
+.rw-r--r--  0 nixbld  1 Jan  1970 d
+.rw-r--r--  0 nixbld  1 Jan  1970 e
+drwxr-xr-x  - nixbld  1 Jan  1970 exa
+.rw-r--r--  0 nixbld  1 Jan  1970 f
+.rw-r--r--  0 nixbld  1 Jan  1970 g
+.rw-r--r--  0 nixbld  1 Jan  1970 h
+.rw-r--r--  0 nixbld  1 Jan  1970 i
+.rw-r--r--  0 nixbld  1 Jan  1970 image.jpg.img.c.rs.log.png
+.rw-r--r-- 19 nixbld  1 Jan  1970 index.svg
+.rw-r--r--  0 nixbld  1 Jan  1970 j
+.rw-r--r--  0 nixbld  1 Jan  1970 k
+.rw-r--r--  0 nixbld  1 Jan  1970 l
+.rw-r--r--  0 nixbld  1 Jan  1970 m
+.rw-r--r--  0 nixbld  1 Jan  1970 n
+.rw-r--r--  0 nixbld  1 Jan  1970 o
+.rw-r--r--  0 nixbld  1 Jan  1970 p
+.rw-r--r--  0 nixbld  1 Jan  1970 q
+drwxr-xr-x  - nixbld  1 Jan  1970 vagrant

+ 21 - 21
tests/cmd/long_git_repos_no_status_nix.stdout

@@ -1,21 +1,21 @@
-.rw-r--r--  0 nixbld  1 Jan  1970 - - a
-.rw-r--r--  0 nixbld  1 Jan  1970 - - b
-.rw-r--r--  0 nixbld  1 Jan  1970 - - c
-.rw-r--r--  0 nixbld  1 Jan  1970 - - d
-.rw-r--r--  0 nixbld  1 Jan  1970 - - e
-drwxr-xr-x  - nixbld  1 Jan  1970 -   exa
-.rw-r--r--  0 nixbld  1 Jan  1970 - - f
-.rw-r--r--  0 nixbld  1 Jan  1970 - - g
-.rw-r--r--  0 nixbld  1 Jan  1970 - - h
-.rw-r--r--  0 nixbld  1 Jan  1970 - - i
-.rw-r--r--  0 nixbld  1 Jan  1970 - - image.jpg.img.c.rs.log.png
-.rw-r--r-- 19 nixbld  1 Jan  1970 - - index.svg
-.rw-r--r--  0 nixbld  1 Jan  1970 - - j
-.rw-r--r--  0 nixbld  1 Jan  1970 - - k
-.rw-r--r--  0 nixbld  1 Jan  1970 - - l
-.rw-r--r--  0 nixbld  1 Jan  1970 - - m
-.rw-r--r--  0 nixbld  1 Jan  1970 - - n
-.rw-r--r--  0 nixbld  1 Jan  1970 - - o
-.rw-r--r--  0 nixbld  1 Jan  1970 - - p
-.rw-r--r--  0 nixbld  1 Jan  1970 - - q
-drwxr-xr-x  - nixbld  1 Jan  1970 -   vagrant
+.rw-r--r--  0 nixbld  1 Jan  1970 a
+.rw-r--r--  0 nixbld  1 Jan  1970 b
+.rw-r--r--  0 nixbld  1 Jan  1970 c
+.rw-r--r--  0 nixbld  1 Jan  1970 d
+.rw-r--r--  0 nixbld  1 Jan  1970 e
+drwxr-xr-x  - nixbld  1 Jan  1970 exa
+.rw-r--r--  0 nixbld  1 Jan  1970 f
+.rw-r--r--  0 nixbld  1 Jan  1970 g
+.rw-r--r--  0 nixbld  1 Jan  1970 h
+.rw-r--r--  0 nixbld  1 Jan  1970 i
+.rw-r--r--  0 nixbld  1 Jan  1970 image.jpg.img.c.rs.log.png
+.rw-r--r-- 19 nixbld  1 Jan  1970 index.svg
+.rw-r--r--  0 nixbld  1 Jan  1970 j
+.rw-r--r--  0 nixbld  1 Jan  1970 k
+.rw-r--r--  0 nixbld  1 Jan  1970 l
+.rw-r--r--  0 nixbld  1 Jan  1970 m
+.rw-r--r--  0 nixbld  1 Jan  1970 n
+.rw-r--r--  0 nixbld  1 Jan  1970 o
+.rw-r--r--  0 nixbld  1 Jan  1970 p
+.rw-r--r--  0 nixbld  1 Jan  1970 q
+drwxr-xr-x  - nixbld  1 Jan  1970 vagrant