Skip to content

Commit

Permalink
change!: fetch::agent() now returns the agent name, `fetch::agent_t…
Browse files Browse the repository at this point in the history
…uple()` returns specific key-value pair.

This is in preparation for allowing user-defined agent strings.
  • Loading branch information
Byron committed Nov 9, 2022
1 parent 328c069 commit f957d9a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
8 changes: 4 additions & 4 deletions git-protocol/src/fetch/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod with_io {
use bstr::{BString, ByteSlice};
use git_transport::client::Capabilities;

use crate::fetch::{agent, command::Feature, Command};
use crate::fetch::{agent_tuple, command::Feature, Command};

impl Command {
/// Only V2
Expand Down Expand Up @@ -134,7 +134,7 @@ mod with_io {
feature => server_capabilities.contains(feature),
})
.map(|s| (s, None))
.chain(Some(agent()))
.chain(Some(agent_tuple()))
.collect()
}
git_transport::Protocol::V2 => {
Expand All @@ -153,11 +153,11 @@ mod with_io {
.copied()
.filter(|feature| supported_features.iter().any(|supported| supported == feature))
.map(|s| (s, None))
.chain(Some(agent()))
.chain(Some(agent_tuple()))
.collect()
}
},
Command::LsRefs => vec![agent()],
Command::LsRefs => vec![agent_tuple()],
}
}
/// Panics if the given arguments and features don't match what's statically known. It's considered a bug in the delegate.
Expand Down
12 changes: 9 additions & 3 deletions git-protocol/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ pub use arguments::Arguments;
pub mod command;
pub use command::Command;

/// Returns the name of the agent as key-value pair, commonly used in HTTP headers.
pub fn agent() -> (&'static str, Option<&'static str>) {
("agent", Some(concat!("git/oxide-", env!("CARGO_PKG_VERSION"))))
/// Produce the name of the `git` client name as key-value pair, suitable for `git` commands on the protocol layer
/// , so that it's valid for `git` servers, using `name` as user-defined portion of the value.
pub fn agent_tuple() -> (&'static str, Option<&'static str>) {
("agent", Some(agent()))
}

/// The name of the `git` client in a format suitable for presentation to a `git` server.
pub fn agent() -> &'static str {
concat!("git/oxide-", env!("CARGO_PKG_VERSION"))
}

///
Expand Down
12 changes: 8 additions & 4 deletions git-protocol/src/fetch/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ mod v1 {
git_transport::Protocol::V1,
&capabilities("multi_ack side-band side-band-64k multi_ack_detailed")
),
&[("side-band-64k", None), ("multi_ack_detailed", None), fetch::agent()]
&[
("side-band-64k", None),
("multi_ack_detailed", None),
fetch::agent_tuple()
]
);
}

Expand All @@ -42,7 +46,7 @@ mod v1 {
("allow-reachable-sha1-in-want", None),
("no-done", None),
("filter", None),
fetch::agent()
fetch::agent_tuple()
],
"we don't enforce include-tag or no-progress"
);
Expand Down Expand Up @@ -73,7 +77,7 @@ mod v2 {
["shallow", "filter", "ref-in-want", "sideband-all", "packfile-uris"]
.iter()
.map(|s| (*s, None))
.chain(Some(fetch::agent()))
.chain(Some(fetch::agent_tuple()))
.collect::<Vec<_>>()
)
}
Expand Down Expand Up @@ -111,7 +115,7 @@ mod v2 {
git_transport::Protocol::V2,
&capabilities("something-else", "does not matter as there are none")
),
&[fetch::agent()]
&[fetch::agent_tuple()]
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions git-protocol/tests/fetch/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn clone_abort_prep() -> crate::Result {
0001000csymrefs
0009peel
00000000",
fetch::agent().1.expect("value set")
fetch::agent_tuple().1.expect("value set")
)
.as_bytes()
.as_bstr()
Expand Down Expand Up @@ -93,7 +93,7 @@ async fn ls_remote() -> crate::Result {
0001000csymrefs
0009peel
0000",
fetch::agent().1.expect("value set")
fetch::agent_tuple().1.expect("value set")
)
.as_bytes()
.as_bstr(),
Expand Down Expand Up @@ -183,7 +183,7 @@ async fn ref_in_want() -> crate::Result {
001dwant-ref refs/heads/main
0009done
00000000",
fetch::agent().1.expect("value set")
fetch::agent_tuple().1.expect("value set")
)
.as_bytes()
.as_bstr()
Expand Down

0 comments on commit f957d9a

Please sign in to comment.