summaryrefslogtreecommitdiffstats
path: root/asyncgit/src/sync/mod.rs
blob: c188f28c63775c9e0adff9b603ed2818312a9e78 (plain)
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
//! sync git api

//TODO: remove once we have this activated on the toplevel
#![deny(clippy::expect_used)]

pub mod blame;
pub mod branch;
mod commit;
mod commit_details;
pub mod commit_files;
mod commit_filter;
mod commit_revert;
mod commits_info;
mod config;
pub mod cred;
pub mod diff;
mod hooks;
mod hunks;
mod ignore;
mod logwalker;
mod merge;
mod patches;
mod rebase;
pub mod remotes;
mod repository;
mod reset;
mod reword;
mod staging;
mod stash;
mod state;
pub mod status;
mod submodules;
mod tags;
mod tree;
pub mod utils;

pub use blame::{blame_file, BlameHunk, FileBlame};
pub use branch::{
	branch_compare_upstream, checkout_branch, checkout_commit,
	config_is_pull_rebase, create_branch, delete_branch,
	get_branch_remote, get_branches_info,
	merge_commit::merge_upstream_commit,
	merge_ff::branch_merge_upstream_fastforward,
	merge_rebase::merge_upstream_rebase, rename::rename_branch,
	validate_branch_name, BranchCompare, BranchDetails, BranchInfo,
};
pub use commit::{amend, commit, tag_commit};
pub use commit_details::{
	get_commit_details, CommitDetails, CommitMessage, CommitSignature,
};
pub use commit_files::get_commit_files;
pub use commit_filter::{
	diff_contains_file, filter_commit_by_search, LogFilterSearch,
	LogFilterSearchOptions, SearchFields, SearchOptions,
	SharedCommitFilterFn,
};
pub use commit_revert::{commit_revert, revert_commit, revert_head};
pub use commits_info::{
	get_commit_info, get_commits_info, CommitId, CommitInfo,
};
pub use config::{
	get_config_string, untracked_files_config,
	ShowUntrackedFilesConfig,
};
pub use diff::get_diff_commit;
pub use git2::BranchType;
pub use hooks::{
	hooks_commit_msg, hooks_post_commit, hooks_pre_commit,
	hooks_prepare_commit_msg, HookResult, PrepareCommitMsgSource,
};
pub use hunks::{reset_hunk, stage_hunk, unstage_hunk};
pub use ignore::add_to_ignore;
pub use logwalker::LogWalker;
pub use merge::{
	abort_pending_rebase, abort_pending_state,
	continue_pending_rebase, merge_branch, merge_commit, merge_msg,
	mergehead_ids, rebase_progress,
};
pub use rebase::rebase_branch;
pub use remotes::{
	get_default_remote, get_remotes, push::AsyncProgress,
	tags::PushTagsProgress,
};
pub(crate) use repository::repo;
pub use repository::{RepoPath, RepoPathRef};
pub use reset::{reset_repo, reset_stage, reset_workdir};
pub use reword::reword;
pub use staging::{discard_lines, stage_lines};
pub use stash::{
	get_stashes, stash_apply, stash_drop, stash_pop, stash_save,
};
pub use state::{repo_state, RepoState};
pub use status::is_workdir_clean;
pub use submodules::{
	get_submodules, submodule_parent_info, update_submodule,
	SubmoduleInfo, SubmoduleParentInfo, SubmoduleStatus,
};
pub use tags::{
	delete_tag, get_tags, get_tags_with_metadata, CommitTags, Tag,
	TagWithMetadata, Tags,
};
pub use tree::{tree_file_content, tree_files, TreeFile};
pub use utils::{
	get_head, get_head_tuple, repo_dir, repo_open_error,
	stage_add_all, stage_add_file, stage_addremoved, Head,
};

pub use git2::ResetType;

#[cfg(test)]
mod tests {
	use super::{
		commit,
		repository::repo,
		stage_add_file,
		status::{get_status, StatusType},
		utils::{get_head_repo, repo_write_file},
		CommitId, LogWalker, RepoPath,
	};
	use crate::error::Result;
	use git2::Repository;
	use std::{path::Path, process::Command};
	use tempfile::TempDir;

	/// Calling `set_search_path` with an empty directory makes sure that there
	/// is no git config interfering with our tests (for example user-local
	/// `.gitconfig`).
	#[allow(unsafe_code)]
	fn sandbox_config_files() {
		use git2::{opts::set_search_path, ConfigLevel};
		use std::sync::Once;

		static INIT: Once = Once::new();

		// Adapted from https://github.com/rust-lang/cargo/pull/9035
		INIT.call_once(|| unsafe {
			let temp_dir = TempDir::new().unwrap();
			let path = temp_dir.path();

			set_search_path(ConfigLevel::System, path).unwrap();
			set_search_path(ConfigLevel::Global, path).unwrap();
			set_search_path(ConfigLevel::XDG, path).unwrap();
			set_search_path(ConfigLevel::ProgramData, path).unwrap();
		});
	}

	/// write, stage and commit a file
	pub fn write_commit_file(
		repo: &Repository,
		file: &str,
		content: &str,
		commit_name: &str,
	) -> CommitId {
		repo_write_file(repo, file, content).unwrap();

		stage_add_file(
			&repo.workdir().unwrap().to_str().unwrap().into(),
			Path::new(file),
		)
		.unwrap();

		commit(
			&repo.workdir().unwrap().to_str().unwrap().into(),
			commit_name,
		)
		.unwrap()
	}

	/// write, stage and commit a file giving the commit a specific timestamp
	pub fn write_commit_file_at(
		repo: &Repository,
		file: &str,
		content: &str,
		commit_name: &str,
		time: git2::Time,
	) -> CommitId {
		repo_write_file(repo, file, content).unwrap();

		let path: &RepoPath =
			&repo.workdir().unwrap().to_str().unwrap().into();

		stage_add_file(path, Path::new(file)).unwrap();

		commit_at(path, commit_name, time)
	}

	fn commit_at(
		repo_path: &RepoPath,
		msg: &str,
		time: git2::Time,
	) -> CommitId {
		let repo = repo(repo_path).unwrap();

		let signature =
			git2::Signature::new("name", "email", &time).unwrap();
		let mut index = repo.index().unwrap();
		let tree_id = index.write_tree().unwrap();
		let tree = repo.find_tree(tree_id).unwrap();

		let parents = if let Ok(id) = get_head_repo(&repo) {
			vec![repo.find_commit(id.into()).unwrap()]
		} else {
			Vec::new()
		};

		let parents = parents.iter().collect::<Vec<_>>();

		let commit = repo
			.commit(
				Some("HEAD"),
				&signature,
				&signature,
				msg,
				&tree,
				parents.as_slice(),
			)
			.unwrap()
			.into();

		commit
	}

	///
	pub fn repo_init_empty() -> Result<(TempDir, Repository)> {
		init_log();

		sandbox_config_files();

		let td = TempDir::new()?;
		let repo = Repository::init(td.path())?;
		{
			let mut config = repo.config()?;
			config.set_str("user.name", "name")?;
			config.set_str("user.email", "email")?;
		}
		Ok((td, repo))
	}

	///
	pub fn repo_init() -> Result<(TempDir, Repository)> {
		init_log();

		sandbox_config_files();

		let td = TempDir::new()?;
		let repo = Repository::init(td.path())?;
		{
			let mut config = repo.config()?;
			config.set_str("user.name", "name")?;
			config.set_str("user.email", "email")?;

			let mut index = repo.index()?;
			let id = index.write_tree()?;

			let tree = repo.find_tree(id)?;
			let sig = repo.signature()?;
			repo.commit(
				Some("HEAD"),
				&sig,
				&sig,
				"initial",