git-tools 0.1.4

Git subcommands to help with your workflow.
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
429
430
431
432
433
434
435
436
437
438
439
440
441
#![allow(dead_code)]

use globset::GlobSet;
use std::env::{current_dir, set_current_dir};
use std::path::{Path, PathBuf};

use git2::{
    Branch, BranchType, Commit, Config, Cred, CredentialType, Error, ErrorCode, FetchOptions,
    MergeOptions, PushOptions, RemoteCallbacks, Sort, StatusOptions,
};
pub use git2::{Oid, Repository};

pub struct Git {
    pub repo: Repository,
    pub head_message: String,
    pub head_hash: String,
    pub branch_name: Option<String>,
    pub upstream: Option<String>,
    pub config: Config,
}

impl Git {
    pub fn open() -> Result<Git, Error> {
        if let Some(path) = find_git_repository()? {
            set_current_dir(path).map_err(|e| Error::from_str(&e.to_string()))?;
        }

        let repo = Repository::open(".")?;
        let head_message;
        let head_hash;
        let branch_name;
        let upstream;

        {
            let (object, maybe_ref) = repo.revparse_ext("HEAD")?;
            let commit = object.as_commit().unwrap();
            head_message = commit.message().unwrap().to_string();
            head_hash = format!("{}", object.id());
            branch_name = maybe_ref.and_then(|x| {
                x.shorthand()
                    .filter(|&x| x != "HEAD")
                    .map(|x| x.to_string())
            });
            upstream = if let Some(name) = branch_name.as_ref() {
                if let Ok(remote_branch) = repo
                    .find_branch(name, BranchType::Local)
                    .and_then(|x| x.upstream())
                {
                    remote_branch.name()?.map(|x| x.to_string())
                } else {
                    None
                }
            } else {
                None
            };
        }

        let config = repo.config()?.snapshot()?;

        Ok(Git {
            repo,
            head_message,
            head_hash,
            branch_name,
            upstream,
            config,
        })
    }

    pub fn get_staged_and_unstaged_files(&self) -> Result<Vec<String>, Error> {
        let mut files = Vec::new();
        let mut options = StatusOptions::new();
        options.include_untracked(true);
        options.include_ignored(false);

        for entry in self.repo.statuses(Some(&mut options))?.iter() {
            files.push(entry.path().unwrap().to_string());
        }

        Ok(files)
    }

    pub fn branch(&self, name: &str, from: Option<&str>) -> Result<String, Error> {
        let object = self.repo.revparse_single(from.unwrap_or("HEAD"))?;
        let commit = object.as_commit().unwrap();
        let branch = self.repo.branch(name, &commit, false)?;

        Ok(branch.get().name().unwrap().to_string())
    }

    pub fn get_branch_hash(&self, branch_name: &str) -> Result<Option<String>, Error> {
        if let (_, Some(reference)) = self.repo.revparse_ext(branch_name)? {
            Ok(Some(format!("{}", reference.target().unwrap())))
        } else {
            Ok(None)
        }
    }

    pub fn get_default_branch(&self, remote: &str) -> Result<String, Error> {
        let reference = match self
            .repo
            .find_reference(format!("refs/remotes/{}/HEAD", remote).as_str())
        {
            Ok(x) => x,
            Err(err) if err.code() == ErrorCode::NotFound => {
                return Ok(format!("{}/master", remote))
            }
            Err(err) => return Err(err),
        };

        Ok(reference
            .symbolic_target()
            .expect("reference HEAD is not symbolic")
            .strip_prefix("refs/remotes/")
            .expect("invalid target")
            .to_string())
    }

    pub fn switch_branch(&mut self, branch_name: &str) -> Result<(), Error> {
        let branch = self.repo.find_branch(branch_name, BranchType::Local)?;
        let object = self.repo.revparse_single(branch_name)?;

        self.repo.checkout_tree(&object, None)?;
        self.repo.set_head(branch.get().name().unwrap())?;

        self.branch_name = Some(branch_name.to_string());
        self.head_hash = format!("{}", object.id());
        if let Ok(upstream) = branch.upstream() {
            self.upstream = upstream.name()?.map(|x| x.to_string());
        }

        Ok(())
    }

    pub fn commit_files(&mut self, message: &str, files: &[&str]) -> Result<Oid, Error> {
        let object = self.repo.revparse_single("HEAD")?;
        let commit = object.as_commit().unwrap();
        let old_tree = commit.tree()?;

        let mut treebuilder = self.repo.treebuilder(Some(&old_tree))?;
        for file in files {
            let oid = self.repo.blob_path(Path::new(file))?;
            treebuilder.insert(file, oid, 0o100644)?;
        }
        let tree_oid = treebuilder.write()?;
        let tree = self.repo.find_tree(tree_oid)?;

        let signature = self.repo.signature()?;
        let oid = self.repo.commit(
            Some("HEAD"),
            &signature,
            &signature,
            message,
            &tree,
            &[&commit],
        )?;

        let mut index = self.repo.index()?;
        index.update_all(files, None)?;
        self.repo.checkout_index(Some(&mut index), None)?;

        self.head_hash = format!("{}", oid);

        Ok(oid)
    }

    pub fn has_file_changes(&self) -> Result<bool, Error> {
        let tree = self.repo.head()?.peel_to_tree()?;

        Ok(self
            .repo
            .diff_tree_to_workdir_with_index(Some(&tree), None)?
            .stats()?
            .files_changed()
            > 0)
    }

    pub fn check_no_conflict(&mut self, branch_name: &str) -> Result<Option<bool>, Error> {
        let mut cargo_lock_conflict = false;
        let our_object = self.repo.revparse_single("HEAD")?;
        let our = our_object.as_commit().expect("our is a commit");
        let their_object = self.repo.revparse_single(branch_name)?;
        let their = their_object.as_commit().expect("their is a commit");

        let mut options = MergeOptions::new();
        options.fail_on_conflict(false);

        let index = self.repo.merge_commits(&our, &their, Some(&options))?;
        let conflicts = index.conflicts()?.collect::<Result<Vec<_>, _>>()?;
        for conflict in conflicts {
            let their = conflict.their.expect("an index entry for their exist");
            let path = std::str::from_utf8(their.path.as_slice()).expect("valid UTF-8");

            if path == "Cargo.lock" {
                cargo_lock_conflict = true;
            } else {
                return Ok(None);
            }
        }

        Ok(Some(cargo_lock_conflict))
    }

    pub fn merge_no_conflict(
        &mut self,
        branch_name: &str,
        message: &str,
        ignore_conflict_globs: &GlobSet,
    ) -> Result<Option<(String, Vec<String>)>, Error> {
        let our_object = self.repo.revparse_single("HEAD")?;
        let our = our_object.as_commit().expect("our is a commit");
        let their_object = self.repo.revparse_single(branch_name)?;
        let their = their_object.as_commit().expect("their is a commit");

        let mut options = MergeOptions::new();
        options.fail_on_conflict(false);

        let mut index = self.repo.merge_commits(&our, &their, Some(&mut options))?;
        let conflicts = index.conflicts()?.collect::<Result<Vec<_>, _>>()?;
        let mut ignored_conflicts = Vec::new();
        for conflict in conflicts {
            let their = match conflict.their {
                Some(x) => x,
                None => return Ok(None),
            };

            let path = std::str::from_utf8(their.path.as_slice()).expect("valid UTF-8");

            if ignore_conflict_globs.matches(path).is_empty() {
                return Ok(None);
            } else {
                use bitvec::prelude::*;

                let path = path.to_owned();
                ignored_conflicts.push(path.clone());

                let mut flags = BitVec::<Msb0, _>::from_element(their.flags);
                // NOTE: Reset stage flags
                // https://github.com/git/git/blob/master/Documentation/technical/index-format.txt
                flags[2..=3].set_all(false);
                let their = git2::IndexEntry {
                    flags: flags.as_slice()[0],
                    ..their
                };
                index.remove_path(Path::new(&path)).unwrap();
                index.add(&their)?;
            }
        }

        let oid = index.write_tree_to(&self.repo)?;
        let tree = self.repo.find_tree(oid)?;

        let signature = self.repo.signature()?;
        let oid = self.repo.commit(
            Some("HEAD"),
            &signature,
            &signature,
            message,
            &tree,
            &[&our, &their],
        )?;

        let mut checkout_builder = git2::build::CheckoutBuilder::new();
        checkout_builder.force();
        self.repo.checkout_head(Some(&mut checkout_builder))?;

        self.head_hash = format!("{}", oid);

        Ok(Some((self.head_hash.clone(), ignored_conflicts)))
    }

    pub fn rev_list(&self, from: &str, to: &str, reversed: bool) -> Result<Vec<String>, Error> {
        let mut revwalk = self.repo.revwalk()?;
        if reversed {
            revwalk.set_sorting(Sort::TOPOLOGICAL | Sort::REVERSE);
        } else {
            revwalk.set_sorting(Sort::TOPOLOGICAL);
        }

        let from_object = self.repo.revparse_single(from)?;
        let to_object = self.repo.revparse_single(to)?;
        revwalk.hide(from_object.id())?;
        revwalk.push(to_object.id())?;

        revwalk
            .map(|x| x.map(|x| format!("{}", x)))
            .collect::<Result<Vec<_>, Error>>()
    }

    pub fn update_upstream(&self, branch_name: &str) -> Result<(), Error> {
        let branch = self.repo.find_branch(branch_name, BranchType::Remote)?;
        let (maybe_remote_name, branch_name) = get_remote_and_branch(&branch);

        // TODO: this method fails if branch_name is not a remote branch
        //       this `if` statement makes no sense
        if let Some(remote_name) = maybe_remote_name {
            let mut remote_callbacks = RemoteCallbacks::new();
            let mut handler = CredentialHandler::new();
            remote_callbacks.credentials(move |x, y, z| handler.credentials_callback(x, y, z));

            let mut fetch_options = FetchOptions::new();
            fetch_options.remote_callbacks(remote_callbacks);

            self.repo.find_remote(remote_name)?.fetch(
                &[branch_name],
                Some(&mut fetch_options),
                None,
            )?;
        }

        Ok(())
    }

    pub fn ancestors(&self, rev: &str) -> Result<Ancestors, Error> {
        let object = self.repo.revparse_single(rev)?;
        let commit = object.peel_to_commit()?;

        Ok(Ancestors {
            current: Some(commit),
        })
    }

    pub fn squash(
        &mut self,
        parent_0: &str,
        parent_1: &str,
        message: &str,
    ) -> Result<String, Error> {
        let parent_0 = self.repo.revparse_single(parent_0)?.peel_to_commit()?;
        let parent_1 = self.repo.revparse_single(parent_1)?.peel_to_commit()?;
        let head = self.repo.revparse_single("HEAD")?.peel_to_commit()?;
        let tree = self.repo.find_tree(head.tree_id())?;

        // git reset --soft to the parent "0" commit
        if let (_, Some(mut reference)) = self
            .repo
            .revparse_ext(self.branch_name.as_ref().unwrap_or(&self.head_hash))?
        {
            reference.set_target(parent_0.id(), message)?;
        } else {
            self.repo.set_head_detached(parent_0.id())?;
        }

        // Make a commit with the current tree
        let signature = self.repo.signature()?;
        let oid = self.repo.commit(
            Some("HEAD"),
            &signature,
            &signature,
            message,
            &tree,
            &[&parent_0, &parent_1],
        )?;

        self.head_hash = format!("{}", oid);

        Ok(self.head_hash.clone())
    }
}

fn find_git_repository() -> Result<Option<PathBuf>, Error> {
    let mut path = current_dir().map_err(|e| Error::from_str(&e.to_string()))?;

    loop {
        if path.join(".git").exists() {
            return Ok(Some(path));
        }
        if !path.pop() {
            break;
        }
    }

    Ok(None)
}

fn get_remote_and_branch<'a>(branch: &'a Branch) -> (Option<&'a str>, &'a str) {
    let mut parts = branch
        .get()
        .shorthand()
        .expect("valid UTF-8")
        .rsplitn(2, '/');
    let branch_name = parts.next().unwrap();
    let maybe_remote_name = parts.next();

    (maybe_remote_name, branch_name)
}

pub struct CredentialHandler {
    second_handler: git2_credentials::CredentialHandler,
    first_attempt_failed: bool,
}

impl CredentialHandler {
    pub fn new() -> CredentialHandler {
        let git_config = git2::Config::open_default().unwrap();
        let second_handler = git2_credentials::CredentialHandler::new(git_config);

        CredentialHandler {
            second_handler,
            first_attempt_failed: false,
        }
    }

    pub fn credentials_callback(
        &mut self,
        url: &str,
        username_from_url: Option<&str>,
        allowed_types: CredentialType,
    ) -> Result<Cred, Error> {
        if !self.first_attempt_failed && allowed_types.contains(CredentialType::SSH_KEY) {
            self.first_attempt_failed = true;
            let user = users::get_current_username().expect("could not get username");
            let home_dir = dirs::home_dir().expect("could not get home directory");

            Cred::ssh_key(
                username_from_url.unwrap_or_else(|| user.to_str().unwrap()),
                Some(&home_dir.join(".ssh/id_rsa.pub")),
                &home_dir.join(".ssh/id_rsa"),
                None,
            )
        } else {
            self.second_handler
                .try_next_credential(url, username_from_url, allowed_types)
        }
    }
}

pub struct Ancestors<'a> {
    current: Option<Commit<'a>>,
}

impl<'a> Iterator for Ancestors<'a> {
    type Item = Commit<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.current.take().map(|this| {
            self.current = this.parent(0).ok();
            this
        })
    }
}