From 9bcdc3522a80e330be20d2ddd6b6a5b352988b3f Mon Sep 17 00:00:00 2001
From: Dan Johansen <strit@manjaro.org>
Date: Sun, 19 Apr 2020 07:53:54 +0200
Subject: [PATCH] add back in SyncFirst option

---
 0002-add-sync-first-option.patch | 126 +++++++++++++++++++++++++++++++
 PKGBUILD                         |  13 +++-
 pacman.conf                      |   2 +-
 3 files changed, 136 insertions(+), 5 deletions(-)
 create mode 100644 0002-add-sync-first-option.patch

diff --git a/0002-add-sync-first-option.patch b/0002-add-sync-first-option.patch
new file mode 100644
index 0000000..0f3799b
--- /dev/null
+++ b/0002-add-sync-first-option.patch
@@ -0,0 +1,126 @@
+diff -Nur a/etc/pacman.conf.in b/etc/pacman.conf.in
+--- a/etc/pacman.conf.in	2013-04-30 13:05:45.000000000 +0200
++++ b/etc/pacman.conf.in	2014-12-23 11:43:38.039141449 +0100
+@@ -15,6 +15,8 @@
+ #LogFile     = @localstatedir@/log/pacman.log
+ #GPGDir      = @sysconfdir@/pacman.d/gnupg/
+ HoldPkg     = pacman glibc
++# If upgrades are available for these packages they will be asked for first
++SyncFirst   = pacman
+ #XferCommand = /usr/bin/curl -L -C - -f -o %o %u
+ #XferCommand = /usr/bin/wget --passive-ftp -c -O %o %u
+ #CleanMethod = KeepInstalled
+diff -Nur a/src/pacman/conf.c b/src/pacman/conf.c
+--- a/src/pacman/conf.c	2014-12-19 04:48:00.000000000 +0100
++++ b/src/pacman/conf.c	2014-12-23 11:45:05.435334649 +0100
+@@ -132,6 +132,7 @@
+ 	alpm_list_free(oldconfig->explicit_removes);
+ 
+ 	FREELIST(oldconfig->holdpkg);
++	FREELIST(oldconfig->syncfirst);
+ 	FREELIST(oldconfig->ignorepkg);
+ 	FREELIST(oldconfig->ignoregrp);
+ 	FREELIST(oldconfig->assumeinstalled);
+@@ -513,6 +514,8 @@
+ 			setrepeatingoption(value, "IgnoreGroup", &(config->ignoregrp));
+ 		} else if(strcmp(key, "HoldPkg") == 0) {
+ 			setrepeatingoption(value, "HoldPkg", &(config->holdpkg));
++		} else if(strcmp(key, "SyncFirst") == 0) {
++			setrepeatingoption(value, "SyncFirst", &(config->syncfirst));
+ 		} else if(strcmp(key, "CacheDir") == 0) {
+ 			setrepeatingoption(value, "CacheDir", &(config->cachedirs));
+ 		} else if(strcmp(key, "Architecture") == 0) {
+diff -Nur a/src/pacman/conf.h b/src/pacman/conf.h
+--- a/src/pacman/conf.h	2014-10-14 02:44:20.000000000 +0200
++++ b/src/pacman/conf.h	2014-12-23 11:45:38.021824780 +0100
+@@ -99,6 +99,7 @@
+ 	/* select -Sc behavior */
+ 	unsigned short cleanmethod;
+ 	alpm_list_t *holdpkg;
++	alpm_list_t *syncfirst;
+ 	alpm_list_t *ignorepkg;
+ 	alpm_list_t *ignoregrp;
+ 	alpm_list_t *assumeinstalled;
+diff -Nur a/src/pacman/sync.c b/src/pacman/sync.c
+--- a/src/pacman/sync.c	2014-12-19 04:48:00.000000000 +0100
++++ b/src/pacman/sync.c	2014-12-23 11:51:26.049939350 +0100
+@@ -548,6 +548,26 @@
+ 	return ret;
+ }
+ 
++static alpm_list_t *syncfirst(void) {
++	alpm_list_t *i, *res = NULL;
++	alpm_db_t *db_local = alpm_get_localdb(config->handle);
++	alpm_list_t *syncdbs = alpm_get_syncdbs(config->handle);
++
++	for(i = config->syncfirst; i; i = alpm_list_next(i)) {
++		const char *pkgname = i->data;
++		alpm_pkg_t *pkg = alpm_db_get_pkg(db_local, pkgname);
++		if(pkg == NULL) {
++			continue;
++		}
++
++		if(alpm_sync_get_new_version(pkg, syncdbs)) {
++			res = alpm_list_add(res, strdup(pkgname));
++		}
++	}
++
++	return res;
++}
++
+ static alpm_db_t *get_db(const char *dbname)
+ {
+ 	alpm_list_t *i;
+@@ -959,6 +979,53 @@
+ 		}
+ 	}
+ 
++	/* check for syncfirsts */
++	if(!config->op_s_downloadonly && !config->print) {
++		/* check for newer versions of packages to be upgraded first */
++		alpm_list_t *i, *j, *syncfirsts = syncfirst();
++		if(syncfirsts) {
++			/* Do not ask user if all the -S targets are SyncFirst packages, see FS#15810 */
++			alpm_list_t *targets_diff = alpm_list_diff(targets, syncfirsts, (alpm_list_fn_cmp)strcmp);
++			if(config->op_s_upgrade || targets_diff) {
++				int syncfirst_ret = 1;
++				colon_printf(_("Some packages should be upgraded first...\n"));
++				if(trans_init(0, 1) == 0) {
++					for(i = syncfirsts; i; i = alpm_list_next(i)) {
++						syncfirst_ret = process_targname(alpm_get_syncdbs(config->handle), i->data, 0);
++						if (syncfirst_ret == 1) {
++							break;
++						}
++					}
++				}
++				if (syncfirst_ret == 0) {
++					syncfirst_ret = sync_prepare_execute();
++				} else {
++					trans_release();
++				}
++				if (syncfirst_ret == 0) {
++					/* reinitialize handle to take care of changes */
++					parseconfig(config->configfile);
++					/* remove syncfirsts from targets */
++					alpm_list_t *i = targets;
++					while(i) {
++						for(j = syncfirsts; j; j = alpm_list_next(j)) {
++							if(strcmp(i->data, j->data) == 0) {
++								targets = alpm_list_remove_item(targets, i);
++								break;
++							}
++						}
++						i = i->next;
++					}
++				}
++				printf("\n");
++			} else {
++				pm_printf(ALPM_LOG_DEBUG, "skipping SyncFirst\n");
++			}
++			alpm_list_free(targets_diff);
++			FREELIST(syncfirsts);
++		}
++	}
++
+ 	return sync_trans(targets);
+ }
diff --git a/PKGBUILD b/PKGBUILD
index bcac16c..7093b49 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -10,12 +10,12 @@
 pkgname=pacman
 pkgver=5.2.1
 _pkgver=1.2.0
-pkgrel=3
+pkgrel=4
 pkgdesc="A library-based package manager with dependency support"
 arch=('i686' 'x86_64' 'armv7h' 'aarch64')
 url="http://www.archlinux.org/pacman/"
 license=('GPL')
-groups=('base' 'base-devel')
+groups=('base-devel')
 depends=('bash' 'glibc' 'libarchive' 'curl'
          'gpgme' 'archlinux-keyring' 'archlinuxarm-keyring' 'manjaro-keyring' 'manjaro-arm-keyring' 'pacman-mirrors')
 checkdepends=('python' 'fakechroot')
@@ -32,6 +32,7 @@ source=(https://sources.archlinux.org/other/pacman/$pkgname-$pkgver.tar.gz
         pacman.conf
         makepkg.conf
         0001-Sychronize-filesystem.patch
+        0002-add-sync-first-option.patch
         makepkg-fix-one-more-file-seccomp-issue.patch
         etc-pacman.d-gnupg.mount
         pacman-init.service)
@@ -39,10 +40,13 @@ source=(https://sources.archlinux.org/other/pacman/$pkgname-$pkgver.tar.gz
 prepare() {
   cd $srcdir/$pkgname-$pkgver
 
-  # Manjaro patches
   # Arch Linux ARM patches
   patch -p1 -i $srcdir/0001-Sychronize-filesystem.patch
   patch -p1 -i $srcdir/makepkg-fix-one-more-file-seccomp-issue.patch
+  
+  # Manjaro patches
+  patch -Np1 -i $srcdir/0002-add-sync-first-option.patch
+  
 
   ./configure --prefix=/usr --sysconfdir=/etc \
     --localstatedir=/var --enable-doc \
@@ -117,9 +121,10 @@ esac
 
 sha256sums=('1930c407265fd039cb3a8e6edc82f69e122aa9239d216d9d57b9d1b9315af312'
             '317f53819e35647a19138cb0d68e16206af4a80f52115a7cd622c4a367f914b7'
-            '7a28742058739fbf931fbe4d72c5e596e855d92e5e80d2addeb7eb084435252e'
+            '476afb87aaaaebd026b57d9f8e75f983b49bfe5e2d905a1253ddbb964a9e702b'
             'f187a5919f06c6e3d3e67f2d4b9ac14217accaae1ce7e226ff133bcbfde3f03d'
             '187bef40b14461ef7caba83e8124b6725e0cc9d46fa84353dae3b2afdc013589'
+            '5c864e6c089f3fa699796b5b010378f1b3eddedcb4122a7ade9641cb348c2f95'
             'e481a161bba76729cd434c97e0b319ddfcb1d93b2e4890d72b4e8a32982531d9'
             'b6d14727ec465bb66d0a0358163b1bbfafcb4eaed55a0f57c30aabafae7eed68'
             '862a0c30d1a1824b446a9400feba165d5cd5871491a4b7aeb5595d99db47ca93')
diff --git a/pacman.conf b/pacman.conf
index 66006b3..59f9485 100644
--- a/pacman.conf
+++ b/pacman.conf
@@ -16,7 +16,7 @@
 #GPGDir      = /etc/pacman.d/gnupg/
 HoldPkg      = pacman glibc manjaro-system
 # If upgrades are available for these packages they will be asked for first
-#SyncFirst    = manjaro-system manjaro-keyring manjaro-arm-keyring
+SyncFirst    = manjaro-system manjaro-keyring manjaro-arm-keyring archlinux-keyring archlinuxarm-keyring
 #XferCommand = /usr/bin/curl -C - -f %u > %o
 #XferCommand = /usr/bin/wget --passive-ftp -c -O %o %u
 #CleanMethod = KeepInstalled
-- 
GitLab