diff options
Diffstat (limited to 'gitolfs3-shell')
-rw-r--r-- | gitolfs3-shell/Cargo.toml | 6 | ||||
-rw-r--r-- | gitolfs3-shell/src/main.rs | 145 |
2 files changed, 151 insertions, 0 deletions
diff --git a/gitolfs3-shell/Cargo.toml b/gitolfs3-shell/Cargo.toml new file mode 100644 index 0000000..c0b5d3a --- /dev/null +++ b/gitolfs3-shell/Cargo.toml | |||
@@ -0,0 +1,6 @@ | |||
1 | [package] | ||
2 | name = "gitolfs3-shell" | ||
3 | version = "0.1.0" | ||
4 | edition = "2021" | ||
5 | |||
6 | [dependencies] | ||
diff --git a/gitolfs3-shell/src/main.rs b/gitolfs3-shell/src/main.rs new file mode 100644 index 0000000..f0c5f34 --- /dev/null +++ b/gitolfs3-shell/src/main.rs | |||
@@ -0,0 +1,145 @@ | |||
1 | use std::{os::unix::process::CommandExt, process::ExitCode}; | ||
2 | |||
3 | fn main() -> ExitCode { | ||
4 | let bad_usage = ExitCode::from(2); | ||
5 | |||
6 | let mut args = std::env::args().skip(1); | ||
7 | if args.next() != Some("-c".to_string()) { | ||
8 | eprintln!("Expected usage: shell -c <argument>"); | ||
9 | return bad_usage; | ||
10 | } | ||
11 | let Some(cmd) = args.next() else { | ||
12 | eprintln!("Missing argument for argument '-c'"); | ||
13 | return bad_usage; | ||
14 | }; | ||
15 | if args.next().is_some() { | ||
16 | eprintln!("Too many arguments passed"); | ||
17 | return bad_usage; | ||
18 | } | ||
19 | |||
20 | let Some(mut cmd) = parse_cmd(&cmd) else { | ||
21 | eprintln!("Bad command"); | ||
22 | return bad_usage; | ||
23 | }; | ||
24 | |||
25 | let Some(mut program) = cmd.drain(0..1).next() else { | ||
26 | eprintln!("Bad command"); | ||
27 | return bad_usage; | ||
28 | }; | ||
29 | if program == "git" { | ||
30 | let Some(subcommand) = cmd.drain(0..1).next() else { | ||
31 | eprintln!("Bad command"); | ||
32 | return bad_usage; | ||
33 | }; | ||
34 | program.push('-'); | ||
35 | program.push_str(&subcommand); | ||
36 | } | ||
37 | |||
38 | let mut args = Vec::new(); | ||
39 | |||
40 | let git_cmds = ["git-receive-pack", "git-upload-archive", "git-upload-pack"]; | ||
41 | if git_cmds.contains(&program.as_str()) { | ||
42 | if cmd.len() != 1 { | ||
43 | eprintln!("Bad command"); | ||
44 | return bad_usage; | ||
45 | } | ||
46 | let repository = cmd[0].trim_start_matches('/'); | ||
47 | args.push(repository); | ||
48 | } else if program == "git-lfs-authenticate" { | ||
49 | program.clear(); | ||
50 | program.push_str("gitolfs3-authenticate"); | ||
51 | if cmd.len() != 2 { | ||
52 | eprintln!("Bad command"); | ||
53 | return bad_usage; | ||
54 | } | ||
55 | let repository = cmd[0].trim_start_matches('/'); | ||
56 | args.push(repository); | ||
57 | args.push(&cmd[1]); | ||
58 | } else { | ||
59 | eprintln!("Unknown command"); | ||
60 | return bad_usage; | ||
61 | } | ||
62 | |||
63 | let e = std::process::Command::new(program).args(args).exec(); | ||
64 | eprintln!("Error: {e}"); | ||
65 | ExitCode::FAILURE | ||
66 | } | ||
67 | |||
68 | fn parse_cmd(mut cmd: &str) -> Option<Vec<String>> { | ||
69 | let mut args = Vec::<String>::new(); | ||
70 | |||
71 | cmd = cmd.trim_matches(is_posix_space); | ||
72 | while !cmd.is_empty() { | ||
73 | if cmd.starts_with('\'') { | ||
74 | let (arg, remaining) = parse_sq(cmd)?; | ||
75 | args.push(arg); | ||
76 | cmd = remaining.trim_start_matches(is_posix_space); | ||
77 | } else if let Some((arg, remaining)) = cmd.split_once(is_posix_space) { | ||
78 | args.push(arg.to_owned()); | ||
79 | cmd = remaining.trim_start_matches(is_posix_space); | ||
80 | } else { | ||
81 | args.push(cmd.to_owned()); | ||
82 | cmd = ""; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | Some(args) | ||
87 | } | ||
88 | |||
89 | fn is_posix_space(c: char) -> bool { | ||
90 | // Form feed: 0x0c | ||
91 | // Vertical tab: 0x0b | ||
92 | c == ' ' || c == '\x0c' || c == '\n' || c == '\r' || c == '\t' || c == '\x0b' | ||
93 | } | ||
94 | |||
95 | fn parse_sq(s: &str) -> Option<(String, &str)> { | ||
96 | #[derive(PartialEq, Eq)] | ||
97 | enum SqState { | ||
98 | Quoted, | ||
99 | Unquoted { may_escape: bool }, | ||
100 | UnquotedEscaped, | ||
101 | } | ||
102 | |||
103 | let mut result = String::new(); | ||
104 | let mut state = SqState::Unquoted { may_escape: false }; | ||
105 | let mut remaining = ""; | ||
106 | for (i, c) in s.char_indices() { | ||
107 | match state { | ||
108 | SqState::Unquoted { may_escape: false } => { | ||
109 | if c != '\'' { | ||
110 | return None; | ||
111 | } | ||
112 | state = SqState::Quoted | ||
113 | } | ||
114 | SqState::Quoted => { | ||
115 | if c == '\'' { | ||
116 | state = SqState::Unquoted { may_escape: true }; | ||
117 | continue; | ||
118 | } | ||
119 | result.push(c); | ||
120 | } | ||
121 | SqState::Unquoted { may_escape: true } => { | ||
122 | if is_posix_space(c) { | ||
123 | remaining = &s[i..]; | ||
124 | break; | ||
125 | } | ||
126 | if c != '\\' { | ||
127 | return None; | ||
128 | } | ||
129 | state = SqState::UnquotedEscaped; | ||
130 | } | ||
131 | SqState::UnquotedEscaped => { | ||
132 | if c != '\\' && c != '!' { | ||
133 | return None; | ||
134 | } | ||
135 | result.push(c); | ||
136 | state = SqState::Unquoted { may_escape: false }; | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | |||
141 | if state != (SqState::Unquoted { may_escape: true }) { | ||
142 | return None; | ||
143 | } | ||
144 | Some((result, remaining)) | ||
145 | } | ||