Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. //! # git-recent
  2. //!
  3. //! Prints recently checked out git branches, most recently checked out branch first.
  4. //!
  5. //! Usage: git recent [lines]
  6. //!
  7. //! Options:
  8. //! lines - number of lines to output [default=10]
  9.  
  10. use std::io::prelude::*;
  11. use std::io::BufReader;
  12. use std::process::{Command, Stdio};
  13. use std::collections::HashSet;
  14. use std::path::PathBuf;
  15. use std::env;
  16.  
  17. fn main() {
  18. let max_lines = env::args().nth(1)
  19. .and_then(|arg| arg.parse::<usize>().ok())
  20. .unwrap_or(10);
  21.  
  22. let revparse = Command::new("git")
  23. .arg("rev-parse")
  24. .arg("--git-dir")
  25. .stdout(Stdio::piped())
  26. .output()
  27. .expect("git-rev-parse command failed")
  28. .stdout;
  29.  
  30. let heads_dir = {
  31. let mut git_dir = PathBuf::from(String::from_utf8_lossy(&revparse).trim_right().to_owned());
  32. git_dir.push("refs");
  33. git_dir.push("heads");
  34. git_dir
  35. };
  36.  
  37. let reflog = BufReader::new(
  38. Command::new("git")
  39. .arg("reflog")
  40. .stdout(Stdio::piped())
  41. .spawn()
  42. .expect("git-reflog command failed")
  43. .stdout.unwrap());
  44.  
  45. let mut seen = HashSet::new();
  46. let branches = reflog.lines()
  47. .filter_map(Result::ok)
  48. .filter(|line| line.contains("checkout: "))
  49. .filter_map(|line| line.rsplitn(2, ' ').next().map(str::to_owned))
  50. .filter(|branch| heads_dir.join(branch).exists())
  51. .filter(|branch| seen.insert(branch.clone()))
  52. .take(max_lines);
  53.  
  54. for branch in branches {
  55. println!("{}", branch);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement