bgit 0.4.2

User-friendly Git wrapper for beginners, automating essential tasks like adding, committing, and pushing changes. It includes smart rules to avoid common pitfalls, such as accidentally adding sensitive files or directories and has exclusive support for portable hooks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use super::AtomicEvent;
use crate::auth::auth_utils::transform_url_for_preference;
use crate::auth::git_auth::setup_auth_callbacks;
use crate::bgit_error::BGitError;
use crate::config::global::BGitGlobalConfig;
use crate::rules::Rule;
use git2::{Oid, Repository};
use log::debug;
use log::info;
use std::path::Path;

pub struct GitPush<'a> {
    pub pre_check_rules: Vec<Box<dyn Rule + Send + Sync>>,
    pub force_with_lease: bool,
    pub set_upstream: bool,
    pub global_config: &'a BGitGlobalConfig,
}

impl<'a> AtomicEvent<'a> for GitPush<'a> {
    fn new(global_config: &'a BGitGlobalConfig) -> Self
    where
        Self: Sized,
    {
        GitPush {
            pre_check_rules: vec![],
            force_with_lease: false,
            set_upstream: false,
            global_config,
        }
    }

    fn get_name(&self) -> &str {
        "git_push"
    }

    fn get_action_description(&self) -> &str {
        "Push changes to remote repository"
    }

    fn add_pre_check_rule(&mut self, rule: Box<dyn Rule + Send + Sync>) {
        self.pre_check_rules.push(rule);
    }

    fn get_pre_check_rule(&self) -> &Vec<Box<dyn Rule + Send + Sync>> {
        &self.pre_check_rules
    }

    fn raw_execute(&self) -> Result<bool, Box<BGitError>> {
        let repo = Repository::discover(Path::new("."))
            .map_err(|e| self.to_bgit_error(&format!("Failed to discover repository: {e}")))?;

        // Get the current branch - handle unborn branch case
        let (head, branch_name) = match repo.head() {
            Ok(head) => {
                let branch_name = head
                    .shorthand()
                    .ok_or_else(|| self.to_bgit_error("Failed to get branch name"))?
                    .to_string();
                (head, branch_name)
            }
            Err(e) if e.code() == git2::ErrorCode::UnbornBranch => {
                return Err(self.to_bgit_error("Cannot push from unborn branch (no commits to push). Create your first commit before pushing."));
            }
            Err(e) => {
                return Err(self.to_bgit_error(&format!("Failed to get HEAD reference: {e}")));
            }
        };

        // Determine which remote to use (prefer branch upstream > single remote > 'origin')
        let remote_name = self
            .determine_remote_name(&repo, &branch_name)
            .map_err(|e| self.to_bgit_error(&e.to_string()))?;

        // Get remote - handle case where no remote is configured
        let mut remote = repo.find_remote(&remote_name).map_err(|e| {
            self.to_bgit_error(&format!("Failed to find remote '{remote_name}': {e}"))
        })?;

        if let Some(url) = remote.url()
            && let Some(new_url) =
                transform_url_for_preference(url, self.global_config.auth.preferred)
        {
            let preferred = self.global_config.auth.preferred;
            log::info!(
                "Using preferred auth ({:?}) URL: {} -> {}",
                preferred,
                url,
                new_url
            );
            if let Ok(temp) = repo.remote_anonymous(new_url.as_str()) {
                remote = temp;
            }
        }

        // Prepare push options with authentication and callbacks
        let mut push_options = self.create_push_options();

        if self.force_with_lease {
            // Best-effort native force-with-lease emulation with libgit2:
            // 1) Capture expected remote OID from tracking ref before fetching
            let tracking_ref = format!("refs/remotes/{remote_name}/{branch_name}");
            let expected_remote_oid = repo
                .refname_to_id(&tracking_ref)
                .unwrap_or_else(|_| Oid::zero());

            // 2) Fetch latest state for the branch to update tracking ref
            let mut fetch_opts = git2::FetchOptions::new();
            fetch_opts.remote_callbacks(setup_auth_callbacks(self.global_config));
            let fetch_refspec = format!(
                "refs/heads/{0}:refs/remotes/{1}/{0}",
                branch_name, remote_name
            );
            remote
                .fetch(&[fetch_refspec], Some(&mut fetch_opts), None)
                .map_err(|e| self.to_bgit_error(&format!("Failed to fetch from remote: {e}")))?;

            // 3) Compare actual vs expected; if diverged, abort
            let actual_remote_oid = repo
                .refname_to_id(&tracking_ref)
                .unwrap_or_else(|_| Oid::zero());
            if actual_remote_oid != expected_remote_oid {
                return Err(self.to_bgit_error(&format!(
                    "Lease broken: remote '{remote_name}/{branch_name}' is at {actual_remote_oid}, expected {expected_remote_oid}. Aborting push."
                )));
            }

            // 4) Lease holds — perform forced update
            let refspec = if self.set_upstream {
                format!("+refs/heads/{branch_name}:refs/heads/{branch_name}")
            } else {
                format!("+refs/heads/{branch_name}")
            };

            remote.push(&[refspec], Some(&mut push_options)).map_err(|e| {
                let transport_hint = self.transport_hint(remote.url());
                self.to_bgit_error(&format!(
                    "Failed to push to remote {transport_hint} (force-with-lease): {e}. If authentication is required, ensure your credentials are set up."
                ))
            })?;
        } else {
            // Pre-flight safety check for regular push
            self.validate_push_safety(&repo, &head, &branch_name)?;

            let refspec = if self.set_upstream {
                format!("refs/heads/{branch_name}:refs/heads/{branch_name}")
            } else {
                format!("refs/heads/{branch_name}")
            };

            remote.push(&[refspec], Some(&mut push_options)).map_err(|e| {
                let transport_hint = self.transport_hint(remote.url());
                self.to_bgit_error(&format!(
                    "Failed to push to remote {transport_hint}: {e}. If authentication is required, ensure your credentials are set up."
                ))
            })?;
        }

        // Set upstream if requested or if there is no upstream yet
        if self.set_upstream || !self.has_upstream(&repo, &branch_name)? {
            self.set_upstream_branch(&repo, &remote_name, &branch_name)?;
            info!("Set upstream to {remote_name}/{branch_name}");
        }

        Ok(true)
    }
}

impl<'a> GitPush<'a> {
    pub fn with_force_with_lease(&mut self, force_with_lease: bool) -> &mut Self {
        self.force_with_lease = force_with_lease;
        self
    }

    pub fn with_upstream_flag(&mut self, set_upstream: bool) -> &mut Self {
        self.set_upstream = set_upstream;
        self
    }

    fn validate_push_safety(
        &self,
        repo: &Repository,
        head: &git2::Reference,
        branch_name: &str,
    ) -> Result<(), Box<BGitError>> {
        let remote_name = self
            .determine_remote_name(repo, branch_name)
            .unwrap_or_else(|_| String::from("origin"));
        if let Ok(remote_ref) =
            repo.find_reference(&format!("refs/remotes/{remote_name}/{branch_name}"))
        {
            let local_commit = head
                .peel_to_commit()
                .map_err(|e| self.to_bgit_error(&format!("Failed to get local commit: {e}")))?;

            let remote_commit = remote_ref
                .peel_to_commit()
                .map_err(|e| self.to_bgit_error(&format!("Failed to get remote commit: {e}")))?;

            // If commits are the same, we're up to date
            if local_commit.id() == remote_commit.id() {
                return Ok(());
            }

            // Check if local is behind remote
            let merge_base = repo
                .merge_base(local_commit.id(), remote_commit.id())
                .map_err(|e| self.to_bgit_error(&format!("Failed to find merge base: {e}")))?;

            if merge_base == local_commit.id() && local_commit.id() != remote_commit.id() {
                return Err(
                    self.to_bgit_error("Local branch is behind remote. Pull changes first.")
                );
            }
        }

        Ok(())
    }

    fn set_upstream_branch(
        &self,
        repo: &Repository,
        remote_name: &str,
        branch_name: &str,
    ) -> Result<(), Box<BGitError>> {
        let mut branch = repo
            .find_branch(branch_name, git2::BranchType::Local)
            .map_err(|e| {
                self.to_bgit_error(&format!("Failed to find local branch {branch_name}: {e}"))
            })?;

        let upstream_name = format!("{remote_name}/{branch_name}");
        branch.set_upstream(Some(&upstream_name)).map_err(|e| {
            self.to_bgit_error(&format!("Failed to set upstream to {upstream_name}: {e}"))
        })?;

        Ok(())
    }

    fn has_upstream(&self, repo: &Repository, branch_name: &str) -> Result<bool, Box<BGitError>> {
        let branch = repo
            .find_branch(branch_name, git2::BranchType::Local)
            .map_err(|e| {
                self.to_bgit_error(&format!("Failed to find local branch {branch_name}: {e}"))
            })?;
        Ok(branch.upstream().is_ok())
    }

    // Determine the remote to use for pushes: prefer branch upstream remote, else if exactly one remote exists, use it, else try 'origin', else error.
    fn determine_remote_name(
        &self,
        repo: &Repository,
        branch_name: &str,
    ) -> Result<String, String> {
        // Try branch upstream
        if let Ok(branch) = repo.find_branch(branch_name, git2::BranchType::Local)
            && let Ok(upstream) = branch.upstream()
            && let Some(name) = upstream.get().name()
        {
            // name like: refs/remotes/<remote>/<branch>
            let parts: Vec<&str> = name.split('/').collect();
            if parts.len() >= 4 && parts[0] == "refs" && parts[1] == "remotes" {
                return Ok(parts[2].to_string());
            }
        }

        // If exactly one remote is configured, use it
        if let Ok(remotes) = repo.remotes() {
            if remotes.len() == 1
                && let Some(r) = remotes.get(0)
            {
                return Ok(r.to_string());
            }
            // If 'origin' exists, prefer it
            for i in 0..remotes.len() {
                if let Some(r) = remotes.get(i)
                    && r == "origin"
                {
                    return Ok("origin".to_string());
                }
            }
        }

        Err("No suitable remote configured. Add a remote or set an upstream (git branch --set-upstream-to <remote>/<branch>).".to_string())
    }

    /// Create push options with authentication
    fn create_push_options(&'a self) -> git2::PushOptions<'a> {
        let mut push_options = git2::PushOptions::new();
        let mut callbacks = setup_auth_callbacks(self.global_config);
        // Surface ref update errors clearly during push
        callbacks.push_update_reference(|refname, status| match status {
            Some(msg) => {
                debug!("Push failed for {refname}: {msg}");
                Err(git2::Error::from_str(msg))
            }
            None => {
                debug!("Push successful for {refname}");
                Ok(())
            }
        });
        push_options.remote_callbacks(callbacks);
        push_options
    }

    fn transport_hint(&self, url_opt: Option<&str>) -> &'static str {
        if let Some(u) = url_opt {
            if u.starts_with("http://") || u.starts_with("https://") {
                "(HTTPS) - check token/credentials"
            } else if u.starts_with("ssh://")
                || u.starts_with("git@")
                || (u.contains('@') && u.contains(':'))
            {
                "(SSH) - check keys/agent"
            } else {
                ""
            }
        } else {
            ""
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::global::BGitGlobalConfig;
    use git2::Signature;
    use std::fs;
    use tempfile::TempDir;

    fn init_repo_with_commit() -> (TempDir, Repository, String) {
        let td = TempDir::with_prefix("bgit_unit_").unwrap();
        let repo = Repository::init(td.path()).unwrap();

        // Configure user
        repo.config().unwrap().set_str("user.name", "Test").unwrap();
        repo.config()
            .unwrap()
            .set_str("user.email", "test@example.com")
            .unwrap();

        // Create initial commit on main
        fs::write(td.path().join("README.md"), b"hello").unwrap();
        let mut idx = repo.index().unwrap();
        idx.add_path(Path::new("README.md")).unwrap();
        idx.write().unwrap();
        let tree_id = idx.write_tree().unwrap();
        let tree = repo.find_tree(tree_id).unwrap();
        let sig = Signature::now("Test", "test@example.com").unwrap();
        let _ = repo
            .commit(Some("HEAD"), &sig, &sig, "init", &tree, &[])
            .unwrap();
        drop(tree);
        // Ensure branch name exists
        let branch_name = {
            let head_ref = repo.head().unwrap();
            head_ref.shorthand().unwrap().to_string()
        };
        (td, repo, branch_name)
    }

    #[test]
    fn determine_remote_prefers_upstream() {
        let (_td, repo, branch) = init_repo_with_commit();
        // add two remotes
        repo.remote("foo", "https://example.com/foo.git").unwrap();
        repo.remote("origin", "https://example.com/origin.git")
            .unwrap();

        // Simulate upstream to foo/<branch> by creating the tracking ref
        let head_id = repo.head().unwrap().target().unwrap();
        repo.reference(&format!("refs/remotes/foo/{branch}"), head_id, true, "test")
            .unwrap();

        // Also set branch upstream in config
        repo.config()
            .unwrap()
            .set_str(&format!("branch.{branch}.remote"), "foo")
            .unwrap();
        repo.config()
            .unwrap()
            .set_str(
                &format!("branch.{branch}.merge"),
                &format!("refs/heads/{branch}"),
            )
            .unwrap();

        let global = BGitGlobalConfig::default();
        let gp = GitPush::new(&global);
        let chosen = gp.determine_remote_name(&repo, &branch).unwrap();
        assert_eq!(chosen, "foo");
    }

    #[test]
    fn expected_remote_oid_uses_remote_name() {
        let (_td, repo, branch) = init_repo_with_commit();
        repo.remote("foo", "https://example.com/foo.git").unwrap();

        // Create tracking ref for foo/<branch> pointing to HEAD
        let head_id = repo.head().unwrap().target().unwrap();
        repo.reference(&format!("refs/remotes/foo/{branch}"), head_id, true, "test")
            .unwrap();

        let tracking = format!("refs/remotes/foo/{branch}");
        let oid = repo.refname_to_id(&tracking).unwrap();
        assert_eq!(oid, head_id);
    }

    #[test]
    fn has_upstream_checks_presence() {
        let (_td, repo, branch) = init_repo_with_commit();
        repo.remote("foo", "https://example.com/foo.git").unwrap();

        let global = BGitGlobalConfig::default();
        let gp = GitPush::new(&global);
        // Initially no upstream
        assert!(!gp.has_upstream(&repo, &branch).unwrap());

        // Set upstream to foo/branch
        // Ensure the tracking reference exists for the remote branch
        let head_id = repo.head().unwrap().target().unwrap();
        repo.reference(&format!("refs/remotes/foo/{branch}"), head_id, true, "test")
            .unwrap();
        let mut br = repo.find_branch(&branch, git2::BranchType::Local).unwrap();
        br.set_upstream(Some(&format!("foo/{branch}"))).unwrap();
        assert!(gp.has_upstream(&repo, &branch).unwrap());
    }
}