@@ -229,8 +229,25 @@ impl GitRepo {
229229 }
230230
231231 /// Clone `url` into `path`.
232+ ///
233+ /// Authenticates the same way fetch and push do (see
234+ /// [`remote_credentials`]), using the global git config since there is
235+ /// no repository config yet. Without this a private HTTPS repo fails
236+ /// with a 401.
232237 pub fn clone ( url : & str , path : impl AsRef < Path > ) -> Result < Self > {
233- let repo = Repository :: clone ( url, path. as_ref ( ) )
238+ let config = git2:: Config :: open_default ( ) . ok ( ) ;
239+
240+ let mut callbacks = git2:: RemoteCallbacks :: new ( ) ;
241+ callbacks. credentials ( |url, username, allowed| {
242+ remote_credentials ( config. as_ref ( ) , url, username, allowed)
243+ } ) ;
244+
245+ let mut fetch_options = git2:: FetchOptions :: new ( ) ;
246+ fetch_options. remote_callbacks ( callbacks) ;
247+
248+ let repo = git2:: build:: RepoBuilder :: new ( )
249+ . fetch_options ( fetch_options)
250+ . clone ( url, path. as_ref ( ) )
234251 . with_context ( || format ! ( "failed to clone {url} into {}" , path. as_ref( ) . display( ) ) ) ?;
235252 Ok ( Self { repo } )
236253 }
@@ -334,6 +351,29 @@ impl GitRepo {
334351 Self :: diff_to_file_entries ( & diff)
335352 }
336353
354+ /// The net file-level change of a range of commits, `oldest` through
355+ /// `newest`: the first parent of `oldest` (or the empty tree, if it's a
356+ /// root commit) against `newest`, one entry per changed file. A file
357+ /// changed and changed back within the range doesn't appear.
358+ pub fn commit_range_diff ( & self , oldest : & str , newest : & str ) -> Result < Vec < DiffFileEntry > > {
359+ let find = |id : & str | -> Result < git2:: Commit < ' _ > > {
360+ let oid =
361+ git2:: Oid :: from_str ( id) . with_context ( || format ! ( "invalid commit id: {id}" ) ) ?;
362+ Ok ( self . repo . find_commit ( oid) ?)
363+ } ;
364+ let oldest = find ( oldest) ?;
365+ let newest = find ( newest) ?;
366+ let base_tree = oldest. parents ( ) . next ( ) . map ( |p| p. tree ( ) ) . transpose ( ) ?;
367+ let tree = newest. tree ( ) ?;
368+
369+ let mut opts = DiffOptions :: new ( ) ;
370+ let diff = self
371+ . repo
372+ . diff_tree_to_tree ( base_tree. as_ref ( ) , Some ( & tree) , Some ( & mut opts) ) ?;
373+
374+ Self :: diff_to_file_entries ( & diff)
375+ }
376+
337377 /// Unified-diff patch (git's standard format, including file and hunk
338378 /// headers) of the uncommitted changes to `path`. With `staged`, this
339379 /// is the index against HEAD (`git diff --cached -- <path>`); otherwise
@@ -1051,53 +1091,15 @@ impl GitRepo {
10511091 Ok ( ( ) )
10521092 }
10531093
1054- /// Supplies credentials for a remote operation (fetch/push): an SSH key
1055- /// from a running ssh-agent for SSH remotes, the configured
1056- /// `credential.helper` for HTTPS remotes (e.g. Git Credential Manager,
1057- /// `osxkeychain`, `store`), or the platform's native default
1058- /// credentials (e.g. Windows SSPI) as a last resort.
1059- ///
1060- /// On Windows, the helper process is spawned with `CREATE_NO_WINDOW` so
1061- /// it doesn't flash a console window (this app has none of its own).
1062- /// Elsewhere it goes through `git2::Cred::credential_helper` directly,
1063- /// which has no such issue.
1094+ /// Supplies credentials for a fetch or push; see [`remote_credentials`].
10641095 fn credentials_callback (
10651096 & self ,
10661097 url : & str ,
10671098 username_from_url : Option < & str > ,
10681099 allowed_types : git2:: CredentialType ,
10691100 ) -> std:: result:: Result < git2:: Cred , git2:: Error > {
1070- if allowed_types. contains ( git2:: CredentialType :: SSH_KEY ) {
1071- if let Some ( username) = username_from_url {
1072- if let Ok ( cred) = git2:: Cred :: ssh_key_from_agent ( username) {
1073- return Ok ( cred) ;
1074- }
1075- }
1076- }
1077-
1078- if allowed_types. contains ( git2:: CredentialType :: USER_PASS_PLAINTEXT ) {
1079- #[ cfg( windows) ]
1080- {
1081- if let Some ( ( username, password) ) =
1082- windows_credential_helper ( & self . repo , url, username_from_url)
1083- {
1084- if let Ok ( cred) = git2:: Cred :: userpass_plaintext ( & username, & password) {
1085- return Ok ( cred) ;
1086- }
1087- }
1088- }
1089- #[ cfg( not( windows) ) ]
1090- {
1091- if let Ok ( config) = self . repo . config ( ) {
1092- if let Ok ( cred) = git2:: Cred :: credential_helper ( & config, url, username_from_url)
1093- {
1094- return Ok ( cred) ;
1095- }
1096- }
1097- }
1098- }
1099-
1100- git2:: Cred :: default ( )
1101+ let config = self . repo . config ( ) . ok ( ) ;
1102+ remote_credentials ( config. as_ref ( ) , url, username_from_url, allowed_types)
11011103 }
11021104
11031105 /// The commit id `rev` resolves to, or `None` if it doesn't resolve
@@ -1571,7 +1573,57 @@ pub fn set_global_identity(name: &str, email: &str) -> Result<()> {
15711573 Ok ( ( ) )
15721574}
15731575
1574- /// Runs the repository's configured `credential.helper` to obtain a
1576+ /// Supplies credentials for a remote operation (clone/fetch/push): an SSH
1577+ /// key from a running ssh-agent for SSH remotes, the configured
1578+ /// `credential.helper` for HTTPS remotes (e.g. Git Credential Manager,
1579+ /// `osxkeychain`, `store`), or the platform's native default credentials
1580+ /// (e.g. Windows SSPI) as a last resort.
1581+ ///
1582+ /// `config` is the repository's config, or the global one for a clone.
1583+ ///
1584+ /// On Windows, the helper process is spawned with `CREATE_NO_WINDOW` so it
1585+ /// doesn't flash a console window (this app has none of its own).
1586+ /// Elsewhere it goes through `git2::Cred::credential_helper` directly,
1587+ /// which has no such issue.
1588+ fn remote_credentials (
1589+ config : Option < & git2:: Config > ,
1590+ url : & str ,
1591+ username_from_url : Option < & str > ,
1592+ allowed_types : git2:: CredentialType ,
1593+ ) -> std:: result:: Result < git2:: Cred , git2:: Error > {
1594+ if allowed_types. contains ( git2:: CredentialType :: SSH_KEY ) {
1595+ if let Some ( username) = username_from_url {
1596+ if let Ok ( cred) = git2:: Cred :: ssh_key_from_agent ( username) {
1597+ return Ok ( cred) ;
1598+ }
1599+ }
1600+ }
1601+
1602+ if allowed_types. contains ( git2:: CredentialType :: USER_PASS_PLAINTEXT ) {
1603+ if let Some ( config) = config {
1604+ #[ cfg( windows) ]
1605+ {
1606+ if let Some ( ( username, password) ) =
1607+ windows_credential_helper ( config, url, username_from_url)
1608+ {
1609+ if let Ok ( cred) = git2:: Cred :: userpass_plaintext ( & username, & password) {
1610+ return Ok ( cred) ;
1611+ }
1612+ }
1613+ }
1614+ #[ cfg( not( windows) ) ]
1615+ {
1616+ if let Ok ( cred) = git2:: Cred :: credential_helper ( config, url, username_from_url) {
1617+ return Ok ( cred) ;
1618+ }
1619+ }
1620+ }
1621+ }
1622+
1623+ git2:: Cred :: default ( )
1624+ }
1625+
1626+ /// Runs the configured `credential.helper` to obtain a
15751627/// username/password for an HTTPS remote, the same protocol `git` itself
15761628/// uses to talk to a helper (`<cmd> get`, `key=value` lines on stdin,
15771629/// `username=`/`password=` lines read back from stdout).
@@ -1581,7 +1633,7 @@ pub fn set_global_identity(name: &str, email: &str) -> Result<()> {
15811633/// GUI app, which has no console of its own.
15821634#[ cfg( windows) ]
15831635fn windows_credential_helper (
1584- repo : & git2:: Repository ,
1636+ config : & git2:: Config ,
15851637 url : & str ,
15861638 username_from_url : Option < & str > ,
15871639) -> Option < ( String , String ) > {
@@ -1591,7 +1643,6 @@ fn windows_credential_helper(
15911643
15921644 const CREATE_NO_WINDOW : u32 = 0x0800_0000 ;
15931645
1594- let config = repo. config ( ) . ok ( ) ?;
15951646 let helper = config. get_string ( "credential.helper" ) . ok ( ) ?;
15961647 if helper. trim ( ) . is_empty ( ) {
15971648 return None ;
@@ -1846,6 +1897,47 @@ mod tests {
18461897 assert_eq ! ( diff[ 0 ] . deletions, 0 ) ;
18471898 }
18481899
1900+ #[ test]
1901+ fn commit_range_diff_shows_net_change ( ) {
1902+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
1903+ let repo = GitRepo :: init ( dir. path ( ) ) . unwrap ( ) ;
1904+ let commit = |message : & str | {
1905+ repo. commit ( message, "Test User" , "test@example.com" )
1906+ . unwrap ( )
1907+ } ;
1908+
1909+ write_file ( dir. path ( ) , "a.txt" , "one\n " ) ;
1910+ repo. stage ( "a.txt" ) . unwrap ( ) ;
1911+ commit ( "init" ) ;
1912+
1913+ write_file ( dir. path ( ) , "a.txt" , "one\n two\n " ) ;
1914+ write_file ( dir. path ( ) , "tmp.txt" , "scratch\n " ) ;
1915+ repo. stage ( "a.txt" ) . unwrap ( ) ;
1916+ repo. stage ( "tmp.txt" ) . unwrap ( ) ;
1917+ let second = commit ( "add line and scratch file" ) ;
1918+
1919+ fs:: remove_file ( dir. path ( ) . join ( "tmp.txt" ) ) . unwrap ( ) ;
1920+ write_file ( dir. path ( ) , "b.txt" , "new\n " ) ;
1921+ let mut index = repo. repo . index ( ) . unwrap ( ) ;
1922+ index. remove_path ( Path :: new ( "tmp.txt" ) ) . unwrap ( ) ;
1923+ index. write ( ) . unwrap ( ) ;
1924+ repo. stage ( "b.txt" ) . unwrap ( ) ;
1925+ let third = commit ( "drop scratch file, add b" ) ;
1926+
1927+ let diff = repo. commit_range_diff ( & second, & third) . unwrap ( ) ;
1928+ let mut paths: Vec < _ > = diff. iter ( ) . map ( |f| f. path . as_str ( ) ) . collect ( ) ;
1929+ paths. sort ( ) ;
1930+ // tmp.txt was added and removed within the range.
1931+ assert_eq ! ( paths, [ "a.txt" , "b.txt" ] ) ;
1932+ let a = diff. iter ( ) . find ( |f| f. path == "a.txt" ) . unwrap ( ) ;
1933+ assert_eq ! ( a. status, FileStatus :: Modified ) ;
1934+ assert_eq ! ( ( a. additions, a. deletions) , ( 1 , 0 ) ) ;
1935+
1936+ // A single-commit range matches commit_diff.
1937+ let single = repo. commit_range_diff ( & third, & third) . unwrap ( ) ;
1938+ assert_eq ! ( single. len( ) , repo. commit_diff( & third) . unwrap( ) . len( ) ) ;
1939+ }
1940+
18491941 #[ test]
18501942 fn log_includes_parent_ids ( ) {
18511943 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
0 commit comments